github.com/vlal/goveralls@v0.0.2-0.20171114042957-b71a1e4855f8/goveralls_test.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "os" 6 "os/exec" 7 "path/filepath" 8 "strings" 9 "testing" 10 11 "net/http" 12 "net/http/httptest" 13 14 "github.com/pborman/uuid" 15 ) 16 17 func fakeServer() *httptest.Server { 18 return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 19 w.WriteHeader(http.StatusOK) 20 fmt.Fprintln(w, `{"error":false,"message":"Fake message","URL":"http://fake.url"}`) 21 })) 22 } 23 24 func TestUsage(t *testing.T) { 25 tmp := prepareTest(t) 26 defer os.RemoveAll(tmp) 27 cmd := exec.Command("goveralls", "-h") 28 b, err := cmd.CombinedOutput() 29 if err == nil { 30 t.Fatal("Expected exit code 1 bot 0") 31 } 32 s := strings.Split(string(b), "\n")[0] 33 if !strings.HasPrefix(s, "Usage: goveralls ") { 34 t.Fatalf("Expected %v, but %v", "Usage: ", s) 35 } 36 } 37 38 func TestInvalidArg(t *testing.T) { 39 tmp := prepareTest(t) 40 defer os.RemoveAll(tmp) 41 cmd := exec.Command("goveralls", "pkg") 42 b, err := cmd.CombinedOutput() 43 if err == nil { 44 t.Fatal("Expected exit code 1 got 0") 45 } 46 s := strings.Split(string(b), "\n")[0] 47 if !strings.HasPrefix(s, "Usage: goveralls ") { 48 t.Fatalf("Expected %v, but %v", "Usage: ", s) 49 } 50 } 51 52 func TestVerboseArg(t *testing.T) { 53 tmp := prepareTest(t) 54 defer os.RemoveAll(tmp) 55 fs := fakeServer() 56 57 t.Run("with verbose", func(t *testing.T) { 58 cmd := exec.Command("goveralls", "-package=github.com/mattn/goveralls/tester", "-v", "-endpoint") 59 cmd.Args = append(cmd.Args, "-v", "-endpoint", fs.URL) 60 b, err := cmd.CombinedOutput() 61 if err != nil { 62 t.Fatal("Expected exit code 0 got 1", err, string(b)) 63 } 64 65 if !strings.Contains(string(b), "--- PASS") { 66 t.Error("Expected to have verbosed go test output in stdout", string(b)) 67 } 68 }) 69 70 t.Run("without verbose", func(t *testing.T) { 71 cmd := exec.Command("goveralls", "-package=github.com/mattn/goveralls/tester", "-endpoint") 72 cmd.Args = append(cmd.Args, "-v", "-endpoint", fs.URL) 73 b, err := cmd.CombinedOutput() 74 if err != nil { 75 t.Fatal("Expected exit code 0 got 1", err, string(b)) 76 } 77 78 if strings.Contains(string(b), "--- PASS") { 79 t.Error("Expected to haven't verbosed go test output in stdout", string(b)) 80 } 81 }) 82 } 83 84 func TestRaceArg(t *testing.T) { 85 tmp := prepareTest(t) 86 defer os.RemoveAll(tmp) 87 fs := fakeServer() 88 89 t.Run("it should pass the test", func(t *testing.T) { 90 cmd := exec.Command("goveralls", "-package=github.com/mattn/goveralls/tester", "-race") 91 cmd.Args = append(cmd.Args, "-endpoint", fs.URL) 92 b, err := cmd.CombinedOutput() 93 if err != nil { 94 t.Fatal("Expected exit code 0 got 1", err, string(b)) 95 } 96 }) 97 } 98 99 /* FIXME: currently this dones't work because the command goveralls will run 100 * another session for this session. 101 func TestGoveralls(t *testing.T) { 102 wd, _ := os.Getwd() 103 tmp := prepareTest(t) 104 os.Chdir(tmp) 105 defer func() { 106 os.Chdir(wd) 107 os.RemoveAll(tmp) 108 }() 109 runCmd(t, "go", "get", "github.com/mattn/goveralls/testergo-runewidth") 110 b := runCmd(t, "goveralls", "-package=github.com/mattn/goveralls/tester") 111 lines := strings.Split(strings.TrimSpace(string(b)), "\n") 112 s := lines[len(lines)-1] 113 if s != "Succeeded" { 114 t.Fatalf("Expected test of tester are succeeded, but failured") 115 } 116 } 117 */ 118 119 func prepareTest(t *testing.T) (tmpPath string) { 120 tmp := os.TempDir() 121 tmp = filepath.Join(tmp, uuid.New()) 122 runCmd(t, "go", "build", "-o", filepath.Join(tmp, "bin", "goveralls"), "github.com/mattn/goveralls") 123 os.Setenv("PATH", filepath.Join(tmp, "bin")+string(filepath.ListSeparator)+os.Getenv("PATH")) 124 os.MkdirAll(filepath.Join(tmp, "src"), 0755) 125 return tmp 126 } 127 128 func runCmd(t *testing.T, cmd string, args ...string) []byte { 129 b, err := exec.Command(cmd, args...).CombinedOutput() 130 if err != nil { 131 t.Fatalf("Expected %v, but %v: %v", nil, err, string(b)) 132 } 133 return b 134 }