lix/tests/unit/libcmd/args.cc
eldritch horrors 81559ea8ad treewide: add evaluator aliases for eval states
this is not necessary in any way, but it will make the following changes
smaller and easier to review. the aliases could also be added piecemeal,
but doing it here lets us lean heavily on our compilers for correctness.

(teacher notes: here the author foreshadows the shape of things to come.
not all names change, and only the names unchanged are those which will,
over time, become ever more unrecognizable. note especially nix/main.cc,
where `state` is not only cloned, but itself changes pointerness. it can
be seen as a nod to the trans community, but more realistically it is no
more than foreshadowing the future where `state` is only seen by proxy.)

Change-Id: I7732025e58df089b7f8e564fc63960cd91729d09
2024-12-03 20:38:41 +01:00

56 lines
1.8 KiB
C++

#include <iostream>
#include <memory>
#include <string_view>
#include <boost/core/demangle.hpp>
#include <gtest/gtest.h>
#include "lix/libcmd/common-eval-args.hh"
#include "lix/libexpr/eval.hh"
#include "lix/libstore/filetransfer.hh"
#include "lix/libmain/shared.hh"
#include "lix/libstore/store-api.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();
initLibExpr();
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 state = std::make_shared<EvalState>(searchPath, store, store);
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);
}
}