beads/scripts/check_doc_freshness_test.go
Julian Knutsen a4509deb2d
ci(bazel): nogo replaces go vet and golangci-lint (ga-96smfk.18) (#7341)
* ci(bazel): nogo replaces go vet and golangci-lint (ga-96smfk.18)

Lint and vet now run as nogo inside the Bazel build, so they gate wherever
the Bazel lanes execute (rbe-west), and the golangci-lint and go vet jobs
leave pr.yml and main.yml.

- //tools/nogo (own go.mod, joined to beads' for go_deps only through
  gazelle.go.work): go test's vet checks (cmd/go's defaultVetFlags, the set
  scripts/ci/go-test-vet.sh ran) plus the eight linters .golangci.yml
  enables, each a wrapper over the library version golangci-lint v2.10.1
  uses (depguard, errcheck, forbidigo, gosec, misspell, sloglint, unconvert,
  unparam).
- .golangci.yml stays the single lint policy: Bazel embeds it and
  tools/nogo/internal/golangci applies it as golangci-lint does (enabled
  set, settings, run.tests: false as library-unit-only analysis, lax
  generated-file detection, path/path-except/text exclusion rules), with
  strict decoding so an unimplemented key fails the build.
- gascity's rules_go //nolint-range patch and x/tools ctrlflow patch,
  byte-identical.
- Windows/darwin: cross-configured, not a golangci leg. The pure lane's
  release cross-compile (//tools/bazel:release_cross, every library and
  binary for all eight release platforms, cgo off) runs nogo on every
  compile, a superset of golangci-lint's windows and darwin legs.
  scripts/pr-lint (make ci-pr-lint, bd preflight) runs `bazel build
  --config=nogo //...` plus release_cross for windows_amd64,darwin_arm64.
  release_cross skips tools/nogo, a separate Go module `go build ./...`
  never compiled.
- pr.yml: scripts-go-checks (vet) and pr-lint-wrapper (x3) are gone, with
  their ci-gate entries; main.yml: pr-lint-wrapper (x6) and go-vet-cache.
  scripts/ci/go-test-vet.sh and install-golangci-lint.sh are deleted.
