nvim-neotest / neotest-go

MIT License
124 stars 43 forks source link

"Run nearest" runs all tests #83

Open fredrikaverpil opened 4 months ago

fredrikaverpil commented 4 months ago

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"
 }
}
cmiran commented 3 months ago

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",
})