Skip to content

Commit

Permalink
Do not return an error when blank config files are read (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
samcoe committed Jul 25, 2022
1 parent 91ca4ef commit ce18bbb
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
5 changes: 4 additions & 1 deletion internal/yamlmap/yaml_map.go
Expand Up @@ -41,7 +41,10 @@ func Unmarshal(data []byte) (*Map, error) {
if err != nil {
return nil, ErrInvalidYaml
}
if len(root.Content) == 0 || root.Content[0].Kind != yaml.MappingNode {
if len(root.Content) == 0 {
return MapValue(), nil
}
if root.Content[0].Kind != yaml.MappingNode {
return nil, ErrInvalidFormat
}
return &Map{root.Content[0]}, nil
Expand Down
41 changes: 41 additions & 0 deletions internal/yamlmap/yaml_map_test.go
Expand Up @@ -209,6 +209,47 @@ func TestMapSetEntry(t *testing.T) {
}
}

func TestUnmarshal(t *testing.T) {
tests := []struct {
name string
data []byte
wantErr string
wantEmpty bool
}{
{
name: "valid yaml",
data: []byte(`{test: "data"}`),
},
{
name: "empty yaml",
data: []byte(``),
wantEmpty: true,
},
{
name: "invalid yaml",
data: []byte(`{test: `),
wantErr: "invalid yaml",
},
{
name: "invalid format",
data: []byte(`data`),
wantErr: "invalid format",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m, err := Unmarshal(tt.data)
if tt.wantErr != "" {
assert.EqualError(t, err, tt.wantErr)
assert.Nil(t, m)
return
}
assert.NoError(t, err)
assert.Equal(t, tt.wantEmpty, m.Empty())
})
}
}

func testMap() *Map {
var data = `
valid: present
Expand Down
1 change: 0 additions & 1 deletion pkg/ssh/ssh_test.go
Expand Up @@ -91,7 +91,6 @@ func Test_sshParser_absolutePath(t *testing.T) {
parentFile string
arg string
want string
wantErr bool
}{
"absolute path": {
parentFile: "/etc/ssh/ssh_config",
Expand Down

0 comments on commit ce18bbb

Please sign in to comment.