Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions import_path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,37 @@ 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-<change>"), 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.
pkg, err := build.Import("github.com/androidand/specsync", ".", build.ImportMode(0))
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)
}
}

Expand Down
35 changes: 35 additions & 0 deletions openspec/changes/fix-import-path-test-worktree-name/proposal.md
Original file line number Diff line number Diff line change
@@ -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-<change>`, 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-<change>` worktree convention) —
it now checks the actual module-root invariant instead of a folder-name
string match.
6 changes: 6 additions & 0 deletions openspec/changes/fix-import-path-test-worktree-name/tasks.md
Original file line number Diff line number Diff line change
@@ -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".
Loading