Repository navigation
Expand file tree
/
Copy pathpath_spec.lua
More file actions
204 lines (193 loc) · 7.26 KB
/
Copy pathpath_spec.lua
File metadata and controls
204 lines (193 loc) · 7.26 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
local util = require("spec.util")
local path = require("cyan.fs.path")
path.separator = '/'
describe("fs.path api", function()
describe("new", function()
it("should split a path on / no matter what path.separator is", function()
path.separator = "teehee"
local p = path.new("foo/bar/baz")
assert.are.same(p, {"foo", "bar", "baz"})
end)
for _, sep in ipairs{'/', '\\'} do
it("should split a path on os path separators [" .. sep .. "]", function()
path.separator = sep
local str = table.concat({"foo", "bar", "baz"}, sep)
local p = path.new(str, true)
assert.are.same({"foo", "bar", "baz"}, p)
finally(function() path.separator = '/' end)
end)
end
it("should be okay with having traversals (..)", function()
assert.are.same({".."}, path.new(".."))
end)
end)
describe("Path:is_absolute", function()
it("should be able to check for absolute paths for unix", function()
local p = path.new("/foo/bar")
assert(p:is_absolute())
end)
it("should be able to check for absolute paths for windows", function()
path.separator = '\\'
local p = path.new("C:\\foo\\bar", true)
local res = p:is_absolute()
path.separator = '/'
assert(res)
end)
end)
describe("Path:ancestors", function()
it("should produce the parents of the path", function()
local p = path.new("a/b/c/d")
local expected = {
{ "a" },
{ "a", "b" },
{ "a", "b", "c" },
}
local actual = {}
for ancestor in p:ancestors() do
table.insert(actual, ancestor)
end
assert.are.same(expected, actual)
end)
end)
describe("Path:to_real_path", function()
it("should concat the path with real path separators", function()
do
local p = path.new("foo/bar/baz")
assert.are.equal(p:to_real_path(), "foo/bar/baz")
end
do
path.separator = '\\'
local p = path.new("foo\\bar\\baz")
local res = p:to_real_path()
path.separator = '/'
assert.are.equal(res, "foo\\bar\\baz")
end
end)
end)
describe("Path:append", function()
it("should mutate the given path by properly appending the path", function()
local p = path.new("a/b/c")
p:append("d/e")
assert.are.same(p, {"a", "b", "c", "d", "e"})
p:append(path.new("f"))
assert.are.same(p, {"a", "b", "c", "d", "e", "f"})
end)
end)
describe("Path:prepend", function()
it("should mutate the given path by properly prepending the path", function()
local p = path.new("a/b/c")
p:prepend("d/e")
assert.are.same(p, {"d", "e", "a", "b", "c"})
p:prepend(path.new("f"))
assert.are.same(p, {"f", "d", "e", "a", "b", "c"})
end)
end)
describe("Path:remove_leading", function()
it("should remove the leading path if it is present", function()
do
local p = path.new("foo/bar/baz")
p:remove_leading("foo/bar")
assert.are.same(p, {"baz"})
end
do
local p = path.new("foo/bar/baz")
p:remove_leading(path.new("foo/bar"))
assert.are.same(p, {"baz"})
end
end)
end)
describe("Path:copy", function()
it("should produce a copy of the given path", function()
local p = path.new("a/b/c")
local copy = p:copy()
p[1] = "d"
assert.are["not"].same(p, copy)
end)
end)
describe("Path:match", function()
local function assert_match(p, patt)
assert(getmetatable(p).__name == "cyan.fs.path.Path", "p is not a Path")
local res = p:match(patt)
assert(res, p:tostring() .. " should have matched " .. patt)
end
local function assert_not_match(p, patt)
assert(getmetatable(p).__name == "cyan.fs.path.Path", "p is not a Path")
local res = p:match(patt)
assert(not res, p:tostring() .. " should not have matched " .. patt)
end
it("should match literals with no globs", function()
local p = path.new("foo/bar/baz")
assert_not_match(p, "foo/bar")
assert_match(p, "foo/bar/baz")
assert_not_match(p, "foo/bar/bazz")
end)
it("should treat globs as matching non path separators", function()
local p = path.new("foo/bar/baz")
assert_match(p, "*/bar/baz")
assert_match(p, "foo/*/baz")
assert_match(p, "*/*/baz")
assert_match(p, "f*/b*/b*z")
assert_match(p, "*/*/*")
assert_not_match(p, "*")
assert_not_match(p, "foo/*")
assert_not_match(p, "*/*")
assert_not_match(p, "*/*/bazzz")
assert_not_match(path.new("build/cyan/commands/blah.tl"), "build/cyan/*")
end)
it("should treat double globs as matching any number of directories", function()
local p = path.new("foo/bar/baz/bat")
assert_match(p, "**/bat")
assert_match(p, "foo/bar/**/bat")
assert_match(p, "foo/bar/baz/**/bat")
assert_not_match(p, "foo/**/foo")
assert_not_match(p, "**/baz/foo")
end)
it("should be able to mix globs and double globs", function()
local p = path.new("foo/bar/baz/bat")
assert_match(p, "foo/b*/**/bat")
assert_match(p, "*/**/*/bat")
assert_match(p, "*/**/bat")
assert_match(p, "**/bat")
assert_match(p, "**/*")
assert_not_match(p, "**/bar/bat")
assert_not_match(p, "foo/*/**/baz")
end)
end)
describe("Path:relative_to", function()
local function assert_eq_relative(expected, p, relative_to)
assert.are.same(
path.new(expected),
path.new(p):relative_to(path.new(relative_to))
)
5356
end
it("should prepend .. as necessary", function()
assert_eq_relative("..", "/foo/bar", "/foo/bar/baz")
assert_eq_relative("../..", "/foo/bar", "/foo/bar/baz/bat")
end)
it("should append parts of the first directory as needed", function()
assert_eq_relative("bar", "/foo/bar", "/foo")
assert_eq_relative("bar/baz", "/foo/bar/baz", "/foo")
end)
it("should prepend .. and append parts of the first dir as needed", function()
assert_eq_relative("../bar", "/foo/bar", "/foo/bat")
assert_eq_relative("../bar/baz", "/foo/bar/baz", "/foo/bat")
assert_eq_relative("../../bar/baz", "/bar/baz", "/foo/bat")
end)
it("should work with relative paths", function()
assert_eq_relative("../bar", "foo/bar", "foo/bat")
assert_eq_relative("../bar/baz", "foo/bar/baz", "foo/bat")
assert_eq_relative("../../bar/baz", "bar/baz", "foo/bat")
end)
end)
describe("Path:is_in", function()
it("should work with relative paths", function()
assert(path.new("foo/bar"):is_in("foo"))
end)
it("should work with absolute paths", function()
assert(path.new("/foo/bar"):is_in("/foo"))
end)
it("should assume an empty path is the current dir", function()
assert(path.new("foo"):is_in(""))
end)
end)
end)