-
Notifications
You must be signed in to change notification settings - Fork 28
Refactor: Extract common template sync logic to eliminate 80 lines of duplication #4256
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,210 @@ | ||
| package cli | ||
|
|
||
| import ( | ||
| "os" | ||
| "os/exec" | ||
| "path/filepath" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/githubnext/gh-aw/pkg/testutil" | ||
| ) | ||
|
|
||
| // TestEnsureFileMatchesTemplate tests the common helper function | ||
| func TestEnsureFileMatchesTemplate(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| subdir string | ||
| fileName string | ||
| templateContent string | ||
| fileType string | ||
| existingContent string | ||
| skipInstructions bool | ||
| expectedFile bool | ||
| expectedContent string | ||
| }{ | ||
| { | ||
| name: "creates new file", | ||
| subdir: ".github/test", | ||
| fileName: "test.md", | ||
| templateContent: "# Test Template", | ||
| fileType: "test file", | ||
| existingContent: "", | ||
| skipInstructions: false, | ||
| expectedFile: true, | ||
| expectedContent: "# Test Template", | ||
| }, | ||
| { | ||
| name: "does not modify existing correct file", | ||
| subdir: ".github/test", | ||
| fileName: "test.md", | ||
| templateContent: "# Test Template", | ||
| fileType: "test file", | ||
| existingContent: "# Test Template", | ||
| skipInstructions: false, | ||
| expectedFile: true, | ||
| expectedContent: "# Test Template", | ||
| }, | ||
| { | ||
| name: "updates modified file", | ||
| subdir: ".github/test", | ||
| fileName: "test.md", | ||
| templateContent: "# Test Template", | ||
| fileType: "test file", | ||
| existingContent: "# Old Content", | ||
| skipInstructions: false, | ||
| expectedFile: true, | ||
| expectedContent: "# Test Template", | ||
| }, | ||
| { | ||
| name: "skips when skipInstructions is true", | ||
| subdir: ".github/test", | ||
| fileName: "test.md", | ||
| templateContent: "# Test Template", | ||
| fileType: "test file", | ||
| existingContent: "", | ||
| skipInstructions: true, | ||
| expectedFile: false, | ||
| expectedContent: "", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| // Create a temporary directory for testing | ||
| tempDir := testutil.TempDir(t, "test-*") | ||
|
|
||
| // Change to temp directory and initialize git repo | ||
| oldWd, _ := os.Getwd() | ||
| defer func() { | ||
| _ = os.Chdir(oldWd) | ||
| }() | ||
| err := os.Chdir(tempDir) | ||
| if err != nil { | ||
| t.Fatalf("Failed to change directory: %v", err) | ||
| } | ||
|
|
||
| // Initialize git repo | ||
| if err := exec.Command("git", "init").Run(); err != nil { | ||
| t.Fatalf("Failed to init git repo: %v", err) | ||
| } | ||
|
|
||
| targetDir := filepath.Join(tempDir, tt.subdir) | ||
| targetPath := filepath.Join(targetDir, tt.fileName) | ||
|
|
||
| // Create initial content if specified | ||
| if tt.existingContent != "" { | ||
| if err := os.MkdirAll(targetDir, 0755); err != nil { | ||
| t.Fatalf("Failed to create target directory: %v", err) | ||
| } | ||
| if err := os.WriteFile(targetPath, []byte(tt.existingContent), 0644); err != nil { | ||
| t.Fatalf("Failed to create initial file: %v", err) | ||
| } | ||
| } | ||
|
|
||
| // Call the helper function | ||
| err = ensureFileMatchesTemplate(tt.subdir, tt.fileName, tt.templateContent, tt.fileType, false, tt.skipInstructions) | ||
| if err != nil { | ||
| t.Fatalf("ensureFileMatchesTemplate() returned error: %v", err) | ||
| } | ||
|
|
||
| // Check file existence | ||
| _, statErr := os.Stat(targetPath) | ||
| if tt.expectedFile && os.IsNotExist(statErr) { | ||
| t.Fatalf("Expected file to exist but it doesn't: %s", targetPath) | ||
| } | ||
| if !tt.expectedFile && !os.IsNotExist(statErr) { | ||
| t.Fatalf("Expected file to not exist but it does: %s", targetPath) | ||
| } | ||
|
|
||
| // Check content if file should exist | ||
| if tt.expectedFile { | ||
| content, err := os.ReadFile(targetPath) | ||
| if err != nil { | ||
| t.Fatalf("Failed to read file: %v", err) | ||
| } | ||
|
|
||
| contentStr := strings.TrimSpace(string(content)) | ||
| expectedStr := strings.TrimSpace(tt.expectedContent) | ||
|
|
||
| if contentStr != expectedStr { | ||
| t.Errorf("Expected content does not match.\nExpected: %q\nActual: %q", expectedStr, contentStr) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // TestEnsureFileMatchesTemplate_VerboseOutput tests verbose logging | ||
| func TestEnsureFileMatchesTemplate_VerboseOutput(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| existingContent string | ||
| expectedLog string | ||
| }{ | ||
| { | ||
| name: "logs creation", | ||
| existingContent: "", | ||
| expectedLog: "Created", | ||
| }, | ||
| { | ||
| name: "logs update", | ||
| existingContent: "# Old Content", | ||
| expectedLog: "Updated", | ||
| }, | ||
| { | ||
| name: "logs up-to-date", | ||
| existingContent: "# Test Template", | ||
| expectedLog: "up-to-date", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| // Create a temporary directory for testing | ||
| tempDir := testutil.TempDir(t, "test-*") | ||
|
|
||
| // Change to temp directory and initialize git repo | ||
| oldWd, _ := os.Getwd() | ||
| defer func() { | ||
| _ = os.Chdir(oldWd) | ||
| }() | ||
| err := os.Chdir(tempDir) | ||
| if err != nil { | ||
| t.Fatalf("Failed to change directory: %v", err) | ||
| } | ||
|
|
||
| // Initialize git repo | ||
| if err := exec.Command("git", "init").Run(); err != nil { | ||
| t.Fatalf("Failed to init git repo: %v", err) | ||
| } | ||
|
|
||
| subdir := ".github/test" | ||
| fileName := "test.md" | ||
| targetDir := filepath.Join(tempDir, subdir) | ||
| targetPath := filepath.Join(targetDir, fileName) | ||
|
|
||
| // Create initial content if specified | ||
| if tt.existingContent != "" { | ||
| if err := os.MkdirAll(targetDir, 0755); err != nil { | ||
| t.Fatalf("Failed to create target directory: %v", err) | ||
| } | ||
| if err := os.WriteFile(targetPath, []byte(tt.existingContent), 0644); err != nil { | ||
| t.Fatalf("Failed to create initial file: %v", err) | ||
| } | ||
| } | ||
|
|
||
| // Call the helper function with verbose=true | ||
| // Note: This test doesn't capture stdout, it just verifies no errors occur | ||
| err = ensureFileMatchesTemplate(subdir, fileName, "# Test Template", "test file", true, false) | ||
| if err != nil { | ||
| t.Fatalf("ensureFileMatchesTemplate() returned error: %v", err) | ||
| } | ||
|
|
||
| // Verify file exists | ||
| if _, err := os.Stat(targetPath); os.IsNotExist(err) { | ||
| t.Fatalf("Expected file to exist") | ||
| } | ||
| }) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
expectedLogfield is defined in the test struct but never used for validation. The test comment on line 198 acknowledges that stdout is not captured. Either implement stdout capture and validation using this field, or remove it to avoid confusion.