mirror of
https://github.com/mikefarah/yq.git
synced 2026-07-04 19:35:38 +00:00
fix: reject negative indent instead of panicking
This commit is contained in:
parent
5cf0adcc5b
commit
bc1f406245
@ -87,6 +87,10 @@ func validateCommandFlags(args []string) error {
|
|||||||
return fmt.Errorf("cannot pass files in when using null-input flag")
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1086,6 +1086,7 @@ func TestValidateCommandFlags(t *testing.T) {
|
|||||||
frontMatter string
|
frontMatter string
|
||||||
splitFileExp string
|
splitFileExp string
|
||||||
nullInput bool
|
nullInput bool
|
||||||
|
indent int
|
||||||
expectError bool
|
expectError bool
|
||||||
errorContains string
|
errorContains string
|
||||||
}{
|
}{
|
||||||
@ -1148,6 +1149,27 @@ func TestValidateCommandFlags(t *testing.T) {
|
|||||||
expectError: true,
|
expectError: true,
|
||||||
errorContains: "cannot pass files in when using null-input flag",
|
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 {
|
for _, tt := range tests {
|
||||||
@ -1157,17 +1179,20 @@ func TestValidateCommandFlags(t *testing.T) {
|
|||||||
originalFrontMatter := frontMatter
|
originalFrontMatter := frontMatter
|
||||||
originalSplitFileExp := splitFileExp
|
originalSplitFileExp := splitFileExp
|
||||||
originalNullInput := nullInput
|
originalNullInput := nullInput
|
||||||
|
originalIndent := indent
|
||||||
defer func() {
|
defer func() {
|
||||||
writeInplace = originalWriteInplace
|
writeInplace = originalWriteInplace
|
||||||
frontMatter = originalFrontMatter
|
frontMatter = originalFrontMatter
|
||||||
splitFileExp = originalSplitFileExp
|
splitFileExp = originalSplitFileExp
|
||||||
nullInput = originalNullInput
|
nullInput = originalNullInput
|
||||||
|
indent = originalIndent
|
||||||
}()
|
}()
|
||||||
|
|
||||||
writeInplace = tt.writeInplace
|
writeInplace = tt.writeInplace
|
||||||
frontMatter = tt.frontMatter
|
frontMatter = tt.frontMatter
|
||||||
splitFileExp = tt.splitFileExp
|
splitFileExp = tt.splitFileExp
|
||||||
nullInput = tt.nullInput
|
nullInput = tt.nullInput
|
||||||
|
indent = tt.indent
|
||||||
|
|
||||||
err := validateCommandFlags(tt.args)
|
err := validateCommandFlags(tt.args)
|
||||||
if tt.expectError {
|
if tt.expectError {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user