Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not return an error when blank config files are read #55

Merged
merged 1 commit into from Jul 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was unused so I removed it, not related to other changes in the PR.

}{
"absolute path": {
parentFile: "/etc/ssh/ssh_config",
Expand Down