From 41083007e6b1d2e434663c2d6c074e806e417ea9 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 30 Jan 2023 13:38:41 +0200 Subject: Add tests for common functions --- bpkg/system-package-manager-debian.test.cxx | 65 ++--------- bpkg/system-package-manager-debian.test.testscript | 2 +- bpkg/system-package-manager.hxx | 8 +- bpkg/system-package-manager.test.cxx | 124 +++++++++++++++++++++ bpkg/system-package-manager.test.hxx | 104 +++++++++++++++++ bpkg/system-package-manager.test.testscript | 102 +++++++++++++++++ 6 files changed, 344 insertions(+), 61 deletions(-) create mode 100644 bpkg/system-package-manager.test.cxx create mode 100644 bpkg/system-package-manager.test.hxx create mode 100644 bpkg/system-package-manager.test.testscript diff --git a/bpkg/system-package-manager-debian.test.cxx b/bpkg/system-package-manager-debian.test.cxx index 1853a4f..562344c 100644 --- a/bpkg/system-package-manager-debian.test.cxx +++ b/bpkg/system-package-manager-debian.test.cxx @@ -3,21 +3,17 @@ #include -#include -#include - #include #include -#include - -#include - -#include +#include +#include #undef NDEBUG #include +#include + using namespace std; namespace bpkg @@ -221,41 +217,7 @@ namespace bpkg if (i == aps.end ()) fail << "unknown package " << n << " in '" << l << "'"; - // Parse the manifest as if it comes from a git repository with a - // single package and make an available package out of it. - // - try - { - ifdstream ifs (f); - manifest_parser mp (ifs, f); - - package_manifest m (mp, - false /* ignore_unknown */, - true /* complete_values */); - - m.alt_naming = false; - m.bootstrap_build = "project = " + m.name.string () + '\n'; - - shared_ptr ap ( - make_shared (move (m))); - - lazy_shared_ptr af ( - make_shared ( - repository_location ("https://example.com/" + i->first, - repository_type::git))); - - ap->locations.push_back (package_location {af, current_dir}); - - i->second.push_back (make_pair (move (ap), move (af))); - } - catch (const manifest_parsing& e) - { - fail (e.name, e.line, e.column) << e.description; - } - catch (const io_error& e) - { - fail << "unable to read from " << f << ": " << e; - } + i->second.push_back (make_available_from_manifest (n, f)); } else if ( map* policy = @@ -307,25 +269,14 @@ namespace bpkg fail << "unknown keyword '" << k << "' in simulation description"; } - // Fallback to stubs as if they come from git repositories with a single - // package. + // Fallback to stubs and sort in the version descending order. // for (pair& p: aps) { if (p.second.empty ()) - { - shared_ptr ap ( - make_shared (package_name (p.first))); + p.second.push_back (make_available_stub (p.first)); - lazy_shared_ptr af ( - make_shared ( - repository_location ("https://example.com/" + p.first, - repository_type::git))); - - ap->locations.push_back (package_location {af, current_dir}); - - p.second.push_back (make_pair (move (ap), move (af))); - } + sort_available (p.second); } system_package_manager_debian m (move (osr), diff --git a/bpkg/system-package-manager-debian.test.testscript b/bpkg/system-package-manager-debian.test.testscript index 180fa5f..1a3b8b2 100644 --- a/bpkg/system-package-manager-debian.test.testscript +++ b/bpkg/system-package-manager-debian.test.testscript @@ -1,4 +1,4 @@ -# file : bpkg/host-os-release.test.testscript +# file : bpkg/system-package-manager-debian.test.testscript # license : MIT; see accompanying LICENSE file : apt-cache-policy diff --git a/bpkg/system-package-manager.hxx b/bpkg/system-package-manager.hxx index e0a424b..a52e9cb 100644 --- a/bpkg/system-package-manager.hxx +++ b/bpkg/system-package-manager.hxx @@ -174,7 +174,9 @@ namespace bpkg yes_ (yes), sudo_ (sudo != "false" ? move (sudo) : string ()) {} - protected: + // Implementation details. + // + public: // Given the available packages (as returned by find_available_all()) // return the list of system package names as mapped by the // -name values. @@ -202,11 +204,11 @@ namespace bpkg // as, for example, for the following distribution values: // // debian_10-name: libcurl4 libcurl4-doc libcurl4-openssl-dev - // debian_10-name: libcurl3-gnutls libcurl4-gnutls-dev (yes, 3 and 4) + // debian_10-name: libcurl3-gnutls libcurl4-gnutls-dev (yes, 3 and 4) // // Note also that the value are returned in the "override order", that is // from the newest package version to oldest and then from the highest - // distribution version to oldest. @@ Is the latter actually the case? + // distribution version to oldest. // static strings system_package_names (const available_packages&, diff --git a/bpkg/system-package-manager.test.cxx b/bpkg/system-package-manager.test.cxx new file mode 100644 index 0000000..1a669da --- /dev/null +++ b/bpkg/system-package-manager.test.cxx @@ -0,0 +1,124 @@ +// file : bpkg/system-package-manager.test.cxx -*- C++ -*- +// license : MIT; see accompanying LICENSE file + +#include + +#include + +#include +#include + +#undef NDEBUG +#include + +#include + +using namespace std; + +namespace bpkg +{ + // Usage: args[0] ... + // + // Where is one of: + // + // system-package-names [...] -- ... + // + // Where is a package name, is a package manifest file. + // + // downstream-package-version [...] -- ... + // + // Where is a system version to translate, is a package + // name, and is a package manifest file. + // + int + main (int argc, char* argv[]) + try + { + assert (argc >= 2); // + + int argi (1); + string cmd (argv[argi++]); + + os_release osr; + if (cmd == "system-package-names" || + cmd == "downstream-package-version") + { + assert (argc >= 4); // + + osr.name_id = argv[argi++]; + osr.version_id = argv[argi++]; + + for (; argi != argc; ++argi) + { + string a (argv[argi]); + + if (a == "--") + break; + + osr.like_ids.push_back (move (a)); + } + } + + if (cmd == "system-package-names") + { + assert (argi != argc); // -- + string a (argv[argi++]); + assert (a == "--"); + + assert (argi != argc); // + string pn (argv[argi++]); + + assert (argi != argc); // + available_packages aps; + for (; argi != argc; ++argi) + aps.push_back (make_available_from_manifest (pn, argv[argi])); + sort_available (aps); + + strings ns ( + system_package_manager::system_package_names ( + aps, osr.name_id, osr.version_id, osr.like_ids)); + + for (const string& n: ns) + cout << n << '\n'; + } + else if (cmd == "downstream-package-version") + { + assert (argi != argc); // -- + string a (argv[argi++]); + assert (a == "--"); + + assert (argi != argc); // + string sv (argv[argi++]); + + assert (argi != argc); // + string pn (argv[argi++]); + + assert (argi != argc); // + available_packages aps; + for (; argi != argc; ++argi) + aps.push_back (make_available_from_manifest (pn, argv[argi])); + sort_available (aps); + + optional v ( + system_package_manager::downstream_package_version ( + sv, aps, osr.name_id, osr.version_id, osr.like_ids)); + + if (v) + cout << *v << '\n'; + } + else + fail << "unknown command '" << cmd << "'"; + + return 0; + } + catch (const failed&) + { + return 1; + } +} + +int +main (int argc, char* argv[]) +{ + return bpkg::main (argc, argv); +} diff --git a/bpkg/system-package-manager.test.hxx b/bpkg/system-package-manager.test.hxx new file mode 100644 index 0000000..0eb6717 --- /dev/null +++ b/bpkg/system-package-manager.test.hxx @@ -0,0 +1,104 @@ +// file : bpkg/system-package-manager.test.hxx -*- C++ -*- +// license : MIT; see accompanying LICENSE file + +#ifndef BPKG_SYSTEM_PACKAGE_MANAGER_TEST_HXX +#define BPKG_SYSTEM_PACKAGE_MANAGER_TEST_HXX + +#include + +#include // sort() + +#include +#include + +#include + +#include + +#include + +namespace bpkg +{ + // Parse the manifest as if it comes from a git repository with a single + // package and make an available package out of it. + // + inline + pair, lazy_shared_ptr> + make_available_from_manifest (const string& n, const string& f) + { + using butl::manifest_parser; + using butl::manifest_parsing; + + try + { + ifdstream ifs (f); + manifest_parser mp (ifs, f); + + package_manifest m (mp, + false /* ignore_unknown */, + true /* complete_values */); + + assert (m.name.string () == n); + + m.alt_naming = false; + m.bootstrap_build = "project = " + n + '\n'; + + shared_ptr ap ( + make_shared (move (m))); + + lazy_shared_ptr af ( + make_shared ( + repository_location ("https://example.com/" + n, + repository_type::git))); + + ap->locations.push_back (package_location {af, current_dir}); + + return make_pair (move (ap), move (af)); + } + catch (const manifest_parsing& e) + { + fail (e.name, e.line, e.column) << e.description << endf; + } + catch (const io_error& e) + { + fail << "unable to read from " << f << ": " << e << endf; + } + } + + // Make an available stub package as if it comes from git repository with + // a single package. + // + inline + pair, lazy_shared_ptr> + make_available_stub (const string& n) + { + shared_ptr ap ( + make_shared (package_name (n))); + + lazy_shared_ptr af ( + make_shared ( + repository_location ("https://example.com/" + n, + repository_type::git))); + + ap->locations.push_back (package_location {af, current_dir}); + + return make_pair (move (ap), move (af)); + } + + // Sort available packages in the version descending order. + // + inline void + sort_available (available_packages& aps) + { + using element_type = + pair, lazy_shared_ptr>; + + std::sort (aps.begin (), aps.end (), + [] (const element_type& x, const element_type& y) + { + return x.first->version > y.first->version; + }); + } +} + +#endif // BPKG_SYSTEM_PACKAGE_MANAGER_TEST_HXX diff --git a/bpkg/system-package-manager.test.testscript b/bpkg/system-package-manager.test.testscript new file mode 100644 index 0000000..b993793 --- /dev/null +++ b/bpkg/system-package-manager.test.testscript @@ -0,0 +1,102 @@ +# file : bpkg/system-package-manager.test.testscript +# license : MIT; see accompanying LICENSE file + +: system-package-names +: +{ + test.arguments += system-package-names + + : basics + : + cat <=libcurl7.64.manifest; + : 1 + name: libcurl + version: 7.64.0 + debian-name: libcurl2 libcurl2-dev + summary: curl + license: curl + EOI + cat <=libcurl7.84.manifest; + : 1 + name: libcurl + version: 7.84.0 + debian_9-name: libcurl2 libcurl2-dev libcurl2-doc + debian_10-name: libcurl4 libcurl4-openssl-dev + debian_10-name: libcurl3-gnutls libcurl4-gnutls-dev + summary: curl + license: curl + EOI +#\ + $* debian 10 -- libcurl libcurl7.64.manifest libcurl7.84.manifest >>EOO; + libcurl4 libcurl4-openssl-dev + libcurl3-gnutls libcurl4-gnutls-dev + libcurl2 libcurl2-dev libcurl2-doc + libcurl2 libcurl2-dev + EOO +#\ + $* debian 9 -- libcurl libcurl7.64.manifest libcurl7.84.manifest >>EOO; + libcurl2 libcurl2-dev libcurl2-doc + libcurl2 libcurl2-dev + EOO + $* debian '' -- libcurl libcurl7.64.manifest libcurl7.84.manifest >>EOO; + libcurl2 libcurl2-dev + EOO + $* ubuntu 16.04 debian -- libcurl libcurl7.64.manifest libcurl7.84.manifest >>EOO + libcurl2 libcurl2-dev + EOO +} + +: downstream-package-version +: +{ + test.arguments += downstream-package-version + + : basics + : + cat <=libssl1.manifest; + : 1 + name: libssl + version: 1.1.1 + upstream-version: 1.1.1n + debian-to-downstream-version: /1\.1\.1[a-z]/1.1.1/ + summary: openssl + license: openssl + EOI + cat <=libssl3.manifest; + : 1 + name: libssl + version: 3.0.0 + debian-to-downstream-version: /([3-9])\.([0-9]+)\.([0-9]+)/\1.\2.\3/ + summary: openssl + license: openssl + EOI + $* debian 10 -- 1.1.1l libssl libssl1.manifest libssl3.manifest >'1.1.1'; + $* debian 10 -- 3.0.7 libssl libssl1.manifest libssl3.manifest >'3.0.7'; + $* debian '' -- 1.1.1l libssl libssl1.manifest libssl3.manifest >'1.1.1'; + $* debian '' -- 3.0.7 libssl libssl1.manifest libssl3.manifest >'3.0.7'; + $* ubuntu 16.04 debian -- 1.1.1l libssl libssl1.manifest libssl3.manifest >'1.1.1'; + $* ubuntu 16.05 debian -- 3.0.7 libssl libssl1.manifest libssl3.manifest >'3.0.7' + + : order + : + cat <=libssl1.manifest; + : 1 + name: libssl + version: 1.1.1 + debian-to-downstream-version: /.*/0/ + summary: openssl + license: openssl + EOI + cat <=libssl3.manifest; + : 1 + name: libssl + version: 3.0.0 + debian_9-to-downstream-version: /.*/9/ + debian_10-to-downstream-version: /.*/10/ + summary: openssl + license: openssl + EOI + $* debian 10 -- 1 libssl libssl1.manifest libssl3.manifest >'10'; + $* debian 9 -- 1 libssl libssl1.manifest libssl3.manifest >'9'; + $* debian 8 -- 1 libssl libssl1.manifest libssl3.manifest >'0' +} -- cgit v1.1