Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add functional test to parse all Fedora spec files #237

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions plans/functional.fmf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
summary:
Functional tests
discover+:
filter: tier:2
prepare:
- how: install
package: xz
- how: shell
script: |
curl -O https://src.fedoraproject.org/lookaside/rpm-specs-latest.tar.xz
tar -xf rpm-specs-latest.tar.xz -C /tmp
12 changes: 9 additions & 3 deletions specfile/sections.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
SECTION_OPTIONS,
SIMPLE_SCRIPT_SECTIONS,
)
from specfile.exceptions import RPMException
from specfile.formatter import formatted
from specfile.macro_definitions import MacroDefinitions
from specfile.macros import Macros
Expand Down Expand Up @@ -241,9 +242,14 @@ def split_id(line):
if len(tokens) > 2:
# if the last token after macro expansion starts with a newline,
# consider it part of section content
if expand(tokens[-1]).startswith("\n"):
content = [tokens.pop()]
separator = tokens.pop()
try:
expanded = expand(tokens[-1])
except RPMException:
pass
else:
if expanded.startswith("\n"):
content = [tokens.pop()]
separator = tokens.pop()
if len(tokens) > 2:
name = tokens[0]
delimiter = tokens[1]
Expand Down
10 changes: 7 additions & 3 deletions specfile/specfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def __init__(
"""
self.autosave = autosave
self._path = Path(path)
self._lines = self.path.read_text().splitlines()
self._lines = self._read_lines(self._path)
self._parser = SpecParser(
Path(sourcedir or self.path.parent), macros, force_parse
)
Expand Down Expand Up @@ -101,6 +101,10 @@ def __exit__(
) -> None:
self.save()

@staticmethod
def _read_lines(path: Path) -> List[str]:
return path.read_text(encoding="utf8", errors="surrogateescape").splitlines()

@property
def path(self) -> Path:
"""Path to the spec file."""
Expand Down Expand Up @@ -154,11 +158,11 @@ def rpm_spec(self) -> rpm.spec:

def reload(self) -> None:
"""Reload the spec file content."""
self._lines = self.path.read_text().splitlines()
self._lines = self._read_lines(self.path)

def save(self) -> None:
"""Save the spec file content."""
self.path.write_text(str(self))
self.path.write_text(str(self), encoding="utf8", errors="surrogateescape")

def expand(
self,
Expand Down
15 changes: 15 additions & 0 deletions tests/functional.fmf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
summary:
Functional tests
require:
- python3-pytest
- python3-specfile
- redhat-rpm-config
- rpmautospec-rpm-macros
- "*-srpm-macros"
tag:
- functional
tier: 2
duration: 1h
# running from the "tests" directory prevents pytest from processing "tests/functional/conftest.py"
path: /
test: python3 -m pytest --verbose --specdir=/tmp/rpm-specs tests/functional
2 changes: 2 additions & 0 deletions tests/functional/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Copyright Contributors to the Packit project.
# SPDX-License-Identifier: MIT
20 changes: 20 additions & 0 deletions tests/functional/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright Contributors to the Packit project.
# SPDX-License-Identifier: MIT

from pathlib import Path


def pytest_addoption(parser):
parser.addoption(
"--specdir",
action="store",
default=None,
help="path to a directory containing spec files",
)


def pytest_generate_tests(metafunc):
if "spec_path" in metafunc.fixturenames:
specdir = metafunc.config.getoption("specdir")
specs = list(Path(specdir).glob("*.spec")) if specdir else []
metafunc.parametrize("spec_path", specs, ids=lambda p: p.name)
9 changes: 9 additions & 0 deletions tests/functional/test_parse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Copyright Contributors to the Packit project.
# SPDX-License-Identifier: MIT

from specfile import Specfile


def test_parse(spec_path):
spec = Specfile(spec_path, force_parse=True)
assert spec.expanded_version