diff --git a/.changelog/10091.txt b/.changelog/10091.txt new file mode 100644 index 0000000000..fab96dee45 --- /dev/null +++ b/.changelog/10091.txt @@ -0,0 +1,3 @@ +```release-note:bug +cli: snapshot inspect command would panic on invalid input. +``` diff --git a/command/snapshot/inspect/snapshot_inspect.go b/command/snapshot/inspect/snapshot_inspect.go index 85e6c895fd..c44acc03e9 100644 --- a/command/snapshot/inspect/snapshot_inspect.go +++ b/command/snapshot/inspect/snapshot_inspect.go @@ -69,6 +69,7 @@ func (c *cmd) Run(args []string) int { readFile, meta, err := snapshot.Read(hclog.New(nil), f) if err != nil { c.UI.Error(fmt.Sprintf("Error reading snapshot: %s", err)) + return 1 } defer func() { if err := readFile.Close(); err != nil { diff --git a/command/snapshot/inspect/snapshot_inspect_test.go b/command/snapshot/inspect/snapshot_inspect_test.go index e04494c109..b59b4595c2 100644 --- a/command/snapshot/inspect/snapshot_inspect_test.go +++ b/command/snapshot/inspect/snapshot_inspect_test.go @@ -95,3 +95,20 @@ func TestSnapshotInspectCommand(t *testing.T) { want := golden(t, t.Name(), ui.OutputWriter.String()) require.Equal(t, want, ui.OutputWriter.String()) } + +func TestSnapshotInspectInvalidFile(t *testing.T) { + // Attempt to open a non-snapshot file. + filepath := "./testdata/TestSnapshotInspectCommand.golden" + + // Inspect the snapshot + ui := cli.NewMockUi() + c := New(ui) + args := []string{filepath} + + code := c.Run(args) + // Just check it was an error code returned and not a panic - originally this + // would panic. + if code == 0 { + t.Fatalf("should return an error code") + } +}