diff --git a/internal/cmd/test_init.go b/internal/cmd/test_init.go index f66cc806..e13b8d70 100644 --- a/internal/cmd/test_init.go +++ b/internal/cmd/test_init.go @@ -192,7 +192,9 @@ func runTestInitCmd(opts testInitArgs) error { marshaled, _ := json.MarshalIndent(specs, "", " ") - _ = os.WriteFile(opts.path+".specs.json", marshaled, 0644) + if err := os.WriteFile(opts.path+".specs.json", marshaled, 0644); err != nil { + return fmt.Errorf("failed to write specs file: %w", err) + } fmt.Printf("✅ Created specs file: %s.specs.json\n", opts.path) diff --git a/internal/cmd/test_init_internal_test.go b/internal/cmd/test_init_internal_test.go new file mode 100644 index 00000000..34596ec8 --- /dev/null +++ b/internal/cmd/test_init_internal_test.go @@ -0,0 +1,30 @@ +package cmd + +import ( + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/require" +) + +const testInitNumscript = `send [USD/2 100] ( + source = @world + destination = @bob +) +` + +func TestRunTestInitCmdPropagatesWriteError(t *testing.T) { + dir := t.TempDir() + scriptPath := filepath.Join(dir, "main.num") + require.NoError(t, os.WriteFile(scriptPath, []byte(testInitNumscript), 0644)) + + // create a directory where the specs file should be written, so that + // os.WriteFile fails deterministically + require.NoError(t, os.Mkdir(scriptPath+".specs.json", 0755)) + + err := runTestInitCmd(testInitArgs{path: scriptPath}) + + require.Error(t, err) + require.ErrorContains(t, err, "failed to write specs file") +}