-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Improvements to yaml.Unmarshal for types.Imports and add some go…
… tests Improvements here: * Drop duplicate code blocks from getImportFromInterface There were 2 identical codeblocks, one with a type convert to map[string]interface{} and one with type convert to map[interface{}]interface{}. I cannot find any reason for the former * Add some golang tests to for unmarshalling. * Type check the values of all string Import fields, rather than just converting them to a string with 'fmt.Sprintf("%v")' Signed-off-by: Scott Moser <[email protected]>
- Loading branch information
Showing
2 changed files
with
179 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package types | ||
|
||
import ( | ||
"io/fs" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
func modePtr(mode int) *fs.FileMode { | ||
m := fs.FileMode(mode) | ||
return &m | ||
} | ||
|
||
func TestGetImportFromInterface(t *testing.T) { | ||
assert := assert.New(t) | ||
hash1 := "b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c" | ||
tables := []struct { | ||
desc string | ||
val interface{} | ||
expected Import | ||
errstr string | ||
}{ | ||
{desc: "basic string", | ||
val: "/path/to/file", | ||
expected: Import{Path: "/path/to/file", Uid: -1, Gid: -1}}, | ||
{desc: "relative string", | ||
val: "path/to/file", | ||
expected: Import{Path: "path/to/file", Uid: -1, Gid: -1}}, | ||
{desc: "dict no dest", | ||
val: map[interface{}]interface{}{ | ||
"path": "/path/to/file", | ||
"hash": hash1, | ||
}, | ||
expected: Import{Path: "/path/to/file", Dest: "", Hash: hash1, Uid: -1, Gid: -1}}, | ||
{desc: "dest cannot be relative", | ||
val: map[interface{}]interface{}{ | ||
"path": "src1", | ||
"dest": "dest1", | ||
}, | ||
errstr: "cannot be relative", | ||
}, | ||
{desc: "guid cannot be negative", | ||
val: map[interface{}]interface{}{ | ||
"path": "src1", | ||
"uid": -2, | ||
}, | ||
errstr: "cannot be negative", | ||
}, | ||
{desc: "mode present", | ||
val: map[interface{}]interface{}{ | ||
"path": "src1", | ||
"mode": 0755, | ||
}, | ||
expected: Import{Path: "src1", Dest: "", Mode: modePtr(0755), Uid: -1, Gid: -1}}, | ||
} | ||
|
||
var found Import | ||
var err error | ||
for _, t := range tables { | ||
found, err = getImportFromInterface(t.val) | ||
if t.errstr == "" { | ||
assert.NoError(err, t.desc) | ||
assert.Equal(t.expected, found, t.desc) | ||
} else { | ||
assert.ErrorContains(err, t.errstr, t.desc) | ||
} | ||
} | ||
} | ||
|
||
func TestUnmarshalImports(t *testing.T) { | ||
assert := assert.New(t) | ||
type importsContainer struct { | ||
Imports []Import `yaml:"imports"` | ||
} | ||
tables := []struct { | ||
desc string | ||
yblob string | ||
expected Imports | ||
errstr string | ||
}{ | ||
{desc: "imports should not be a string", | ||
yblob: "/path/to/file", | ||
expected: Imports{}, | ||
errstr: "xpected an array"}, | ||
{desc: "imports should not be a dict", | ||
yblob: "path: /path/to/file\ndest: /path/to/dest\n", | ||
expected: Imports{}, | ||
errstr: "xpected an array"}, | ||
{desc: "example valid mixed string and dict", | ||
yblob: "- f1\n- path: f2\n", | ||
expected: Imports{ | ||
Import{Path: "f1", Uid: -1, Gid: -1}, | ||
Import{Path: "f2", Uid: -1, Gid: -1}, | ||
}}, | ||
} | ||
var err error | ||
found := Imports{} | ||
for _, t := range tables { | ||
err = yaml.Unmarshal([]byte(t.yblob), &found) | ||
if t.errstr == "" { | ||
if !assert.NoError(err, t.desc) { | ||
continue | ||
} | ||
assert.Equal(t.expected, found) | ||
} else { | ||
assert.ErrorContains(err, t.errstr, t.desc) | ||
} | ||
} | ||
} |