Skip to content

Commit

Permalink
feat: add configurable commands
Browse files Browse the repository at this point in the history
  • Loading branch information
olimorris authored May 26, 2023
1 parent 9960ae6 commit cb4808c
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 36 deletions.
93 changes: 76 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,43 @@ This plugin provides a [PHPUnit](https://phpunit.de) adapter for the [Neotest](h

## :package: Installation

Install the plugin using packer:
Install with the package manager of your choice:

**Lazy**

```lua
{
"nvim-neotest/neotest",
lazy = true,
dependencies = {
...,
"olimorris/neotest-phpunit",
},
config = function()
require("neotest").setup({
...,
adapters = {
require("neotest-phpunit")
},
}
end
}
```

**Packer**

```lua
use({
'nvim-neotest/neotest',
"nvim-neotest/neotest",
requires = {
...,
'olimorris/neotest-phpunit',
"olimorris/neotest-phpunit",
},
config = function()
require('neotest').setup({
require("neotest").setup({
...,
adapters = {
require('neotest-phpunit'),
require("neotest-phpunit"),
}
})
end
Expand All @@ -32,35 +55,76 @@ use({

## :wrench: Configuration

The plugin may be configured as below:
### Default configuration

> **Note**: You only need to the call the `setup` function if you wish to change any of the defaults.
<details>
<summary>Click to see the default configuration</summary>

```lua
adapters = {
require('neotest-phpunit')({
require("neotest-phpunit")({
phpunit_cmd = function()
return "vendor/bin/phpunit"
end
end,
root_files = { "composer.json", "phpunit.xml", ".gitignore" },
filter_dirs = { ".git", "node_modules" }
}),
}
```

</details>

### The test command

The command used to run tests can be changed via the `phpunit_cmd` option:

```lua
require("neotest-phpunit")({
phpunit_cmd = function()
return "vendor/bin/phpunit"
end
})
```

### Setting the root directory

For Neotest adapters to work, they need to define a project root whereby the process of discovering tests can take place. By default, the adapter looks for a `composer.json`, `phpunit.xml` or `.gitignore` file. These can be added to with:

```lua
require("neotest-phpunit")({
root_files = { "README.md" }
})
```

### Filtering directories

By default, the adapter will search test files in all dirs in the root with the exception of `node_modules` and `.git`. In a big project, this may result in slow performance. You can also add additional directories to filter out:

```lua
require("neotest-phpunit")({
filter_dirs = { "vendor" }
})
```

## :rocket: Usage

#### Test single method

To test a single test, hover over the test and run `lua require('neotest').run.run()`
To test a single test, hover over the test and run `lua require("neotest").run.run()`

#### Test file

To test a file run `lua require('neotest').run.run(vim.fn.expand('%'))`
To test a file run `lua require("neotest").run.run(vim.fn.expand("%"))`

#### Test directory

To test a directory run `lua require('neotest').run.run("path/to/directory")`
To test a directory run `lua require("neotest").run.run("path/to/directory")`

#### Test suite

To test the full test suite run `lua require('neotest').run.run({ suite = true })`
To test the full test suite run `lua require("neotest").run.run({ suite = true })`

## :gift: Contributing

Expand All @@ -72,8 +136,3 @@ To trigger the tests for the adapter, run:
./scripts/test
```

## :clap: Thanks

A special thanks to the following contributers:

- [boonkerz](https://github.com/boonkerz)
15 changes: 15 additions & 0 deletions lua/neotest-phpunit/config.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
local M = {}

M.get_phpunit_cmd = function()
return "vendor/bin/phpunit"
end

M.get_root_files = function()
return {}
end

M.get_filter_dirs = function()
return {}
end

return M
63 changes: 44 additions & 19 deletions lua/neotest-phpunit/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ end
local lib = require("neotest.lib")
local logger = require("neotest.logging")
local utils = require("neotest-phpunit.utils")
local config = require("neotest-phpunit.config")

---@class neotest.Adapter
---@field name string
Expand All @@ -16,18 +17,40 @@ local NeotestAdapter = { name = "neotest-phpunit" }
---@async
---@param dir string @Directory to treat as cwd
---@return string | nil @Absolute root dir of test suite
NeotestAdapter.root = lib.files.match_root_pattern("composer.json", "phpunit.xml")
function NeotestAdapter.root(dir)
local result = nil
local root_files =
vim.list_extend(config.get_root_files(), { "composer.json", "phpunit.xml", ".gitignore" })

for _, root_file in ipairs(root_files) do
result = lib.files.match_root_pattern(root_file)(dir)
if result then break end
end

return result
end

---@async
---@param file_path string
---@return boolean
function NeotestAdapter.is_test_file(file_path)
if string.match(file_path, "vendor/") or not string.match(file_path, "[Tt]ests/") then
return false
end
return vim.endswith(file_path, "Test.php")
end

---Filter directories when searching for test files
---@async
---@param name string Name of directory
---@return boolean
function NeotestAdapter.filter_dir(name)
local filter_dirs = vim.list_extend(config.get_filter_dirs(), { ".git", "node_modules" })

for _, filter_dir in ipairs(filter_dirs) do
if name == filter_dir then return false end
end

return true
end

---Given a file path, parse all the tests within it.
---@async
---@param file_path string Absolute file path
Expand All @@ -54,27 +77,15 @@ function NeotestAdapter.discover_positions(path)
})
end

---@return string
local function get_phpunit_cmd()
local binary = "phpunit"

if vim.fn.filereadable("vendor/bin/phpunit") then
binary = "vendor/bin/phpunit"
end

return binary
end

---@param args neotest.RunArgs
---@return neotest.RunSpec | nil
function NeotestAdapter.build_spec(args)
local position = args.tree:data()
local results_path = async.fn.tempname()

local binary = get_phpunit_cmd()

local command = vim.tbl_flatten({
binary,
config.get_phpunit_cmd(),
position.name ~= "tests" and position.path,
"--log-junit=" .. results_path,
})
Expand Down Expand Up @@ -138,12 +149,26 @@ end
setmetatable(NeotestAdapter, {
__call = function(_, opts)
if is_callable(opts.phpunit_cmd) then
get_phpunit_cmd = opts.phpunit_cmd
config.get_phpunit_cmd = opts.phpunit_cmd
elseif opts.phpunit_cmd then
get_phpunit_cmd = function()
config.get_phpunit_cmd = function()
return opts.phpunit_cmd
end
end
if is_callable(opts.root_files) then
config.get_root_files = opts.root_files
elseif opts.root_files then
config.get_root_files = function()
return opts.root_files
end
end
if is_callable(opts.filter_dirs) then
config.get_filter_dirs = opts.filter_dirs
elseif opts.filter_dirs then
config.get_filter_dirs = function()
return opts.filter_dirs
end
end
return NeotestAdapter
end,
})
Expand Down

0 comments on commit cb4808c

Please sign in to comment.