diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..59f0abe --- /dev/null +++ b/.travis.yml @@ -0,0 +1,9 @@ +language: go +sudo: false +go: + - tip +before_install: + - go get github.com/mattn/goveralls +script: + - ./gen-coverage.sh coverage.out + - $HOME/gopath/bin/goveralls -coverprofile=coverage.out -service=travis-ci diff --git a/README.md b/README.md index 0ca9e57..79a0af5 100644 --- a/README.md +++ b/README.md @@ -3,11 +3,11 @@ Instructions Install the package with: - go get gopkg.in/check.v1 - + go get github.com/elopio/check + Import it with: - import "gopkg.in/check.v1" + import "github.com/elopio/check" and use _check_ as the package name inside the code. diff --git a/benchmark_test.go b/benchmark_test.go index 4dd827c..7fa8fea 100644 --- a/benchmark_test.go +++ b/benchmark_test.go @@ -4,7 +4,8 @@ package check_test import ( "time" - . "gopkg.in/check.v1" + + . "github.com/elopio/check" ) var benchmarkS = Suite(&BenchmarkS{}) diff --git a/bootstrap_test.go b/bootstrap_test.go index e55f327..4c7bb4e 100644 --- a/bootstrap_test.go +++ b/bootstrap_test.go @@ -14,8 +14,9 @@ package check_test import ( "fmt" - "gopkg.in/check.v1" "strings" + + "github.com/elopio/check" ) type BootstrapS struct{} diff --git a/check.go b/check.go index 82c26fa..a8c8d4d 100644 --- a/check.go +++ b/check.go @@ -84,7 +84,6 @@ type C struct { testName string _status funcStatus logb *logger - logw io.Writer done chan *C reason string mustFail bool @@ -109,25 +108,30 @@ func (c *C) stopNow() { // logger is a concurrency safe byte.Buffer type logger struct { sync.Mutex - writer bytes.Buffer + buffer bytes.Buffer + output io.Writer + verbosity uint8 } func (l *logger) Write(buf []byte) (int, error) { l.Lock() defer l.Unlock() - return l.writer.Write(buf) + if l.verbosity > 1 { + l.output.Write(buf) + } + return l.buffer.Write(buf) } func (l *logger) WriteTo(w io.Writer) (int64, error) { l.Lock() defer l.Unlock() - return l.writer.WriteTo(w) + return l.buffer.WriteTo(w) } func (l *logger) String() string { l.Lock() defer l.Unlock() - return l.writer.String() + return l.buffer.String() } // ----------------------------------------------------------------------- @@ -198,9 +202,6 @@ func (c *C) logNewLine() { func (c *C) writeLog(buf []byte) { c.logb.Write(buf) - if c.logw != nil { - c.logw.Write(buf) - } } func hasStringOrError(x interface{}) (ok bool) { @@ -518,10 +519,12 @@ type suiteRunner struct { tracker *resultTracker tempDir *tempDir keepDir bool + logOutput io.Writer output *outputWriter reportedProblemLast bool benchTime time.Duration benchMem bool + verbosity uint8 } type RunConf struct { @@ -551,16 +554,25 @@ func newSuiteRunner(suite interface{}, runConf *RunConf) *suiteRunner { suiteType := reflect.TypeOf(suite) suiteNumMethods := suiteType.NumMethod() suiteValue := reflect.ValueOf(suite) - + var verbosity uint8 + if conf.Verbose { + verbosity = 1 + } + if conf.Stream { + verbosity = 2 + } + runner := &suiteRunner{ suite: suite, - output: newOutputWriter(conf.Output, conf.Stream, conf.Verbose), + logOutput: conf.Output, + output: newOutputWriter(conf.Output, verbosity), tracker: newResultTracker(), benchTime: conf.BenchmarkTime, benchMem: conf.BenchmarkMem, tempDir: &tempDir{}, keepDir: conf.KeepWorkDir, tests: make([]*methodType, 0, suiteNumMethods), + verbosity: verbosity, } if runner.benchTime == 0 { runner.benchTime = 1 * time.Second @@ -640,19 +652,17 @@ func (runner *suiteRunner) run() *Result { // Create a call object with the given suite method, and fork a // goroutine with the provided dispatcher for running it. func (runner *suiteRunner) forkCall(method *methodType, kind funcKind, testName string, logb *logger, dispatcher func(c *C)) *C { - var logw io.Writer - if runner.output.Stream { - logw = runner.output - } if logb == nil { - logb = new(logger) + logb = &logger{ + output: runner.logOutput, + verbosity: runner.verbosity, + } } c := &C{ method: method, kind: kind, testName: testName, logb: logb, - logw: logw, tempDir: runner.tempDir, done: make(chan *C, 1), timer: timer{benchTime: runner.benchTime}, diff --git a/check_test.go b/check_test.go index 871b325..7b5eac8 100644 --- a/check_test.go +++ b/check_test.go @@ -12,7 +12,7 @@ import ( "testing" "time" - "gopkg.in/check.v1" + . "github.com/elopio/check" ) // We count the number of suites run at least to get a vague hint that the @@ -23,7 +23,7 @@ const suitesRunExpected = 8 var suitesRun int = 0 func Test(t *testing.T) { - check.TestingT(t) + TestingT(t) if suitesRun != suitesRunExpected && flag.Lookup("check.f").Value.String() == "" { critical(fmt.Sprintf("Expected %d suites to run rather than %d", suitesRunExpected, suitesRun)) @@ -65,8 +65,8 @@ func (s *String) Write(p []byte) (n int, err error) { // Trivial wrapper to test errors happening on a different file // than the test itself. -func checkEqualWrapper(c *check.C, obtained, expected interface{}) (result bool, line int) { - return c.Check(obtained, check.Equals, expected), getMyLine() +func checkEqualWrapper(c *C, obtained, expected interface{}) (result bool, line int) { + return c.Check(obtained, Equals, expected), getMyLine() } // ----------------------------------------------------------------------- @@ -76,7 +76,7 @@ type FailHelper struct { testLine int } -func (s *FailHelper) TestLogAndFail(c *check.C) { +func (s *FailHelper) TestLogAndFail(c *C) { s.testLine = getMyLine() - 1 c.Log("Expected failure!") c.Fail() @@ -87,7 +87,7 @@ func (s *FailHelper) TestLogAndFail(c *check.C) { type SuccessHelper struct{} -func (s *SuccessHelper) TestLogAndSucceed(c *check.C) { +func (s *SuccessHelper) TestLogAndSucceed(c *C) { c.Log("Expected success!") } @@ -104,7 +104,7 @@ type FixtureHelper struct { bytes int64 } -func (s *FixtureHelper) trace(name string, c *check.C) { +func (s *FixtureHelper) trace(name string, c *C) { s.calls = append(s.calls, name) if name == s.panicOn { panic(name) @@ -117,38 +117,38 @@ func (s *FixtureHelper) trace(name string, c *check.C) { } } -func (s *FixtureHelper) SetUpSuite(c *check.C) { +func (s *FixtureHelper) SetUpSuite(c *C) { s.trace("SetUpSuite", c) } -func (s *FixtureHelper) TearDownSuite(c *check.C) { +func (s *FixtureHelper) TearDownSuite(c *C) { s.trace("TearDownSuite", c) } -func (s *FixtureHelper) SetUpTest(c *check.C) { +func (s *FixtureHelper) SetUpTest(c *C) { s.trace("SetUpTest", c) } -func (s *FixtureHelper) TearDownTest(c *check.C) { +func (s *FixtureHelper) TearDownTest(c *C) { s.trace("TearDownTest", c) } -func (s *FixtureHelper) Test1(c *check.C) { +func (s *FixtureHelper) Test1(c *C) { s.trace("Test1", c) } -func (s *FixtureHelper) Test2(c *check.C) { +func (s *FixtureHelper) Test2(c *C) { s.trace("Test2", c) } -func (s *FixtureHelper) Benchmark1(c *check.C) { +func (s *FixtureHelper) Benchmark1(c *C) { s.trace("Benchmark1", c) for i := 0; i < c.N; i++ { time.Sleep(s.sleep) } } -func (s *FixtureHelper) Benchmark2(c *check.C) { +func (s *FixtureHelper) Benchmark2(c *C) { s.trace("Benchmark2", c) c.SetBytes(1024) for i := 0; i < c.N; i++ { @@ -156,7 +156,7 @@ func (s *FixtureHelper) Benchmark2(c *check.C) { } } -func (s *FixtureHelper) Benchmark3(c *check.C) { +func (s *FixtureHelper) Benchmark3(c *C) { var x []int64 s.trace("Benchmark3", c) for i := 0; i < c.N; i++ { @@ -181,7 +181,7 @@ type expectedState struct { // Verify the state of the test. Note that since this also verifies if // the test is supposed to be in a failed state, no other checks should // be done in addition to what is being tested. -func checkState(c *check.C, result interface{}, expected *expectedState) { +func checkState(c *C, result interface{}, expected *expectedState) { failed := c.Failed() c.Succeed() log := c.GetTestLog() diff --git a/checkers_test.go b/checkers_test.go index 5c69747..120b2dd 100644 --- a/checkers_test.go +++ b/checkers_test.go @@ -2,9 +2,10 @@ package check_test import ( "errors" - "gopkg.in/check.v1" "reflect" "runtime" + + "github.com/elopio/check" ) type CheckersS struct{} diff --git a/export_test.go b/export_test.go index abb89a2..cf51a99 100644 --- a/export_test.go +++ b/export_test.go @@ -10,8 +10,8 @@ func Indent(s, with string) string { return indent(s, with) } -func NewOutputWriter(writer io.Writer, stream, verbose bool) *outputWriter { - return newOutputWriter(writer, stream, verbose) +func NewOutputWriter(writer io.Writer, verbosity uint8) *outputWriter { + return newOutputWriter(writer, verbosity) } func (c *C) FakeSkip(reason string) { diff --git a/fixture_test.go b/fixture_test.go index 2bff9e1..134e543 100644 --- a/fixture_test.go +++ b/fixture_test.go @@ -2,9 +2,7 @@ package check_test -import ( - . "gopkg.in/check.v1" -) +import . "github.com/elopio/check" // ----------------------------------------------------------------------- // Fixture test suite. @@ -14,7 +12,7 @@ type FixtureS struct{} var fixtureS = Suite(&FixtureS{}) func (s *FixtureS) TestCountSuite(c *C) { - suitesRun += 1 + suitesRun++ } // ----------------------------------------------------------------------- diff --git a/foundation_test.go b/foundation_test.go index 8ecf791..bc019bc 100644 --- a/foundation_test.go +++ b/foundation_test.go @@ -8,11 +8,12 @@ package check_test import ( "fmt" - "gopkg.in/check.v1" "log" "os" "regexp" "strings" + + "github.com/elopio/check" ) // ----------------------------------------------------------------------- @@ -175,7 +176,7 @@ func (s *FoundationS) TestCallerLoggingInDifferentFile(c *check.C) { "foundation_test.go:%d:\n"+ " result, line := checkEqualWrapper\\(c, 10, 20\\)\n"+ "check_test.go:%d:\n"+ - " return c.Check\\(obtained, check.Equals, expected\\), getMyLine\\(\\)\n"+ + " return c.Check\\(obtained, Equals, expected\\), getMyLine\\(\\)\n"+ "\\.\\.\\. obtained int = 10\n"+ "\\.\\.\\. expected int = 20\n\n", testLine, line) diff --git a/gen-coverage.sh b/gen-coverage.sh new file mode 100755 index 0000000..75147aa --- /dev/null +++ b/gen-coverage.sh @@ -0,0 +1,17 @@ +#!/bin/sh +out_file=$1 + +append_coverage() { + local profile="$1" + if [ -f $profile ]; then + cat $profile | grep -v "mode: count" >> "$out_file" + rm $profile + fi +} + +echo "mode: count" > "$out_file" + +for pkg in $(go list ./...); do + go test -covermode=count -coverprofile=profile.out "$pkg" + append_coverage profile.out +done diff --git a/helpers_test.go b/helpers_test.go index 4baa656..52247ef 100644 --- a/helpers_test.go +++ b/helpers_test.go @@ -4,11 +4,12 @@ package check_test import ( - "gopkg.in/check.v1" "os" "reflect" "runtime" "sync" + + "github.com/elopio/check" ) var helpersS = check.Suite(&HelpersS{}) diff --git a/printer_test.go b/printer_test.go index 538b2d5..b02fa51 100644 --- a/printer_test.go +++ b/printer_test.go @@ -1,7 +1,7 @@ package check_test import ( - . "gopkg.in/check.v1" + . "github.com/elopio/check" ) var _ = Suite(&PrinterS{}) diff --git a/reporter.go b/reporter.go index fb04f76..f963659 100644 --- a/reporter.go +++ b/reporter.go @@ -13,23 +13,15 @@ type outputWriter struct { m sync.Mutex writer io.Writer wroteCallProblemLast bool - Stream bool - Verbose bool + verbosity uint8 } -func newOutputWriter(writer io.Writer, stream, verbose bool) *outputWriter { - return &outputWriter{writer: writer, Stream: stream, Verbose: verbose} -} - -func (ow *outputWriter) Write(content []byte) (n int, err error) { - ow.m.Lock() - n, err = ow.writer.Write(content) - ow.m.Unlock() - return +func newOutputWriter(writer io.Writer, verbosity uint8) *outputWriter { + return &outputWriter{writer: writer, verbosity: verbosity} } func (ow *outputWriter) WriteCallStarted(label string, c *C) { - if ow.Stream { + if ow.verbosity > 1 { header := renderCallHeader(label, c, "", "\n") ow.m.Lock() ow.writer.Write([]byte(header)) @@ -39,7 +31,7 @@ func (ow *outputWriter) WriteCallStarted(label string, c *C) { func (ow *outputWriter) WriteCallProblem(label string, c *C) { var prefix string - if !ow.Stream { + if ow.verbosity < 2 { prefix = "\n-----------------------------------" + "-----------------------------------\n" } @@ -47,14 +39,14 @@ func (ow *outputWriter) WriteCallProblem(label string, c *C) { ow.m.Lock() ow.wroteCallProblemLast = true ow.writer.Write([]byte(header)) - if !ow.Stream { + if ow.verbosity < 2 { c.logb.WriteTo(ow.writer) } ow.m.Unlock() } func (ow *outputWriter) WriteCallSuccess(label string, c *C) { - if ow.Stream || (ow.Verbose && c.kind == testKd) { + if ow.verbosity > 1 || (ow.verbosity == 1 && c.kind == testKd) { // TODO Use a buffer here. var suffix string if c.reason != "" { @@ -64,13 +56,13 @@ func (ow *outputWriter) WriteCallSuccess(label string, c *C) { suffix += "\t" + c.timerString() } suffix += "\n" - if ow.Stream { + if ow.verbosity > 1 { suffix += "\n" } header := renderCallHeader(label, c, "", suffix) ow.m.Lock() // Resist temptation of using line as prefix above due to race. - if !ow.Stream && ow.wroteCallProblemLast { + if ow.verbosity < 2 && ow.wroteCallProblemLast { header = "\n-----------------------------------" + "-----------------------------------\n" + header diff --git a/reporter_test.go b/reporter_test.go index 0b7ed76..11892aa 100644 --- a/reporter_test.go +++ b/reporter_test.go @@ -5,7 +5,7 @@ import ( "path/filepath" "runtime" - . "gopkg.in/check.v1" + . "github.com/elopio/check" ) var _ = Suite(&reporterS{}) @@ -20,25 +20,12 @@ func (s *reporterS) SetUpSuite(c *C) { s.testFile = filepath.Base(fileName) } -func (s *reporterS) TestWrite(c *C) { - testString := "test string" - output := String{} - - dummyStream := true - dummyVerbose := true - o := NewOutputWriter(&output, dummyStream, dummyVerbose) - - o.Write([]byte(testString)) - c.Assert(output.value, Equals, testString) -} - func (s *reporterS) TestWriteCallStartedWithStreamFlag(c *C) { testLabel := "test started label" - stream := true + var verbosity uint8 = 2 output := String{} - dummyVerbose := true - o := NewOutputWriter(&output, stream, dummyVerbose) + o := NewOutputWriter(&output, verbosity) o.WriteCallStarted(testLabel, c) expected := fmt.Sprintf("%s: %s:\\d+: %s\n", testLabel, s.testFile, c.TestName()) @@ -46,12 +33,11 @@ func (s *reporterS) TestWriteCallStartedWithStreamFlag(c *C) { } func (s *reporterS) TestWriteCallStartedWithoutStreamFlag(c *C) { - stream := false + var verbosity uint8 = 1 output := String{} dummyLabel := "dummy" - dummyVerbose := true - o := NewOutputWriter(&output, stream, dummyVerbose) + o := NewOutputWriter(&output, verbosity) o.WriteCallStarted(dummyLabel, c) c.Assert(output.value, Equals, "") @@ -59,11 +45,10 @@ func (s *reporterS) TestWriteCallStartedWithoutStreamFlag(c *C) { func (s *reporterS) TestWriteCallProblemWithStreamFlag(c *C) { testLabel := "test problem label" - stream := true + var verbosity uint8 = 2 output := String{} - dummyVerbose := true - o := NewOutputWriter(&output, stream, dummyVerbose) + o := NewOutputWriter(&output, verbosity) o.WriteCallProblem(testLabel, c) expected := fmt.Sprintf("%s: %s:\\d+: %s\n\n", testLabel, s.testFile, c.TestName()) @@ -72,11 +57,10 @@ func (s *reporterS) TestWriteCallProblemWithStreamFlag(c *C) { func (s *reporterS) TestWriteCallProblemWithoutStreamFlag(c *C) { testLabel := "test problem label" - stream := false + var verbosity uint8 = 1 output := String{} - dummyVerbose := true - o := NewOutputWriter(&output, stream, dummyVerbose) + o := NewOutputWriter(&output, verbosity) o.WriteCallProblem(testLabel, c) expected := fmt.Sprintf(""+ @@ -89,11 +73,10 @@ func (s *reporterS) TestWriteCallProblemWithoutStreamFlag(c *C) { func (s *reporterS) TestWriteCallProblemWithoutStreamFlagWithLog(c *C) { testLabel := "test problem label" testLog := "test log" - stream := false + var verbosity uint8 = 1 output := String{} - dummyVerbose := true - o := NewOutputWriter(&output, stream, dummyVerbose) + o := NewOutputWriter(&output, verbosity) c.Log(testLog) o.WriteCallProblem(testLabel, c) @@ -106,11 +89,10 @@ func (s *reporterS) TestWriteCallProblemWithoutStreamFlagWithLog(c *C) { func (s *reporterS) TestWriteCallSuccessWithStreamFlag(c *C) { testLabel := "test success label" - stream := true + var verbosity uint8 = 2 output := String{} - dummyVerbose := true - o := NewOutputWriter(&output, stream, dummyVerbose) + o := NewOutputWriter(&output, verbosity) o.WriteCallSuccess(testLabel, c) expected := fmt.Sprintf("%s: %s:\\d+: %s\t\\d\\.\\d+s\n\n", testLabel, s.testFile, c.TestName()) @@ -120,11 +102,10 @@ func (s *reporterS) TestWriteCallSuccessWithStreamFlag(c *C) { func (s *reporterS) TestWriteCallSuccessWithStreamFlagAndReason(c *C) { testLabel := "test success label" testReason := "test skip reason" - stream := true + var verbosity uint8 = 2 output := String{} - dummyVerbose := true - o := NewOutputWriter(&output, stream, dummyVerbose) + o := NewOutputWriter(&output, verbosity) c.FakeSkip(testReason) o.WriteCallSuccess(testLabel, c) @@ -135,11 +116,10 @@ func (s *reporterS) TestWriteCallSuccessWithStreamFlagAndReason(c *C) { func (s *reporterS) TestWriteCallSuccessWithoutStreamFlagWithVerboseFlag(c *C) { testLabel := "test success label" - stream := false - verbose := true + var verbosity uint8 = 1 output := String{} - o := NewOutputWriter(&output, stream, verbose) + o := NewOutputWriter(&output, verbosity) o.WriteCallSuccess(testLabel, c) expected := fmt.Sprintf("%s: %s:\\d+: %s\t\\d\\.\\d+s\n", testLabel, s.testFile, c.TestName()) @@ -148,11 +128,10 @@ func (s *reporterS) TestWriteCallSuccessWithoutStreamFlagWithVerboseFlag(c *C) { func (s *reporterS) TestWriteCallSuccessWithoutStreamFlagWithoutVerboseFlag(c *C) { testLabel := "test success label" - stream := false - verbose := false + var verbosity uint8 = 0 output := String{} - o := NewOutputWriter(&output, stream, verbose) + o := NewOutputWriter(&output, verbosity) o.WriteCallSuccess(testLabel, c) c.Assert(output.value, Equals, "") diff --git a/run_test.go b/run_test.go index f41fffc..2252698 100644 --- a/run_test.go +++ b/run_test.go @@ -4,9 +4,10 @@ package check_test import ( "errors" - . "gopkg.in/check.v1" "os" "sync" + + . "github.com/elopio/check" ) var runnerS = Suite(&RunS{}) @@ -400,7 +401,7 @@ func (s *RunS) TestStreamModeWithMiss(c *C) { // ----------------------------------------------------------------------- // Verify that that the keep work dir request indeed does so. -type WorkDirSuite struct {} +type WorkDirSuite struct{} func (s *WorkDirSuite) Test(c *C) { c.MkDir() @@ -411,7 +412,7 @@ func (s *RunS) TestKeepWorkDir(c *C) { runConf := RunConf{Output: &output, Verbose: true, KeepWorkDir: true} result := Run(&WorkDirSuite{}, &runConf) - c.Assert(result.String(), Matches, ".*\nWORK=" + result.WorkDir) + c.Assert(result.String(), Matches, ".*\nWORK="+result.WorkDir) stat, err := os.Stat(result.WorkDir) c.Assert(err, IsNil)