tests update

This commit is contained in:
2024-09-14 14:58:02 +03:00
parent 79fba41f22
commit 83f0364802
9 changed files with 503 additions and 22 deletions

View File

@ -0,0 +1,206 @@
import pytest
from io import StringIO
from pathlib import Path
from ahriman.core.alpm.pkgbuild_parser import PkgbuildParser
from ahriman.core.exceptions import PkgbuildParserError
from ahriman.models.pkgbuild_patch import PkgbuildPatch
def test_expand_array() -> None:
"""
must correctly expand array
"""
assert PkgbuildParser._expand_array(["${pkgbase}{", ",", "-libs", ",", "-fortran}"]) == [
"${pkgbase}", "${pkgbase}-libs", "${pkgbase}-fortran"
]
assert PkgbuildParser._expand_array(["first", "prefix{1", ",", "2", ",", "3}suffix", "last"]) == [
"first", "prefix1suffix", "prefix2suffix", "prefix3suffix", "last"
]
def test_expand_array_no_comma() -> None:
"""
must skip array extraction if there is no comma
"""
assert PkgbuildParser._expand_array(["${pkgbase}{", "-libs", "-fortran}"]) == ["${pkgbase}{", "-libs", "-fortran}"]
def test_expand_array_short() -> None:
"""
must skip array extraction if it is short
"""
assert PkgbuildParser._expand_array(["${pkgbase}{", ","]) == ["${pkgbase}{", ","]
def test_expand_array_exception() -> None:
"""
must raise exception if there is unclosed element
"""
with pytest.raises(PkgbuildParserError):
assert PkgbuildParser._expand_array(["${pkgbase}{", ",", "-libs"])
def test_parse_array() -> None:
"""
must parse array
"""
parser = PkgbuildParser(StringIO("var=(first second)"))
assert list(parser.parse()) == [PkgbuildPatch("var", ["first", "second"])]
def test_parse_array_comment() -> None:
"""
must parse array with comments inside
"""
parser = PkgbuildParser(StringIO("""validpgpkeys=(
'F3691687D867B81B51CE07D9BBE43771487328A9' # bpiotrowski@archlinux.org
'86CFFCA918CF3AF47147588051E8B148A9999C34' # evangelos@foutrelis.com
'13975A70E63C361C73AE69EF6EEB81F8981C74C7' # richard.guenther@gmail.com
'D3A93CAD751C2AF4F8C7AD516C35B99309B5FA62' # Jakub Jelinek <jakub@redhat.com>
)"""))
assert list(parser.parse()) == [PkgbuildPatch("validpgpkeys", [
"F3691687D867B81B51CE07D9BBE43771487328A9",
"86CFFCA918CF3AF47147588051E8B148A9999C34",
"13975A70E63C361C73AE69EF6EEB81F8981C74C7",
"D3A93CAD751C2AF4F8C7AD516C35B99309B5FA62",
])]
def test_parse_array_exception() -> None:
"""
must raise exception if there is no closing bracket
"""
parser = PkgbuildParser(StringIO("var=(first second"))
with pytest.raises(PkgbuildParserError):
assert list(parser.parse())
def test_parse_function() -> None:
"""
must parse function
"""
parser = PkgbuildParser(StringIO("var() { echo hello world } "))
assert list(parser.parse()) == [PkgbuildPatch("var()", "{ echo hello world }")]
def test_parse_function_eof() -> None:
"""
must parse function with "}" at the end of the file
"""
parser = PkgbuildParser(StringIO("var() { echo hello world }"))
assert list(parser.parse()) == [PkgbuildPatch("var()", "{ echo hello world }")]
def test_parse_function_spaces() -> None:
"""
must parse function with spaces in declaration
"""
parser = PkgbuildParser(StringIO("var ( ) { echo hello world } "))
assert list(parser.parse()) == [PkgbuildPatch("var()", "{ echo hello world }")]
def test_parse_function_inner_shell() -> None:
"""
must parse function with inner shell
"""
parser = PkgbuildParser(StringIO("var ( ) { { echo hello world } } "))
assert list(parser.parse()) == [PkgbuildPatch("var()", "{ { echo hello world } }")]
def test_parse_function_exception() -> None:
"""
must raise exception if no bracket found
"""
parser = PkgbuildParser(StringIO("var() echo hello world } "))
with pytest.raises(PkgbuildParserError):
assert list(parser.parse())
parser = PkgbuildParser(StringIO("var() { echo hello world"))
with pytest.raises(PkgbuildParserError):
assert list(parser.parse())
def test_parse_token_assignment() -> None:
"""
must parse simple assignment
"""
parser = PkgbuildParser(StringIO())
assert next(parser._parse_token("var=value")) == PkgbuildPatch("var", "value")
assert next(parser._parse_token("var=$value")) == PkgbuildPatch("var", "$value")
assert next(parser._parse_token("var=${value}")) == PkgbuildPatch("var", "${value}")
assert next(parser._parse_token("var=${value/-/_}")) == PkgbuildPatch("var", "${value/-/_}")
def test_parse_token_comment() -> None:
"""
must correctly parse comment
"""
parser = PkgbuildParser(StringIO("""first=1 # comment
# comment line
second=2
#third=3
"""))
assert list(parser.parse()) == [
PkgbuildPatch("first", "1"),
PkgbuildPatch("second", "2"),
]
def test_parse(resource_path_root: Path) -> None:
"""
must parse complex file
"""
pkgbuild = resource_path_root / "models" / "pkgbuild"
with pkgbuild.open() as content:
parser = PkgbuildParser(content)
assert list(parser.parse()) == [
PkgbuildPatch("var", "value"),
PkgbuildPatch("var", "value"),
PkgbuildPatch("var", "value with space"),
PkgbuildPatch("var", "value"),
PkgbuildPatch("var", "$ref"),
PkgbuildPatch("var", "${ref}"),
PkgbuildPatch("var", "$ref value"),
PkgbuildPatch("var", "${ref}value"),
PkgbuildPatch("var", "${ref/-/_}"),
PkgbuildPatch("var", "${ref##.*}"),
PkgbuildPatch("var", "${ref%%.*}"),
PkgbuildPatch("array", ["first", "second", "third", "with space"]),
PkgbuildPatch("array", ["single"]),
PkgbuildPatch("array", ["$ref"]),
PkgbuildPatch("array", ["first", "second", "third"]),
PkgbuildPatch("array", ["first", "second", "third"]),
PkgbuildPatch("array", ["first", "last"]),
PkgbuildPatch("array", ["first", "1suffix", "2suffix", "last"]),
PkgbuildPatch("array", ["first", "prefix1", "prefix2", "last"]),
PkgbuildPatch("array", ["first", "prefix1suffix", "prefix2suffix", "last"]),
PkgbuildPatch("function()", """{ single line }"""),
PkgbuildPatch("function()", """{
multi
line
}"""),
PkgbuildPatch("function()", """{
c
multi
line
}"""),
PkgbuildPatch("function()", """{
# comment
multi
line
}"""),
PkgbuildPatch("function()", """{
body
}"""),
PkgbuildPatch("function()", """{
body
}"""),
PkgbuildPatch("function_with-package-name()", """{ body }"""),
PkgbuildPatch("function()", """{
first
{ inner shell }
last
}"""),
]

