]> git.ipfire.org Git - pakfire.git/commitdiff
package: Add function to check whether a depenency matches
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 8 Dec 2022 17:46:26 +0000 (17:46 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 8 Dec 2022 17:46:26 +0000 (17:46 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/include/pakfire/package.h
src/libpakfire/package.c
tests/libpakfire/dependencies.c

index eb6857b34747d087d6de49b96807d51c7fb15b53..993bf9dd3d1110ed49f3e4a08cc666d6fff5d641 100644 (file)
@@ -154,6 +154,8 @@ int pakfire_package_add_dep(struct pakfire_package* pkg,
        const enum pakfire_package_key key, const char* dep);
 
 int pakfire_package_has_rich_deps(struct pakfire_package* pkg);
+int pakfire_package_matches_dep(struct pakfire_package* pkg,
+       const enum pakfire_package_key key, const char* dep);
 
 struct json_object* pakfire_package_to_json(struct pakfire_package* pkg);
 
index d7d85bb112d3db6b9036fa5674a361f113005ba7..f3ffa55eaf993fbae95fe0e6bb6caa75979d4e3c 100644 (file)
@@ -1090,6 +1090,30 @@ int pakfire_package_has_rich_deps(struct pakfire_package* pkg) {
        return 0;
 }
 
+int pakfire_package_matches_dep(struct pakfire_package* pkg,
+               const enum pakfire_package_key key, const char* dep) {
+       int r;
+
+       Id id = ID_NULL;
+       int marker = 0;
+
+       // Translate the dependency type
+       r = pakfire_package_dep2id(key, &id, &marker);
+       if (r)
+               return r;
+
+       // Get the dependency
+       Id depid = pakfire_str2dep(pkg->pakfire, dep);
+       if (!depid)
+               return 0;
+
+       // Fetch the solvable
+       Solvable* s = get_solvable(pkg);
+
+       // Check whether this solvable matches the requested dependency
+       return solvable_matchesdep(s, id, depid, marker) == 0;
+}
+
 PAKFIRE_EXPORT struct pakfire_repo* pakfire_package_get_repo(struct pakfire_package* pkg) {
        if (!pkg->repo) {
                Solvable* s = get_solvable(pkg);
index bd1cc534b9cac379d54596610876769ede12e44b..d46f27c2d6ab3a326aa62d3b9fa8b9f4caf024e4 100644 (file)
@@ -169,8 +169,47 @@ FAIL:
        return EXIT_FAILURE;
 }
 
+static int test_dep_match(const struct test* t) {
+       struct pakfire_package* pkg = NULL;
+       int r = EXIT_FAILURE;
+
+       ASSERT_SUCCESS(pakfire_package_create(&pkg, t->pakfire, NULL,
+               "test", "1.0-1", "x86_64"));
+
+       // Check if the package matches itself
+       ASSERT_SUCCESS(pakfire_package_matches_dep(pkg, PAKFIRE_PKG_PROVIDES, "test") == 1);
+       ASSERT_SUCCESS(pakfire_package_matches_dep(pkg, PAKFIRE_PKG_PROVIDES, "test = 1.0-1") == 1);
+
+       // Add a couple of things this package provides
+       ASSERT_SUCCESS(pakfire_package_add_dep(pkg, PAKFIRE_PKG_PROVIDES, "a = 1"));
+       ASSERT_SUCCESS(pakfire_package_add_dep(pkg, PAKFIRE_PKG_PROVIDES, "b"));
+
+       // Check if the package matches those dependencies
+       ASSERT_SUCCESS(pakfire_package_matches_dep(pkg, PAKFIRE_PKG_PROVIDES, "a") == 1);
+       ASSERT_SUCCESS(pakfire_package_matches_dep(pkg, PAKFIRE_PKG_PROVIDES, "a = 1") == 1);
+       ASSERT_SUCCESS(pakfire_package_matches_dep(pkg, PAKFIRE_PKG_PROVIDES, "a >= 1") == 1);
+       ASSERT_SUCCESS(pakfire_package_matches_dep(pkg, PAKFIRE_PKG_PROVIDES, "a <= 1") == 1);
+
+       ASSERT_SUCCESS(pakfire_package_matches_dep(pkg, PAKFIRE_PKG_PROVIDES, "b") == 1);
+       ASSERT_SUCCESS(pakfire_package_matches_dep(pkg, PAKFIRE_PKG_PROVIDES, "b = 1") == 1);
+
+       // Check for something that doesn't exist
+       ASSERT_SUCCESS(pakfire_package_matches_dep(pkg, PAKFIRE_PKG_PROVIDES, "c") == 0);
+       ASSERT_SUCCESS(pakfire_package_matches_dep(pkg, PAKFIRE_PKG_PROVIDES, "c = 2") == 0);
+
+       // Everything passed
+       r = EXIT_SUCCESS;
+
+FAIL:
+       if (pkg)
+               pakfire_package_unref(pkg);
+
+       return r;
+}
+
 int main(int argc, const char* argv[]) {
        testsuite_add_test(test_dependencies);
+       testsuite_add_test(test_dep_match);
 
        return testsuite_run(argc, argv);
 }