Skip to content

Commit

Permalink
Parallelize all tests, remove multiple binaries creation
Browse files Browse the repository at this point in the history
Remove broken test which would upload to coveralls.io
  • Loading branch information
gdm85 committed Nov 4, 2020
1 parent 28a623e commit e9f79a4
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 65 deletions.
2 changes: 2 additions & 0 deletions gitinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
)

func TestLoadBranchFromEnv(t *testing.T) {
t.Parallel()

var tests = []struct {
testCase string
envs map[string]string
Expand Down
2 changes: 2 additions & 0 deletions gocover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
)

func TestMergeProfs(t *testing.T) {
t.Parallel()

tests := []struct {
in [][]*cover.Profile
want []*cover.Profile
Expand Down
3 changes: 1 addition & 2 deletions goveralls.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ func init() {

// usage supplants package flag's Usage variable
var usage = func() {
cmd := os.Args[0]
// fmt.Fprintf(os.Stderr, "Usage of %s:\n", cmd)
cmd := filepath.Base(os.Args[0])
s := "Usage: %s [options]\n"
fmt.Fprintf(os.Stderr, s, cmd)
flag.PrintDefaults()
Expand Down
121 changes: 66 additions & 55 deletions goveralls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,31 @@ import (
"net/http/httptest"
)

var goverallsTestBin string

func TestMain(m *testing.M) {
tmpBin, err := ioutil.TempDir("", "goveralls_")
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
os.Exit(1)
}

// generate the test binary used by all tests
goverallsTestBin = filepath.Join(tmpBin, "bin", "goveralls")
_, err = exec.Command("go", "build", "-o", goverallsTestBin, ".").CombinedOutput()
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
os.Exit(1)
}

// run all tests
exitVal := m.Run()

os.RemoveAll(tmpBin)

os.Exit(exitVal)
}

func fakeServer() *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
Expand Down Expand Up @@ -47,12 +72,12 @@ func fakeServerWithPayloadChannel(payload chan Job) *httptest.Server {
}

func TestCustomJobId(t *testing.T) {
tmp := prepareTest(t)
defer os.RemoveAll(tmp)
jobBodyChannel := make(chan Job, 8096)
t.Parallel()

jobBodyChannel := make(chan Job, 16)
fs := fakeServerWithPayloadChannel(jobBodyChannel)

cmd := exec.Command("goveralls", "-jobid=123abc", "-package=github.com/mattn/goveralls/tester", "-endpoint")
cmd := exec.Command(goverallsTestBin, "-jobid=123abc", "-package=github.com/mattn/goveralls/tester", "-endpoint")
cmd.Args = append(cmd.Args, "-v", "-endpoint", fs.URL)
b, err := cmd.CombinedOutput()
if err != nil {
Expand All @@ -67,26 +92,28 @@ func TestCustomJobId(t *testing.T) {
}

func TestInvalidArg(t *testing.T) {
tmp := prepareTest(t)
defer os.RemoveAll(tmp)
cmd := exec.Command("goveralls", "pkg")
t.Parallel()

cmd := exec.Command(goverallsTestBin, "pkg")
b, err := cmd.CombinedOutput()
if err == nil {
t.Fatal("Expected exit code 1 got 0")
}
s := strings.Split(string(b), "\n")[0]
if !strings.HasPrefix(s, "Usage: goveralls ") {
t.Fatalf("Expected %v, but %v", "Usage: ", s)
t.Fatalf("Expected %q, but got %q", "Usage: goveralls", s)
}
}

func TestVerboseArg(t *testing.T) {
tmp := prepareTest(t)
defer os.RemoveAll(tmp)
t.Parallel()

fs := fakeServer()

t.Run("with verbose", func(t *testing.T) {
cmd := exec.Command("goveralls", "-package=github.com/mattn/goveralls/tester", "-v", "-endpoint")
t.Parallel()

cmd := exec.Command(goverallsTestBin, "-package=github.com/mattn/goveralls/tester", "-v", "-endpoint")
cmd.Args = append(cmd.Args, "-v", "-endpoint", fs.URL)
b, err := cmd.CombinedOutput()
if err != nil {
Expand All @@ -99,7 +126,9 @@ func TestVerboseArg(t *testing.T) {
})

t.Run("without verbose", func(t *testing.T) {
cmd := exec.Command("goveralls", "-package=github.com/mattn/goveralls/tester", "-endpoint")
t.Parallel()

cmd := exec.Command(goverallsTestBin, "-package=github.com/mattn/goveralls/tester", "-endpoint")
cmd.Args = append(cmd.Args, "-v", "-endpoint", fs.URL)
b, err := cmd.CombinedOutput()
if err != nil {
Expand All @@ -113,12 +142,14 @@ func TestVerboseArg(t *testing.T) {
}

func TestShowArg(t *testing.T) {
tmp := prepareTest(t)
defer os.RemoveAll(tmp)
t.Parallel()

fs := fakeServer()

t.Run("with show", func(t *testing.T) {
cmd := exec.Command("goveralls", "-package=github.com/mattn/goveralls/tester/...", "-show", "-endpoint")
t.Parallel()

cmd := exec.Command(goverallsTestBin, "-package=github.com/mattn/goveralls/tester/...", "-show", "-endpoint")
cmd.Args = append(cmd.Args, "-show", "-endpoint", fs.URL)
b, err := cmd.CombinedOutput()
if err != nil {
Expand All @@ -136,12 +167,14 @@ http://fake.url
}

func TestRaceArg(t *testing.T) {
tmp := prepareTest(t)
defer os.RemoveAll(tmp)
t.Parallel()

fs := fakeServer()

t.Run("it should pass the test", func(t *testing.T) {
cmd := exec.Command("goveralls", "-package=github.com/mattn/goveralls/tester", "-race")
t.Parallel()

cmd := exec.Command(goverallsTestBin, "-package=github.com/mattn/goveralls/tester", "-race")
cmd.Args = append(cmd.Args, "-endpoint", fs.URL)
b, err := cmd.CombinedOutput()
if err != nil {
Expand All @@ -150,34 +183,16 @@ func TestRaceArg(t *testing.T) {
})
}

/* FIXME: currently this doesn't work because the command goveralls will run
* another session for this session.
func TestGoveralls(t *testing.T) {
wd, _ := os.Getwd()
tmp := prepareTest(t)
os.Chdir(tmp)
defer func() {
os.Chdir(wd)
os.RemoveAll(tmp)
}()
runCmd(t, "go", "get", "github.com/mattn/goveralls/testergo-runewidth")
b := runCmd(t, "goveralls", "-package=github.com/mattn/goveralls/tester")
lines := strings.Split(strings.TrimSpace(string(b)), "\n")
s := lines[len(lines)-1]
if s != "Succeeded" {
t.Fatalf("Expected test of tester are succeeded, but failed")
}
}
*/

func TestUploadSource(t *testing.T) {
tmp := prepareTest(t)
defer os.RemoveAll(tmp)
jobBodyChannel := make(chan Job, 8096)
fs := fakeServerWithPayloadChannel(jobBodyChannel)
t.Parallel()

t.Run("with uploadsource", func(t *testing.T) {
cmd := exec.Command("goveralls", "-uploadsource=true", "-package=github.com/mattn/goveralls/tester", "-endpoint")
t.Parallel()

jobBodyChannel := make(chan Job, 16)
fs := fakeServerWithPayloadChannel(jobBodyChannel)

cmd := exec.Command(goverallsTestBin, "-uploadsource=true", "-package=github.com/mattn/goveralls/tester", "-endpoint")
cmd.Args = append(cmd.Args, "-v", "-endpoint", fs.URL)
b, err := cmd.CombinedOutput()
if err != nil {
Expand All @@ -194,7 +209,12 @@ func TestUploadSource(t *testing.T) {
})

t.Run("without uploadsource", func(t *testing.T) {
cmd := exec.Command("goveralls", "-uploadsource=false", "-package=github.com/mattn/goveralls/tester", "-endpoint")
t.Parallel()

jobBodyChannel := make(chan Job, 16)
fs := fakeServerWithPayloadChannel(jobBodyChannel)

cmd := exec.Command(goverallsTestBin, "-uploadsource=false", "-package=github.com/mattn/goveralls/tester", "-endpoint")
cmd.Args = append(cmd.Args, "-v", "-endpoint", fs.URL)
b, err := cmd.CombinedOutput()
if err != nil {
Expand All @@ -211,20 +231,11 @@ func TestUploadSource(t *testing.T) {
}

func prepareTest(t *testing.T) (tmpPath string) {
tmp, err := ioutil.TempDir("", "goveralls")
tmp, err := ioutil.TempDir("", "goveralls_")
if err != nil {
t.Fatal("prepareTest:", err)
}
runCmd(t, "go", "build", "-o", filepath.Join(tmp, "bin", "goveralls"), "github.com/mattn/goveralls")
os.Setenv("PATH", filepath.Join(tmp, "bin")+string(filepath.ListSeparator)+os.Getenv("PATH"))

os.MkdirAll(filepath.Join(tmp, "src"), 0755)
return tmp
}

func runCmd(t *testing.T, cmd string, args ...string) []byte {
b, err := exec.Command(cmd, args...).CombinedOutput()
if err != nil {
t.Fatalf("Expected %v, but %v: %v", nil, err, string(b))
}
return b
}
7 changes: 3 additions & 4 deletions usage_ge1.15_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@
package main

import (
"os"
"os/exec"
"runtime"
"strings"
"testing"
)

func TestUsage(t *testing.T) {
tmp := prepareTest(t)
defer os.RemoveAll(tmp)
cmd := exec.Command("goveralls", "-h")
t.Parallel()

cmd := exec.Command(goverallsTestBin, "-h")
b, err := cmd.CombinedOutput()
runtime.Version()
if err != nil {
Expand Down
7 changes: 3 additions & 4 deletions usage_lt1.15_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@
package main

import (
"os"
"os/exec"
"runtime"
"strings"
"testing"
)

func TestUsage(t *testing.T) {
tmp := prepareTest(t)
defer os.RemoveAll(tmp)
cmd := exec.Command("goveralls", "-h")
t.Parallel()

cmd := exec.Command(goverallsTestBin, "-h")
b, err := cmd.CombinedOutput()
runtime.Version()
if err == nil {
Expand Down

0 comments on commit e9f79a4

Please sign in to comment.