github.com/kat-co/gorkin@v0.0.0-20150628020237-0eebb48d172e/features/steps/gorkin_test.go (about) 1 package steps 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 "strings" 8 "testing" 9 10 "os/exec" 11 12 . "github.com/kat-co/gorkin/gorkin" 13 ) 14 15 func TestFeatures(t *testing.T) { 16 17 // Isolation layer. 18 type I struct { 19 dir string 20 featureFile string 21 gorkResult string 22 } 23 24 Step(`the path \"([^"]+)\"( doesn't)? exists?`, func(f *I, dirName string, deleteIfExists bool) { 25 26 // Create a temporary directory to operate within. 27 if f.dir == "" { 28 isolationDir, err := ioutil.TempDir("", "gorkin-test") 29 if err != nil { 30 t.Fatalf("could not create tmp host directory: %s", err) 31 } 32 f.dir = isolationDir 33 } 34 35 if newDir := filepath.Join(f.dir, dirName); deleteIfExists == false { 36 if err := os.Mkdir(newDir, 0777); err != nil { 37 t.Fatalf("could not create directory: %s", err) 38 } 39 } else if err := os.RemoveAll(newDir); err != nil { 40 t.Fatalf("could not remove directory: %v", err) 41 } 42 }) 43 44 Step(`the file \"([^"]+)\" exists(?: with content)?`, func(f *I, filePath, content string) { 45 46 file := filepath.Join(f.dir, filePath) 47 if err := ioutil.WriteFile(file, []byte(content), 0666); err != nil { 48 t.Fatalf("could not create feature file: %v", err) 49 } 50 }) 51 52 Step(`there is a steps directory under features`, func(f *I) { 53 if err := os.Mkdir(filepath.Join(f.dir, "features", "steps"), 0777); err != nil { 54 t.Fatalf("could not create feature file: %v", err) 55 } 56 }) 57 58 Step(`a user runs \"([^"]+)\"`, func(f *I, command string) { 59 60 if f.dir != "" { 61 cwd, err := os.Getwd() 62 if err != nil { 63 t.Fatalf("could not get cwd: %v", err) 64 } 65 defer func() { 66 os.Chdir(cwd) 67 }() 68 if err := os.Chdir(f.dir); err != nil { 69 t.Fatalf("could not change into temp directory: %v\n", err) 70 } 71 } 72 73 cmdAndArgs := strings.Split(command, " ") 74 cmd := exec.Command(cmdAndArgs[0], cmdAndArgs[1:]...) 75 output, err := cmd.CombinedOutput() 76 if err != nil { 77 t.Fatalf("could not run gorkin: %v", err) 78 } 79 80 f.gorkResult = string(output) 81 }) 82 83 Step(`gorkin should find the features directory`, func(f *I) { 84 if strings.Contains(f.gorkResult, "Processing:") == false { 85 t.Fatalf("gorkin did not find the features directory: %s", f.gorkResult) 86 } 87 }) 88 89 Step(`the output should be`, func(f *I, output string) { 90 if f.gorkResult != output { 91 t.Errorf(`Expected output: "%s"`, output) 92 t.Fatalf(`unexpected result from gorkin: "%v"`, f.gorkResult) 93 } 94 }) 95 96 Step(`the output should contain`, func(f *I, output string) { 97 if !strings.Contains(f.gorkResult, output) { 98 t.Logf(`Expected output: "%s"`, output) 99 t.Fatalf(`unexpected result from gorkin: "%v"`, f.gorkResult) 100 } 101 }) 102 103 RunFeatureTests(t, &I{}) 104 }