- pre-commit hook and .pre-commit-config.yaml run `make lint-changed
  LINT_CHANGED_SCOPE=staged` (nogo over the staged packages); Makefile adds
  lint, vet and lint-changed. pr-lint.sh no longer runs gofmt
  (//scripts/repochecks:fmt_test owns it; ga-96smfk.41).
- internal/testutil/integration: five gosec findings in integration-tagged
  library files golangci-lint never built, now linted by the integration
  lanes, carry reasoned //nolint directives.

Parity: with the exclusion rules removed, golangci-lint and nogo report the
same 3,916 findings natively and 3,859 each for windows and darwin (file,
line, linter, text); with .golangci.yml as committed both report none.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* ci(bazel): equivalence.py leaves nested Go modules' tests out of go test's set

tools/nogo is its own Go module, so `go test ./...` in the root never runs
its tests; Bazel's unit lane does. Its packages are neither expected nor
extra.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-07 05:17:22 +00:00

652 lines
22 KiB
Go

//go:build integration
package scripts_test
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/steveyegge/beads/scripts/internal/testbash"
)
var freshnessDocuments = []struct {
path string
sources []string
}{
{"docs/reference/configuration.md", []string{"cmd/bd/main.go", "cmd/bd/config.go", "internal/configfile/"}},
{"docs/getting-started/ide-setup.md", []string{"cmd/bd/setup*.go", "internal/recipes/"}},
{"docs/integrations/azure-devops.md", []string{"cmd/bd/ado*.go", "internal/ado/"}},
{"docs/reference/json-schema.md", []string{"cmd/bd/output.go", "cmd/bd/errors.go", "cmd/bd/protocol/json_contract_test.go"}},
{"docs/recovery/init-safety.md", []string{"cmd/bd/init.go", "cmd/bd/init_safety.go", "cmd/bd/init_safety_test.go", "cmd/bd/init_safety_help.go", "cmd/bd/dolt.go"}},
{"engdocs/ERROR_HANDLING.md", []string{"cmd/bd/*.go", "cmd/bd/errors.go"}},
{"engdocs/SERVE_RUNBOOK.md", []string{"internal/httpapi/server.go", "internal/httpapi/events_watch.go", "cmd/bd/serve.go", "internal/httpapi/auth.go"}},
{"engdocs/LINTING.md", []string{".golangci.yml", "tools/nogo/", "scripts/ci/pr-lint.sh", "scripts/pr-lint/", "Makefile", ".github/workflows/pr.yml", ".github/workflows/main.yml"}},
{"engdocs/CI_CLEANUP_PLAN.md", []string{"engdocs/CI_TEST_SURFACE_AUDIT.md", ".github/workflows/*.yml", ".buildflags", ".golangci.yml", "scripts/ci/pr-lint.sh", "scripts/pr-lint/", "Makefile"}},
{"engdocs/design/otel/otel-data-model.md", []string{"internal/telemetry/", "internal/storage/dolt/store.go", "internal/compact/haiku.go", "cmd/bd/find_duplicates.go", "internal/hooks/"}},
}
type freshnessRun struct {
output string
err error
pythonInvoked bool
dateInvoked bool
}
const pythonPoisonFunction = `python3() {
printf 'invoked\n' >"$PYTHON_POISON_MARKER"
return 97
}
`
const brokenDateFunction = `
date() {
printf 'invoked\n' >"$DATE_BACKEND_MARKER"
return 69
}
`
const garbageDateFunction = `
date() {
printf 'invoked\n' >"$DATE_BACKEND_MARKER"
printf 'garbage\n'
}
`
const recordingNativeDateFunction = `
date() {
printf 'invoked\n' >"$DATE_BACKEND_MARKER"
command date "$@"
}
`
// Register every TestDocFreshness* test in requiredSuiteDefinitions in required_suite_test.go.
func TestDocFreshnessDoesNotRequirePython(t *testing.T) {
run := runDocFreshness(t, "2026-01-15", "2026-01-31", "90")
if run.err != nil {
t.Fatalf("check-doc-freshness failed without Python: %v\n%s", run.err, run.output)
}
if run.pythonInvoked {
t.Fatalf("check-doc-freshness invoked python3:\n%s", run.output)
}
for _, want := range []string{
"PASS: Last reviewed marker is current: 2026-01-15 (16 days old)",
"PASSED: Reference doc freshness markers are current and checkable",
} {
if !strings.Contains(run.output, want) {
t.Fatalf("missing %q:\n%s", want, run.output)
}
}
}
func TestDocFreshnessValidatesMaxAge(t *testing.T) {
t.Run("unset uses default", func(t *testing.T) {
run := runDocFreshnessWithoutMaxAge(t, "2026-01-15", "2026-04-15")
if run.err != nil {
t.Fatalf("check-doc-freshness rejected the default max age: %v\n%s", run.err, run.output)
}
if !strings.Contains(run.output, "Max age: 90 days") {
t.Fatalf("missing default max-age result:\n%s", run.output)
}
})
valid := []struct {
name string
reviewed string
today string
maxAge string
want string
}{
{name: "empty uses default", reviewed: "2026-01-15", today: "2026-01-31", maxAge: "", want: "Max age: 90 days"},
{name: "zero", reviewed: "2026-01-15", today: "2026-01-15", maxAge: "0", want: "Max age: 0 days"},
{name: "leading zeroes", reviewed: "2026-01-15", today: "2026-01-31", maxAge: "000000090", want: "Max age: 90 days"},
{name: "full calendar span", reviewed: "0001-01-01", today: "9999-12-31", maxAge: "3652058", want: "PASS: Last reviewed marker is current: 0001-01-01 (3652058 days old)"},
{name: "larger than Bash integer range", reviewed: "2026-01-15", today: "2026-01-31", maxAge: strings.Repeat("9", 100), want: "PASS: Last reviewed marker is current: 2026-01-15 (16 days old)"},
}
for _, test := range valid {
t.Run(test.name, func(t *testing.T) {
run := runDocFreshness(t, test.reviewed, test.today, test.maxAge)
if run.err != nil {
t.Fatalf("check-doc-freshness rejected max age %q: %v\n%s", test.maxAge, run.err, run.output)
}
if !strings.Contains(run.output, test.want) {
t.Fatalf("missing %q:\n%s", test.want, run.output)
}
})
}
invalid := []struct {
name string
maxAge string
}{
{name: "negative", maxAge: "-1"},
{name: "signed", maxAge: "+90"},
{name: "expression", maxAge: "1+2"},
{name: "variable reference", maxAge: "TODAY_DAY"},
{name: "whitespace", maxAge: " 90"},
}
for _, test := range invalid {
t.Run(test.name, func(t *testing.T) {
run := runDocFreshness(t, "2026-01-15", "2026-01-31", test.maxAge)
if exitCode(run.err) != 2 {
t.Fatalf("exit = %d, want 2; error=%v\n%s", exitCode(run.err), run.err, run.output)
}
if !strings.Contains(run.output, "ERROR: DOC_FRESHNESS_MAX_AGE_DAYS must be a nonnegative decimal integer") {
t.Fatalf("missing max-age diagnostic:\n%s", run.output)
}
if strings.Contains(run.output, "Checking reference doc freshness markers") {
t.Fatalf("invalid max age reached document checks:\n%s", run.output)
}
})
}
t.Run("long leading-zero value keeps decimal semantics", func(t *testing.T) {
run := runDocFreshness(t, "2026-01-15", "2026-01-31", strings.Repeat("0", 100)+"15")
if exitCode(run.err) != 1 {
t.Fatalf("exit = %d, want 1; error=%v\n%s", exitCode(run.err), run.err, run.output)
}
if !strings.Contains(run.output, "Max age: 15 days") ||
!strings.Contains(run.output, "FAIL: Last reviewed date is stale: 2026-01-15 (16 days old)") {
t.Fatalf("long leading-zero threshold did not compare as decimal 15:\n%s", run.output)
}
})
}
func TestDocFreshnessBashProbeIgnoresStartupEnvironment(t *testing.T) {
if runtime.GOOS != "windows" {
t.Skip("Git Bash discovery is Windows-specific")
}
t.Run("startup and option controls", func(t *testing.T) {
poison := filepath.Join(t.TempDir(), "poison.sh")
if err := os.WriteFile(poison, []byte("exit 97\n"), 0o600); err != nil {
t.Fatalf("write startup poison: %v", err)
}
t.Setenv("BASH_ENV", poison)
t.Setenv("ENV", poison)
t.Setenv("SHELLOPTS", "noexec")
t.Setenv("BASHOPTS", "failglob")
t.Setenv("BASH_FUNC_uname%%", "() { builtin printf 'Linux\\n'; }")
if bash := docFreshnessBash(t); bash == "" {
t.Fatal("docFreshnessBash returned an empty executable")
}
})
t.Run("valid explicit override", func(t *testing.T) {
t.Setenv(testbash.OverrideEnv, "")
resolved := docFreshnessBash(t)
t.Setenv(testbash.OverrideEnv, resolved)
if got := docFreshnessBash(t); !strings.EqualFold(filepath.Clean(got), filepath.Clean(resolved)) {
t.Fatalf("resolved override = %q, want %q", got, resolved)
}
})
t.Run("invalid explicit override is configuration", func(t *testing.T) {
t.Setenv(testbash.OverrideEnv, "bash.exe")
if _, err := testbash.Resolve(); err == nil {
t.Fatal("relative Git Bash override unexpectedly succeeded")
} else if !testbash.IsConfigurationError(err) {
t.Fatalf("relative override error was not classified as configuration: %v", err)
}
})
t.Run("poisoned Git exec path", func(t *testing.T) {
t.Setenv(testbash.OverrideEnv, "")
t.Setenv("GIT_EXEC_PATH", t.TempDir())
if bash := docFreshnessBash(t); bash == "" {
t.Fatal("docFreshnessBash returned an empty executable")
}
})
t.Run("success without execution sentinel", func(t *testing.T) {
t.Setenv(testbash.OverrideEnv, "")
bash := docFreshnessBash(t)
err := testbash.Probe(bash, "early exit", "exit 0", os.Environ())
if err == nil {
t.Fatal("early successful exit unexpectedly passed without the execution sentinel")
}
if !strings.Contains(err.Error(), "without the exact execution sentinel") {
t.Fatalf("early-exit error = %v", err)
}
})
}
func TestDocFreshnessPreservesDateDiagnostics(t *testing.T) {
tests := []struct {
name string
reviewed string
today string
maxAge string
want string
}{
{
name: "invalid date",
reviewed: "2026-02-30",
today: "2026-03-01",
maxAge: "90",
want: "FAIL: invalid Last reviewed date: 2026-02-30",
},
{
name: "year zero",
reviewed: "0000-01-01",
today: "2026-03-01",
maxAge: "90",
want: "FAIL: invalid Last reviewed date: 0000-01-01",
},
{
name: "future date",
reviewed: "2026-03-02",
today: "2026-03-01",
maxAge: "90",
want: "FAIL: Last reviewed date is in the future: 2026-03-02",
},
{
name: "stale date",
reviewed: "2025-01-01",
today: "2026-03-01",
maxAge: "30",
want: "FAIL: Last reviewed date is stale: 2025-01-01",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
run := runDocFreshness(t, test.reviewed, test.today, test.maxAge)
if exitCode(run.err) != 1 {
t.Fatalf("exit = %d, want 1; error=%v\n%s", exitCode(run.err), run.err, run.output)
}
if run.pythonInvoked {
t.Fatalf("check-doc-freshness invoked python3:\n%s", run.output)
}
if !strings.Contains(run.output, test.want) {
t.Fatalf("missing %q:\n%s", test.want, run.output)
}
})
}
}
func TestDocFreshnessPreservesISODateBoundaries(t *testing.T) {
for _, value := range []string{"0001-01-01", "2000-02-29", "9999-12-31"} {
t.Run(value, func(t *testing.T) {
run := runDocFreshness(t, value, value, "90")
if run.err != nil {
t.Fatalf("check-doc-freshness rejected supported boundary %s: %v\n%s", value, run.err, run.output)
}
if !strings.Contains(run.output, "PASS: Last reviewed marker is current: "+value+" (0 days old)") {
t.Fatalf("missing boundary date result for %s:\n%s", value, run.output)
}
})
}
}
func TestDocFreshnessUsesProlepticGregorianLeapRules(t *testing.T) {
tests := []struct {
name string
reviewed string
today string
wantExit int
want string
}{
{
name: "common century",
reviewed: "1900-02-28",
today: "1900-03-01",
wantExit: 0,
want: "PASS: Last reviewed marker is current: 1900-02-28 (1 days old)",
},
{
name: "leap century",
reviewed: "2000-02-28",
today: "2000-03-01",
wantExit: 0,
want: "PASS: Last reviewed marker is current: 2000-02-28 (2 days old)",
},
{
name: "ordinary leap year",
reviewed: "2024-02-28",
today: "2024-03-01",
wantExit: 0,
want: "PASS: Last reviewed marker is current: 2024-02-28 (2 days old)",
},
{
name: "invalid common-century leap day",
reviewed: "1900-02-29",
today: "1900-03-01",
wantExit: 1,
want: "FAIL: invalid Last reviewed date: 1900-02-29",
},
{
name: "invalid month zero",
reviewed: "2026-00-01",
today: "2026-03-01",
wantExit: 1,
want: "FAIL: invalid Last reviewed date: 2026-00-01",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
run := runDocFreshness(t, test.reviewed, test.today, "90")
if exitCode(run.err) != test.wantExit {
t.Fatalf("exit = %d, want %d; error=%v\n%s", exitCode(run.err), test.wantExit, run.err, run.output)
}
if run.pythonInvoked {
t.Fatalf("check-doc-freshness invoked python3:\n%s", run.output)
}
if !strings.Contains(run.output, test.want) {
t.Fatalf("missing %q:\n%s", test.want, run.output)
}
})
}
}
func TestDocFreshnessUsesNativeDefaultTodayProvider(t *testing.T) {
run := runDocFreshnessWithDefaultToday(t, "0001-01-01", "4000000", recordingNativeDateFunction)
if run.err != nil {
t.Fatalf("check-doc-freshness failed with native default TODAY: %v\n%s", run.err, run.output)
}
if !run.dateInvoked {
t.Fatalf("check-doc-freshness did not invoke native date for the default TODAY value:\n%s", run.output)
}
if run.pythonInvoked {
t.Fatalf("check-doc-freshness invoked python3:\n%s", run.output)
}
if !strings.Contains(run.output, "PASSED: Reference doc freshness markers are current and checkable") {
t.Fatalf("missing successful default-TODAY result:\n%s", run.output)
}
}
func TestDocFreshnessReportsUnavailableTodayProvider(t *testing.T) {
run := runDocFreshnessWithDefaultToday(t, "2026-01-15", "90", brokenDateFunction)
if exitCode(run.err) != 2 {
t.Fatalf("exit = %d, want 2; error=%v\n%s", exitCode(run.err), run.err, run.output)
}
if !run.dateInvoked {
t.Fatalf("check-doc-freshness did not invoke date for the default TODAY value:\n%s", run.output)
}
if !strings.Contains(run.output, "ERROR: check-doc-freshness could not determine today's date") {
t.Fatalf("missing explicit TODAY provider diagnostic:\n%s", run.output)
}
if strings.Contains(run.output, "invalid Last reviewed date") {
t.Fatalf("broken TODAY provider was mislabeled as document data:\n%s", run.output)
}
}
func TestDocFreshnessReportsInvalidTodayProviderOutput(t *testing.T) {
run := runDocFreshnessWithDefaultToday(t, "2026-01-15", "90", garbageDateFunction)
if exitCode(run.err) != 2 {
t.Fatalf("exit = %d, want 2; error=%v\n%s", exitCode(run.err), run.err, run.output)
}
if !run.dateInvoked {
t.Fatalf("check-doc-freshness did not invoke date for the default TODAY value:\n%s", run.output)
}
if !strings.Contains(run.output, "ERROR: date returned an invalid current date: garbage") {
t.Fatalf("missing invalid-provider-output diagnostic:\n%s", run.output)
}
if strings.Contains(run.output, "DOC_FRESHNESS_TODAY") {
t.Fatalf("provider output was mislabeled as an override:\n%s", run.output)
}
}
func TestDocFreshnessReportsInvalidTodayOverride(t *testing.T) {
run := runDocFreshness(t, "2026-01-15", "garbage", "90")
if exitCode(run.err) != 2 {
t.Fatalf("exit = %d, want 2; error=%v\n%s", exitCode(run.err), run.err, run.output)
}
if run.dateInvoked {
t.Fatalf("check-doc-freshness invoked date despite an explicit TODAY override:\n%s", run.output)
}
if !strings.Contains(run.output, "ERROR: invalid DOC_FRESHNESS_TODAY date: garbage") {
t.Fatalf("missing invalid-override diagnostic:\n%s", run.output)
}
}
func TestDocFreshnessRejectsMultipleLastReviewedMarkers(t *testing.T) {
tests := []struct {
name string
dates []string
}{
{name: "two markers both fresh", dates: []string{"2026-01-10", "2026-01-12"}},
{name: "two markers one stale", dates: []string{"2026-01-15", "2020-01-01"}},
{name: "three markers", dates: []string{"2026-01-15", "2026-01-16", "2026-01-17"}},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
run := runDocFreshnessWithDuplicateMarker(t, "docs/reference/configuration.md", test.dates, "2026-01-31", "90")
if exitCode(run.err) != 1 {
t.Fatalf("exit = %d, want 1; error=%v\n%s", exitCode(run.err), run.err, run.output)
}
want := fmt.Sprintf("FAIL: found %d Last reviewed markers; expected exactly one", len(test.dates))
if !strings.Contains(run.output, want) {
t.Fatalf("missing %q:\n%s", want, run.output)
}
})
}
}
func runDocFreshness(t *testing.T, reviewed, today, maxAge string) freshnessRun {
t.Helper()
return runDocFreshnessWithDateFunction(t, reviewed, today, &maxAge, "")
}
func runDocFreshnessWithoutMaxAge(t *testing.T, reviewed, today string) freshnessRun {
t.Helper()
return runDocFreshnessWithDateFunction(t, reviewed, today, nil, "")
}
func runDocFreshnessWithDateFunction(t *testing.T, reviewed, today string, maxAge *string, dateFunction string) freshnessRun {
t.Helper()
return runDocFreshnessProcess(t, reviewed, &today, maxAge, dateFunction)
}
func runDocFreshnessWithDefaultToday(t *testing.T, reviewed, maxAge, dateFunction string) freshnessRun {
t.Helper()
return runDocFreshnessProcess(t, reviewed, nil, &maxAge, dateFunction)
}
func runDocFreshnessWithDuplicateMarker(t *testing.T, targetPath string, targetReviewedDates []string, today, maxAge string) freshnessRun {
t.Helper()
root := newDocFreshnessFixtureWithMarkers(t, "2026-01-15", targetPath, targetReviewedDates)
return runDocFreshnessProcessInRoot(t, root, &today, &maxAge, "")
}
func runDocFreshnessProcess(t *testing.T, reviewed string, today, maxAge *string, dateFunction string) freshnessRun {
t.Helper()
root := newDocFreshnessFixture(t, reviewed)
return runDocFreshnessProcessInRoot(t, root, today, maxAge, dateFunction)
}
func runDocFreshnessProcessInRoot(t *testing.T, root string, today, maxAge *string, dateFunction string) freshnessRun {
t.Helper()
bash := docFreshnessBash(t)
markerDir := t.TempDir()
pythonMarker := filepath.Join(markerDir, "python-invoked")
dateMarker := filepath.Join(markerDir, "date-invoked")
bashEnvironment := filepath.Join(t.TempDir(), "bash-env.sh")
if err := os.WriteFile(bashEnvironment, []byte(pythonPoisonFunction+dateFunction), 0o600); err != nil {
t.Fatalf("write Python poison environment: %v", err)
}
cmd := exec.Command(
bash,
"--noprofile",
"--norc",
msysPath(filepath.Join(root, "scripts", "check-doc-freshness.sh")),
)
cmd.Dir = root
cmd.Env = []string{
"PATH=/usr/bin:/bin",
"HOME=" + msysPath(t.TempDir()),
"LC_ALL=C",
"LANG=C",
"BASH_ENV=" + msysPath(bashEnvironment),
"ENV=",
"DATE_BACKEND_MARKER=" + msysPath(dateMarker),
"PYTHON_POISON_MARKER=" + msysPath(pythonMarker),
}
if maxAge != nil {
cmd.Env = append(cmd.Env, "DOC_FRESHNESS_MAX_AGE_DAYS="+*maxAge)
}
if today != nil {
cmd.Env = append(cmd.Env, "DOC_FRESHNESS_TODAY="+*today)
}
output, runErr := cmd.CombinedOutput()
return freshnessRun{
output: string(output),
err: runErr,
pythonInvoked: docFreshnessMarkerExists(t, pythonMarker),
dateInvoked: docFreshnessMarkerExists(t, dateMarker),
}
}
func docFreshnessBash(t *testing.T) string {
t.Helper()
bash, err := testbash.Resolve()
if err != nil {
t.Fatalf("bash is required to exercise check-doc-freshness.sh: %v", err)
}
if runtime.GOOS != "windows" {
return bash
}
err = testbash.Probe(bash, "Git Bash date environment", `set -eu
PATH=/usr/bin:/bin
export PATH
command -v date >/dev/null
`, docFreshnessProbeEnv())
if err != nil {
t.Fatalf("a working Git Bash date environment is required: %v", err)
}
return bash
}
func docFreshnessMarkerExists(t *testing.T, path string) bool {
t.Helper()
_, err := os.Stat(path)
if err == nil {
return true
}
if !os.IsNotExist(err) {
t.Fatalf("inspect marker %s: %v", path, err)
}
return false
}
func newDocFreshnessFixture(t *testing.T, reviewed string) string {
t.Helper()
root := t.TempDir()
repoRoot := sourceRepoRoot(t)
script, err := os.ReadFile(filepath.Join(repoRoot, "scripts", "check-doc-freshness.sh"))
if err != nil {
t.Fatalf("read check-doc-freshness.sh: %v", err)
}
writeFixtureFile(t, root, "scripts/check-doc-freshness.sh", string(script))
var inventory strings.Builder
for _, document := range freshnessDocuments {
inventoryRef := strings.TrimPrefix(strings.TrimPrefix(document.path, "docs/"), "engdocs/")
fmt.Fprintf(&inventory, "- `%s`\n", inventoryRef)
writeFixtureFile(t, root, document.path, fmt.Sprintf(
"Last reviewed: %s\nFreshness source: %s\n",
reviewed,
strings.Join(document.sources, ";"),
))
for _, source := range document.sources {
createFreshnessSource(t, root, source)
}
}
writeFixtureFile(t, root, "engdocs/DOC_INVENTORY.md", inventory.String())
return root
}
// newDocFreshnessFixtureWithMarkers is like newDocFreshnessFixture, but the
// document at targetPath gets one "Last reviewed:" line per entry in
// targetReviewedDates instead of exactly one. Every other document keeps the
// normal single marker at reviewed.
func newDocFreshnessFixtureWithMarkers(t *testing.T, reviewed, targetPath string, targetReviewedDates []string) string {
t.Helper()
root := t.TempDir()
repoRoot := sourceRepoRoot(t)
script, err := os.ReadFile(filepath.Join(repoRoot, "scripts", "check-doc-freshness.sh"))
if err != nil {
t.Fatalf("read check-doc-freshness.sh: %v", err)
}
writeFixtureFile(t, root, "scripts/check-doc-freshness.sh", string(script))
var inventory strings.Builder
for _, document := range freshnessDocuments {
inventoryRef := strings.TrimPrefix(strings.TrimPrefix(document.path, "docs/"), "engdocs/")
fmt.Fprintf(&inventory, "- `%s`\n", inventoryRef)
var body strings.Builder
reviewedDates := []string{reviewed}
if document.path == targetPath {
reviewedDates = targetReviewedDates
}
for _, date := range reviewedDates {
fmt.Fprintf(&body, "Last reviewed: %s\n", date)
}
fmt.Fprintf(&body, "Freshness source: %s\n", strings.Join(document.sources, ";"))
writeFixtureFile(t, root, document.path, body.String())
for _, source := range document.sources {
createFreshnessSource(t, root, source)
}
}
writeFixtureFile(t, root, "engdocs/DOC_INVENTORY.md", inventory.String())
return root
}
func createFreshnessSource(t *testing.T, root, source string) {
t.Helper()
if strings.HasSuffix(source, "/") {
if err := os.MkdirAll(filepath.Join(root, filepath.FromSlash(source)), 0o755); err != nil {
t.Fatalf("create source directory %s: %v", source, err)
}
return
}
source = strings.ReplaceAll(source, "*", "fixture")
source = strings.ReplaceAll(source, "?", "x")
writeFixtureFile(t, root, source, "")
}
func writeFixtureFile(t *testing.T, root, relative, body string) {
t.Helper()
path := filepath.Join(root, filepath.FromSlash(relative))
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
t.Fatalf("create parent for %s: %v", relative, err)
}
if err := os.WriteFile(path, []byte(body), 0o600); err != nil {
t.Fatalf("write %s: %v", relative, err)
}
}
func docFreshnessProbeEnv() []string {
env := make([]string, 0, len(os.Environ())+3)
for _, entry := range os.Environ() {
key, _, ok := strings.Cut(entry, "=")
if !ok {
continue
}
upperKey := strings.ToUpper(key)
if upperKey == "BASH_ENV" || upperKey == "ENV" || strings.HasPrefix(upperKey, "GIT_") {
continue
}
env = append(env, entry)
}
return append(env, "BASH_ENV=", "ENV=", "GIT_EXEC_PATH=")
}