Skip to content

Commit

Permalink
fix: do not detect TestMain as test function (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikaverpil authored Sep 9, 2024
1 parent f187434 commit b96b1cb
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lua/neotest-golang/features/testify/query.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ M.namespace_query = [[
; name: (identifier)
type: (pointer_type
(type_identifier) @namespace.name )))) @namespace.definition
name: (field_identifier) @test_function (#match? @test_function "^(Test|Example)")
name: (field_identifier) @test_function (#match? @test_function "^(Test|Example)") (#not-match? @test.name "^TestMain$")
]]

M.test_method_query = [[
; query for test method
(method_declaration
name: (field_identifier) @test.name (#match? @test.name "^(Test|Example)")) @test.definition
name: (field_identifier) @test.name (#match? @test.name "^(Test|Example)") (#not-match? @test.name "^TestMain$")) @test.definition
]]

--- Run a TreeSitter query on a file and return the matches.
Expand Down
2 changes: 1 addition & 1 deletion lua/neotest-golang/query.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ local M = {}
M.test_function = [[
; query for test function
((function_declaration
name: (identifier) @test.name) (#match? @test.name "^(Test|Example)"))
name: (identifier) @test.name) (#match? @test.name "^(Test|Example)") (#not-match? @test.name "^TestMain$"))
@test.definition
; query for subtest, like t.Run()
Expand Down
7 changes: 7 additions & 0 deletions tests/go/positions_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package main

import (
"os"
"testing"
)

// This function should not be detected by neotest-golang.
func TestMain(m *testing.M) {
code := m.Run()
os.Exit(code)
}

// Vanilla top-level test.
func TestTopLevel(t *testing.T) {
if Add(1, 2) != 3 {
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/golist_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ describe("go list output", function()
StaleReason = "build ID mismatch",
Target = "/Users/fredrik/go/bin/neotest-golang",
TestGoFiles = { "positions_test.go", "testname_test.go" },
TestImports = { "testing" },
TestImports = { "os", "testing" },
}

-- ignored keys, as they might differ between OS/CI/platforms/too often
Expand Down

0 comments on commit b96b1cb

Please sign in to comment.