2014-08-05 16:17:40 -04:00
|
|
|
package parser
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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
|
|
|
|
2014-08-07 03:42:10 -04:00
|
|
|
func getDirs(t *testing.T, dir string) []os.FileInfo {
|
|
|
|
f, err := os.Open(dir)
|
2014-08-05 16:17:40 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
dirs, err := f.Readdir(0)
|
|
|
|
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) {
|
|
|
|
dockerfile := filepath.Join(negativeTestDir, dir.Name(), "Dockerfile")
|
|
|
|
|
|
|
|
df, err := os.Open(dockerfile)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Dockerfile missing for %s: %s", dir.Name(), err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = Parse(df)
|
|
|
|
if err == nil {
|
2014-08-08 16:44:57 -04:00
|
|
|
t.Fatalf("No error parsing broken dockerfile for %s", dir.Name())
|
2014-08-07 03:42:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
df.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTestData(t *testing.T) {
|
|
|
|
for _, dir := range getDirs(t, testDir) {
|
2014-08-05 16:17:40 -04:00
|
|
|
dockerfile := filepath.Join(testDir, dir.Name(), "Dockerfile")
|
|
|
|
resultfile := filepath.Join(testDir, dir.Name(), "result")
|
|
|
|
|
|
|
|
df, err := os.Open(dockerfile)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Dockerfile missing for %s: %s", dir.Name(), err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
rf, err := os.Open(resultfile)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Result file missing for %s: %s", dir.Name(), err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
ast, err := Parse(df)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error parsing %s's dockerfile: %s", dir.Name(), err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
content, err := ioutil.ReadAll(rf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error reading %s's result file: %s", dir.Name(), err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if ast.Dump() != string(content) {
|
|
|
|
t.Fatalf("%s: AST dump of dockerfile does not match result", dir.Name())
|
|
|
|
}
|
|
|
|
|
|
|
|
df.Close()
|
|
|
|
rf.Close()
|
|
|
|
}
|
|
|
|
}
|