Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove custom cli support #48

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 0 additions & 14 deletions .busted.example

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/style.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ jobs:
with:
token: ${{ secrets.GITHUB_TOKEN }}
version: 0.16.1
args: --check lua/ tests/ scripts/
args: --check lua/ tests/ test_files/
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
2. Make changes.
3. Make sure tests and styling checks are passing.
* Run tests by running `./tests/run_tests.sh` in the project directory. Running the tests requires [`plenary.nvim`](https://github.com/nvim-lua/plenary.nvim), [`neotest`](https://github.com/nvim-neotest/neotest), [`nvim-nio`](https://github.com/nvim-neotest/nvim-nio), and [`nvim-treesitter`](https://github.com/nvim-treesitter/nvim-treesitter). You may need to update the paths in `./tests/minimal_init.lua` to match those of your local installations to be able to run the tests. A `busted` executable is also needed to run the tests so set it up as per the instructions in the [README](/README.md).
* Install [stylua](https://github.com/JohnnyMorganz/StyLua) and check styling using `stylua --check lua/ scripts/ tests/ test_files/`. Omit `--check` in order to fix styling.
* Install [stylua](https://github.com/JohnnyMorganz/StyLua) and check styling using `stylua --check lua/ tests/ test_files/`. Omit `--check` in order to fix styling.
4. Submit a pull request.
5. Get it approved.
159 changes: 146 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,24 +212,62 @@ The following command will install busted in your home directory.

## Running from the command line

A `test-runner.lua` script is provided in the `scripts/` folder for running
tests via the command line. This is useful for running all tests during CI for
example.
There are several ways to run your tests from the command line.

If you do not provide a `minimal_init.lua` to set up your test environment, the
script will look for one and source it. If you don't specify any tests to run,
the command will automatically try to find your tests in a `spec/`, `test/`, or
`tests/` directory.
> [!WARNING]
> Running busted with neovim as the lua interpreter means that the same neovim
> instance is used in all your tests which could break test isolation. For
> example, setting `_G.foo = 10` in a test that runs before a test containing
> `vim.print(_G.foo)` will print 10.

```shell
$ nvim -l <path-to-neotest-busted>/scripts/test-runner.lua tests/my_spec.lua
```
<details>
<summary>Using plenary.nvim</summary>

This repo uses [`plenary.nvim`](https://github.com/nvim-lua/plenary.nvim) to run
its tests so feel free to use the setup in your own projects.

Running tests this way has the benefit that a separate neovim instance is used
for each test file giving better test isolation than running busted with neovim
as the lua interpreter.

See `plenary.nvim`'s GitHub repo or run `:help plenary-test` if you already have it
installed.

---

### Using busted directly
</details>

You can also provide a `.busted` config file and run your tests using busted.
<details>
<summary>Using a busted configuration file</summary>

You can provide a `.busted` config file and run your tests using busted.
Learn more about busted configuration files from the [official
docs](https://lunarmodules.github.io/busted/#usage) or take a look at the example [here](/.busted.example).
docs](https://lunarmodules.github.io/busted/#usage).

```lua
return {
_all = {
-- Use neovim as the lua interpreter for all tasks
lua = "nvim -l",
-- Ensures that your plugin and test files will be found
lpath = "lua/?.lua;lua/?/init.lua;tests/?.lua",
},
-- Default task to run if no task was specified
default = {
-- Runs your minimal init file (if any) so package dependencies can be found
helper = "./tests/minimal_init.lua",
},
-- Some other task
integration = {
tags = "integration",
shuffle_files = true,
},
}
```

Then run your tests using either `busted <test_dir>` or use `luarocks test
--test-type busted <test_dir>` (or omit `--test-type busted` if you set up a
test command in the rockspec, see below).

Pass extra arguments to `neotest` to run a specific task. For example, to run
the `"integration"` task in a test file:
Expand All @@ -238,6 +276,101 @@ the `"integration"` task in a test file:
require("neotest").run.run({ vim.fn.expand("%"), extra_args = { "--run", "integration" } })
```

---

</details>

<details>
<summary>Using luarocks</summary>

Luarocks allows you to specify a test command in the rockspec which can be run
using `luarocks test`. Additionally, you can specify `test_dependencies` and
they will automatically be installed before running tests.

If your tests do not need to run in a neovim context the rockspec below should
suffice, otherwise you can use a `.busted` config file to setup this up (see
above).

```lua
rockspec_format = "3.0"
package = "rockspec-example.nvim"
version = "scm-1"

description = {
summary = "Example rockspec",
}

-- More definitions...

test_dependencies = {
"busted >= 2.2.0, < 3.0.0",
}

test = {
type = "busted",
}
```

This will work if you use a [user-](#user-home-directory-install) or
[system-level](#global-install) luarocks installation but if you want to use a
[project-level](#directory-local-install) luarocks installation, you can use
this small script to correctly set up the paths.

```lua
---@param command_name string
---@param args string[]
---@return string
local function run_command(command_name, args)
local command = vim.list_extend({ command_name }, args)
local result = vim.fn.system(command)

if vim.v.shell_error ~= 0 then
error(("Failed to run command: '%s'"):format(command))
end

return result
end

-- Path for the plugin being tested
vim.opt.rtp:append(".")

local lua_path = run_command("luarocks", { "path", "--lr-path" })
local lua_cpath = run_command("luarocks", { "path", "--lr-cpath" })

-- Paths for the project-local luarocks packages
package.path = package.path .. ";" .. lua_path

-- Paths for the project-local shared libraries
package.cpath = package.cpath .. ";" .. lua_cpath

require("busted.runner")({ standalone = false })
```

Then change the test command in your rockspec to the following.

```lua
test = {
type = "command",
command = "nvim -l ./run-tests.lua",
}
```

---

</details>

</details>

<details>
<summary>Using lazy.nvim</summary>

The `lazy.nvim` package manager directly provides a way to run busted tests.
Please see the [official docs](https://lazy.folke.io/developers#minit-minimal-init).

---

</details>

## Contributing

Thanks for considering to contribute. Please see the instructions [here](/CONTRIBUTING.md).
Expand Down
1 change: 0 additions & 1 deletion neotest-busted-scm-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ build = {
type = "builtin",
copy_directories = {
"doc",
"scripts",
},
}

Expand Down
Loading
Loading