diff --git a/src/libfetchers-tests/input.cc b/src/libfetchers-tests/input.cc new file mode 100644 index 000000000..faff55f2c --- /dev/null +++ b/src/libfetchers-tests/input.cc @@ -0,0 +1,61 @@ +#include "nix/fetchers/fetch-settings.hh" +#include "nix/fetchers/attrs.hh" +#include "nix/fetchers/fetchers.hh" + +#include + +#include + +namespace nix { + +using fetchers::Attr; + +struct InputFromAttrsTestCase +{ + fetchers::Attrs attrs; + std::string expectedUrl; + std::string description; + fetchers::Attrs expectedAttrs = attrs; +}; + +class InputFromAttrsTest : public ::testing::WithParamInterface, public ::testing::Test +{}; + +TEST_P(InputFromAttrsTest, attrsAreCorrectAndRoundTrips) +{ + fetchers::Settings fetchSettings; + + const auto & testCase = GetParam(); + + auto input = fetchers::Input::fromAttrs(fetchSettings, fetchers::Attrs(testCase.attrs)); + + EXPECT_EQ(input.toAttrs(), testCase.expectedAttrs); + EXPECT_EQ(input.toURLString(), testCase.expectedUrl); + + auto input2 = fetchers::Input::fromAttrs(fetchSettings, input.toAttrs()); + EXPECT_EQ(input, input2); + EXPECT_EQ(input.toAttrs(), input2.toAttrs()); +} + +INSTANTIATE_TEST_SUITE_P( + InputFromAttrs, + InputFromAttrsTest, + ::testing::Values( + // Test for issue #14429. + InputFromAttrsTestCase{ + .attrs = + { + {"url", Attr("git+ssh://git@github.com/NixOS/nixpkgs")}, + {"type", Attr("git")}, + }, + .expectedUrl = "git+ssh://git@github.com/NixOS/nixpkgs", + .description = "strips_git_plus_prefix", + .expectedAttrs = + { + {"url", Attr("ssh://git@github.com/NixOS/nixpkgs")}, + {"type", Attr("git")}, + }, + }), + [](const ::testing::TestParamInfo & info) { return info.param.description; }); + +} // namespace nix diff --git a/src/libfetchers-tests/meson.build b/src/libfetchers-tests/meson.build index a18f64d79..6bccdb05c 100644 --- a/src/libfetchers-tests/meson.build +++ b/src/libfetchers-tests/meson.build @@ -42,6 +42,7 @@ sources = files( 'access-tokens.cc', 'git-utils.cc', 'git.cc', + 'input.cc', 'nix_api_fetchers.cc', 'public-key.cc', )