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

fix: properly handle XTestGoFiles #225

Merged
merged 2 commits into from
Dec 8, 2024
Merged

fix: properly handle XTestGoFiles #225

merged 2 commits into from
Dec 8, 2024

Conversation

fredrikaverpil
Copy link
Owner

@fredrikaverpil fredrikaverpil commented Dec 8, 2024

Why this change?

If you add tests as part of [packagename]_test (an otherwise non-existing
package), go list -json did not properly aid in mapping from filename to Go
package, and cause the go test command to fall back to using ./... instead
of the desired package name. This become a problem if you want to run all tests
of a given file.

What was changed?

Not only look for the TestGoFiles key in the JSON output from go list -json.
Also look for the XTestGoFiles key.

Notes

To do

  • Fix.
  • Tests.

TestGoFiles vs XTestGoFiles

TestGoFiles:

  • Contains test files that are part of the same package as the code being
    tested.
  • These are files named *_test.go that have the same package name as the
    source files.
  • Used for white-box testing where tests can access unexported (private)
    members.

XTestGoFiles:

  • Contains test files that are in a separate package named [packagename]_test.
  • These are files named *_test.go that have the package name
    [packagename]_test.
  • Used for black-box testing where tests can only access exported (public)
    members.
  • This field is absent in the JSON output when there are no external test files.

Example:

// main.go
package mypackage

func Add(a, b int) int { return a + b }
func internal() string { return "private" }
// internal_test.go (TestGoFiles)
package mypackage

func TestAdd(t *testing.T) {
    // Can access both Add() and internal()
}
// external_test.go (XTestGoFiles)
package mypackage_test

func TestAdd(t *testing.T) {
    // Can only access Add(), cannot access internal()
}

fixes: #222

@fredrikaverpil fredrikaverpil self-assigned this Dec 8, 2024
@fredrikaverpil fredrikaverpil force-pushed the xtestgofiles branch 3 times, most recently from c53700d to 51ba60f Compare December 8, 2024 14:13
@fredrikaverpil fredrikaverpil enabled auto-merge (squash) December 8, 2024 14:30
@fredrikaverpil fredrikaverpil merged commit 04f3d44 into main Dec 8, 2024
9 checks passed
@fredrikaverpil fredrikaverpil deleted the xtestgofiles branch December 8, 2024 14:31
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

Successfully merging this pull request may close these issues.

bug: TestMain on other packages are run when executing Run File (neotest) if the test package has _test suffix
1 participant