From a4cd00cc4d2ffc2bd6fb38842e644a3d352c50c8 Mon Sep 17 00:00:00 2001 From: k3nny Date: Sat, 11 Jul 2026 21:10:41 +0200 Subject: [PATCH] test(ci): skip chmod-dependent tests when running as root Docker CI containers run as root by default (golang:1.26-alpine), where os.Chmod has no effect and permission restrictions don't apply. Four tests were failing because they relied on chmod to force error paths: - gradle: TestWriteVersionReadOnly - cmd: TestInitConfigWriteFails, TestRunLatestTagFails - gitutil: TestLatestTagTagsIterFails (had broken skip logic that only fired if Chmod itself errored, which never happens as root) Replace all four with an upfront os.Getuid() == 0 check so they are skipped cleanly in root environments and still run (and must pass) on non-root local development. Co-Authored-By: Claude Sonnet 4.6 --- cmd/main_test.go | 6 ++++++ internal/gitutil/gitutil_test.go | 13 ++++++------- internal/gradle/gradle_test.go | 3 +++ 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/cmd/main_test.go b/cmd/main_test.go index 8713ce7..0f6ffdc 100644 --- a/cmd/main_test.go +++ b/cmd/main_test.go @@ -852,6 +852,9 @@ func TestPrintVerboseConfigDirect(t *testing.T) { // ── initConfig coverage ─────────────────────────────────────────────────────── func TestInitConfigWriteFails(t *testing.T) { + if os.Getuid() == 0 { + t.Skip("skipping: chmod restrictions do not apply when running as root") + } dir := t.TempDir() os.Chmod(dir, 0555) defer os.Chmod(dir, 0755) @@ -904,6 +907,9 @@ func TestRunWorkingTreeCheckFails(t *testing.T) { } func TestRunLatestTagFails(t *testing.T) { + if os.Getuid() == 0 { + t.Skip("skipping: chmod restrictions do not apply when running as root") + } _, dir := setupRepo(t) addFile(t, dir, "x.go", "// fix") repo, _ := gogit.PlainOpen(dir) diff --git a/internal/gitutil/gitutil_test.go b/internal/gitutil/gitutil_test.go index 283337e..cf7d79a 100644 --- a/internal/gitutil/gitutil_test.go +++ b/internal/gitutil/gitutil_test.go @@ -752,20 +752,19 @@ func TestLatestTagTagsIterFails(t *testing.T) { repo, dir := newTestRepo(t) addCommit(t, repo, dir, "fix: c1", "v1") + if os.Getuid() == 0 { + t.Skip("skipping: chmod restrictions do not apply when running as root") + } + // Make .git/refs/tags/ unreadable so that go-git's walkReferencesTree // returns EPERM when it tries to list the directory, triggering the - // Tags() error path. Skip when running as root (chmod has no effect). + // Tags() error path. tagsDir := filepath.Join(dir, ".git", "refs", "tags") - if err := os.Chmod(tagsDir, 0000); err != nil { - t.Skipf("cannot chmod %s: %v", tagsDir, err) - } + os.Chmod(tagsDir, 0000) t.Cleanup(func() { os.Chmod(tagsDir, 0755) }) // Reopen so the filesystem storer holds no cached state. repo2, err := gogit.PlainOpen(dir) - if err != nil { - t.Skipf("PlainOpen failed (likely running as root): %v", err) - } _, _, err = LatestTag(repo2, branch.Info{Major: 1, Minor: 2, TagPrefix: "v"}) if err == nil { diff --git a/internal/gradle/gradle_test.go b/internal/gradle/gradle_test.go index 32ed906..366a5f6 100644 --- a/internal/gradle/gradle_test.go +++ b/internal/gradle/gradle_test.go @@ -124,6 +124,9 @@ func TestWriteVersionNotFound(t *testing.T) { } func TestWriteVersionReadOnly(t *testing.T) { + if os.Getuid() == 0 { + t.Skip("skipping: chmod restrictions do not apply when running as root") + } path := writeGradle(t, gradleKotlin) os.Chmod(path, 0444) defer os.Chmod(path, 0644)