github.com/raychaser/docker@v1.5.0/builder/parser/parser_test.go (about) 1 package parser 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 "path/filepath" 8 "testing" 9 ) 10 11 const testDir = "testfiles" 12 const negativeTestDir = "testfiles-negative" 13 14 func getDirs(t *testing.T, dir string) []os.FileInfo { 15 f, err := os.Open(dir) 16 if err != nil { 17 t.Fatal(err) 18 } 19 20 defer f.Close() 21 22 dirs, err := f.Readdir(0) 23 if err != nil { 24 t.Fatal(err) 25 } 26 27 return dirs 28 } 29 30 func TestTestNegative(t *testing.T) { 31 for _, dir := range getDirs(t, negativeTestDir) { 32 dockerfile := filepath.Join(negativeTestDir, dir.Name(), "Dockerfile") 33 34 df, err := os.Open(dockerfile) 35 if err != nil { 36 t.Fatalf("Dockerfile missing for %s: %s", dir.Name(), err.Error()) 37 } 38 39 _, err = Parse(df) 40 if err == nil { 41 t.Fatalf("No error parsing broken dockerfile for %s", dir.Name()) 42 } 43 44 df.Close() 45 } 46 } 47 48 func TestTestData(t *testing.T) { 49 for _, dir := range getDirs(t, testDir) { 50 dockerfile := filepath.Join(testDir, dir.Name(), "Dockerfile") 51 resultfile := filepath.Join(testDir, dir.Name(), "result") 52 53 df, err := os.Open(dockerfile) 54 if err != nil { 55 t.Fatalf("Dockerfile missing for %s: %s", dir.Name(), err.Error()) 56 } 57 defer df.Close() 58 59 ast, err := Parse(df) 60 if err != nil { 61 t.Fatalf("Error parsing %s's dockerfile: %s", dir.Name(), err.Error()) 62 } 63 64 content, err := ioutil.ReadFile(resultfile) 65 if err != nil { 66 t.Fatalf("Error reading %s's result file: %s", dir.Name(), err.Error()) 67 } 68 69 if ast.Dump()+"\n" != string(content) { 70 fmt.Fprintln(os.Stderr, "Result:\n"+ast.Dump()) 71 fmt.Fprintln(os.Stderr, "Expected:\n"+string(content)) 72 t.Fatalf("%s: AST dump of dockerfile does not match result", dir.Name()) 73 } 74 } 75 }