@@ -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
212288func TestHelp (t * testing.T ) {
213289 // This test just makes sure the CLI can run and produce some output
0 commit comments