diff options
author | Qyriad <qyriad@qyriad.me> | 2024-05-21 12:10:24 -0600 |
---|---|---|
committer | Qyriad <qyriad@qyriad.me> | 2024-05-23 14:11:10 -0600 |
commit | 0565f97e78f9ebd97849f79a3327f51e2157fc53 (patch) | |
tree | 34f221389a19bab7dd76533d0309eb5d05396590 /tests | |
parent | 677cf75473d0dd86119c4535d8733a6a0b1100c0 (diff) |
add libcmd test for lookupFileArg
Change-Id: I9e2ef170ffe916f902daec8b5630d29434c5d5f2
Diffstat (limited to 'tests')
-rw-r--r-- | tests/unit/libcmd/args.cc | 57 | ||||
-rw-r--r-- | tests/unit/meson.build | 27 |
2 files changed, 84 insertions, 0 deletions
diff --git a/tests/unit/libcmd/args.cc b/tests/unit/libcmd/args.cc new file mode 100644 index 000000000..73550dacf --- /dev/null +++ b/tests/unit/libcmd/args.cc @@ -0,0 +1,57 @@ +#include <iostream> +#include <memory> +#include <string_view> + +#include <boost/core/demangle.hpp> +#include <gtest/gtest.h> + +#include "common-eval-args.hh" +#include "eval.hh" +#include "filetransfer.hh" +#include "shared.hh" +#include "store-api.hh" +#include "util.hh" + +constexpr std::string_view INVALID_CHANNEL = "channel:example"; +constexpr std::string_view CHANNEL_URL = "https://nixos.org/channels/example/nixexprs.tar.xz"; + +namespace nix +{ + +TEST(Arguments, lookupFileArg) { + initNix(); + initGC(); + + std::string const unitDataPath = getEnv("_NIX_TEST_UNIT_DATA").value(); + // Meson should be allowed to pass us a relative path here tbh. + auto const canonDataPath = CanonPath::fromCwd(unitDataPath); + + std::string const searchPathElem = fmt("example=%s", unitDataPath); + + SearchPath searchPath; + searchPath.elements.push_back(SearchPath::Elem::parse(searchPathElem)); + + auto store = openStore("dummy://"); + auto statePtr = std::make_shared<EvalState>(searchPath, store, store); + auto & state = *statePtr; + + SourcePath const foundUnitData = lookupFileArg(state, "<example>"); + EXPECT_EQ(foundUnitData.path, canonDataPath); + + // lookupFileArg should not resolve <search paths> if anything else is before or after it. + SourcePath const yepEvenSpaces = lookupFileArg(state, " <example>"); + EXPECT_EQ(yepEvenSpaces.path, CanonPath::fromCwd(" <example>")); + EXPECT_EQ(lookupFileArg(state, "<example>/nixos").path, CanonPath::fromCwd("<example>/nixos")); + + try { + lookupFileArg(state, INVALID_CHANNEL); + } catch (FileTransferError const & ex) { + std::string_view const msg(ex.what()); + EXPECT_NE(msg.find(CHANNEL_URL), msg.npos); + } + + SourcePath const normalFile = lookupFileArg(state, unitDataPath); + EXPECT_EQ(normalFile.path, canonDataPath); +} + +} diff --git a/tests/unit/meson.build b/tests/unit/meson.build index f5355cce8..6041ba497 100644 --- a/tests/unit/meson.build +++ b/tests/unit/meson.build @@ -212,3 +212,30 @@ test( protocol : 'gtest', verbose : true, ) + +libcmd_tester = executable( + 'liblixcmd-tests', + files('libcmd/args.cc'), + dependencies : [ + liblixcmd, + liblixutil, + liblixmain, + liblixexpr, + liblixstore, + gtest, + boost, + ], +) + +test( + 'libcmd-unit-tests', + libcmd_tester, + args : tests_args, + env : { + # No special meaning here, it's just a file laying around that is unlikely to go anywhere + # any time soon. + '_NIX_TEST_UNIT_DATA': meson.project_source_root() / 'src/nix-env/buildenv.nix', + }, + suite : 'check', + protocol : 'gtest', +) |