Skip to content

[testify-expert] Improve Test Quality: pkg/cli/gitattributes_test.go #76289

Done

Description

@github-actions

Current State

  • Test file: pkg/cli/gitattributes_test.go (181 LOC, 2 test functions, 1 table-driven with 6 cases)
  • Source pair: no gitattributes.go; the code under test lives in pkg/cli/git.go: ensureGitAttributes, readGitAttributesFile, ensureRequiredGitAttributesEntry, stageGitAttributesIfChanged
  • Testify usage: none. The file uses t.Fatalf/t.Errorf and os.MkdirTemp + os.Chdir.

Strengths

  • Table-driven main test with legacy-migration and idempotency (second call) checks.
  • Covers the not-in-git-repo error path.

Prioritized Improvements

1. Missing / high-value tests

  • ensureRequiredGitAttributesEntry is a pure function but has no direct tests. Add a table test with nil input, an empty slice, a trailing-empty-line input, legacy-entry replacement (assert the returned slice and modified), and a whitespace-padded existing entry.
  • readGitAttributesFile is untested: a missing file returns an empty slice, and an existing file is split by \n.
  • ensureGitAttributes write failure (e.g. .gitattributes is a directory) is not tested. Assert the error contains failed to write .gitattributes.
  • stageGitAttributesIfChanged is untested.
  • Per-case isolation: the current table reuses one repo and removes .gitattributes manually. Prefer a fresh git init per subtest.

2. Testify assertion upgrades

Before / after
// before
tmpDir, err := os.MkdirTemp("", "gh-aw-gitattributes-test")
if err != nil { t.Fatalf(...) }
defer os.RemoveAll(tmpDir)
...
if updated != tt.expectUpdated {
    t.Errorf("updated=%v, want %v", updated, tt.expectUpdated)
}

// after
tmpDir := t.TempDir()
t.Chdir(tmpDir)
updated, err := ensureGitAttributes()
require.NoError(t, err, "ensureGitAttributes should succeed")
assert.Equal(t, tt.expectUpdated, updated, "updated flag mismatch")
assert.Equal(t, strings.TrimSpace(tt.expectedContent), strings.TrimSpace(string(content)))
assert.Contains(t, string(content), constants.WorkflowsLockYmlGitAttributesEntry)
assert.NotContains(t, string(content), ".github/workflows/*.campaign.g.md")

Other upgrades:

  • os.Stat + os.IsNotExist becomes assert.NoFileExists / require.FileExists.
  • t.Skip on a git init failure becomes require.NoError after an exec.LookPath("git") skip.
  • Use t.Chdir (Go 1.24+) in place of the manual os.Getwd/Chdir defer.
  • Use constants.WorkflowsLockYmlGitAttributesEntry instead of the hard-coded string.

3. Table-driven refactors

  • Split the existingContent/expectedContent table: move the pure-logic cases into an ensureRequiredGitAttributesEntry test and keep a few file-based cases for ensureGitAttributes.
  • Add a wantErr column to cover the error paths in one table.

4. Organization / readability

  • Extract a helper setupGitRepo(t) (TempDir + Chdir + git init) shared by both tests.
  • Rename TestEnsureGitAttributesNotInGitRepo to TestEnsureGitAttributes_NotInGitRepo, matching nearby tests.
  • Move the repeated "campaign.g.md not present" check into its own case rather than asserting it in every iteration.

Acceptance Checklist

  • Replace t.Fatalf/t.Errorf with require/assert
  • Add direct tests for ensureRequiredGitAttributesEntry and readGitAttributesFile
  • Add a write-failure test
  • Use t.TempDir() / t.Chdir() and a shared setup helper
  • make test-unit passes

Generated by 🧪 Daily Testify Uber Super Expert · copilot · auto (Claude Sonnet 5.5) · ~16 AIC · ⌖ ~2 AIC · ⊞ 9.5K · ◷

  • expires on Nov 11, 2026, 10:05 AM UTC-08:00

Activity

  1. github-actions commented on Nov 9, 2026

    @github-actions
    ContributorAuthor

    🍪 Issue Monster selected this for Copilot

    I've identified this issue as a good candidate for automated resolution and requested assignment to the Copilot coding agent.

    If assignment succeeds, Copilot will analyze the issue and create a pull request with the fix.

    Om nom nom! 🍪

    🍪 Om nom nom by Issue Monster · pi · gpt50 · ~1 AIC · ⌖ ~2 AIC · ⊞ 11.2K · ◷

  2. linked a pull request that will close this issueExpand `.gitattributes` test coverage #76294on Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions