2014-08-05 16:17:40 -04:00
|
|
|
package parser
|
|
|
|
|
|
|
|
import (
|
2014-08-29 16:04:34 -04:00
|
|
|
"fmt"
|
2014-08-05 16:17:40 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
const testDir = "testfiles"
|
2014-08-07 03:42:10 -04:00
|
|
|
const negativeTestDir = "testfiles-negative"
|
2014-08-05 16:17:40 -04:00
|
|
|
|
2015-03-09 19:55:00 -04:00
|
|
|
func getDirs(t *testing.T, dir string) []string {
|
2014-08-07 03:42:10 -04:00
|
|
|
f, err := os.Open(dir)
|
2014-08-05 16:17:40 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer f.Close()
|
|
|
|
|
2015-03-09 19:55:00 -04:00
|
|
|
dirs, err := f.Readdirnames(0)
|
2014-08-05 16:17:40 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-08-07 03:42:10 -04:00
|
|
|
return dirs
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTestNegative(t *testing.T) {
|
|
|
|
for _, dir := range getDirs(t, negativeTestDir) {
|
2015-03-09 19:55:00 -04:00
|
|
|
dockerfile := filepath.Join(negativeTestDir, dir, "Dockerfile")
|
2014-08-07 03:42:10 -04:00
|
|
|
|
|
|
|
df, err := os.Open(dockerfile)
|
|
|
|
if err != nil {
|
2015-03-16 04:35:15 -04:00
|
|
|
t.Fatalf("Dockerfile missing for %s: %v", dir, err)
|
2014-08-07 03:42:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = Parse(df)
|
|
|
|
if err == nil {
|
2015-03-09 19:55:00 -04:00
|
|
|
t.Fatalf("No error parsing broken dockerfile for %s", dir)
|
2014-08-07 03:42:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
df.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTestData(t *testing.T) {
|
|
|
|
for _, dir := range getDirs(t, testDir) {
|
2015-03-09 19:55:00 -04:00
|
|
|
dockerfile := filepath.Join(testDir, dir, "Dockerfile")
|
|
|
|
resultfile := filepath.Join(testDir, dir, "result")
|
2014-08-05 16:17:40 -04:00
|
|
|
|
|
|
|
df, err := os.Open(dockerfile)
|
|
|
|
if err != nil {
|
2015-03-16 04:35:15 -04:00
|
|
|
t.Fatalf("Dockerfile missing for %s: %v", dir, err)
|
2014-08-05 16:17:40 -04:00
|
|
|
}
|
2015-01-03 00:38:52 -05:00
|
|
|
defer df.Close()
|
2014-08-05 16:17:40 -04:00
|
|
|
|
|
|
|
ast, err := Parse(df)
|
|
|
|
if err != nil {
|
2015-03-16 04:35:15 -04:00
|
|
|
t.Fatalf("Error parsing %s's dockerfile: %v", dir, err)
|
2014-08-05 16:17:40 -04:00
|
|
|
}
|
|
|
|
|
2015-01-03 00:38:52 -05:00
|
|
|
content, err := ioutil.ReadFile(resultfile)
|
2014-08-05 16:17:40 -04:00
|
|
|
if err != nil {
|
2015-03-16 04:35:15 -04:00
|
|
|
t.Fatalf("Error reading %s's result file: %v", dir, err)
|
2014-08-05 16:17:40 -04:00
|
|
|
}
|
|
|
|
|
2014-08-29 16:04:34 -04:00
|
|
|
if ast.Dump()+"\n" != string(content) {
|
2014-10-02 19:58:53 -04:00
|
|
|
fmt.Fprintln(os.Stderr, "Result:\n"+ast.Dump())
|
|
|
|
fmt.Fprintln(os.Stderr, "Expected:\n"+string(content))
|
2015-03-09 19:55:00 -04:00
|
|
|
t.Fatalf("%s: AST dump of dockerfile does not match result", dir)
|
2014-08-05 16:17:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|