2024-05-21 18:10:24 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <memory>
|
|
|
|
#include <string_view>
|
|
|
|
|
|
|
|
#include <boost/core/demangle.hpp>
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2024-11-12 23:07:17 +00:00
|
|
|
#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"
|
2024-05-21 18:10:24 +00:00
|
|
|
|
|
|
|
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();
|
2024-11-15 15:15:11 +00:00
|
|
|
initLibExpr();
|
2024-05-21 18:10:24 +00:00
|
|
|
|
|
|
|
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://");
|
2024-12-03 19:38:41 +00:00
|
|
|
auto state = std::make_shared<Evaluator>(searchPath, store, store);
|
2024-05-21 18:10:24 +00:00
|
|
|
|
2024-12-03 19:38:41 +00:00
|
|
|
SourcePath const foundUnitData = lookupFileArg(*state, "<example>");
|
2024-05-21 18:10:24 +00:00
|
|
|
EXPECT_EQ(foundUnitData.path, canonDataPath);
|
|
|
|
|
|
|
|
// lookupFileArg should not resolve <search paths> if anything else is before or after it.
|
2024-12-03 19:38:41 +00:00
|
|
|
SourcePath const yepEvenSpaces = lookupFileArg(*state, " <example>");
|
2024-05-21 18:10:24 +00:00
|
|
|
EXPECT_EQ(yepEvenSpaces.path, CanonPath::fromCwd(" <example>"));
|
2024-12-03 19:38:41 +00:00
|
|
|
EXPECT_EQ(lookupFileArg(*state, "<example>/nixos").path, CanonPath::fromCwd("<example>/nixos"));
|
2024-05-21 18:10:24 +00:00
|
|
|
|
|
|
|
try {
|
2024-12-03 19:38:41 +00:00
|
|
|
lookupFileArg(*state, INVALID_CHANNEL);
|
2024-05-21 18:10:24 +00:00
|
|
|
} catch (FileTransferError const & ex) {
|
|
|
|
std::string_view const msg(ex.what());
|
|
|
|
EXPECT_NE(msg.find(CHANNEL_URL), msg.npos);
|
|
|
|
}
|
|
|
|
|
2024-12-03 19:38:41 +00:00
|
|
|
SourcePath const normalFile = lookupFileArg(*state, unitDataPath);
|
2024-05-21 18:10:24 +00:00
|
|
|
EXPECT_EQ(normalFile.path, canonDataPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|