Repository navigation
Expand file tree
/
Copy pathcopilot_instructions_test.go
More file actions
135 lines (116 loc) · 3.86 KB
/
Copy pathcopilot_instructions_test.go
File metadata and controls
135 lines (116 loc) · 3.86 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
package cli
import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
func TestEnsureCopilotInstructions(t *testing.T) {
tests := []struct {
name string
existingContent string
expectedContent string
}{
{
name: "creates new copilot instructions file",
existingContent: "",
expectedContent: strings.TrimSpace(copilotInstructionsTemplate),
},
{
name: "does not modify existing correct file",
existingContent: copilotInstructionsTemplate,
expectedContent: strings.TrimSpace(copilotInstructionsTemplate),
},
{
name: "updates modified file",
existingContent: "# Modified GitHub Agentic Workflows - Copilot Instructions\n\nThis is a modified version.",
expectedContent: strings.TrimSpace(copilotInstructionsTemplate),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create a temporary directory for testing
tempDir := t.TempDir()
// Change to temp directory and initialize git repo for findGitRoot to work
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)
}
copilotDir := filepath.Join(tempDir, ".github", "instructions")
copilotInstructionsPath := filepath.Join(copilotDir, "github-agentic-workflows.instructions.md")
// Create initial content if specified
if tt.existingContent != "" {
if err := os.MkdirAll(copilotDir, 0755); err != nil {
t.Fatalf("Failed to create copilot directory: %v", err)
}
if err := os.WriteFile(copilotInstructionsPath, []byte(tt.existingContent), 0644); err != nil {
t.Fatalf("Failed to create initial copilot instructions: %v", err)
}
}
// Call the function with writeInstructions=true to test the functionality
err = ensureCopilotInstructions(false, true)
if err != nil {
t.Fatalf("ensureCopilotInstructions() returned error: %v", err)
}
// Check that file exists
if _, err := os.Stat(copilotInstructionsPath); os.IsNotExist(err) {
t.Fatalf("Expected copilot instructions file to exist")
}
// Check content
content, err := os.ReadFile(copilotInstructionsPath)
if err != nil {
t.Fatalf("Failed to read copilot instructions: %v", err)
}
contentStr := strings.TrimSpace(string(content))
expectedStr := strings.TrimSpace(tt.expectedContent)
if contentStr != expectedStr {
t.Errorf("Expected content does not match.\nExpected first 100 chars: %q\nActual first 100 chars: %q",
expectedStr[:min(100, len(expectedStr))],
contentStr[:min(100, len(contentStr))])
}
})
}
}
func TestEnsureCopilotInstructions_WithWriteInstructionsFalse(t *testing.T) {
// Create a temporary directory for testing
tempDir := t.TempDir()
// Change to temp directory and initialize git repo for findGitRoot to work
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)
}
copilotDir := filepath.Join(tempDir, ".github", "instructions")
copilotInstructionsPath := filepath.Join(copilotDir, "github-agentic-workflows.instructions.md")
// Call the function with writeInstructions=false
err = ensureCopilotInstructions(false, false)
if err != nil {
t.Fatalf("ensureCopilotInstructions() returned error: %v", err)
}
// Check that file does not exist
if _, err := os.Stat(copilotInstructionsPath); !os.IsNotExist(err) {
t.Fatalf("Expected copilot instructions file to not exist when writeInstructions=false")
}
}
func min(a, b int) int {
if a < b {
return a
}
return b
}