mirror of
https://github.com/mikefarah/yq.git
synced 2024-11-12 13:48:06 +00:00
Fixes npe when given filename ending with "." #1994
This commit is contained in:
parent
101cf14b8c
commit
28c406706a
@ -123,9 +123,11 @@ func FormatStringFromFilename(filename string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func FormatFromString(format string) (*Format, error) {
|
func FormatFromString(format string) (*Format, error) {
|
||||||
for _, printerFormat := range Formats {
|
if format != "" {
|
||||||
if printerFormat.MatchesName(format) {
|
for _, printerFormat := range Formats {
|
||||||
return printerFormat, nil
|
if printerFormat.MatchesName(format) {
|
||||||
|
return printerFormat, nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil, fmt.Errorf("unknown format '%v' please use [%v]", format, GetAvailableOutputFormatString())
|
return nil, fmt.Errorf("unknown format '%v' please use [%v]", format, GetAvailableOutputFormatString())
|
||||||
|
52
pkg/yqlib/format_test.go
Normal file
52
pkg/yqlib/format_test.go
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
package yqlib
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/mikefarah/yq/v4/test"
|
||||||
|
)
|
||||||
|
|
||||||
|
type formatStringScenario struct {
|
||||||
|
description string
|
||||||
|
input string
|
||||||
|
expectedFormat *Format
|
||||||
|
expectedError string
|
||||||
|
}
|
||||||
|
|
||||||
|
var formatStringScenarios = []formatStringScenario{
|
||||||
|
{
|
||||||
|
description: "yaml",
|
||||||
|
input: "yaml",
|
||||||
|
expectedFormat: YamlFormat,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description: "Unknown format type",
|
||||||
|
input: "doc",
|
||||||
|
expectedError: "unknown format 'doc' please use",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description: "blank should error",
|
||||||
|
input: "",
|
||||||
|
expectedError: "unknown format '' please use",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFormatFromString(t *testing.T) {
|
||||||
|
for _, tt := range formatStringScenarios {
|
||||||
|
actualFormat, actualError := FormatFromString(tt.input)
|
||||||
|
|
||||||
|
if tt.expectedError != "" {
|
||||||
|
if actualError == nil {
|
||||||
|
t.Errorf("Expected [%v] error but found none", tt.expectedError)
|
||||||
|
} else {
|
||||||
|
test.AssertResultWithContext(t, true, strings.Contains(actualError.Error(), tt.expectedError),
|
||||||
|
fmt.Sprintf("Expected [%v] to contain [%v]", actualError.Error(), tt.expectedError),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
test.AssertResult(t, tt.expectedFormat, actualFormat)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user