github.com/goonzoid/gcli@v0.2.3-0.20150926213610-155587606ea1/tests/flag_framework_test.go (about) 1 package main 2 3 import ( 4 "bytes" 5 "fmt" 6 "os" 7 "os/exec" 8 "strconv" 9 "strings" 10 "testing" 11 "time" 12 ) 13 14 func TestNew_flag_frameworks(t *testing.T) { 15 16 tests := []struct { 17 framework string 18 expectOut string 19 }{ 20 { 21 framework: "flag", 22 expectOut: "Usage of ", 23 }, 24 } 25 26 owner := "awesome_user_" + strconv.Itoa(int(time.Now().Unix())) 27 cleanFunc, err := chdirSrcPath(owner) 28 if err != nil { 29 t.Fatal(err) 30 } 31 defer cleanFunc() 32 33 for _, tt := range tests { 34 35 artifactBin := fmt.Sprintf("%s_grep", tt.framework) 36 args := []string{ 37 "new", 38 "-framework", tt.framework, 39 "-owner", owner, 40 "-flag=ignore:Bool:'Perform case insensitive matching'", 41 "-flag=context:Int:'Print num lines of leading and trailing context'", 42 artifactBin, 43 } 44 45 output, err := runGcli(args) 46 if err != nil { 47 t.Fatal(err) 48 } 49 50 expect := "Successfully generated" 51 if !strings.Contains(output, expect) { 52 t.Fatalf("[%s] expect %q to contain %q", tt.framework, output, expect) 53 } 54 55 if err := goTests(artifactBin); err != nil { 56 t.Fatal(err) 57 } 58 59 if err := os.Chdir(artifactBin); err != nil { 60 t.Fatal(err) 61 } 62 63 var stdout, stderr bytes.Buffer 64 cmd := exec.Command("./"+artifactBin, "-help") 65 cmd.Stderr = &stderr 66 cmd.Stdout = &stdout 67 68 // cmd.Wait() returns error 69 _ = cmd.Run() 70 71 output = stdout.String() + stderr.String() 72 // t.Logf("%s \n\n%s", tt.framework, output) 73 if !strings.Contains(output, tt.expectOut) { 74 t.Errorf("[%s] expects %q to contain %q", tt.framework, output, tt.expectOut) 75 } 76 77 // Back to src directory 78 if err := os.Chdir(".."); err != nil { 79 t.Fatal(err) 80 } 81 } 82 }