|
@@ -8,6 +8,8 @@ import (
|
|
|
"path/filepath"
|
|
|
"runtime"
|
|
|
"testing"
|
|
|
+
|
|
|
+ "github.com/docker/docker/pkg/testutil/assert"
|
|
|
)
|
|
|
|
|
|
const testDir = "testfiles"
|
|
@@ -16,17 +18,11 @@ const testFileLineInfo = "testfile-line/Dockerfile"
|
|
|
|
|
|
func getDirs(t *testing.T, dir string) []string {
|
|
|
f, err := os.Open(dir)
|
|
|
- if err != nil {
|
|
|
- t.Fatal(err)
|
|
|
- }
|
|
|
-
|
|
|
+ assert.NilError(t, err)
|
|
|
defer f.Close()
|
|
|
|
|
|
dirs, err := f.Readdirnames(0)
|
|
|
- if err != nil {
|
|
|
- t.Fatal(err)
|
|
|
- }
|
|
|
-
|
|
|
+ assert.NilError(t, err)
|
|
|
return dirs
|
|
|
}
|
|
|
|
|
@@ -35,15 +31,11 @@ func TestTestNegative(t *testing.T) {
|
|
|
dockerfile := filepath.Join(negativeTestDir, dir, "Dockerfile")
|
|
|
|
|
|
df, err := os.Open(dockerfile)
|
|
|
- if err != nil {
|
|
|
- t.Fatalf("Dockerfile missing for %s: %v", dir, err)
|
|
|
- }
|
|
|
+ assert.NilError(t, err)
|
|
|
defer df.Close()
|
|
|
|
|
|
- _, err = Parse(df, NewDefaultDirective())
|
|
|
- if err == nil {
|
|
|
- t.Fatalf("No error parsing broken dockerfile for %s", dir)
|
|
|
- }
|
|
|
+ _, err = Parse(df)
|
|
|
+ assert.Error(t, err, "")
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -53,31 +45,21 @@ func TestTestData(t *testing.T) {
|
|
|
resultfile := filepath.Join(testDir, dir, "result")
|
|
|
|
|
|
df, err := os.Open(dockerfile)
|
|
|
- if err != nil {
|
|
|
- t.Fatalf("Dockerfile missing for %s: %v", dir, err)
|
|
|
- }
|
|
|
+ assert.NilError(t, err)
|
|
|
defer df.Close()
|
|
|
|
|
|
- ast, err := Parse(df, NewDefaultDirective())
|
|
|
- if err != nil {
|
|
|
- t.Fatalf("Error parsing %s's dockerfile: %v", dir, err)
|
|
|
- }
|
|
|
+ result, err := Parse(df)
|
|
|
+ assert.NilError(t, err)
|
|
|
|
|
|
content, err := ioutil.ReadFile(resultfile)
|
|
|
- if err != nil {
|
|
|
- t.Fatalf("Error reading %s's result file: %v", dir, err)
|
|
|
- }
|
|
|
+ assert.NilError(t, err)
|
|
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
// CRLF --> CR to match Unix behavior
|
|
|
content = bytes.Replace(content, []byte{'\x0d', '\x0a'}, []byte{'\x0a'}, -1)
|
|
|
}
|
|
|
|
|
|
- if ast.Dump()+"\n" != string(content) {
|
|
|
- fmt.Fprintln(os.Stderr, "Result:\n"+ast.Dump())
|
|
|
- fmt.Fprintln(os.Stderr, "Expected:\n"+string(content))
|
|
|
- t.Fatalf("%s: AST dump of dockerfile does not match result", dir)
|
|
|
- }
|
|
|
+ assert.Equal(t, result.AST.Dump()+"\n", string(content))
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -119,46 +101,33 @@ func TestParseWords(t *testing.T) {
|
|
|
|
|
|
for _, test := range tests {
|
|
|
words := parseWords(test["input"][0], NewDefaultDirective())
|
|
|
- if len(words) != len(test["expect"]) {
|
|
|
- t.Fatalf("length check failed. input: %v, expect: %q, output: %q", test["input"][0], test["expect"], words)
|
|
|
- }
|
|
|
- for i, word := range words {
|
|
|
- if word != test["expect"][i] {
|
|
|
- t.Fatalf("word check failed for word: %q. input: %q, expect: %q, output: %q", word, test["input"][0], test["expect"], words)
|
|
|
- }
|
|
|
- }
|
|
|
+ assert.DeepEqual(t, words, test["expect"])
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func TestLineInformation(t *testing.T) {
|
|
|
df, err := os.Open(testFileLineInfo)
|
|
|
- if err != nil {
|
|
|
- t.Fatalf("Dockerfile missing for %s: %v", testFileLineInfo, err)
|
|
|
- }
|
|
|
+ assert.NilError(t, err)
|
|
|
defer df.Close()
|
|
|
|
|
|
- ast, err := Parse(df, NewDefaultDirective())
|
|
|
- if err != nil {
|
|
|
- t.Fatalf("Error parsing dockerfile %s: %v", testFileLineInfo, err)
|
|
|
- }
|
|
|
+ result, err := Parse(df)
|
|
|
+ assert.NilError(t, err)
|
|
|
|
|
|
- if ast.StartLine != 5 || ast.EndLine != 31 {
|
|
|
- fmt.Fprintf(os.Stderr, "Wrong root line information: expected(%d-%d), actual(%d-%d)\n", 5, 31, ast.StartLine, ast.EndLine)
|
|
|
+ ast := result.AST
|
|
|
+ if ast.StartLine != 5 || ast.endLine != 31 {
|
|
|
+ fmt.Fprintf(os.Stderr, "Wrong root line information: expected(%d-%d), actual(%d-%d)\n", 5, 31, ast.StartLine, ast.endLine)
|
|
|
t.Fatal("Root line information doesn't match result.")
|
|
|
}
|
|
|
- if len(ast.Children) != 3 {
|
|
|
- fmt.Fprintf(os.Stderr, "Wrong number of child: expected(%d), actual(%d)\n", 3, len(ast.Children))
|
|
|
- t.Fatalf("Root line information doesn't match result for %s", testFileLineInfo)
|
|
|
- }
|
|
|
+ assert.Equal(t, len(ast.Children), 3)
|
|
|
expected := [][]int{
|
|
|
{5, 5},
|
|
|
{11, 12},
|
|
|
{17, 31},
|
|
|
}
|
|
|
for i, child := range ast.Children {
|
|
|
- if child.StartLine != expected[i][0] || child.EndLine != expected[i][1] {
|
|
|
+ if child.StartLine != expected[i][0] || child.endLine != expected[i][1] {
|
|
|
t.Logf("Wrong line information for child %d: expected(%d-%d), actual(%d-%d)\n",
|
|
|
- i, expected[i][0], expected[i][1], child.StartLine, child.EndLine)
|
|
|
+ i, expected[i][0], expected[i][1], child.StartLine, child.endLine)
|
|
|
t.Fatal("Root line information doesn't match result.")
|
|
|
}
|
|
|
}
|