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