forked from AckslD/nvim-pytrize.lua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusages_spec.lua
More file actions
256 lines (224 loc) · 7.42 KB
/
usages_spec.lua
File metadata and controls
256 lines (224 loc) · 7.42 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
local has_parser = pcall(function()
vim.treesitter.language.inspect("python")
end)
if not has_parser then
describe("usages (skipped)", function()
it("SKIPPED: python treesitter parser not installed", function()
print("Skipping usages tests: python treesitter parser not available")
end)
end)
return
end
local usages = require("pytrize.usages")
local function create_python_buf(lines)
local bufnr = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
vim.api.nvim_set_option_value("filetype", "python", { buf = bufnr })
local parser = vim.treesitter.get_parser(bufnr, "python")
parser:parse()
return bufnr
end
describe("find_usage_positions", function()
it("finds fixture used as a plain parameter", function()
local bufnr = create_python_buf({
"def test_foo(my_fixture):",
" assert my_fixture == 42",
})
local positions = usages._find_fixture_references(bufnr, "my_fixture")
-- param (row 0) + body ref (row 1)
assert.are.equal(2, #positions)
vim.api.nvim_buf_delete(bufnr, { force = true })
end)
it("finds fixture used as a typed parameter", function()
local bufnr = create_python_buf({
"def test_foo(my_fixture: MagicMock):",
" my_fixture.assert_called()",
})
local positions = usages._find_fixture_references(bufnr, "my_fixture")
-- typed param (row 0) + body ref (row 1)
assert.are.equal(2, #positions)
vim.api.nvim_buf_delete(bufnr, { force = true })
end)
it("finds fixture in @pytest.mark.usefixtures", function()
local bufnr = create_python_buf({
"import pytest",
"",
'@pytest.mark.usefixtures("my_fixture")',
"def test_foo(self):",
" pass",
})
local positions = usages._find_fixture_references(bufnr, "my_fixture")
assert.are.equal(1, #positions)
vim.api.nvim_buf_delete(bufnr, { force = true })
end)
it("does NOT include the fixture definition itself", function()
local bufnr = create_python_buf({
"import pytest",
"",
"@pytest.fixture",
"def my_fixture():",
" return 42",
})
local positions = usages._find_fixture_references(bufnr, "my_fixture")
assert.are.equal(0, #positions)
vim.api.nvim_buf_delete(bufnr, { force = true })
end)
it("does NOT find attribute access (db.my_fixture)", function()
local bufnr = create_python_buf({
"def test_foo(db):",
" x = db.my_fixture",
})
local positions = usages._find_fixture_references(bufnr, "my_fixture")
assert.are.equal(0, #positions)
vim.api.nvim_buf_delete(bufnr, { force = true })
end)
it("does NOT find keyword argument names", function()
local bufnr = create_python_buf({
"def test_foo(db):",
" call(my_fixture=1)",
})
local positions = usages._find_fixture_references(bufnr, "my_fixture")
assert.are.equal(0, #positions)
vim.api.nvim_buf_delete(bufnr, { force = true })
end)
it("finds both definition and consumer when they coexist, but only the consumer positions", function()
local bufnr = create_python_buf({
"import pytest",
"",
"@pytest.fixture",
"def my_fixture():",
" return 42",
"",
"def test_uses(my_fixture):",
" assert my_fixture",
})
local positions = usages._find_fixture_references(bufnr, "my_fixture")
-- param (row 6) + body ref (row 7); definition on row 3 is excluded
assert.are.equal(2, #positions)
vim.api.nvim_buf_delete(bufnr, { force = true })
end)
end)
local tmp_root = "/tmp/pytrize_usages_test"
local function write_py(path, lines)
local dir = vim.fn.fnamemodify(path, ":h")
vim.fn.mkdir(dir, "p")
local f = io.open(path, "w")
f:write(table.concat(lines, "\n") .. "\n")
f:close()
end
describe("find_all_usages", function()
after_each(function()
for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do
local name = vim.api.nvim_buf_get_name(bufnr)
if name:find(tmp_root, 1, true) then
vim.api.nvim_buf_delete(bufnr, { force = true })
end
end
vim.fn.delete(tmp_root, "rf")
end)
it("returns quickfix items for usages across files", function()
vim.fn.mkdir(tmp_root .. "/.git", "p")
write_py(tmp_root .. "/conftest.py", {
"import pytest",
"",
"@pytest.fixture",
"def my_fixture():",
" return 42",
})
write_py(tmp_root .. "/test_a.py", {
"def test_uses(my_fixture):",
" assert my_fixture",
})
write_py(tmp_root .. "/test_b.py", {
"import pytest",
"",
'@pytest.mark.usefixtures("my_fixture")',
"def test_indirect(self):",
" pass",
})
local items = usages._find_all_usages("my_fixture", tmp_root)
-- Each item must have the quickfix fields
assert.is_true(#items > 0)
for _, item in ipairs(items) do
assert.is_not_nil(item.filename)
assert.is_not_nil(item.lnum)
assert.is_not_nil(item.col)
assert.is_not_nil(item.text)
end
end)
it("does NOT include the fixture definition in results", function()
vim.fn.mkdir(tmp_root .. "/.git", "p")
write_py(tmp_root .. "/conftest.py", {
"import pytest",
"",
"@pytest.fixture",
"def my_fixture():",
" return 42",
})
write_py(tmp_root .. "/test_a.py", {
"def test_uses(my_fixture):",
" assert my_fixture",
})
local items = usages._find_all_usages("my_fixture", tmp_root)
-- conftest.py line 4 is the definition — must not appear
for _, item in ipairs(items) do
local in_conftest = item.filename:find("conftest.py", 1, true) ~= nil
local is_def_line = item.lnum == 4
assert.is_false(in_conftest and is_def_line, "definition should not appear in usages")
end
end)
it("falls back to quickfix when preferred_input is fzf-lua but fzf-lua is unavailable", function()
vim.fn.mkdir(tmp_root .. "/.git", "p")
write_py(tmp_root .. "/test_a.py", {
"def test_uses(my_fixture):",
" assert my_fixture",
})
local settings = require("pytrize.settings")
local original = settings.settings.preferred_input
settings.settings.preferred_input = "fzf-lua"
-- Ensure fzf-lua cannot be loaded by temporarily breaking the require
local original_fzf = package.loaded["fzf-lua"]
package.loaded["fzf-lua"] = nil
local original_preload = package.preload["fzf-lua"]
package.preload["fzf-lua"] = function()
error("fzf-lua not installed")
end
local items = usages._find_all_usages("my_fixture", tmp_root)
assert.is_true(#items > 0)
-- show_usages should not error — it falls back to quickfix
-- We can't easily call show_usages (it reads <cword>), so we test
-- the dispatch logic directly: pcall require pytrize.fzf should fail
-- when fzf-lua is broken, confirming fallback path is taken.
local ok, _ = pcall(require, "fzf-lua")
assert.is_false(ok)
-- Restore
package.preload["fzf-lua"] = original_preload
package.loaded["fzf-lua"] = original_fzf
settings.settings.preferred_input = original
end)
it("finds the right files and line numbers", function()
vim.fn.mkdir(tmp_root .. "/.git", "p")
write_py(tmp_root .. "/conftest.py", {
"import pytest",
"",
"@pytest.fixture",
"def my_fixture():",
" return 42",
})
write_py(tmp_root .. "/test_a.py", {
"def test_uses(my_fixture):",
" assert my_fixture",
})
local items = usages._find_all_usages("my_fixture", tmp_root)
-- Find the parameter usage in test_a.py line 1
local found_param = false
for _, item in ipairs(items) do
if item.filename:find("test_a.py", 1, true) and item.lnum == 1 then
found_param = true
assert.is_true(item.col > 0)
assert.is_not_nil(item.text:find("my_fixture"))
end
end
assert.is_true(found_param, "expected to find parameter usage in test_a.py line 1")
end)
end)