Skip to content

Commit 41b9d56

Browse files
committed
fix linting errors/tests
1 parent 7b2f24f commit 41b9d56

File tree

2 files changed

+39
-28
lines changed

2 files changed

+39
-28
lines changed

lua/plenary/path2.lua

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,9 @@
6767
---
6868
--- - `find_upwards` returns `nil` if file not found rather than an empty string
6969

70-
7170
-- TODO: could probably do with more `make_relative` tests
7271
-- - walk up close to root
7372
-- - add "walk_up" in test name
74-
-- TODO: shorten: i think `vim.list_contains` is not nvim-0.7 compat (maybe use like a set?)
75-
-- TODO: verify unix tests pass
7673
-- TODO: add windows test for path2_spec only?
7774

7875
local bit = require "plenary.bit"
@@ -388,20 +385,21 @@ path.root = (function()
388385
end)()
389386

390387
---@param parts string[]
391-
---@param _flavor plenary._Path
388+
---@param flavor plenary._Path
392389
---@return string drv
393390
---@return string root
394391
---@return string[]
395-
local function parse_parts(parts, _flavor)
396-
local drv, root, rel, parsed = "", "", "", {}
392+
local function parse_parts(parts, flavor)
393+
local rel
394+
local drv, root, parsed = "", "", {}
397395

398396
if #parts == 0 then
399397
return drv, root, parsed
400398
end
401399

402-
local sep = _flavor.sep
403-
local p = _flavor:join(unpack(parts))
404-
drv, root, rel = _flavor:split_root(p)
400+
local sep = flavor.sep
401+
local p = flavor:join(unpack(parts))
402+
drv, root, rel = flavor:split_root(p)
405403

406404
if root == "" and drv:sub(1, 1) == sep and drv:sub(-1) ~= sep then
407405
local drv_parts = vim.split(drv, sep)
@@ -423,6 +421,8 @@ local function parse_parts(parts, _flavor)
423421
return drv, root, parsed
424422
end
425423

