aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2023-01-30 13:38:41 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2023-01-30 13:39:06 +0200
commit41083007e6b1d2e434663c2d6c074e806e417ea9 (patch)
tree1eb46dff36ede91f7025c83cb4754687a030a2b2
parent05f3943825404dc80ce7d9a061ac15a98907feaa (diff)
Add tests for common functions
-rw-r--r--bpkg/system-package-manager-debian.test.cxx65
-rw-r--r--bpkg/system-package-manager-debian.test.testscript2
-rw-r--r--bpkg/system-package-manager.hxx8
-rw-r--r--bpkg/system-package-manager.test.cxx124
-rw-r--r--bpkg/system-package-manager.test.hxx104
-rw-r--r--bpkg/system-package-manager.test.testscript102
6 files changed, 344 insertions, 61 deletions
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 <bpkg/system-package-manager-debian.hxx>
-#include <bpkg/types.hxx>
-#include <bpkg/utility.hxx>
-
#include <map>
#include <iostream>
-#include <libbutl/manifest-parser.hxx>
-
-#include <libbpkg/manifest.hxx>
-
-#include <bpkg/package.hxx>
+#include <bpkg/types.hxx>
+#include <bpkg/utility.hxx>
#undef NDEBUG
#include <cassert>
+#include <bpkg/system-package-manager.test.hxx>
+
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<available_package> ap (
- make_shared<available_package> (move (m)));
-
- lazy_shared_ptr<repository_fragment> af (
- make_shared<repository_fragment> (
- 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<strings, path>* 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<const string, available_packages>& p: aps)
{
if (p.second.empty ())
- {
- shared_ptr<available_package> ap (
- make_shared<available_package> (package_name (p.first)));
+ p.second.push_back (make_available_stub (p.first));
- lazy_shared_ptr<repository_fragment> af (
- make_shared<repository_fragment> (
- 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
// <distribution>-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 <bpkg/system-package-manager.hxx>
+
+#include <iostream>
+
+#include <bpkg/types.hxx>
+#include <bpkg/utility.hxx>
+
+#undef NDEBUG
+#include <cassert>
+
+#include <bpkg/system-package-manager.test.hxx>
+
+using namespace std;
+
+namespace bpkg
+{
+ // Usage: args[0] <command> ...
+ //
+ // Where <command> is one of:
+ //
+ // system-package-names <name-id> <ver-id> [<like-id>...] -- <pkg> <file>...
+ //
+ // Where <pkg> is a package name, <file> is a package manifest file.
+ //
+ // downstream-package-version <name-id> <ver-id> [<like-id>...] -- <ver> <pkg> <file>...
+ //
+ // Where <ver> is a system version to translate, <pkg> is a package
+ // name, and <file> is a package manifest file.
+ //
+ int
+ main (int argc, char* argv[])
+ try
+ {
+ assert (argc >= 2); // <command>
+
+ int argi (1);
+ string cmd (argv[argi++]);
+
+ os_release osr;
+ if (cmd == "system-package-names" ||
+ cmd == "downstream-package-version")
+ {
+ assert (argc >= 4); // <name-id> <ver-id>
+
+ 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); // <pkg>
+ string pn (argv[argi++]);
+
+ assert (argi != argc); // <file>
+ 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); // <ver>
+ string sv (argv[argi++]);
+
+ assert (argi != argc); // <pkg>
+ string pn (argv[argi++]);
+
+ assert (argi != argc); // <file>
+ available_packages aps;
+ for (; argi != argc; ++argi)
+ aps.push_back (make_available_from_manifest (pn, argv[argi]));
+ sort_available (aps);
+
+ optional<version> 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 <bpkg/system-package-manager.hxx>
+
+#include <algorithm> // sort()
+
+#include <bpkg/types.hxx>
+#include <bpkg/utility.hxx>
+
+#include <libbutl/manifest-parser.hxx>
+
+#include <libbpkg/manifest.hxx>
+
+#include <bpkg/package.hxx>
+
+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<shared_ptr<available_package>, lazy_shared_ptr<repository_fragment>>
+ 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<available_package> ap (
+ make_shared<available_package> (move (m)));
+
+ lazy_shared_ptr<repository_fragment> af (
+ make_shared<repository_fragment> (
+ 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<shared_ptr<available_package>, lazy_shared_ptr<repository_fragment>>
+ make_available_stub (const string& n)
+ {
+ shared_ptr<available_package> ap (
+ make_shared<available_package> (package_name (n)));
+
+ lazy_shared_ptr<repository_fragment> af (
+ make_shared<repository_fragment> (
+ 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<shared_ptr<available_package>, lazy_shared_ptr<repository_fragment>>;
+
+ 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 <<EOI >=libcurl7.64.manifest;
+ : 1
+ name: libcurl
+ version: 7.64.0
+ debian-name: libcurl2 libcurl2-dev
+ summary: curl
+ license: curl
+ EOI
+ cat <<EOI >=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 <<EOI >=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 <<EOI >=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 <<EOI >=libssl1.manifest;
+ : 1
+ name: libssl
+ version: 1.1.1
+ debian-to-downstream-version: /.*/0/
+ summary: openssl
+ license: openssl
+ EOI
+ cat <<EOI >=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'
+}