From bdb86456ec665f4367bc1caf52902a3a3a4fbbed Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Sat, 20 Jun 2020 18:54:44 +0300 Subject: Merge package external test, example, and benchmark dependencies into typed test dependency --- libbpkg/manifest.cxx | 90 +++++++++++++++++++++++++++++++--------------------- libbpkg/manifest.hxx | 38 +++++++++++++++++++--- 2 files changed, 88 insertions(+), 40 deletions(-) diff --git a/libbpkg/manifest.cxx b/libbpkg/manifest.cxx index 9caa229..4a104f4 100644 --- a/libbpkg/manifest.cxx +++ b/libbpkg/manifest.cxx @@ -1511,6 +1511,31 @@ namespace bpkg return r; } + // test_dependency_type + // + string + to_string (test_dependency_type t) + { + switch (t) + { + case test_dependency_type::tests: return "tests"; + case test_dependency_type::examples: return "examples"; + case test_dependency_type::benchmarks: return "benchmarks"; + } + + assert (false); // Can't be here. + return string (); + } + + test_dependency_type + to_test_dependency_type (const string& t) + { + if (t == "tests") return test_dependency_type::tests; + else if (t == "examples") return test_dependency_type::examples; + else if (t == "benchmarks") return test_dependency_type::benchmarks; + else throw invalid_argument ("invalid test dependency type '" + t + "'"); + } + // pkg_package_manifest // static build_class_expr @@ -1716,14 +1741,12 @@ namespace bpkg // optional upstream_version; - // We will cache the depends, tests, examples, and benchmarks manifest - // values to parse and, if requested, complete the version constraints - // later, after the version value is parsed. + // We will cache the depends and the test dependency manifest values to + // parse and, if requested, complete the version constraints later, after + // the version value is parsed. // vector dependencies; small_vector tests; - small_vector examples; - small_vector benchmarks; // We will cache the description and its type values to validate them // later, after both are parsed. @@ -2040,18 +2063,10 @@ namespace bpkg { dependencies.push_back (move (nv)); } - else if (n == "tests") + else if (n == "tests" || n == "examples" || n == "benchmarks") { tests.push_back (move (nv)); } - else if (n == "examples") - { - examples.push_back (move (nv)); - } - else if (n == "benchmarks") - { - benchmarks.push_back (move (nv)); - } else if (n == "location") { if (flag (package_manifest_flags::forbid_location)) @@ -2201,8 +2216,7 @@ namespace bpkg } // Now, when the version manifest value is parsed, we can parse the - // dependencies, tests, examples, and benchmarks and complete their - // constraints, if requested. + // dependencies and complete their constraints, if requested. // auto parse_dependency = [&m, cd, &flag, &bad_value] (string&& d, const char* what) @@ -2268,6 +2282,8 @@ namespace bpkg return r; }; + // Parse the regular dependencies. + // for (name_value& d: dependencies) { nv = move (d); // Restore as bad_value() uses its line/column. @@ -2304,21 +2320,29 @@ namespace bpkg m.dependencies.push_back (da); } - auto parse_deps = [&nv, &parse_dependency] - (small_vector&& nvs, const char* what) + // Parse the test dependencies. + // + for (name_value& v: tests) { - small_vector r; - for (name_value& v: nvs) + nv = move (v); // Restore as bad_value() uses its line/column. + + dependency d (parse_dependency (move (nv.value), nv.name.c_str ())); + + try { - nv = move (v); // Restore as bad_value() uses its line/column. - r.push_back (parse_dependency (move (nv.value), what)); + m.tests.emplace_back ( + move (d.name), + to_test_dependency_type (nv.name), + move (d.constraint)); } - return r; - }; - - m.tests = parse_deps (move (tests), "tests"); - m.examples = parse_deps (move (examples), "examples"); - m.benchmarks = parse_deps (move (benchmarks), "benchmarks"); + catch (const invalid_argument&) + { + // to_test_dependency_type() can't throw since the type string is + // already validated. + // + assert (false); + } + } if (m.description && !m.description_type && @@ -2699,14 +2723,8 @@ namespace bpkg : (r.buildtime ? "* " : "")) + serializer::merge_comment (concatenate (r, " | "), r.comment)); - for (const dependency& t: m.tests) - s.next ("tests", t.string ()); - - for (const dependency& t: m.examples) - s.next ("examples", t.string ()); - - for (const dependency& t: m.benchmarks) - s.next ("benchmarks", t.string ()); + for (const test_dependency& p: m.tests) + s.next (to_string (p.type), p.string ()); for (const build_class_expr& e: m.builds) s.next ("builds", serializer::merge_comment (e.string (), e.comment)); diff --git a/libbpkg/manifest.hxx b/libbpkg/manifest.hxx index b82b87e..fd528cd 100644 --- a/libbpkg/manifest.hxx +++ b/libbpkg/manifest.hxx @@ -276,7 +276,7 @@ namespace bpkg // // See libbutl/url.mxx for details. // - class manifest_url: public butl::url + class LIBBPKG_EXPORT manifest_url: public butl::url { public: std::string comment; @@ -668,6 +668,38 @@ namespace bpkg return os << to_string (t); } + enum class test_dependency_type + { + tests, + examples, + benchmarks + }; + + LIBBPKG_EXPORT std::string + to_string (test_dependency_type); + + // May throw std::invalid_argument. + // + LIBBPKG_EXPORT test_dependency_type + to_test_dependency_type (const std::string&); + + inline std::ostream& + operator<< (std::ostream& os, test_dependency_type t) + { + return os << to_string (t); + } + + struct test_dependency: dependency + { + test_dependency_type type; + + test_dependency () = default; + test_dependency (package_name n, + test_dependency_type t, + butl::optional c) + : dependency {std::move (n), std::move (c)}, type (t) {} + }; + class LIBBPKG_EXPORT package_manifest { public: @@ -704,9 +736,7 @@ namespace bpkg butl::optional build_error_email; std::vector dependencies; std::vector requirements; - butl::small_vector tests; - butl::small_vector examples; - butl::small_vector benchmarks; + butl::small_vector tests; butl::small_vector builds; std::vector build_constraints; -- cgit v1.1