424+
local FILE_MODE = 438 -- o666 (aka -rw-rw-rw-)
425+
426426
---@class plenary.Path2
427427
---@field path plenary.path2
428428
---@field private _flavor plenary._Path
@@ -546,7 +546,9 @@ function Path:new(...)
546546
elseif type(arg) ~= "string" and not self.is_path(arg) then
547547
error(
548548
string.format(
549-
"Invalid type passed to 'Path:new'. Expects any number of 'string' or 'Path' objects. Got type '%s', shape '%s'",
549+
"Invalid type passed to 'Path:new'. "
550+
.. "Expects any number of 'string' or 'Path' objects. "
551+
.. "Got type '%s', shape '%s'",
550552
type(arg),
551553
vim.inspect(arg)
552554
)
@@ -582,13 +584,13 @@ end
582584
---@param relparts string[]?
583585
---@return string
584586
function Path:_filename(drv, root, relparts)
585-
drv = vim.F.if_nil(drv, self.drv)
587+
drv = vim.F.if_nil(drv, self.drv) -- luacheck: ignore
586588
drv = self.drv ~= "" and self.drv:gsub(self._flavor.sep, self.sep) or ""
587589

588590
if self._flavor.has_drv and drv == "" then
589591
root = ""
590592
else
591-
root = vim.F.if_nil(root, self.root)
593+
root = vim.F.if_nil(root, self.root) -- luacheck: ignore
592594
root = self.root ~= "" and self.sep:rep(#self.root) or ""
593595
end
594596

@@ -658,7 +660,7 @@ function Path:lstat()
658660
return res
659661
end
660662

661-
---@return integer
663+
---@return integer # numeric mode in octal values
662664
function Path:permission()
663665
local stat = self:stat()
664666
local perm = bit.band(stat.mode, 0x1FF)
@@ -914,11 +916,15 @@ function Path:shorten(len, excludes)
914916
len = vim.F.if_nil(len, 1)
915917
excludes = vim.F.if_nil(excludes, { #self.relparts })
916918

917-
local new_parts = {}
919+
local excl_set = {}
920+
for _, idx in ipairs(excludes) do
921+
excl_set[idx] = true
922+
end
918923

924+
local new_parts = {}
919925
for i, part in ipairs(self.relparts) do
920926
local neg_i = -(#self.relparts + 1) + i
921-
if #part > len and not vim.list_contains(excludes, i) and not vim.list_contains(excludes, neg_i) then
927+
if #part > len and not excl_set[i] and not excl_set[neg_i] then
922928
part = part:sub(1, len)
923929
end
924930
table.insert(new_parts, part)
@@ -928,7 +934,10 @@ function Path:shorten(len, excludes)
928934
end
929935

930936
---@class plenary.Path2.mkdirOpts
931-
---@field mode integer? permission to give to the directory, no umask effect will be applied (default: `o777`)
937+
--- permission to give to the directory, this is modified by the process's umask
938+
--- (default: `o777`)
939+
--- (currently not implemented in Windows by libuv)
940+
---@field mode integer?
932941
---@field parents boolean? creates parent directories if true and necessary (default: `false`)
933942
---@field exists_ok boolean? ignores error if true and target directory exists (default: `false`)
934943

@@ -942,7 +951,7 @@ function Path:mkdir(opts)
942951
exists_ok = { opts.exists_ok, "b", true },
943952
}
944953

945-
opts.mode = vim.F.if_nil(opts.mode, 511)
954+
opts.mode = vim.F.if_nil(opts.mode, 511) -- o777
946955
opts.parents = vim.F.if_nil(opts.parents, false)
947956
opts.exists_ok = vim.F.if_nil(opts.exists_ok, false)
948957

@@ -998,7 +1007,7 @@ function Path:touch(opts)
9981007
mode = { opts.mode, "n", true },
9991008
parents = { opts.parents, { "n", "b" }, true },
10001009
}
1001-
opts.mode = vim.F.if_nil(opts.mode, 438)
1010+
opts.mode = vim.F.if_nil(opts.mode, FILE_MODE) -- o666
10021011
opts.parents = vim.F.if_nil(opts.parents, false)
10031012

10041013
local abs_path = self:absolute()
@@ -1101,7 +1110,9 @@ end
11011110
---@field exists_ok boolean? whether ok if `opts.destination` exists, if so folders are merged (default: `true`)
11021111

11031112
---@param opts plenary.Path2.copyOpts
1104-
---@return {[plenary.Path2]: {success:boolean, err: string?}} # indicating success of copy; nested tables constitute sub dirs
1113+
--- a flat dictionary of destination paths and their copy result.
1114+
--- if successful, `{ success = true }`, else `{ success = false, err = "some msg" }`
1115+
---@return {[plenary.Path2]: {success:boolean, err: string?}}
11051116
function Path:copy(opts)
11061117
vim.validate {
11071118
destination = { opts.destination, is_path_like },
@@ -1192,7 +1203,7 @@ end
11921203
function Path:_read_sync()
11931204
local stat = self:_get_readable_stat()
11941205

1195-
local fd, err = uv.fs_open(self:absolute(), "r", 438)
1206+
local fd, err = uv.fs_open(self:absolute(), "r", FILE_MODE)
11961207
if fd == nil then
11971208
error(err)
11981209
end
@@ -1213,7 +1224,7 @@ end
12131224
---@private
12141225
---@param callback fun(data: string)
12151226
function Path:_read_async(callback)
1216-
uv.fs_open(self:absolute(), "r", 438, function(err_open, fd)
1227+
uv.fs_open(self:absolute(), "r", FILE_MODE, function(err_open, fd)
12171228
if err_open then
12181229
error(err_open)
12191230
end
@@ -1263,7 +1274,7 @@ function Path:head(lines)
12631274
lines = vim.F.if_nil(lines, 10)
12641275
local chunk_size = 256
12651276

1266-
local fd, err = uv.fs_open(self:absolute(), "r", 438)
1277+
local fd, err = uv.fs_open(self:absolute(), "r", FILE_MODE)
12671278
if fd == nil then
12681279
error(err)
12691280
end
@@ -1318,7 +1329,7 @@ function Path:tail(lines)
13181329
lines = vim.F.if_nil(lines, 10)
13191330
local chunk_size = 256
13201331

1321-
local fd, err = uv.fs_open(self:absolute(), "r", 438)
1332+
local fd, err = uv.fs_open(self:absolute(), "r", FILE_MODE)
13221333
if fd == nil then
13231334
error(err)
13241335
end
@@ -1378,7 +1389,7 @@ function Path:readbyterange(offset, length)
13781389
}
13791390

13801391
local stat = self:_get_readable_stat()
1381-
local fd, err = uv.fs_open(self:absolute(), "r", 438)
1392+
local fd, err = uv.fs_open(self:absolute(), "r", FILE_MODE)
13821393
if fd == nil then
13831394
error(err)
13841395
end
@@ -1427,7 +1438,7 @@ function Path:write(data, flags, mode)
14271438
mode = { mode, "n", true },
14281439
}
14291440

1430-
mode = vim.F.if_nil(mode, 438)
1441+
mode = vim.F.if_nil(mode, FILE_MODE)
14311442
local fd, err = uv.fs_open(self:absolute(), flags, mode)
14321443
if fd == nil then
14331444
error(err)

tests/plenary/path2_spec.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ describe("Path2", function()
431431
p:mkdir()
432432
assert.is_true(p:exists())
433433
assert.is_true(p:is_dir())
434-
assert_permission(0777, p:permission())
434+
assert_permission(755, p:permission()) -- umask dependent, probably bad test
435435

436436
p:rmdir()
437437
assert.is_false(p:exists())
@@ -468,9 +468,9 @@ describe("Path2", function()
468468
it_cross_plat("can set different modes", function()
469469
local p = Path:new "_dir_not_exist"
470470
assert.has_no_error(function()
471-
p:mkdir { mode = 0755 }
471+
p:mkdir { mode = 292 } -- o444
472472
end)
473-
assert_permission(0755, p:permission())
473+
assert_permission(444, p:permission())
474474

475475
p:rmdir()
476476
assert.is_false(p:exists())

0 commit comments

Comments
 (0)