aboutsummaryrefslogtreecommitdiff
path: root/tests/unit/libcmd/args.cc
blob: 73550dacfdb5b70fe3e0d98d844035bff08ce80e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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);
}

}