aboutsummaryrefslogtreecommitdiffstats
path: root/tests/mason-core/path_spec.lua
blob: 905df6ab6e0c167b3c3c69907ec4879cb8a16705 (plain) (blame)
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
local path = require "mason-core.path"

describe("path", function()
    it("concatenates paths", function()
        assert.equals("foo/bar/baz", path.concat { "foo", "bar", "baz" })
        assert.equals("foo/bar/baz", path.concat { "foo/", "bar/", "baz/" })
    end)

    it("identifies subdirectories", function()
        assert.is_true(path.is_subdirectory("/foo/bar", "/foo/bar/baz"))
        assert.is_true(path.is_subdirectory("/foo/bar", "/foo/bar"))
        assert.is_false(path.is_subdirectory("/foo/bar", "/foo/bas/baz"))
        assert.is_false(path.is_subdirectory("/foo/bar", "/foo/bars/baz"))
    end)

    describe("relative ::", function()
        local matrix = {
            {
                from = "/home/user/dir1/fileA",
                to = "/home/user/dir1/fileB",
                expected = "fileB",
            },
            {
                from = "/home/user/dir1/fileA",
                to = "/home/user/dir2/fileC",
                expected = "../dir2/fileC",
            },
            {
                from = "/home/user/dir1/subdir/fileD",
                to = "/home/user/dir1/fileE",
                expected = "../fileE",
            },
            {
                from = "/home/user/dir1/subdir/fileD",
                to = "/home/user/dir1/subdir/fileF",
                expected = "fileF",
            },
            {
                from = "/home/user/dir1/fileG",
                to = "/home/user/dir2/subdir/fileH",
                expected = "../dir2/subdir/fileH",
            },
            {
                from = "/home/user/dir1/subdir1/subdir2/fileI",
                to = "/home/user/dir1/fileJ",
                expected = "../../fileJ",
            },
            {
                from = "/fileK",
                to = "/home/fileL",
                expected = "home/fileL",
            },
            {
                from = "/home/user/fileM",
                to = "/home/user/dir1/dir2/fileL",
                expected = "dir1/dir2/fileL",
            },
        }

        for _, test_case in ipairs(matrix) do
            it(("should resolve from %s to %s: %s"):format(test_case.from, test_case.to, test_case.expected), function()
                assert.equals(test_case.expected, path.relative(test_case.from, test_case.to))
            end)
        end
    end)
end)