From bc1f406245aca1ba2e0bbbe6efb1379f10ccba19 Mon Sep 17 00:00:00 2001 From: StressTestor <212606152+StressTestor@users.noreply.github.com> Date: Fri, 12 Jun 2026 16:38:21 -0600 Subject: [PATCH] fix: reject negative indent instead of panicking --- cmd/utils.go | 4 ++++ cmd/utils_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/cmd/utils.go b/cmd/utils.go index 553fc994..26631fc4 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -87,6 +87,10 @@ func validateCommandFlags(args []string) error { return fmt.Errorf("cannot pass files in when using null-input flag") } + if indent < 0 { + return fmt.Errorf("indent must not be negative") + } + return nil } diff --git a/cmd/utils_test.go b/cmd/utils_test.go index c87af958..dd1fa61a 100644 --- a/cmd/utils_test.go +++ b/cmd/utils_test.go @@ -1086,6 +1086,7 @@ func TestValidateCommandFlags(t *testing.T) { frontMatter string splitFileExp string nullInput bool + indent int expectError bool errorContains string }{ @@ -1148,6 +1149,27 @@ func TestValidateCommandFlags(t *testing.T) { expectError: true, errorContains: "cannot pass files in when using null-input flag", }, + { + name: "negative indent", + args: []string{"file.yaml"}, + writeInplace: false, + frontMatter: "", + splitFileExp: "", + nullInput: false, + indent: -1, + expectError: true, + errorContains: "indent must not be negative", + }, + { + name: "zero indent is valid", + args: []string{"file.yaml"}, + writeInplace: false, + frontMatter: "", + splitFileExp: "", + nullInput: false, + indent: 0, + expectError: false, + }, } for _, tt := range tests { @@ -1157,17 +1179,20 @@ func TestValidateCommandFlags(t *testing.T) { originalFrontMatter := frontMatter originalSplitFileExp := splitFileExp originalNullInput := nullInput + originalIndent := indent defer func() { writeInplace = originalWriteInplace frontMatter = originalFrontMatter splitFileExp = originalSplitFileExp nullInput = originalNullInput + indent = originalIndent }() writeInplace = tt.writeInplace frontMatter = tt.frontMatter splitFileExp = tt.splitFileExp nullInput = tt.nullInput + indent = tt.indent err := validateCommandFlags(tt.args) if tt.expectError {