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
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include "lix/libutil/config.hh"

#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include <sstream>

using testing::Eq;

namespace nix {

class PathsSettingTestConfig : public Config
{
public:
    PathsSettingTestConfig() : Config() {}

    PathsSetting<Paths> paths{this, Paths(), "paths", "documentation"};
};

struct PathsSettingTest : public ::testing::Test
{
public:
    PathsSettingTestConfig mkConfig()
    {
        return PathsSettingTestConfig();
    }
};

TEST_F(PathsSettingTest, parse)
{
    auto config = mkConfig();
    // Not an absolute path:
    ASSERT_THROW(config.paths.parse("puppy.nix", {}), Error);

    ASSERT_THAT(config.paths.parse("/puppy.nix", {}), Eq<Paths>({"/puppy.nix"}));

    // Splits on whitespace:
    ASSERT_THAT(
        config.paths.parse("/puppy.nix /doggy.nix", {}), Eq<Paths>({"/puppy.nix", "/doggy.nix"})
    );

    // Splits on _any_ whitespace:
    ASSERT_THAT(
        config.paths.parse("/puppy.nix \t  /doggy.nix\n\n\n/borzoi.nix\r/goldie.nix", {}),
        Eq<Paths>({"/puppy.nix", "/doggy.nix", "/borzoi.nix", "/goldie.nix"})
    );

    // Canonicizes paths:
    ASSERT_THAT(config.paths.parse("/puppy/../doggy.nix", {}), Eq<Paths>({"/doggy.nix"}));
}

TEST_F(PathsSettingTest, parseRelative)
{
    auto options = ApplyConfigOptions{.path = "/doggy/kinds/config.nix"};
    auto config = mkConfig();
    ASSERT_THAT(
        config.paths.parse("puppy.nix", options),
        Eq<Paths>({"/doggy/kinds/puppy.nix"})
    );

    // Splits on whitespace:
    ASSERT_THAT(
        config.paths.parse("puppy.nix /doggy.nix", options), Eq<Paths>({"/doggy/kinds/puppy.nix", "/doggy.nix"})
    );

    // Canonicizes paths:
    ASSERT_THAT(config.paths.parse("../soft.nix", options), Eq<Paths>({"/doggy/soft.nix"}));

    // Canonicizes paths:
    ASSERT_THAT(config.paths.parse("./soft.nix", options), Eq<Paths>({"/doggy/kinds/soft.nix"}));
}

TEST_F(PathsSettingTest, parseHome)
{
    auto options = ApplyConfigOptions{
        .path = "/doggy/kinds/config.nix",
        .home = "/home/puppy"
    };
    auto config = mkConfig();

    ASSERT_THAT(
        config.paths.parse("puppy.nix", options),
        Eq<Paths>({"/doggy/kinds/puppy.nix"})
    );

    ASSERT_THAT(
        config.paths.parse("~/.config/nix/puppy.nix", options),
        Eq<Paths>({"/home/puppy/.config/nix/puppy.nix"})
    );

    // Splits on whitespace:
    ASSERT_THAT(
        config.paths.parse("~/puppy.nix ~/doggy.nix", options),
        Eq<Paths>({"/home/puppy/puppy.nix", "/home/puppy/doggy.nix"})
    );

    // Canonicizes paths:
    ASSERT_THAT(config.paths.parse("~/../why.nix", options), Eq<Paths>({"/home/why.nix"}));

    // Home paths for other users not allowed. Needs to start with `~/`.
    ASSERT_THROW(config.paths.parse("~root/config.nix", options), Error);
}

TEST_F(PathsSettingTest, append)
{
    auto config = mkConfig();

    ASSERT_TRUE(config.paths.isAppendable());

    // Starts with no paths:
    ASSERT_THAT(config.paths.get(), Eq<Paths>({}));

    // Can append a path:
    config.paths.set("/puppy.nix", true);

    ASSERT_THAT(config.paths.get(), Eq<Paths>({"/puppy.nix"}));

    // Can append multiple paths:
    config.paths.set("/silly.nix /doggy.nix", true);

    ASSERT_THAT(config.paths.get(), Eq<Paths>({"/puppy.nix", "/silly.nix", "/doggy.nix"}));
}

} // namespace nix