aboutsummaryrefslogtreecommitdiff
path: root/tests/unit/libcmd/args.cc
diff options
context:
space:
mode:
authorQyriad <qyriad@qyriad.me>2024-05-23 23:28:42 +0000
committerGerrit Code Review <gerrit@lix-systems>2024-05-23 23:28:42 +0000
commit00bf2b105d677461bda0a5ba9b29bedb350c1eb1 (patch)
treeba1c87c6385655da6ce9336a2bf79423cf5c2a62 /tests/unit/libcmd/args.cc
parent9530b7f2b2b653fc11753ce452636896350324ff (diff)
parenta0172dc81bff7f0665de77e771919e2e0b554788 (diff)
Merge changes I462a8cf0,I3b0bcea3,I2acd56e7,Ifc149764,I9e2ef170 into main
* changes: docs: linkify nix3-build mention in nix-build.md build: make internal-api-docs PHONY cleanup lookupFileArg add docstring to lookupFileArg add libcmd test for lookupFileArg
Diffstat (limited to 'tests/unit/libcmd/args.cc')
-rw-r--r--tests/unit/libcmd/args.cc57
1 files changed, 57 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);
+}
+
+}