1
1
local Path = require " plenary.path2"
2
2
local path = Path .path
3
- -- local compat = require "plenary.compat"
3
+ local compat = require " plenary.compat"
4
4
local iswin = vim .loop .os_uname ().sysname == " Windows_NT"
5
5
6
6
local hasshellslash = vim .fn .exists " +shellslash" == 1
@@ -12,25 +12,21 @@ local function set_shellslash(bool)
12
12
end
13
13
end
14
14
15
- local function it_ssl (name , test_fn )
16
- if not hasshellslash then
17
- it (name , test_fn )
18
- else
19
- local orig = vim .o .shellslash
20
- vim .o .shellslash = true
21
- it (name .. " - shellslash" , test_fn )
22
-
23
- vim .o .shellslash = false
24
- it (name .. " - noshellslash" , test_fn )
25
- vim .o .shellslash = orig
26
- end
27
- end
28
-
29
15
local function it_cross_plat (name , test_fn )
30
16
if not iswin then
31
17
it (name .. " - unix" , test_fn )
32
18
else
33
- it_ssl (name .. " - windows" , test_fn )
19
+ if not hasshellslash then
20
+ it (name .. " - windows" , test_fn )
21
+ else
22
+ local orig = vim .o .shellslash
23
+ vim .o .shellslash = true
24
+ it (name .. " - windows (shellslash)" , test_fn )
25
+
26
+ vim .o .shellslash = false
27
+ it (name .. " - windows (noshellslash)" , test_fn )
28
+ vim .o .shellslash = orig
29
+ end
34
30
end
35
31
end
36
32
@@ -179,7 +175,7 @@ describe("Path2", function()
179
175
180
176
local function get_windows_paths ()
181
177
local nossl = hasshellslash and not vim .o .shellslash
182
- local drive = Path :new (vim .loop . cwd ()).drv
178
+ local drive = Path :new (vim .fn . getcwd ()).drv
183
179
local readme_path = vim .fn .fnamemodify (" README.md" , " :p" )
184
180
185
181
--- @type [string[] | string , string , boolean][]
@@ -273,56 +269,56 @@ describe("Path2", function()
273
269
274
270
describe (" :make_relative" , function ()
275
271
local root = iswin and " c:\\ " or " /"
276
- -- it_cross_plat("can take absolute paths and make them relative to the cwd", function()
277
- -- local p = Path:new { "lua", "plenary", "path.lua" }
278
- -- local absolute = vim.loop.cwd () .. path.sep .. p.filename
279
- -- local relative = Path:new(absolute):make_relative()
280
- -- assert.are.same(p.filename, relative)
281
- -- end)
272
+ it_cross_plat (" can take absolute paths and make them relative to the cwd" , function ()
273
+ local p = Path :new { " lua" , " plenary" , " path.lua" }
274
+ local absolute = vim .fn . getcwd () .. path .sep .. p .filename
275
+ local relative = Path :new (absolute ):make_relative ()
276
+ assert .are .same (p .filename , relative )
277
+ end )
282
278
283
- -- it_cross_plat("can take absolute paths and make them relative to a given path", function()
284
- -- local r = Path:new { root, "home", "prime" }
285
- -- local p = Path:new { "aoeu", "agen.lua" }
286
- -- local absolute = r.filename .. path.sep .. p.filename
287
- -- local relative = Path:new(absolute):make_relative(r.filename)
288
- -- assert.are.same(relative, p.filename)
289
- -- end)
279
+ it_cross_plat (" can take absolute paths and make them relative to a given path" , function ()
280
+ local r = Path :new { root , " home" , " prime" }
281
+ local p = Path :new { " aoeu" , " agen.lua" }
282
+ local absolute = r .filename .. path .sep .. p .filename
283
+ local relative = Path :new (absolute ):make_relative (r .filename )
284
+ assert .are .same (p .filename , relative )
285
+ end )
290
286
291
- -- it_cross_plat("can take double separator absolute paths and make them relative to the cwd", function()
292
- -- local p = Path:new { "lua", "plenary", "path.lua" }
293
- -- local absolute = vim.loop.cwd () .. path.sep .. path.sep .. p.filename
294
- -- local relative = Path:new(absolute):make_relative()
295
- -- assert.are.same(relative, p.filename)
296
- -- end)
287
+ it_cross_plat (" can take double separator absolute paths and make them relative to the cwd" , function ()
288
+ local p = Path :new { " lua" , " plenary" , " path.lua" }
289
+ local absolute = vim .fn . getcwd () .. path .sep .. path .sep .. p .filename
290
+ local relative = Path :new (absolute ):make_relative ()
291
+ assert .are .same (p .filename , relative )
292
+ end )
297
293
298
- -- it_cross_plat("can take double separator absolute paths and make them relative to a given path", function()
299
- -- local r = Path:new { root, "home", "prime" }
300
- -- local p = Path:new { "aoeu", "agen.lua" }
301
- -- local absolute = r.filename .. path.sep .. path.sep .. p.filename
302
- -- local relative = Path:new(absolute):make_relative(r.filename)
303
- -- assert.are.same(relative, p.filename)
304
- -- end)
294
+ it_cross_plat (" can take double separator absolute paths and make them relative to a given path" , function ()
295
+ local r = Path :new { root , " home" , " prime" }
296
+ local p = Path :new { " aoeu" , " agen.lua" }
297
+ local absolute = r .filename .. path .sep .. path .sep .. p .filename
298
+ local relative = Path :new (absolute ):make_relative (r .filename )
299
+ assert .are .same (p .filename , relative )
300
+ end )
305
301
306
- -- it_cross_plat("can take absolute paths and make them relative to a given path with trailing separator", function()
307
- -- local r = Path:new { root, "home", "prime" }
308
- -- local p = Path:new { "aoeu", "agen.lua" }
309
- -- local absolute = r.filename .. path.sep .. p.filename
310
- -- local relative = Path:new(absolute):make_relative(r.filename .. path.sep)
311
- -- assert.are.same(relative, p.filename)
312
- -- end)
302
+ it_cross_plat (" can take absolute paths and make them relative to a given path with trailing separator" , function ()
303
+ local r = Path :new { root , " home" , " prime" }
304
+ local p = Path :new { " aoeu" , " agen.lua" }
305
+ local absolute = r .filename .. path .sep .. p .filename
306
+ local relative = Path :new (absolute ):make_relative (r .filename .. path .sep )
307
+ assert .are .same (p .filename , relative )
308
+ end )
313
309
314
310
-- it_cross_plat("can take absolute paths and make them relative to the root directory", function()
315
- -- local p = Path:new { "home" , "prime", "aoeu", "agen.lua" }
311
+ -- local p = Path:new { root , "prime", "aoeu", "agen.lua" }
316
312
-- local absolute = root .. p.filename
317
313
-- local relative = Path:new(absolute):make_relative(root)
318
- -- assert.are.same(relative, p.filename)
314
+ -- assert.are.same(p.filename, relative )
319
315
-- end)
320
316
321
- -- it_cross_plat("can take absolute paths and make them relative to themselves", function()
322
- -- local p = Path:new { root, "home", "prime", "aoeu", "agen.lua" }
323
- -- local relative = Path:new(p.filename):make_relative(p.filename)
324
- -- assert.are.same(relative, ".")
325
- -- end)
317
+ it_cross_plat (" can take absolute paths and make them relative to themselves" , function ()
318
+ local p = Path :new { root , " home" , " prime" , " aoeu" , " agen.lua" }
319
+ local relative = Path :new (p .filename ):make_relative (p .filename )
320
+ assert .are .same (" ." , relative )
321
+ end )
326
322
327
323
-- it_cross_plat("should not truncate if path separator is not present after cwd", function()
328
324
-- local cwd = "tmp" .. path.sep .. "foo"
@@ -338,4 +334,21 @@ describe("Path2", function()
338
334
-- assert.are.same(p.filename, relative)
339
335
-- end)
340
336
end )
337
+
338
+ describe (" parents" , function ()
339
+ it_cross_plat (" should extract the ancestors of the path" , function ()
340
+ local p = Path :new (vim .fn .getcwd ())
341
+ local parents = p :parents ()
342
+ assert (compat .islist (parents ))
343
+ for _ , parent in pairs (parents ) do
344
+ assert .are .same (type (parent ), " string" )
345
+ end
346
+ end )
347
+
348
+ it_cross_plat (" should return itself if it corresponds to path.root" , function ()
349
+ local p = Path :new (Path .path .root (vim .fn .getcwd ()))
350
+ assert .are .same (p :absolute (), p :parent ():absolute ())
351
+ -- assert.are.same(p, p:parent())
352
+ end )
353
+ end )
341
354
end )
0 commit comments