-
Notifications
You must be signed in to change notification settings - Fork 481
Expand file tree
/
Copy pathadd_gitattributes_test.go
More file actions
163 lines (140 loc) · 4.92 KB
/
Copy pathadd_gitattributes_test.go
File metadata and controls
163 lines (140 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//go:build !integration
package cli
import (
"context"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
// TestAddCommandUpdatesGitAttributes tests that the add command updates .gitattributes by default
func TestAddCommandUpdatesGitAttributes(t *testing.T) {
// Create a temporary directory for testing
tmpDir, err := os.MkdirTemp("", "gh-aw-add-gitattributes-test")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(tmpDir)
// Change to temp directory
originalDir, err := os.Getwd()
if err != nil {
t.Fatalf("Failed to get current directory: %v", err)
}
defer func() {
_ = os.Chdir(originalDir)
}()
if err := os.Chdir(tmpDir); err != nil {
t.Fatalf("Failed to change to temp directory: %v", err)
}
// Initialize a git repository
if err := os.WriteFile("test.txt", []byte("test"), 0644); err != nil {
t.Fatalf("Failed to create test file: %v", err)
}
if cmd := exec.Command("git", "init"); cmd.Run() != nil {
t.Skip("Skipping test - git not available")
}
if cmd := exec.Command("git", "config", "user.email", "test@example.com"); cmd.Run() != nil {
t.Skip("Skipping test - git config failed")
}
if cmd := exec.Command("git", "config", "user.name", "Test User"); cmd.Run() != nil {
t.Skip("Skipping test - git config failed")
}
// Create .github/workflows directory
workflowsDir := filepath.Join(tmpDir, ".github", "workflows")
if err := os.MkdirAll(workflowsDir, 0755); err != nil {
t.Fatalf("Failed to create .github/workflows directory: %v", err)
}
// Create a minimal workflow content for testing
workflowContent := `---
on: push
permissions:
contents: read
engine: copilot
---
# Test Workflow
This is a test workflow.`
// Create a ResolvedWorkflow for testing
resolved := &ResolvedWorkflow{
Spec: &WorkflowSpec{
RepoSpec: RepoSpec{
RepoSlug: "test/repo",
Version: "",
},
WorkflowPath: "./test.md",
WorkflowName: "test",
},
Content: []byte(workflowContent),
SourceInfo: &FetchedWorkflow{
Content: []byte(workflowContent),
IsLocal: true,
SourcePath: "./test.md",
CommitSHA: "",
},
}
t.Run("gitattributes_updated_by_default", func(t *testing.T) {
// Remove any existing .gitattributes
os.Remove(".gitattributes")
// Call addWorkflows with noGitattributes=false
opts := AddOptions{}
err := addWorkflows(context.Background(), []*ResolvedWorkflow{resolved}, opts)
if err != nil {
// Log any error but don't fail - we're testing gitattributes behavior
t.Logf("Note: workflow addition returned: %v", err)
}
// Check that .gitattributes was created
gitAttributesPath := filepath.Join(tmpDir, ".gitattributes")
if _, err := os.Stat(gitAttributesPath); os.IsNotExist(err) {
t.Errorf("Expected .gitattributes file to be created")
return
}
// Check content
content, err := os.ReadFile(gitAttributesPath)
if err != nil {
t.Fatalf("Failed to read .gitattributes: %v", err)
}
if !strings.Contains(string(content), ".github/workflows/*.lock.yml linguist-generated=true") {
t.Errorf("Expected .gitattributes to contain '.github/workflows/*.lock.yml linguist-generated=true'")
}
})
t.Run("gitattributes_not_updated_with_flag", func(t *testing.T) {
// Remove any existing .gitattributes
os.Remove(".gitattributes")
opts := AddOptions{NoGitattributes: true}
// Call addWorkflows with noGitattributes=true
err := addWorkflows(context.Background(), []*ResolvedWorkflow{resolved}, opts)
if err != nil {
// Log any error but don't fail - we're testing gitattributes behavior
t.Logf("Note: workflow addition returned: %v", err)
}
// Check that .gitattributes was NOT created
gitAttributesPath := filepath.Join(tmpDir, ".gitattributes")
if _, err := os.Stat(gitAttributesPath); !os.IsNotExist(err) {
t.Errorf("Expected .gitattributes file NOT to be created when --no-gitattributes flag is set")
}
})
t.Run("existing_gitattributes_not_modified_with_flag", func(t *testing.T) {
// Create a .gitattributes file with existing content
existingContent := "*.txt linguist-vendored=true\n"
gitAttributesPath := filepath.Join(tmpDir, ".gitattributes")
if err := os.WriteFile(gitAttributesPath, []byte(existingContent), 0644); err != nil {
t.Fatalf("Failed to create .gitattributes: %v", err)
}
opts := AddOptions{NoGitattributes: true}
// Call addWorkflows with noGitattributes=true
err := addWorkflows(context.Background(), []*ResolvedWorkflow{resolved}, opts)
if err != nil {
// Log any error but don't fail - we're testing gitattributes behavior
t.Logf("Note: workflow addition returned: %v", err)
}
// Check that .gitattributes was NOT modified
content, err := os.ReadFile(gitAttributesPath)
if err != nil {
t.Fatalf("Failed to read .gitattributes: %v", err)
}
if string(content) != existingContent {
t.Errorf("Expected .gitattributes to remain unchanged when --no-gitattributes flag is set.\nExpected:\n%s\nGot:\n%s",
existingContent, string(content))
}
})
}