aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Turner <jturner.usa@gmail.com>2024-02-19 19:32:45 -0500
committerSam James <sam@gentoo.org>2024-03-07 18:49:06 +0000
commit34a96faca419b81be2c88655daec4a24056fd926 (patch)
tree6aa517d5abfda50e711c8ed3e8e7a8f00ca26baa
parentdependencies.py: use Enum rather than StrEnum for DependencyKind (diff)
downloadgentoolkit-34a96faca419b81be2c88655daec4a24056fd926.tar.gz
gentoolkit-34a96faca419b81be2c88655daec4a24056fd926.tar.bz2
gentoolkit-34a96faca419b81be2c88655daec4a24056fd926.zip
dependencies.py: introduce unit testing for graph_reverse_depends
This commit introduces a new file with a basic unit test for the graph_reverse_depends method on the Dependencies class. Only 1 test exists right now, but various setup code and the general pattern of the tests are valuable for creating more advanced tests, and more tests for the Dependencies class in general. Pytest is used to run all of the functions in the file that start with test_, and a monkeypatch object is passed into test cases and allows us to mock out methods on the Dependencies class, specifically the "environment" method, which is used to query for packages variables like DEPEND and RDEPEND. We are able to test against a small fake denendency graph by patching the method! Signed-off-by: John Turner <jturner.usa@gmail.com> Signed-off-by: Sam James <sam@gentoo.org>
-rw-r--r--pym/gentoolkit/test/test_dependencies.py71
1 files changed, 71 insertions, 0 deletions
diff --git a/pym/gentoolkit/test/test_dependencies.py b/pym/gentoolkit/test/test_dependencies.py
new file mode 100644
index 0000000..48e9507
--- /dev/null
+++ b/pym/gentoolkit/test/test_dependencies.py
@@ -0,0 +1,71 @@
+import portage
+from typing import List, Dict, Optional
+from pytest import MonkeyPatch
+from gentoolkit.dependencies import Dependencies
+
+
+def is_cp_in_cpv(cp: str, cpv: str) -> bool:
+ other_cp, _, _ = portage.pkgsplit(cpv)
+ return cp == other_cp
+
+
+def environment(
+ self: Dependencies,
+ env_vars: List[str],
+ fake_depends: Dict[str, Optional[Dict[str, str]]],
+ fake_pkgs: List[str],
+) -> List[str]:
+ metadata = None
+ for pkg in fake_pkgs:
+ if is_cp_in_cpv(self.cp, pkg):
+ if (metadata := fake_depends[pkg]) is not None:
+ break
+ else:
+ return [""]
+ results = list()
+ for env_var in env_vars:
+ try:
+ value = metadata[env_var]
+ except KeyError:
+ value = ""
+ results.append(value)
+ return results
+
+
+def test_basic_revdeps(monkeypatch: MonkeyPatch) -> None:
+ fake_depends = {
+ "app-misc/root-1.0": None,
+ "app-misc/a-1.0": {"DEPEND": "app-misc/root"},
+ "app-misc/b-1.0": {"DEPEND": "app-misc/a"},
+ "app-misc/c-1.0": {"DEPEND": "app-misc/b"},
+ "app-misc/d-1.0": None,
+ }
+ fake_pkgs = list(fake_depends.keys())
+
+ def e(self, env_vars):
+ return environment(self, env_vars, fake_depends, fake_pkgs)
+
+ monkeypatch.setattr(Dependencies, "environment", e)
+
+ # confirm that monkeypatch is working as expected
+ assert Dependencies("app-misc/root").environment(["DEPEND"]) == [""]
+ assert Dependencies("app-misc/a").environment(["DEPEND"]) == ["app-misc/root"]
+ assert Dependencies("app-misc/b").environment(["DEPEND"]) == ["app-misc/a"]
+ assert Dependencies("app-misc/c").environment(["DEPEND"]) == ["app-misc/b"]
+ assert Dependencies("app-misc/d").environment(["DEPEND"]) == [""]
+
+ assert sorted(
+ pkg.cpv
+ for pkg in Dependencies("app-misc/root").graph_reverse_depends(pkgset=fake_pkgs)
+ ) == ["app-misc/a-1.0"]
+
+ assert sorted(
+ pkg.cpv
+ for pkg in Dependencies("app-misc/root").graph_reverse_depends(
+ pkgset=fake_pkgs, only_direct=False
+ )
+ ) == [
+ "app-misc/a-1.0",
+ "app-misc/b-1.0",
+ "app-misc/c-1.0",
+ ]