Skip to content

Commit c4d6e55

Browse files
authored
feat(test_harness): use minimal configuration by default (#459)
1 parent 7cc390e commit c4d6e55

File tree

3 files changed

+135
-11
lines changed

3 files changed

+135
-11
lines changed

README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,8 @@ assert(result == "# plenary.nvim")
212212

213213
### plenary.test_harness
214214

215+
`:help plenary-test`
216+
215217
Supports (simple) busted-style testing. It implements a mock-ed busted interface, that will allow you to run simple
216218
busted style tests in separate neovim instances.
217219

@@ -220,20 +222,23 @@ To run the current spec file in a floating window, you can use the keymap `<Plug
220222
```
221223
nmap <leader>t <Plug>PlenaryTestFile
222224
```
225+
In this case, the test is run with a minimal configuration, that includes in
226+
its runtimepath only plenary.nvim and the current working directory.
223227

224228
To run a whole directory from the command line, you could do something like:
225229

226230
```
227-
nvim --headless -c "PlenaryBustedDirectory tests/plenary/ {minimal_init = 'tests/minimal_init.vim'}"
231+
nvim --headless -c "PlenaryBustedDirectory tests/plenary/ {options}"
228232
```
229233

230234
Where the first argument is the directory you'd like to test. It will search for files with
231235
the pattern `*_spec.lua` and execute them in separate neovim instances.
232236

233-
The second argument is a Lua option table with the following fields:
237+
Without second argument, `PlenaryBustedDirectory` is also run with a minimal
238+
configuration. Otherwise it is a Lua option table with the following fields:
234239
- `nvim_cmd`: specify the command to launch this neovim instance (defaults to `vim.v.progpath`)
235-
- `minimal_init`: specify an init.vim to use for this instance, uses `--noplugin`
236-
- `minimal`: uses `--noplugin` without an init script (overrides `minimal_init`)
240+
- `init`: specify an init.vim to use for this instance
241+
- `minimal_init`: as for `init`, but also run the neovim instance with `--clean`
237242
- `sequential`: whether to run tests sequentially (default is to run in parallel)
238243
- `keep_going`: if `sequential`, whether to continue on test failure (default true)
239244
- `timeout`: controls the maximum time allotted to each job in parallel or

doc/plenary-test.txt

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
*plenary-test*
2+
3+
4+
Supports (simple) busted-style testing. It implements a mock-ed busted
5+
interface, that will allow you to run simple busted style tests in separate
6+
neovim instances.
7+
8+
9+
10+
USAGE *plenary-test-usage*
11+
==============================================================================
12+
13+
To run the current spec file in a floating window, you can use the keymap
14+
`<Plug>PlenaryTestFile`. For example:
15+
>
16+
nmap <leader>t <Plug>PlenaryTestFile
17+
<
18+
In this case, the test is run with a minimal configuration, that includes in
19+
its runtimepath only plenary.nvim and the current working directory.
20+
21+
To run a whole directory from the command line, you could do something like:
22+
>
23+
nvim --headless -c "PlenaryBustedDirectory tests/plenary/ { <options> }"
24+
25+
Where the first argument is the directory you'd like to test. It will search
26+
for files with the pattern `*_spec.lua` and execute them in separate neovim
27+
instances.
28+
29+
The second argument is a Lua option table with the following fields:
30+
31+
`nvim_cmd` specify the command to launch this neovim instance (defaults
32+
to `vim.v.progpath`).
33+
`init` specify an init.vim to use for this instance, if not given
34+
a minimal configuration is used.
35+
`minimal_init` as for `init`, but also run the neovim instance with
36+
`--clean`.
37+
`sequential` whether to run tests sequentially (default is to run in
38+
parallel).
39+
`keep_going` if `sequential`, whether to continue on test failure (default
40+
true).
41+
`timeout` controls the maximum time allotted to each job in parallel or
42+
sequential operation (defaults to 50,000 milliseconds).
43+
44+
Unless `init` is given, the neovim instance is run with the `--clean`
45+
argument.
46+
47+
The exit code is 0 for success and 1 for fail, so you can use it easily in
48+
a `Makefile`.
49+
50+
51+
52+
SUPPORTED BUSTED ITEMS *plenary-test-busted*
53+
==============================================================================
54+
55+
So far, the only supported busted items are:
56+
57+
- `describe`
58+
- `it`
59+
- `pending`
60+
- `before_each`
61+
- `after_each`
62+
- `clear`
63+
- `assert.*` etc. (from luassert, which is bundled)
64+
65+
We used to support `luaunit` and original `busted` but it turns out it was way
66+
too hard and not worthwhile for the difficulty of getting them setup,
67+
particularly on other platforms or in CI. Now, we have a dep free (or at
68+
least, no other installation steps necessary) `busted` implementation that can
69+
be used more easily.
70+
71+
Please take a look at the new APIs and make any issues for things that aren't
72+
clear.
73+
74+
75+
76+
COMMANDS *plenary-test-commands*
77+
==============================================================================
78+
79+
*:PlenaryBustedFile* {path}
80+
81+
Run a test on a single `_spec.lua` file.
82+
83+
84+
*:PlenaryBustedDirectory* {path} {options}
85+
86+
Run tests for all `*_spec.lua` files in the given path.
87+
88+
{options} is a table, see |plenary-test-usage|.
89+
90+
91+
92+
93+
PLUGS *plenary-test-plugs*
94+
==============================================================================
95+
96+
<Plug>PlenaryTestFile
97+
98+
Can be used to run a test on a single file, with a minimal configuration.
99+
100+
101+
102+
103+
LICENSE *plenary-test-license*
104+
==============================================================================
105+
106+
MIT license
107+
108+
109+
==============================================================================
110+
vim:tw=78:ft=help:norl:et:ts=2:sw=2:fen:fdl=0:

lua/plenary/test_harness.lua

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ local win_float = require "plenary.window.float"
77

88
local headless = require("plenary.nvim_meta").is_headless
99

10+
local plenary_dir = vim.fn.fnamemodify(debug.getinfo(1).source:match "@?(.*[/\\])", ":p:h:h:h")
11+
1012
local harness = {}
1113

1214
local print_output = vim.schedule_wrap(function(_, ...)
@@ -40,6 +42,8 @@ end
4042

4143
function harness.test_directory(directory, opts)
4244
print "Starting..."
45+
local minimal = not opts or not opts.init or opts.minimal or opts.minimal_init
46+
4347
opts = vim.tbl_deep_extend("force", {
4448
nvim_cmd = vim.v.progpath,
4549
winopts = { winblend = 3 },
@@ -83,18 +87,23 @@ function harness.test_directory(directory, opts)
8387
local args = {
8488
"--headless",
8589
"-c",
86-
string.format('lua require("plenary.busted").run("%s")', p:absolute():gsub("\\", "\\\\")),
90+
"set rtp+=.," .. vim.fn.escape(plenary_dir, " ") .. " | runtime plugin/plenary.vim",
8791
}
8892

89-
if opts.minimal ~= nil then
90-
table.insert(args, "--noplugin")
91-
elseif opts.minimal_init ~= nil then
92-
table.insert(args, "--noplugin")
93-
93+
if minimal then
94+
table.insert(args, "--clean")
95+
if opts.minimal_init then
96+
table.insert(args, "-u")
97+
table.insert(args, opts.minimal_init)
98+
end
99+
elseif opts.init ~= nil then
94100
table.insert(args, "-u")
95-
table.insert(args, opts.minimal_init)
101+
table.insert(args, opts.init)
96102
end
97103

104+
table.insert(args, "-c")
105+
table.insert(args, string.format('lua require("plenary.busted").run("%s")', p:absolute():gsub("\\", "\\\\")))
106+
98107
local job = Job:new {
99108
command = opts.nvim_cmd,
100109
args = args,

0 commit comments

Comments
 (0)