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

"Run nearest" runs all tests #83

Open
fredrikaverpil opened this issue Feb 24, 2024 · 1 comment
Open

"Run nearest" runs all tests #83

fredrikaverpil opened this issue Feb 24, 2024 · 1 comment

Comments

@fredrikaverpil
Copy link

fredrikaverpil commented Feb 24, 2024

When I "run nearest test" (require("neotest").run.run()) when positioning my cursor on the test function, I can see how neotest-go executes all tests.

Inspecting the go test command generated by neotest-go

I inspected the go test command produced by printing the command variable value in init.lua:

local command = vim.tbl_flatten({
  ...
})
print(vim.inspect(command))

...which gave me this command:

{ "cd", "/Users/fredrik/code/public/go-playground/bugs/neotest-go", "&&", "go", "test", "-v", "-json", "", "-coverprofile=/Users/fredrik/code/public/go-playground/bugs/neotest-go/coverage.out", "./" }

Note that I'm adding coverprofile myself here.

I would've expected something different, such as having added the -run flag to go test along with a regex matching the test name to the command. For example: go test -run ^Test_Level_1$ to the command.

Inspecting the tests map returned by marshal_gotest_output.tests

I then inspected the value of the tests returned from marshal_gotest_output which again shows how a bunch of tests in other files are being executed.

{
 ["neotest-go::TestAdd"] = {
   file_output = {},
   output = { "=== RUN   TestAdd\n", "\27[32m--- PASS: TestAdd (0.00s)\n\27[0m" },
   progress = { "run", "output", "output", "pass" },
   status = "passed"
 },
 ["neotest-go::TestAddSubTestLevel1"] = {
   file_output = {},
   output = { "=== RUN   TestAddSubTestLevel1\n", "=== RUN   TestAddSubTestLevel1/Add1\n", "\27[32m--- PASS: TestAddSubTestLevel1 (0.00s)\n\27[0m", "\27[32m    --- PASS: TestAddSubTestLevel1/Add1 (0.00s)\n\27[0m" },
   progress = { "run", "output", "output", "pass" },
   status = "passed"
 },
 ["neotest-go::TestAddSubTestLevel1::Add1"] = {
   file_output = {},
   output = { "=== RUN   TestAddSubTestLevel1/Add1\n", "\27[32m    --- PASS: TestAddSubTestLevel1/Add1 (0.00s)\n\27[0m" },
   progress = { "run", "output", "output", "pass" },
   status = "passed"
 },
 ["neotest-go::TestAddSubTestLevel2"] = {
   file_output = {},
   output = { "=== RUN   TestAddSubTestLevel2\n", "=== RUN   TestAddSubTestLevel2/Add1\n", "=== RUN   TestAddSubTestLevel2/Add1/Add2\n", "    marshaloutput_test.go:31: 1 + 2 did not equal 3\n", "\27[31m--- FAIL: TestAddSubTestLevel2 (0.00s)\n\27[0m", "\27[31m    --- FAIL: TestAddSubTestLevel2/Add1 (0.00s)\n\27[0m", "\27[31m        --- FAIL: TestAddSubTestLevel2/Add1/Add2 (0.00s)\n\27[0m" },
   progress = { "run", "output", "output", "fail" },
   status = "failed"
 },
 ["neotest-go::TestAddSubTestLevel2::Add1"] = {
   file_output = {},
   output = { "=== RUN   TestAddSubTestLevel2/Add1\n", "\27[31m    --- FAIL: TestAddSubTestLevel2/Add1 (0.00s)\n\27[0m" },
   progress = { "run", "output", "output", "fail" },
   status = "failed"
 },
 ["neotest-go::TestAddSubTestLevel2::Add1::Add2"] = {
   file_output = {
     ["marshaloutput_test.go"] = {
       [31] = { "1 + 2 did not equal 3\n", "--- FAIL: TestAddSubTestLevel2/Add1/Add2 (0.00s)\n" }
     }
   },
   output = { "=== RUN   TestAddSubTestLevel2/Add1/Add2\n", "    marshaloutput_test.go:31: 1 + 2 did not equal 3\n", "\27[31m        --- FAIL: TestAddSubTestLevel2/Add1/Add2 (0.00s)\n\27[0m" },
   progress = { "run", "output", "output", "output", "fail" },
   status = "failed"
 },
 ["neotest-go::Test_Level_1"] = {
   file_output = {},
   output = { "=== RUN   Test_Level_1\n", "=== RUN   Test_Level_1/Level_2\n", "=== RUN   Test_Level_1/Level_2/Level_3\n", "\27[32m--- PASS: Test_Level_1 (0.00s)\n\27[0m", "\27[32m    --- PASS: Test_Level_1/Level_2 (0.00s)\n\27[0m", "\27[32m        --- PASS: Test_Level_1/Level_2/Level_3 (0.00s)\n\27[0m" },
   progress = { "run", "output", "output", "pass" },
   status = "passed"
 },
 ["neotest-go::Test_Level_1::Level_2"] = {
   file_output = {},
   output = { "=== RUN   Test_Level_1/Level_2\n", "\27[32m    --- PASS: Test_Level_1/Level_2 (0.00s)\n\27[0m" },
   progress = { "run", "output", "output", "pass" },
   status = "passed"
 },
 ["neotest-go::Test_Level_1::Level_2::Level_3"] = {
   file_output = {},
   output = { "=== RUN   Test_Level_1/Level_2/Level_3\n", "\27[32m        --- PASS: Test_Level_1/Level_2/Level_3 (0.00s)\n\27[0m" },
   progress = { "run", "output", "output", "pass" },
   status = "passed"
 }
}
@fredrikaverpil fredrikaverpil changed the title Run nearest test runs all tests "Run nearest" runs all tests Feb 24, 2024
@cmiran
Copy link