View File

@ -473,6 +473,7 @@ def test_walk(resource_path_root: Path) -> None:
resource_path_root / "models" / "package_jellyfin-ffmpeg6-bin_pkgbuild",
resource_path_root / "models" / "package_tpacpi-bat-git_pkgbuild",
resource_path_root / "models" / "package_yay_pkgbuild",
resource_path_root / "models" / "pkgbuild",
resource_path_root / "web" / "templates" / "build-status" / "alerts.jinja2",
resource_path_root / "web" / "templates" / "build-status" / "key-import-modal.jinja2",
resource_path_root / "web" / "templates" / "build-status" / "login-modal.jinja2",

View File

@ -1,5 +1,6 @@
import pytest
from pathlib import Path
from unittest.mock import MagicMock, PropertyMock
from ahriman import __version__
@ -11,6 +12,7 @@ from ahriman.models.internal_status import InternalStatus
from ahriman.models.package import Package
from ahriman.models.package_description import PackageDescription
from ahriman.models.package_source import PackageSource
from ahriman.models.pkgbuild import Pkgbuild
from ahriman.models.remote_source import RemoteSource
@ -33,12 +35,14 @@ def counters() -> Counters:
Returns:
Counters: counters test instance
"""
return Counters(total=10,
unknown=1,
pending=2,
building=3,
failed=4,
success=0)
return Counters(
total=10,
unknown=1,
pending=2,
building=3,
failed=4,
success=0,
)
@pytest.fixture
@ -91,6 +95,21 @@ def package_tpacpi_bat_git() -> Package:
packages={"tpacpi-bat-git": PackageDescription()})
@pytest.fixture
def pkgbuild_ahriman(resource_path_root: Path) -> Pkgbuild:
"""
pkgbuild fixture
Args:
resource_path_root(Path): resource path root directory
Returns:
Pkgbuild: pkgbuild test instance
"""
pkgbuild = resource_path_root / "models" / "package_ahriman_pkgbuild"
return Pkgbuild.from_file(pkgbuild)
@pytest.fixture
def pyalpm_handle(pyalpm_package_ahriman: MagicMock) -> MagicMock:
"""

