Skip to content

Commit a650ce9

Browse files
committed
fix golden tests ordering
1 parent dcc29e2 commit a650ce9

3 files changed

Lines changed: 80 additions & 4 deletions

File tree

tests/golden/login/login.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Starting login process...
22

33
Starting authentication with Continue...
4-
Opening browser to sign in at: https://api.workos.com/user_management/authorize?client_id=client_01J0FW6XN8N2XJAECF7NE0Y65J&provider=authkit&redirect_uri=https%3A%2F%2Fhub.continue.dev%2Ftokens%2Fcallback&response_type=code&state=<STATE_PLACEHOLDER>
4+
Opening browser to sign in at: https://api.workos.com/user_management/authorize?client_id=client_01J0FW6XN8N2XJAECF7NE0Y65J&provider=authkit&redirect_uri=https%3A%2F%2Fhub.continue.dev%2Ftokens%2Fcallback%3FclientName%3Drules&response_type=code&state=<STATE_PLACEHOLDER>
55

66
After signing in, you'll receive a token.
77
Paste your sign-in token here: Login failed: <ERROR_PLACEHOLDER>

tests/golden/v/version.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
rules version dev
1+
rules version 1.0.11

tests/golden_test.go

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,21 +165,74 @@ func TestGoldenFiles(t *testing.T) {
165165
return
166166
}
167167

168+
// Create a temporary directory for this test
169+
tempDir, err := os.MkdirTemp("", "rules-cli-test-*")
170+
if err != nil {
171+
t.Fatalf("Failed to create temporary directory: %v", err)
172+
}
173+
defer os.RemoveAll(tempDir)
174+
175+
// Copy the CLI binary to the temporary directory
176+
tempCliPath := filepath.Join(tempDir, "rules-cli")
177+
if err := copyFile(cliPath, tempCliPath); err != nil {
178+
t.Fatalf("Failed to copy CLI binary: %v", err)
179+
}
180+
181+
// Change to the temporary directory
182+
originalDir, err := os.Getwd()
183+
if err != nil {
184+
t.Fatalf("Failed to get current directory: %v", err)
185+
}
186+
defer os.Chdir(originalDir)
187+
188+
if err := os.Chdir(tempDir); err != nil {
189+
t.Fatalf("Failed to change to temporary directory: %v", err)
190+
}
191+
192+
// Run prerequisite commands based on the current command
193+
if strings.HasPrefix(cmd, "add ") {
194+
// For add commands, run init first to create rules.json
195+
initCmd := exec.Command(tempCliPath, "init")
196+
if err := initCmd.Run(); err != nil {
197+
t.Logf("Init command failed (this might be expected): %v", err)
198+
}
199+
} else if cmd == "install" {
200+
// For install command, run init and add to set up rules.json with rules
201+
initCmd := exec.Command(tempCliPath, "init")
202+
if err := initCmd.Run(); err != nil {
203+
t.Logf("Init command failed (this might be expected): %v", err)
204+
}
205+
addCmd := exec.Command(tempCliPath, "add", "starter/nextjs-rules")
206+
if err := addCmd.Run(); err != nil {
207+
t.Logf("Add command failed (this might be expected): %v", err)
208+
}
209+
} else if strings.HasPrefix(cmd, "remove ") {
210+
// For remove commands, run init and add to set up rules.json with rules
211+
initCmd := exec.Command(tempCliPath, "init")
212+
if err := initCmd.Run(); err != nil {
213+
t.Logf("Init command failed (this might be expected): %v", err)
214+
}
215+
addCmd := exec.Command(tempCliPath, "add", "starter/nextjs-rules")
216+
if err := addCmd.Run(); err != nil {
217+
t.Logf("Add command failed (this might be expected): %v", err)
218+
}
219+
}
220+
168221
// Split the command into arguments
169222
var args []string
170223
if cmd != "" {
171224
args = strings.Fields(cmd)
172225
}
173226

174227
// Run the command
175-
execCmd := exec.Command(cliPath, args...)
228+
execCmd := exec.Command(tempCliPath, args...)
176229
output, err := execCmd.CombinedOutput()
177230

178231
// We don't fail the test if the command returns non-zero
179232
// because some golden files might be testing error cases
180233

181234
// Read expected output
182-
expectedBytes, err := os.ReadFile(goldenFile)
235+
expectedBytes, err := os.ReadFile(filepath.Join(originalDir, goldenFile))
183236
if err != nil {
184237
t.Fatalf("Failed to read golden file: %v", err)
185238
}
@@ -208,6 +261,29 @@ func TestGoldenFiles(t *testing.T) {
208261
}
209262
}
210263

264+
// copyFile copies a file from src to dst
265+
func copyFile(src, dst string) error {
266+
sourceFile, err := os.Open(src)
267+
if err != nil {
268+
return err
269+
}
270+
defer sourceFile.Close()
271+
272+
destFile, err := os.Create(dst)
273+
if err != nil {
274+
return err
275+
}
276+
defer destFile.Close()
277+
278+
_, err = destFile.ReadFrom(sourceFile)
279+
if err != nil {
280+
return err
281+
}
282+
283+
// Make the copied file executable
284+
return os.Chmod(dst, 0755)
285+
}
286+
211287
// TestHelp provides a simple example to ensure our testing framework works
212288
func TestHelp(t *testing.T) {
213289
// This test just makes sure the CLI can run and produce some output

0 commit comments

Comments
 (0)