Complete audit remediation and verification

This commit is contained in:
2026-11-01 21:18:07 +00:00
parent d368bb32f9
commit 67963102d9
7 changed files with 117 additions and 34 deletions

View File

@@ -8,6 +8,7 @@ import (
"path/filepath"
"strings"
"testing"
"time"
)
func TestDiscoverRepositoryRoot(t *testing.T) {
@@ -39,6 +40,9 @@ func TestDiscoverRepositoryRoot(t *testing.T) {
if _, err := RepositoryRoot(context.Background(), standalone); err == nil {
t.Fatal("strict lookup accepted standalone")
}
if _, found, err := DiscoverRepositoryRoot(context.Background(), repo.bare); err == nil || found {
t.Fatalf("bare repository: found %t, error %v", found, err)
}
broken := t.TempDir()
if err := os.WriteFile(filepath.Join(broken, ".git"), []byte("gitdir: missing\n"), 0600); err != nil {
t.Fatal(err)
@@ -80,6 +84,13 @@ func TestDiscoverRepositoryRootFailures(t *testing.T) {
if found || !errors.As(err, &exitErr) || exitErr.ExitCode() != 128 || !strings.Contains(err.Error(), "dubious ownership") {
t.Fatalf("found %t, error %v", found, err)
}
if err := os.WriteFile(filepath.Join(bin, "git"), []byte("#!/bin/sh\nprintf '%s\\n' 'fatal: permission denied' >&2\nexit 128\n"), 0o755); err != nil {
t.Fatal(err)
}
_, found, err = DiscoverRepositoryRoot(context.Background(), dir)
if found || !errors.As(err, &exitErr) || exitErr.ExitCode() != 128 || !strings.Contains(err.Error(), "permission denied") {
t.Fatalf("found %t, error %v", found, err)
}
ctx, cancel := context.WithCancel(context.Background())
cancel()
if _, _, err := DiscoverRepositoryRoot(ctx, dir); !errors.Is(err, context.Canceled) {
@@ -87,6 +98,48 @@ func TestDiscoverRepositoryRootFailures(t *testing.T) {
}
}
func TestDiscoverRepositoryRootInterruptedGit(t *testing.T) {
dir := t.TempDir()
bin := t.TempDir()
ready := filepath.Join(t.TempDir(), "ready")
script := "#!/bin/sh\n[ \"$LC_ALL\" = C ] || exit 42\n: > \"$DISCOVERY_READY\"\nexec /bin/sleep 60\n"
if err := os.WriteFile(filepath.Join(bin, "git"), []byte(script), 0o755); err != nil {
t.Fatal(err)
}
t.Setenv("PATH", bin)
t.Setenv("DISCOVERY_READY", ready)
t.Setenv("LC_ALL", "different-locale")
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
done := make(chan error, 1)
go func() { _, _, err := DiscoverRepositoryRoot(ctx, dir); done <- err }()
timer := time.NewTimer(5 * time.Second)
defer timer.Stop()
ticker := time.NewTicker(10 * time.Millisecond)
defer ticker.Stop()
for {
if _, err := os.Stat(ready); err == nil {
break
}
select {
case err := <-done:
t.Fatalf("Git ended before readiness: %v", err)
case <-timer.C:
cancel()
err := <-done
t.Fatalf("Git did not become ready: %v", err)
case <-ticker.C:
}
}
cancel()
if err := <-done; !errors.Is(err, context.Canceled) {
t.Fatalf("cancellation = %v", err)
}
if got := os.Getenv("LC_ALL"); got != "different-locale" {
t.Fatalf("parent locale changed to %q", got)
}
}
func TestDiscoverRepositoryRootWithoutGit(t *testing.T) {
repo := newRemoteFixture(t)
standalone := t.TempDir()