cmiran commented Mar 10, 2024

For anyone having the same issue, as a workaround I wrote a function that leverages Treesitter to get the nearest function name, then I add -run=xxx as extra arg to a dedicate keymap. It only works when the cursor is within a function definition.

local function get_nearest_function_name()
  local ts_utils = require("nvim-treesitter.ts_utils")
  local node = ts_utils.get_node_at_cursor()

  while node do
    if node:type() == "function_declaration" then
      return ts_utils.get_node_text(node:child(1))[1]
    end
    node = node:parent()
  end
end

vim.keymap.set({"n", "<leader>tf",
  function()
    local name = get_nearest_function_name()
    if not name then
      return
    end

    require("neotest").run.run({
      extra_args = { "-run", name ) },
    })
  end,
  desc = "Test nearest function",
})

folke pushed a commit to LazyVim/LazyVim that referenced this issue Jun 23, 2024
## What is this PR for?

This PR switches
[nvim-neotest/neotest-go](https://github.com/nvim-neotest/neotest-go)
for
[fredrikaverpil/neotest-golang](https://github.com/fredrikaverpil/neotest-golang).

## Does this PR fix an existing issue?

Neotest-go comes with some problems which are mitigated in
neotest-golang. A full description/background is available in the
project README, but here are some highlights:

### Neotest-go issues mitigated in neotest-golang

- Test Output in JSON, making it difficult to read:
[neotest-go#52](nvim-neotest/neotest-go#52)
- "Run nearest" runs all tests:
[neotest-go#83](nvim-neotest/neotest-go#83)
- Running test suite doesn't work:
[neotest-go#89](nvim-neotest/neotest-go#89)
- Diagnostics for table tests on the line of failure:
[neotest-go#75](nvim-neotest/neotest-go#75)
- Support for Nested Subtests:
[neotest-go#74](nvim-neotest/neotest-go#74)
- DAP support:
[neotest-go#12](nvim-neotest/neotest-go#12)

### Features

- Supports all [Neotest
usage](https://github.com/nvim-neotest/neotest#usage).
- Integrates with [nvim-dap-go](https://github.com/leoluz/nvim-dap-go)
for debugging of tests using delve.
- Inline diagnostics.
- Works great with
[andythigpen/nvim-coverage](https://github.com/andythigpen/nvim-coverage)
for displaying coverage in the sign column (per-Go package, or per-test
basis).
- Monorepo support (detect, run and debug tests in sub-projects).
- Supports table tests (relies on treesitter AST detection).
- Supports nested test functions.

## Notes

- I'm the author of
[fredrikaverpil/neotest-golang](https://github.com/fredrikaverpil/neotest-golang).


## Checklist

- [x] I've read the
[CONTRIBUTING](https://github.com/LazyVim/LazyVim/blob/main/CONTRIBUTING.md)
guidelines.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants