Skip to content

Commit 73ab15a

Browse files
Update readme and local only option (#10)
1 parent 683113f commit 73ab15a

File tree

9 files changed

+91
-25
lines changed

9 files changed

+91
-25
lines changed

README.md

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ require("neotest").setup({
7070
-- Custom config to load via -u to set up testing.
7171
-- If nil, will look for a 'minimal_init.lua' file
7272
minimal_init = "custom_init.lua",
73+
-- Only use a luarocks installation in the project's directory. If
74+
-- true, installations in $HOME and global installations will be
75+
-- ignored. Useful for isolating the test environment
76+
local_luarocks_only = true,
7377
}),
7478
},
7579
})
@@ -124,6 +128,15 @@ listed below and in that priority (i.e. a directory-local install takes
124128
precedence over a global install). You can check the installation by running
125129
`luarocks list busted`.
126130

131+
> [!WARNING]
132+
> If you have set `busted_command` to a non-nil value in the `setup` function,
133+
> `neotest-busted` will not know where to look for appropriate lua paths and
134+
> will not look for installations as specified below to avoid setting up paths
135+
> for a different busted installation.
136+
>
137+
> In this case, you should set `busted_paths` and `busted_cpaths` to appropriate
138+
> paths.
139+
127140
### Directory-local install
128141

129142
You can install busted in your project's directory by running the following commands.
@@ -137,6 +150,10 @@ You can install busted in your project's directory by running the following comm
137150

138151
### User home directory install
139152

153+
> [!IMPORTANT]
154+
> You need to set `local_luarocks_only` to `false` for `neotest-busted` to find
155+
> your home directory installation.
156+
140157
The following command will install busted in your home directory.
141158

142159
```shell
@@ -145,6 +162,10 @@ The following command will install busted in your home directory.
145162

146163
### Global install
147164

165+
> [!IMPORTANT]
166+
> You need to set `local_luarocks_only` to `false` for `neotest-busted` to find
167+
> your global installation.
168+
148169
```shell
149170
> luarocks install busted
150171
```
@@ -161,21 +182,7 @@ the command will automatically try to find your tests in a `spec/`, `test/`, or
161182
`tests/` directory.
162183

163184
```shell
164-
$ nvim -l ./scripts/test-runner.lua tests/my_spec.lua
165-
```
166-
167-
#### Test via rockspec
168-
169-
If you use a rockspec, you can provide a test command so you can run tests using
170-
`luarocks test`.
171-
172-
```lua
173-
-- Your rockspec...
174-
175-
test = {
176-
type = "command",
177-
command = "nvim -u NONE -l ./scripts/test-runner.lua",
178-
}
185+
$ nvim -l <path-to-neotest-busted>/scripts/test-runner.lua tests/my_spec.lua
179186
```
180187

181188
## Debugging tests
@@ -193,6 +200,12 @@ Yes. Please see the instructions [here](#async-tests).
193200
([even though the docs still mention it](https://lunarmodules.github.io/busted/#async-tests)) so you could install
194201
busted v1 but I haven't tested that.
195202

203+
#### Q: Why is `neotest-busted` tested using plenary?
204+
205+
The test could be run via `neotest-busted` itself but I decided to use plenary
206+
instead to use another test runner so that bugs in `neotest-busted` won't affect
207+
its own tests.
208+
196209
## Inspiration
197210

198211
* [Using Neovim as Lua interpreter with Luarocks](https://zignar.net/2023/01/21/using-luarocks-as-lua-interpreter-with-luarocks/)

lua/neotest-busted/config.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ local default_config = {
77
busted_paths = nil,
88
busted_cpaths = nil,
99
minimal_init = nil,
10+
local_luarocks_only = true,
1011
}
1112

1213
local _user_config = default_config
@@ -70,6 +71,10 @@ function config.validate(_config)
7071
is_non_empty_string,
7172
"optional non-empty string"
7273
},
74+
local_luarocks_only = {
75+
_config.local_luarocks_only,
76+
"boolean",
77+
},
7378
})
7479
-- stylua: ignore end
7580

lua/neotest-busted/health.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function health.check()
2525
vim.health.report_error("neotest-busted requires at least neovim " .. min_neovim_version)
2626
end
2727

28-
-- NOTE: We cannot check the neotest version because it isn't avertised as
28+
-- NOTE: We cannot check the neotest version because it isn't advertised as
2929
-- part of its public api
3030
check_module_installed("neotest")
3131
check_module_installed("nio")
@@ -38,6 +38,8 @@ function health.check()
3838
vim.health.report_error("config has errors: " .. error)
3939
end
4040

41+
-- We skip looking for a local luarocks installation as the healthcheck
42+
-- could have been invoked from anywhere
4143
local busted = adapter.find_busted_command(true)
4244

4345
if busted then

lua/neotest-busted/init.lua

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ local function log_and_notify(message, level)
2222
return
2323
end
2424

25-
logger[log_method](message)
26-
vim.notify(message, level)
25+
vim.schedule(function()
26+
logger[log_method](message)
27+
vim.notify(message, level)
28+
end)
2729
end
2830

2931
---@type neotest.Adapter
@@ -67,6 +69,11 @@ function BustedNeotestAdapter.find_busted_command(ignore_local)
6769
end
6870
end
6971

72+
-- Only skip checking further installations if we are not doing the healthcheck
73+
if not ignore_local and config.local_luarocks_only == true then
74+
return nil
75+
end
76+
7077
-- Try to find a local (user home directory) busted executable
7178
local user_globs =
7279
util.glob(util.create_path("~", ".luarocks", "lib", "luarocks", "**", "bin", "busted"))
@@ -75,7 +82,7 @@ function BustedNeotestAdapter.find_busted_command(ignore_local)
7582
logger.debug("Using local (~/.luarocks) busted executable")
7683

7784
return {
78-
type = "local",
85+
type = "user",
7986
command = user_globs[1],
8087
lua_paths = {
8188
util.create_path("~", ".luarocks", "share", "lua", "5.1", "?.lua"),
@@ -171,7 +178,7 @@ function BustedNeotestAdapter.create_test_command(results_path, paths, filters,
171178
local busted = BustedNeotestAdapter.find_busted_command()
172179

173180
if not busted then
174-
log_and_notify("Could not find a busted command", vim.log.levels.ERROR)
181+
log_and_notify("Could not find busted executable", vim.log.levels.ERROR)
175182
return
176183
end
177184

lua/neotest-busted/types.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
---@field busted_paths string[]?
55
---@field busted_cpaths string[]?
66
---@field minimal_init string?
7+
---@field local_luarocks_only boolean?
78

89
---@class neotest-busted.BustedCommandConfig
910
---@field type "config" | "project" | "user" | "global"
10-
---@field command string[]
11+
---@field command string
1112
---@field lua_paths string[]
1213
---@field lua_cpaths string[]
1314

neotest-busted-scm-1.rockspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ build = {
2828
type = "builtin",
2929
copy_directories = {
3030
"doc",
31+
"scripts",
3132
},
3233
}
3334

tests/adapter_results_spec.lua

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,12 @@ describe("adapter.results", function()
147147
end)
148148

149149
it("handles failure to read json test output", function()
150+
stub(vim, "schedule", function(func)
151+
func()
152+
end)
153+
154+
stub(vim, "notify")
155+
150156
stub(lib.files, "read", function()
151157
error("Could not read file", 0)
152158
end)
@@ -155,15 +161,26 @@ describe("adapter.results", function()
155161

156162
assert.are.same(neotest_results, {})
157163

158-
assert.stub(lib.files.read).was.called_with(spec.context.results_path)
164+
assert.stub(vim.schedule).was.called()
165+
assert.stub(vim.notify).was.called()
159166
assert
160167
.stub(logger.error).was
161168
.called_with(
162169
"Failed to read json test output file test_output.json with error: Could not read file"
163170
)
171+
assert.stub(lib.files.read).was.called_with(spec.context.results_path)
172+
173+
vim.schedule:revert()
174+
vim.notify:revert()
164175
end)
165176

166177
it("handles failure to decode json", function()
178+
stub(vim, "schedule", function(func)
179+
func()
180+
end)
181+
182+
stub(vim, "notify")
183+
167184
stub(vim.json, "decode", function()
168185
error("Expected value but found invalid token at character 1", 0)
169186
end)
@@ -172,15 +189,25 @@ describe("adapter.results", function()
172189

173190
assert.are.same(neotest_results, {})
174191

175-
assert.stub(lib.files.read).was.called_with(spec.context.results_path)
192+
assert.stub(vim.schedule).was.called()
193+
assert.stub(vim.notify).was.called()
176194
assert.stub(logger.error).was.called_with(
177195
"Failed to parse json test output file test_output.json with error: Expected value but found invalid token at character 1"
178196
)
197+
assert.stub(lib.files.read).was.called_with(spec.context.results_path)
179198

199+
vim.schedule:revert()
200+
vim.notify:revert()
180201
vim.json.decode:revert()
181202
end)
182203

183204
it("logs not finding a matching position id", function()
205+
stub(vim, "schedule", function(func)
206+
func()
207+
end)
208+
209+
stub(vim, "notify")
210+
184211
spec.context.position_ids[test_path .. "::namespace tests a failing test::7"] = nil
185212

186213
local neotest_results = adapter.results(spec, strategy_result)
@@ -209,11 +236,16 @@ describe("adapter.results", function()
209236
},
210237
})
211238

239+
assert.stub(vim.schedule).was.called()
240+
assert.stub(vim.notify).was.called()
212241
assert.stub(lib.files.read).was.called_with(spec.context.results_path)
213242
assert.stub(logger.error).was.called_with(
214243
"Failed to find matching position id for key "
215244
.. test_path
216245
.. "::namespace tests a failing test::7"
217246
)
247+
248+
vim.schedule:revert()
249+
vim.notify:revert()
218250
end)
219251
end)

tests/async_test_spec.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ describe("async tests", function()
1919
local timer = vim.loop.new_timer()
2020
local event = control.event()
2121

22-
-- Print a message after 1 second
23-
timer:start(1000, 0, function()
22+
-- Print a message after 200 milliseconds
23+
timer:start(200, 0, function()
2424
timer:stop()
2525
timer:close()
2626
vim.print("Hello from async test")

tests/config_spec.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ describe("config", function()
4747
config = { minimal_init = "" },
4848
error_message = non_empty_string,
4949
},
50+
{
51+
config = { local_luarocks_only = 1 },
52+
error_message = "expected boolean, got number",
53+
},
5054
}
5155

5256
stub(vim.api, "nvim_echo")
@@ -77,6 +81,7 @@ describe("config", function()
7781
busted_paths = { "some/path" },
7882
busted_cpaths = {},
7983
minimal_init = "some_init_file.lua",
84+
local_luarocks_only = false,
8085
})
8186

8287
assert.is_true(ok)

0 commit comments

Comments
 (0)