diff --git a/import_path_test.go b/import_path_test.go index 935e942..2857f38 100644 --- a/import_path_test.go +++ b/import_path_test.go @@ -2,14 +2,24 @@ package specsync import ( "go/build" - "strings" + "os" "testing" ) // TestImportPath guards the documented import path: the package must live at // the module root so that the path in doc.go (github.com/androidand/specsync) -// always resolves. If the package is ever relocated, this test fails until -// the documentation is updated to match. +// always resolves, not nested under pkg/ or similar. If the package is ever +// relocated, this test fails until the documentation is updated to match. +// +// The check compares against the test binary's own working directory (which +// `go test` always sets to the package's source directory) rather than a +// literal "specsync" folder-name check: a checkout or git worktree can be +// named anything (this repo's own worktree convention names them after the +// branch, e.g. "specsync-"), so asserting on the basename would fail +// outside the one clone happening to be named "specsync" — checking that the +// canonical import path resolves back to wherever the package's own files +// actually are is the real invariant, and it holds regardless of the +// directory's name. func TestImportPath(t *testing.T) { // Import the module by its canonical path; this resolves to the actual // directory regardless of where the test is run from. @@ -17,8 +27,12 @@ func TestImportPath(t *testing.T) { if err != nil { t.Fatalf("import specsync: %v", err) } - if !strings.HasSuffix(pkg.Dir, "specsync") { - t.Errorf("package directory %q does not match module root", pkg.Dir) + wd, err := os.Getwd() + if err != nil { + t.Fatalf("getwd: %v", err) + } + if pkg.Dir != wd { + t.Errorf("package directory %q does not match the module root %q", pkg.Dir, wd) } } diff --git a/openspec/changes/fix-import-path-test-worktree-name/proposal.md b/openspec/changes/fix-import-path-test-worktree-name/proposal.md new file mode 100644 index 0000000..a556f19 --- /dev/null +++ b/openspec/changes/fix-import-path-test-worktree-name/proposal.md @@ -0,0 +1,35 @@ +# Fix TestImportPath failing outside a folder literally named "specsync" + +## Why + +`TestImportPath` (import_path_test.go) is meant to guard doc.go's stated +invariant — "the package lives at the module root, not under pkg/" — but it +actually asserted something narrower and wrong: that the resolved package +directory's *basename* ends in the literal string `"specsync"` +(`strings.HasSuffix(pkg.Dir, "specsync")`). That's true for the primary clone +(`/…/specsync`) but false for any git worktree named after its branch — this +repo's own convention (`specsync-`, e.g. this session's +`specsync-design-notes`) — so the test fails in every worktree despite +nothing being wrong with the package's location. + +## Proposed Changes + +- Compare the resolved package directory (`build.Import`'s `pkg.Dir`) + against the test binary's own working directory (`os.Getwd()`) instead of + a literal folder-name substring. `go test` always sets the process's + working directory to the package's source directory, so this checks the + actual invariant — resolving the canonical import path lands back at + wherever the package's files really are — regardless of what the + containing checkout or worktree happens to be named on disk. +- The test still fails the way it's meant to: if the package were ever + relocated under `pkg/` while this test file moved with it, `build.Import` + either errors (no Go files left at the module root) or resolves to a + different directory than the moved test's own `cwd` — both are still + caught. + +## Release Notes + +Fixed `TestImportPath` failing in any git worktree not literally named +"specsync" (e.g. this repo's own `specsync-` worktree convention) — +it now checks the actual module-root invariant instead of a folder-name +string match. diff --git a/openspec/changes/fix-import-path-test-worktree-name/tasks.md b/openspec/changes/fix-import-path-test-worktree-name/tasks.md new file mode 100644 index 0000000..0d37519 --- /dev/null +++ b/openspec/changes/fix-import-path-test-worktree-name/tasks.md @@ -0,0 +1,6 @@ +# Tasks + +- [x] Rewrite `TestImportPath` to compare `pkg.Dir` against `os.Getwd()` + instead of a literal `"specsync"` basename suffix check. + Validation: `go test ./...` passes with zero failures when run from a + worktree whose folder is not literally named "specsync".