From b432efec4ee8ea86fe1fabf0121a1a8613aec5b1 Mon Sep 17 00:00:00 2001 From: ascandone Date: Tue, 4 Aug 2026 15:42:14 +0200 Subject: [PATCH] fix(cmd): propagate specs file write errors in test-init runTestInitCmd discarded os.WriteFile's error, so a failed write (disk full, permission denied, target path already a directory, ...) was reported as success: "Created specs file" printed and exit 0, with nothing actually written. Left the json.MarshalIndent error un-propagated: Specs's fields are all strings/bools/*big.Int, none of which json.Marshal can fail on, so there's no reachable input that exercises that branch. --- internal/cmd/test_init.go | 4 +++- internal/cmd/test_init_internal_test.go | 30 +++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 internal/cmd/test_init_internal_test.go 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") +}