dockerfile: nicer subtest cases in parser tests

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
v0.8
Tonis Tiigi 2020-04-14 20:46:40 -07:00
parent 7c0493de8d
commit f2307db707
1 changed files with 24 additions and 20 deletions

View File

@ -31,37 +31,41 @@ func getDirs(t *testing.T, dir string) []string {
func TestParseErrorCases(t *testing.T) { func TestParseErrorCases(t *testing.T) {
for _, dir := range getDirs(t, negativeTestDir) { for _, dir := range getDirs(t, negativeTestDir) {
dockerfile := filepath.Join(negativeTestDir, dir, "Dockerfile") t.Run(dir, func(t *testing.T) {
dockerfile := filepath.Join(negativeTestDir, dir, "Dockerfile")
df, err := os.Open(dockerfile) df, err := os.Open(dockerfile)
assert.NilError(t, err, dockerfile) assert.NilError(t, err, dockerfile)
defer df.Close() defer df.Close()
_, err = Parse(df) _, err = Parse(df)
assert.Check(t, is.ErrorContains(err, ""), dockerfile) assert.Check(t, is.ErrorContains(err, ""), dockerfile)
})
} }
} }
func TestParseCases(t *testing.T) { func TestParseCases(t *testing.T) {
for _, dir := range getDirs(t, testDir) { for _, dir := range getDirs(t, testDir) {
dockerfile := filepath.Join(testDir, dir, "Dockerfile") t.Run(dir, func(t *testing.T) {
resultfile := filepath.Join(testDir, dir, "result") dockerfile := filepath.Join(testDir, dir, "Dockerfile")
resultfile := filepath.Join(testDir, dir, "result")
df, err := os.Open(dockerfile) df, err := os.Open(dockerfile)
assert.NilError(t, err, dockerfile) assert.NilError(t, err, dockerfile)
defer df.Close() defer df.Close()
result, err := Parse(df) result, err := Parse(df)
assert.NilError(t, err, dockerfile) assert.NilError(t, err, dockerfile)
content, err := ioutil.ReadFile(resultfile) content, err := ioutil.ReadFile(resultfile)
assert.NilError(t, err, resultfile) assert.NilError(t, err, resultfile)
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
// CRLF --> CR to match Unix behavior // CRLF --> CR to match Unix behavior
content = bytes.Replace(content, []byte{'\x0d', '\x0a'}, []byte{'\x0a'}, -1) content = bytes.Replace(content, []byte{'\x0d', '\x0a'}, []byte{'\x0a'}, -1)
} }
assert.Check(t, is.Equal(result.AST.Dump()+"\n", string(content)), "In "+dockerfile) assert.Check(t, is.Equal(result.AST.Dump()+"\n", string(content)), "In "+dockerfile)
})
} }
} }