From: Michael Tremer Date: Thu, 8 Dec 2022 17:46:26 +0000 (+0000) Subject: package: Add function to check whether a depenency matches X-Git-Tag: 0.9.28~21 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=f35eadaba923b6e0ba7dc5c0bfb25d22be443070;p=pakfire.git package: Add function to check whether a depenency matches Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/include/pakfire/package.h b/src/libpakfire/include/pakfire/package.h index eb6857b34..993bf9dd3 100644 --- a/src/libpakfire/include/pakfire/package.h +++ b/src/libpakfire/include/pakfire/package.h @@ -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); diff --git a/src/libpakfire/package.c b/src/libpakfire/package.c index d7d85bb11..f3ffa55ea 100644 --- a/src/libpakfire/package.c +++ b/src/libpakfire/package.c @@ -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); diff --git a/tests/libpakfire/dependencies.c b/tests/libpakfire/dependencies.c index bd1cc534b..d46f27c2d 100644 --- a/tests/libpakfire/dependencies.c +++ b/tests/libpakfire/dependencies.c @@ -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); }