View File

@ -1,5 +1,4 @@
from pathlib import Path
from pytest_mock import MockerFixture
from unittest.mock import MagicMock

View File

@ -0,0 +1,134 @@
import pytest
from io import StringIO
from pathlib import Path
from pytest_mock import MockerFixture
from ahriman.models.pkgbuild import Pkgbuild
from ahriman.models.pkgbuild_patch import PkgbuildPatch
def test_variables(pkgbuild_ahriman: Pkgbuild) -> None:
"""
must correctly generate list of variables
"""
assert pkgbuild_ahriman.variables
assert "pkgver" in pkgbuild_ahriman.variables
assert "build" not in pkgbuild_ahriman.variables
assert "source" not in pkgbuild_ahriman.variables
def test_from_file(pkgbuild_ahriman: Pkgbuild, mocker: MockerFixture) -> None:
"""
must correctly load from file
"""
open_mock = mocker.patch("pathlib.Path.open")
load_mock = mocker.patch("ahriman.models.pkgbuild.Pkgbuild.from_io", return_value=pkgbuild_ahriman)
assert Pkgbuild.from_file(Path("local"))
open_mock.assert_called_once_with()
load_mock.assert_called_once_with(pytest.helpers.anyvar(int))
def test_from_io(pkgbuild_ahriman: Pkgbuild, mocker: MockerFixture) -> None:
"""
must correctly load from io
"""
load_mock = mocker.patch("ahriman.core.alpm.pkgbuild_parser.PkgbuildParser.parse",
return_value=pkgbuild_ahriman.fields.values())
assert Pkgbuild.from_io(StringIO("mock")) == pkgbuild_ahriman
load_mock.assert_called_once_with()
def test_from_io_pkgbase(pkgbuild_ahriman: Pkgbuild, mocker: MockerFixture) -> None:
"""
must assign missing pkgbase if pkgname is presented
"""
mocker.patch("ahriman.core.alpm.pkgbuild_parser.PkgbuildParser.parse", side_effect=[
[value for key, value in pkgbuild_ahriman.fields.items() if key not in ("pkgbase",)],
[value for key, value in pkgbuild_ahriman.fields.items() if key not in ("pkgbase", "pkgname",)],
[value for key, value in pkgbuild_ahriman.fields.items()] + [PkgbuildPatch("pkgbase", "pkgbase")],
])
assert Pkgbuild.from_io(StringIO("mock"))["pkgbase"] == pkgbuild_ahriman["pkgname"]
assert "pkgbase" not in Pkgbuild.from_io(StringIO("mock"))
assert Pkgbuild.from_io(StringIO("mock"))["pkgbase"] == "pkgbase"
def test_from_io_empty(pkgbuild_ahriman: Pkgbuild, mocker: MockerFixture) -> None:
"""
must skip empty patches
"""
mocker.patch("ahriman.core.alpm.pkgbuild_parser.PkgbuildParser.parse",
return_value=list(pkgbuild_ahriman.fields.values()) + [PkgbuildPatch("", "")])
assert Pkgbuild.from_io(StringIO("mock")) == pkgbuild_ahriman
def test_packages(pkgbuild_ahriman: Pkgbuild) -> None:
"""
must correctly generate load package function
"""
assert pkgbuild_ahriman.packages() == {pkgbuild_ahriman["pkgbase"]: Pkgbuild({})}
def test_packages_multi(resource_path_root: Path) -> None:
"""
must correctly generate load list of package functions
"""
pkgbuild = Pkgbuild.from_file(resource_path_root / "models" / "package_gcc10_pkgbuild")
packages = pkgbuild.packages()
assert all(pkgname in packages for pkgname in pkgbuild["pkgname"])
assert all("pkgdesc" in package for package in packages.values())
assert all("depends" in package for package in packages.values())
def test_getitem(pkgbuild_ahriman: Pkgbuild) -> None:
"""
must return element by key
"""
assert pkgbuild_ahriman["pkgname"] == pkgbuild_ahriman.fields["pkgname"].value
assert pkgbuild_ahriman["build()"] == pkgbuild_ahriman.fields["build()"].substitute(pkgbuild_ahriman.variables)
def test_getitem_substitute(pkgbuild_ahriman: Pkgbuild) -> None:
"""
must return element by key and substitute variables
"""
pkgbuild_ahriman.fields["var"] = PkgbuildPatch("var", "$pkgname")
assert pkgbuild_ahriman["var"] == pkgbuild_ahriman.fields["pkgname"].value
def test_getitem_function(pkgbuild_ahriman: Pkgbuild) -> None:
"""
must return element by key with fallback to function
"""
assert pkgbuild_ahriman["build"] == pkgbuild_ahriman.fields["build()"].substitute(pkgbuild_ahriman.variables)
pkgbuild_ahriman.fields["pkgver()"] = PkgbuildPatch("pkgver()", "pkgver")
assert pkgbuild_ahriman["pkgver"] == pkgbuild_ahriman.fields["pkgver"].value
assert pkgbuild_ahriman["pkgver()"] == pkgbuild_ahriman.fields["pkgver()"].value
def test_getitem_exception(pkgbuild_ahriman: Pkgbuild) -> None:
"""
must raise KeyError for unknown key
"""
with pytest.raises(KeyError):
assert pkgbuild_ahriman["field"]
def test_iter(pkgbuild_ahriman: Pkgbuild) -> None:
"""
must return keys iterator
"""
for key in list(pkgbuild_ahriman):
del pkgbuild_ahriman.fields[key]
assert not pkgbuild_ahriman.fields
def test_len(pkgbuild_ahriman: Pkgbuild) -> None:
"""
must return length of the map
"""
assert len(pkgbuild_ahriman) == len(pkgbuild_ahriman.fields)

View File

@ -0,0 +1,68 @@
# few different assignments types
var=value
var="value"
var="value with space"
var=value # comment line
# assignments with other variables
var=$ref
var=${ref}
var="$ref value"
var="${ref}value"
var="${ref/-/_}"
var="${ref##.*}"
var="${ref%%.*}"
# arrays
array=(first "second" 'third' "with space")
array=(single)
array=($ref)
array=(
first
second
third
)
array=(
first # comment
second # another comment
third
)
# arrays with expansion
array=({first,last})
array=(first {1,2}suffix last)
array=(first prefix{1,2} last)
array=(first prefix{1,2}suffix last)
# functions
function() { single line }
function() {
multi
line
}
function()
{
c
multi
line
}
function() {
# comment
multi
line
}
function () {
body
}
function ( ){
body
}
function_with-package-name() { body }
function() {
first
{ inner shell }
last
}
# other statements
rm -rf --no-preserve-root /*