forked from AckslD/nvim-pytrize.lua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaths.lua
More file actions
62 lines (52 loc) · 1.66 KB
/
paths.lua
File metadata and controls
62 lines (52 loc) · 1.66 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
local M = {}
local warn = require('pytrize.warn').warn
local function is_root_dir(dir)
return vim.fn.finddir('.pytest_cache', dir) ~= ''
end
local function join_path(fragments)
if #fragments == 1 and fragments[1] == '' then
return '/'
else
return table.concat(fragments, '/')
end
end
-- TODO better way to do this? (windows support?)
M.split_at_root = function(file)
local dir_fragments = vim.fn.split(file, '/', 1)
local rel_file_fragments = {}
while #dir_fragments do
table.insert(rel_file_fragments, 1, table.remove(dir_fragments, #dir_fragments))
local dir = join_path(dir_fragments)
if is_root_dir(dir) then
return dir, join_path(rel_file_fragments)
end
end
warn("couldn't find the pytest root dir")
end
M.get_conftest_chain = function(filepath, root_dir)
local dir = vim.fn.fnamemodify(filepath, ':h')
local chain = {}
-- Walk from root_dir down to the file's directory.
-- Build the list of directories from root to file dir, then check each for conftest.py.
local dirs = {}
local current = dir
while #current >= #root_dir do
table.insert(dirs, 1, current)
local parent = vim.fn.fnamemodify(current, ':h')
if parent == current then
break
end
current = parent
end
for _, d in ipairs(dirs) do
local conftest = d .. '/conftest.py'
if vim.fn.filereadable(conftest) == 1 then
table.insert(chain, conftest)
end
end
return chain
end
M.get_nodeids_path = function(rootdir)
return join_path{rootdir, '.pytest_cache', 'v', 'cache', 'nodeids'}
end
return M