diff --git a/cmd/dep/integration_test.go b/cmd/dep/integration_test.go index cf83c9a7a1..b886596eb4 100644 --- a/cmd/dep/integration_test.go +++ b/cmd/dep/integration_test.go @@ -116,6 +116,9 @@ func testIntegration(name, wd string, externalProc bool, run test.RunFunc) func( // Check error raised in final command testCase.CompareError(err, testProj.GetStderr()) + // Check output + testCase.CompareOutput(testProj.GetStdout()) + // Check final manifest and lock testCase.CompareFile(dep.ManifestName, testProj.ProjPath(dep.ManifestName)) testCase.CompareFile(dep.LockName, testProj.ProjPath(dep.LockName)) diff --git a/cmd/dep/status.go b/cmd/dep/status.go index 36383113f8..d9f7306db0 100644 --- a/cmd/dep/status.go +++ b/cmd/dep/status.go @@ -10,7 +10,6 @@ import ( "flag" "fmt" "io" - "log" "sort" "text/tabwriter" @@ -194,41 +193,33 @@ func (cmd *statusCommand) Run(ctx *dep.Ctx, loggers *Loggers, args []string) err sm.UseDefaultSignalHandling() defer sm.Release() + var buf bytes.Buffer var out outputter switch { case cmd.detailed: return errors.Errorf("not implemented") case cmd.json: out = &jsonOutput{ - w: &logWriter{Logger: loggers.Out}, + w: &buf, } case cmd.dot: out = &dotOutput{ p: p, o: cmd.output, - w: &logWriter{Logger: loggers.Out}, + w: &buf, } default: out = &tableOutput{ - w: tabwriter.NewWriter(&logWriter{Logger: loggers.Out}, 0, 4, 2, ' ', 0), + w: tabwriter.NewWriter(&buf, 0, 4, 2, ' ', 0), } } - return runStatusAll(loggers, out, p, sm) -} - -// A logWriter adapts a log.Logger to the io.Writer interface. -type logWriter struct { - *log.Logger -} -func (l *logWriter) Write(b []byte) (n int, err error) { - str := string(b) - if len(str) == 0 { - return 0, nil + if err := runStatusAll(loggers, out, p, sm); err != nil { + return err } - l.Print(str) - return len(b), err + loggers.Out.Print(buf.String()) + return nil } // BasicStatus contains all the information reported about a single dependency diff --git a/cmd/dep/testdata/harness_tests/README.md b/cmd/dep/testdata/harness_tests/README.md index 41004b029f..fc678bd012 100644 --- a/cmd/dep/testdata/harness_tests/README.md +++ b/cmd/dep/testdata/harness_tests/README.md @@ -12,6 +12,7 @@ The directory structure is as follows: subcategory1/ case1/ testcase.json + stdout.txt initial/ file1.go Gopkg.toml @@ -36,7 +37,8 @@ will be included automatically. The json file needs to be accompanied by `initial` and `final` directories. The `initial` is copied verbatim into the test project before the `dep` commands are run, are the `manifest` and `lock` files in `final`, if present, are used to -compare against the test project results after the commands. +compare against the test project results after the commands. The `stdout.txt` file +is optional, and if present will be compared with command output. The `testcase.json` file has the following format: @@ -74,9 +76,10 @@ The test procedure is as follows: 5. Fetch the repos and versions in `vendor-initial` to the project's `vendor` directory 6. Run `commands` on the project, in declaration order 7. Ensure that, if any errors are raised, it is only by the final command and their string output matches `error-expected` -8. Check the resulting files against those in the `final` input directory -9. Check the `vendor` directory for the projects listed under `vendor-final` -10. Check that there were no changes to `src` listings -11. Clean up +8. Ensure that, if a stdout.txt file is present, the command's output matches (excluding trailing whitespace). +9. Check the resulting files against those in the `final` input directory +10. Check the `vendor` directory for the projects listed under `vendor-final` +11. Check that there were no changes to `src` listings +12. Clean up Note that for the remote fetches, only git repos are currently supported. diff --git a/cmd/dep/testdata/harness_tests/status/case1/final/Gopkg.lock b/cmd/dep/testdata/harness_tests/status/case1/dot/final/Gopkg.lock similarity index 100% rename from cmd/dep/testdata/harness_tests/status/case1/final/Gopkg.lock rename to cmd/dep/testdata/harness_tests/status/case1/dot/final/Gopkg.lock diff --git a/cmd/dep/testdata/harness_tests/status/case1/final/Gopkg.toml b/cmd/dep/testdata/harness_tests/status/case1/dot/final/Gopkg.toml similarity index 100% rename from cmd/dep/testdata/harness_tests/status/case1/final/Gopkg.toml rename to cmd/dep/testdata/harness_tests/status/case1/dot/final/Gopkg.toml diff --git a/cmd/dep/testdata/harness_tests/status/case1/initial/Gopkg.lock b/cmd/dep/testdata/harness_tests/status/case1/dot/initial/Gopkg.lock similarity index 100% rename from cmd/dep/testdata/harness_tests/status/case1/initial/Gopkg.lock rename to cmd/dep/testdata/harness_tests/status/case1/dot/initial/Gopkg.lock diff --git a/cmd/dep/testdata/harness_tests/status/case1/initial/Gopkg.toml b/cmd/dep/testdata/harness_tests/status/case1/dot/initial/Gopkg.toml similarity index 100% rename from cmd/dep/testdata/harness_tests/status/case1/initial/Gopkg.toml rename to cmd/dep/testdata/harness_tests/status/case1/dot/initial/Gopkg.toml diff --git a/cmd/dep/testdata/harness_tests/status/case1/initial/main.go b/cmd/dep/testdata/harness_tests/status/case1/dot/initial/main.go similarity index 100% rename from cmd/dep/testdata/harness_tests/status/case1/initial/main.go rename to cmd/dep/testdata/harness_tests/status/case1/dot/initial/main.go diff --git a/cmd/dep/testdata/harness_tests/status/case1/dot/stdout.txt b/cmd/dep/testdata/harness_tests/status/case1/dot/stdout.txt new file mode 100644 index 0000000000..2dbc7914da --- /dev/null +++ b/cmd/dep/testdata/harness_tests/status/case1/dot/stdout.txt @@ -0,0 +1,9 @@ +digraph { + node [shape=box]; + 388407825 [label="github.com/golang/notexist"]; + 2304687900 [label="github.com/sdboyer/deptest\nv0.8.0"]; + 2659405890 [label="github.com/sdboyer/deptestdos\nv2.0.0"]; + 388407825 -> 2304687900; + 388407825 -> 2659405890; + 2659405890 -> 2304687900; +} diff --git a/cmd/dep/testdata/harness_tests/status/case1/dot/testcase.json b/cmd/dep/testdata/harness_tests/status/case1/dot/testcase.json new file mode 100644 index 0000000000..8295f9bed1 --- /dev/null +++ b/cmd/dep/testdata/harness_tests/status/case1/dot/testcase.json @@ -0,0 +1,10 @@ +{ + "commands": [ + ["ensure"], + ["status","-dot"] + ], + "vendor-final": [ + "github.com/sdboyer/deptest", + "github.com/sdboyer/deptestdos" + ] +} diff --git a/cmd/dep/testdata/harness_tests/status/case1/json/final/Gopkg.lock b/cmd/dep/testdata/harness_tests/status/case1/json/final/Gopkg.lock new file mode 100644 index 0000000000..9b7e4cbf29 --- /dev/null +++ b/cmd/dep/testdata/harness_tests/status/case1/json/final/Gopkg.lock @@ -0,0 +1,13 @@ +memo = "88d2718cda70cce45158f953d2c6ead79c1db38e67e9704aff72be8fddb096e7" + +[[projects]] + name = "github.com/sdboyer/deptest" + packages = ["."] + revision = "ff2948a2ac8f538c4ecd55962e919d1e13e74baf" + version = "v0.8.0" + +[[projects]] + name = "github.com/sdboyer/deptestdos" + packages = ["."] + revision = "5c607206be5decd28e6263ffffdcee067266015e" + version = "v2.0.0" diff --git a/cmd/dep/testdata/harness_tests/status/case1/json/final/Gopkg.toml b/cmd/dep/testdata/harness_tests/status/case1/json/final/Gopkg.toml new file mode 100644 index 0000000000..122d0340fd --- /dev/null +++ b/cmd/dep/testdata/harness_tests/status/case1/json/final/Gopkg.toml @@ -0,0 +1,3 @@ +[[dependencies]] + name = "github.com/sdboyer/deptest" + version = "^0.8.0" diff --git a/cmd/dep/testdata/harness_tests/status/case1/json/initial/Gopkg.lock b/cmd/dep/testdata/harness_tests/status/case1/json/initial/Gopkg.lock new file mode 100644 index 0000000000..04268862a1 --- /dev/null +++ b/cmd/dep/testdata/harness_tests/status/case1/json/initial/Gopkg.lock @@ -0,0 +1,13 @@ +memo = "9a5243dd3fa20feeaa20398e7283d6c566532e2af1aae279a010df34793761c5" + +[[projects]] + name = "github.com/sdboyer/deptest" + version = "v0.8.0" + revision = "ff2948a2ac8f538c4ecd55962e919d1e13e74baf" + packages = ["."] + +[[projects]] + name = "github.com/sdboyer/deptestdos" + version = "v2.0.0" + revision = "5c607206be5decd28e6263ffffdcee067266015e" + packages = ["."] diff --git a/cmd/dep/testdata/harness_tests/status/case1/json/initial/Gopkg.toml b/cmd/dep/testdata/harness_tests/status/case1/json/initial/Gopkg.toml new file mode 100644 index 0000000000..122d0340fd --- /dev/null +++ b/cmd/dep/testdata/harness_tests/status/case1/json/initial/Gopkg.toml @@ -0,0 +1,3 @@ +[[dependencies]] + name = "github.com/sdboyer/deptest" + version = "^0.8.0" diff --git a/cmd/dep/testdata/harness_tests/status/case1/json/initial/main.go b/cmd/dep/testdata/harness_tests/status/case1/json/initial/main.go new file mode 100644 index 0000000000..2eae5b511d --- /dev/null +++ b/cmd/dep/testdata/harness_tests/status/case1/json/initial/main.go @@ -0,0 +1,18 @@ +// Copyright 2016 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package main + +import ( + "github.com/sdboyer/deptest" + "github.com/sdboyer/deptestdos" +) + +func main() { + err := nil + if err != nil { + deptest.Map["yo yo!"] + } + deptestdos.diMeLo("whatev") +} diff --git a/cmd/dep/testdata/harness_tests/status/case1/json/stdout.txt b/cmd/dep/testdata/harness_tests/status/case1/json/stdout.txt new file mode 100644 index 0000000000..a7fd40c5be --- /dev/null +++ b/cmd/dep/testdata/harness_tests/status/case1/json/stdout.txt @@ -0,0 +1 @@ +[{"ProjectRoot":"github.com/sdboyer/deptest","Children":null,"Constraint":{},"Version":{},"Revision":"ff2948a2ac8f538c4ecd55962e919d1e13e74baf","Latest":"3f4c3bea144e112a69bbe5d8d01c1b09a544253f","PackageCount":1},{"ProjectRoot":"github.com/sdboyer/deptestdos","Children":null,"Constraint":{},"Version":{},"Revision":"5c607206be5decd28e6263ffffdcee067266015e","Latest":"5c607206be5decd28e6263ffffdcee067266015e","PackageCount":1}] diff --git a/cmd/dep/testdata/harness_tests/status/case1/json/testcase.json b/cmd/dep/testdata/harness_tests/status/case1/json/testcase.json new file mode 100644 index 0000000000..2444d2888c --- /dev/null +++ b/cmd/dep/testdata/harness_tests/status/case1/json/testcase.json @@ -0,0 +1,10 @@ +{ + "commands": [ + ["ensure"], + ["status","-json"] + ], + "vendor-final": [ + "github.com/sdboyer/deptest", + "github.com/sdboyer/deptestdos" + ] +} diff --git a/cmd/dep/testdata/harness_tests/status/case1/table/final/Gopkg.lock b/cmd/dep/testdata/harness_tests/status/case1/table/final/Gopkg.lock new file mode 100644 index 0000000000..9b7e4cbf29 --- /dev/null +++ b/cmd/dep/testdata/harness_tests/status/case1/table/final/Gopkg.lock @@ -0,0 +1,13 @@ +memo = "88d2718cda70cce45158f953d2c6ead79c1db38e67e9704aff72be8fddb096e7" + +[[projects]] + name = "github.com/sdboyer/deptest" + packages = ["."] + revision = "ff2948a2ac8f538c4ecd55962e919d1e13e74baf" + version = "v0.8.0" + +[[projects]] + name = "github.com/sdboyer/deptestdos" + packages = ["."] + revision = "5c607206be5decd28e6263ffffdcee067266015e" + version = "v2.0.0" diff --git a/cmd/dep/testdata/harness_tests/status/case1/table/final/Gopkg.toml b/cmd/dep/testdata/harness_tests/status/case1/table/final/Gopkg.toml new file mode 100644 index 0000000000..122d0340fd --- /dev/null +++ b/cmd/dep/testdata/harness_tests/status/case1/table/final/Gopkg.toml @@ -0,0 +1,3 @@ +[[dependencies]] + name = "github.com/sdboyer/deptest" + version = "^0.8.0" diff --git a/cmd/dep/testdata/harness_tests/status/case1/table/initial/Gopkg.lock b/cmd/dep/testdata/harness_tests/status/case1/table/initial/Gopkg.lock new file mode 100644 index 0000000000..04268862a1 --- /dev/null +++ b/cmd/dep/testdata/harness_tests/status/case1/table/initial/Gopkg.lock @@ -0,0 +1,13 @@ +memo = "9a5243dd3fa20feeaa20398e7283d6c566532e2af1aae279a010df34793761c5" + +[[projects]] + name = "github.com/sdboyer/deptest" + version = "v0.8.0" + revision = "ff2948a2ac8f538c4ecd55962e919d1e13e74baf" + packages = ["."] + +[[projects]] + name = "github.com/sdboyer/deptestdos" + version = "v2.0.0" + revision = "5c607206be5decd28e6263ffffdcee067266015e" + packages = ["."] diff --git a/cmd/dep/testdata/harness_tests/status/case1/table/initial/Gopkg.toml b/cmd/dep/testdata/harness_tests/status/case1/table/initial/Gopkg.toml new file mode 100644 index 0000000000..122d0340fd --- /dev/null +++ b/cmd/dep/testdata/harness_tests/status/case1/table/initial/Gopkg.toml @@ -0,0 +1,3 @@ +[[dependencies]] + name = "github.com/sdboyer/deptest" + version = "^0.8.0" diff --git a/cmd/dep/testdata/harness_tests/status/case1/table/initial/main.go b/cmd/dep/testdata/harness_tests/status/case1/table/initial/main.go new file mode 100644 index 0000000000..2eae5b511d --- /dev/null +++ b/cmd/dep/testdata/harness_tests/status/case1/table/initial/main.go @@ -0,0 +1,18 @@ +// Copyright 2016 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package main + +import ( + "github.com/sdboyer/deptest" + "github.com/sdboyer/deptestdos" +) + +func main() { + err := nil + if err != nil { + deptest.Map["yo yo!"] + } + deptestdos.diMeLo("whatev") +} diff --git a/cmd/dep/testdata/harness_tests/status/case1/table/stdout.txt b/cmd/dep/testdata/harness_tests/status/case1/table/stdout.txt new file mode 100644 index 0000000000..f78a4eb4b1 --- /dev/null +++ b/cmd/dep/testdata/harness_tests/status/case1/table/stdout.txt @@ -0,0 +1,3 @@ +PROJECT CONSTRAINT VERSION REVISION LATEST PKGS USED +github.com/sdboyer/deptest >=0.8.0, <1.0.0 v0.8.0 ff2948a 3f4c3be 1 +github.com/sdboyer/deptestdos * v2.0.0 5c60720 5c60720 1 diff --git a/cmd/dep/testdata/harness_tests/status/case1/testcase.json b/cmd/dep/testdata/harness_tests/status/case1/table/testcase.json similarity index 100% rename from cmd/dep/testdata/harness_tests/status/case1/testcase.json rename to cmd/dep/testdata/harness_tests/status/case1/table/testcase.json diff --git a/internal/test/integration_testcase.go b/internal/test/integration_testcase.go index e784b1628d..241612b034 100644 --- a/internal/test/integration_testcase.go +++ b/internal/test/integration_testcase.go @@ -14,6 +14,7 @@ import ( "regexp" "strings" "testing" + "unicode" ) var ( @@ -128,6 +129,34 @@ func (tc *IntegrationTestCase) CompareFile(goldenPath, working string) { } } +// CompareError compares expected and actual stdout output +func (tc *IntegrationTestCase) CompareOutput(stdout string) { + expected, err := ioutil.ReadFile(filepath.Join(tc.rootPath, "stdout.txt")) + if err != nil { + if os.IsNotExist(err) { + // Nothing to verify + return + } + panic(err) + } + + expStr := normalizeLines(string(expected)) + stdout = normalizeLines(stdout) + + if expStr != stdout { + tc.t.Errorf("expected: %q but got: %q", expStr, stdout) + } +} + +// normalizeLines returns a version with trailing whitespace stripped from each line. +func normalizeLines(s string) string { + lines := strings.Split(s, "\n") + for i := range lines { + lines[i] = strings.TrimRightFunc(lines[i], unicode.IsSpace) + } + return strings.Join(lines, "\n") +} + // CompareError compares exected and actual error func (tc *IntegrationTestCase) CompareError(err error, stderr string) { wantExists, want := tc.ErrorExpected != "", tc.ErrorExpected diff --git a/internal/test/integration_testproj.go b/internal/test/integration_testproj.go index 018a05afff..85fbca8b5d 100644 --- a/internal/test/integration_testproj.go +++ b/internal/test/integration_testproj.go @@ -143,6 +143,11 @@ func (p *IntegrationTestProject) RunGit(dir string, args ...string) { } } +// GetStdout gets the Stdout output from test run +func (p *IntegrationTestProject) GetStdout() string { + return p.stdout.String() +} + // GetStderr gets the Stderr output from test run func (p *IntegrationTestProject) GetStderr() string { return p.stderr.String()