-
Notifications
You must be signed in to change notification settings - Fork 322
fix: only update .gitattributes if needed, skip if already up to date #24124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -42,31 +42,37 @@ func TestEnsureGitAttributes(t *testing.T) { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| name string | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| existingContent string | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| expectedContent string | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| expectUpdated bool | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| }{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| name: "creates new gitattributes file", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| existingContent: "", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| expectedContent: ".github/workflows/*.lock.yml linguist-generated=true merge=ours", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| expectUpdated: true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| name: "adds entry to existing file", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| existingContent: "*.generated linguist-generated=true\n", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| expectedContent: "*.generated linguist-generated=true\n\n.github/workflows/*.lock.yml linguist-generated=true merge=ours", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| expectUpdated: true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| name: "does not duplicate existing entry", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| existingContent: ".github/workflows/*.lock.yml linguist-generated=true merge=ours\n", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| expectedContent: ".github/workflows/*.lock.yml linguist-generated=true merge=ours", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| expectUpdated: false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| name: "does not duplicate entry with different order", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| existingContent: "*.md linguist-documentation=true\n.github/workflows/*.lock.yml linguist-generated=true merge=ours\n*.txt text=auto\n", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| expectedContent: "*.md linguist-documentation=true\n.github/workflows/*.lock.yml linguist-generated=true merge=ours\n*.txt text=auto", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| expectUpdated: false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| name: "updates old format entry", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| existingContent: "*.md linguist-documentation=true\n.github/workflows/*.lock.yml linguist-generated=true\n*.txt text=auto\n", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| expectedContent: "*.md linguist-documentation=true\n.github/workflows/*.lock.yml linguist-generated=true merge=ours\n*.txt text=auto", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| expectUpdated: true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -85,11 +91,15 @@ func TestEnsureGitAttributes(t *testing.T) { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Call the function | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| err := ensureGitAttributes() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| updated, err := ensureGitAttributes() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| t.Fatalf("ensureGitAttributes() returned error: %v", err) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| if updated != tt.expectUpdated { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| t.Errorf("ensureGitAttributes() updated=%v, want %v", updated, tt.expectUpdated) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Check that file exists | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if _, err := os.Stat(gitAttributesPath); os.IsNotExist(err) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| t.Fatalf("Expected .gitattributes file to exist") | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -116,6 +126,17 @@ func TestEnsureGitAttributes(t *testing.T) { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| if strings.Contains(string(content), ".github/workflows/*.campaign.g.md") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| t.Errorf("Did not expect .gitattributes to contain '.github/workflows/*.campaign.g.md' (should be in .gitignore)") | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Verify that calling again when already up to date returns updated=false | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if tt.expectUpdated { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| updatedAgain, errAgain := ensureGitAttributes() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if errAgain != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| t.Fatalf("ensureGitAttributes() second call returned error: %v", errAgain) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if updatedAgain { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| t.Errorf("ensureGitAttributes() second call updated=true, want false (already up to date)") | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+131
to
+139
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| if tt.expectUpdated { | |
| updatedAgain, errAgain := ensureGitAttributes() | |
| if errAgain != nil { | |
| t.Fatalf("ensureGitAttributes() second call returned error: %v", errAgain) | |
| } | |
| if updatedAgain { | |
| t.Errorf("ensureGitAttributes() second call updated=true, want false (already up to date)") | |
| } | |
| } | |
| updatedAgain, errAgain := ensureGitAttributes() | |
| if errAgain != nil { | |
| t.Fatalf("ensureGitAttributes() second call returned error: %v", errAgain) | |
| } | |
| if updatedAgain { | |
| t.Errorf("ensureGitAttributes() second call updated=true, want false (already up to date)") | |
| } | |
| // Verify that the content is unchanged after the second call | |
| contentAfter, errAfter := os.ReadFile(gitAttributesPath) | |
| if errAfter != nil { | |
| t.Fatalf("Failed to read .gitattributes after second call: %v", errAfter) | |
| } | |
| if string(contentAfter) != string(content) { | |
| t.Errorf("Expected .gitattributes content to remain unchanged after second call.\nBefore:\n%s\n\nAfter:\n%s", string(content), string(contentAfter)) | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The inline comment
// Not in a git repository, skipis misleading because this branch returns a non-nil error (which some callers treat as fatal). Consider rewording to reflect that the error is propagated so callers can decide whether to skip or fail (or adjust behavior to truly “skip” by returning(false, nil)if that’s the intent).