github.com/irfanurrehman/goveralls@v0.0.7/goveralls_test.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"net/url"
     8  	"os"
     9  	"os/exec"
    10  	"path/filepath"
    11  	"strings"
    12  	"testing"
    13  
    14  	"net/http"
    15  	"net/http/httptest"
    16  )
    17  
    18  func fakeServer() *httptest.Server {
    19  	return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    20  		w.WriteHeader(http.StatusOK)
    21  		fmt.Fprintln(w, `{"error":false,"message":"Fake message","URL":"http://fake.url"}`)
    22  	}))
    23  }
    24  
    25  func fakeServerWithPayloadChannel(payload chan Job) *httptest.Server {
    26  	return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    27  		// this is a standard baked response
    28  		fmt.Fprintln(w, `{"error":false,"message":"Fake message","URL":"http://fake.url"}`)
    29  
    30  		body, err := ioutil.ReadAll(r.Body)
    31  		// query params are used for the body payload
    32  		vals, err := url.ParseQuery(string(body))
    33  		if err != nil {
    34  			w.WriteHeader(http.StatusInternalServerError)
    35  			return
    36  		}
    37  
    38  		var job Job
    39  		err = json.Unmarshal([]byte(vals["json"][0]), &job)
    40  		if err != nil {
    41  			w.WriteHeader(http.StatusInternalServerError)
    42  			return
    43  		}
    44  		payload <- job
    45  
    46  		w.WriteHeader(http.StatusOK)
    47  	}))
    48  }
    49  
    50  func TestUsage(t *testing.T) {
    51  	tmp := prepareTest(t)
    52  	defer os.RemoveAll(tmp)
    53  	cmd := exec.Command("goveralls", "-h")
    54  	b, err := cmd.CombinedOutput()
    55  	if err == nil {
    56  		t.Fatal("Expected exit code 1 bot 0")
    57  	}
    58  	s := strings.Split(string(b), "\n")[0]
    59  	if !strings.HasPrefix(s, "Usage: goveralls ") {
    60  		t.Fatalf("Expected %v, but %v", "Usage: ", s)
    61  	}
    62  }
    63  
    64  func TestCustomJobId(t *testing.T) {
    65  	tmp := prepareTest(t)
    66  	defer os.RemoveAll(tmp)
    67  	jobBodyChannel := make(chan Job, 8096)
    68  	fs := fakeServerWithPayloadChannel(jobBodyChannel)
    69  
    70  	cmd := exec.Command("goveralls", "-jobid=123abc", "-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  	jobBody := <-jobBodyChannel
    78  
    79  	if jobBody.ServiceJobID != "123abc" {
    80  		t.Fatalf("Expected job id of 123abc, but was %s", jobBody.ServiceJobID)
    81  	}
    82  }
    83  
    84  func TestInvalidArg(t *testing.T) {
    85  	tmp := prepareTest(t)
    86  	defer os.RemoveAll(tmp)
    87  	cmd := exec.Command("goveralls", "pkg")
    88  	b, err := cmd.CombinedOutput()
    89  	if err == nil {
    90  		t.Fatal("Expected exit code 1 got 0")
    91  	}
    92  	s := strings.Split(string(b), "\n")[0]
    93  	if !strings.HasPrefix(s, "Usage: goveralls ") {
    94  		t.Fatalf("Expected %v, but %v", "Usage: ", s)
    95  	}
    96  }
    97  
    98  func TestVerboseArg(t *testing.T) {
    99  	tmp := prepareTest(t)
   100  	defer os.RemoveAll(tmp)
   101  	fs := fakeServer()
   102  
   103  	t.Run("with verbose", func(t *testing.T) {
   104  		cmd := exec.Command("goveralls", "-package=github.com/mattn/goveralls/tester", "-v", "-endpoint")
   105  		cmd.Args = append(cmd.Args, "-v", "-endpoint", fs.URL)
   106  		b, err := cmd.CombinedOutput()
   107  		if err != nil {
   108  			t.Fatal("Expected exit code 0 got 1", err, string(b))
   109  		}
   110  
   111  		if !strings.Contains(string(b), "--- PASS") {
   112  			t.Error("Expected to have verbose go test output in stdout", string(b))
   113  		}
   114  	})
   115  
   116  	t.Run("without verbose", func(t *testing.T) {
   117  		cmd := exec.Command("goveralls", "-package=github.com/mattn/goveralls/tester", "-endpoint")
   118  		cmd.Args = append(cmd.Args, "-v", "-endpoint", fs.URL)
   119  		b, err := cmd.CombinedOutput()
   120  		if err != nil {
   121  			t.Fatal("Expected exit code 0 got 1", err, string(b))
   122  		}
   123  
   124  		if strings.Contains(string(b), "--- PASS") {
   125  			t.Error("Expected to haven't verbose go test output in stdout", string(b))
   126  		}
   127  	})
   128  }
   129  
   130  func TestShowArg(t *testing.T) {
   131  	tmp := prepareTest(t)
   132  	defer os.RemoveAll(tmp)
   133  	fs := fakeServer()
   134  
   135  	t.Run("with show", func(t *testing.T) {
   136  		cmd := exec.Command("goveralls", "-package=github.com/mattn/goveralls/tester/...", "-show", "-endpoint")
   137  		cmd.Args = append(cmd.Args, "-show", "-endpoint", fs.URL)
   138  		b, err := cmd.CombinedOutput()
   139  		if err != nil {
   140  			t.Fatal("Expected exit code 0 got 1", err, string(b))
   141  		}
   142  
   143  		expected := `goveralls: github.com/mattn/goveralls/tester
   144  Fake message
   145  http://fake.url
   146  `
   147  		if string(b) != expected {
   148  			t.Error("Unexpected output for -show:", string(b))
   149  		}
   150  	})
   151  }
   152  
   153  func TestRaceArg(t *testing.T) {
   154  	tmp := prepareTest(t)
   155  	defer os.RemoveAll(tmp)
   156  	fs := fakeServer()
   157  
   158  	t.Run("it should pass the test", func(t *testing.T) {
   159  		cmd := exec.Command("goveralls", "-package=github.com/mattn/goveralls/tester", "-race")
   160  		cmd.Args = append(cmd.Args, "-endpoint", fs.URL)
   161  		b, err := cmd.CombinedOutput()
   162  		if err != nil {
   163  			t.Fatal("Expected exit code 0 got 1", err, string(b))
   164  		}
   165  	})
   166  }
   167  
   168  /* FIXME: currently this doesn't work because the command goveralls will run
   169   * another session for this session.
   170  func TestGoveralls(t *testing.T) {
   171  	wd, _ := os.Getwd()
   172  	tmp := prepareTest(t)
   173  	os.Chdir(tmp)
   174  	defer func() {
   175  		os.Chdir(wd)
   176  		os.RemoveAll(tmp)
   177  	}()
   178  	runCmd(t, "go", "get", "github.com/mattn/goveralls/testergo-runewidth")
   179  	b := runCmd(t, "goveralls", "-package=github.com/mattn/goveralls/tester")
   180  	lines := strings.Split(strings.TrimSpace(string(b)), "\n")
   181  	s := lines[len(lines)-1]
   182  	if s != "Succeeded" {
   183  		t.Fatalf("Expected test of tester are succeeded, but failed")
   184  	}
   185  }
   186  */
   187  
   188  func prepareTest(t *testing.T) (tmpPath string) {
   189  	tmp, err := ioutil.TempDir("", "goveralls")
   190  	if err != nil {
   191  		t.Fatal("prepareTest:", err)
   192  	}
   193  	runCmd(t, "go", "build", "-o", filepath.Join(tmp, "bin", "goveralls"), "github.com/mattn/goveralls")
   194  	os.Setenv("PATH", filepath.Join(tmp, "bin")+string(filepath.ListSeparator)+os.Getenv("PATH"))
   195  	os.MkdirAll(filepath.Join(tmp, "src"), 0755)
   196  	return tmp
   197  }
   198  
   199  func runCmd(t *testing.T, cmd string, args ...string) []byte {
   200  	b, err := exec.Command(cmd, args...).CombinedOutput()
   201  	if err != nil {
   202  		t.Fatalf("Expected %v, but %v: %v", nil, err, string(b))
   203  	}
   204  	return b
   205  }