Skip to content
This repository has been archived by the owner on Sep 9, 2020. It is now read-only.

Commit

Permalink
Merge pull request #535 from jmank88/status_table
Browse files Browse the repository at this point in the history
buffer status output - fixes #532
  • Loading branch information
sdboyer committed May 11, 2017
2 parents 30cce2a + 05893d5 commit 070b761
Show file tree
Hide file tree
Showing 26 changed files with 186 additions and 22 deletions.
3 changes: 3 additions & 0 deletions cmd/dep/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
25 changes: 8 additions & 17 deletions cmd/dep/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"flag"
"fmt"
"io"
"log"
"sort"
"text/tabwriter"

Expand Down Expand Up @@ -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
Expand Down
13 changes: 8 additions & 5 deletions cmd/dep/testdata/harness_tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ The directory structure is as follows:
subcategory1/
case1/
testcase.json
stdout.txt
initial/
file1.go
Gopkg.toml
Expand All @@ -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:

Expand Down Expand Up @@ -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.
9 changes: 9 additions & 0 deletions cmd/dep/testdata/harness_tests/status/case1/dot/stdout.txt
Original file line number Diff line number Diff line change
@@ -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;
}
10 changes: 10 additions & 0 deletions cmd/dep/testdata/harness_tests/status/case1/dot/testcase.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"commands": [
["ensure"],
["status","-dot"]
],
"vendor-final": [
"github.com/sdboyer/deptest",
"github.com/sdboyer/deptestdos"
]
}
13 changes: 13 additions & 0 deletions cmd/dep/testdata/harness_tests/status/case1/json/final/Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[[dependencies]]
name = "github.com/sdboyer/deptest"
version = "^0.8.0"

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[[dependencies]]
name = "github.com/sdboyer/deptest"
version = "^0.8.0"
18 changes: 18 additions & 0 deletions cmd/dep/testdata/harness_tests/status/case1/json/initial/main.go
Original file line number Diff line number Diff line change
@@ -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")
}
Original file line number Diff line number Diff line change
@@ -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}]
10 changes: 10 additions & 0 deletions cmd/dep/testdata/harness_tests/status/case1/json/testcase.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"commands": [
["ensure"],
["status","-json"]
],
"vendor-final": [
"github.com/sdboyer/deptest",
"github.com/sdboyer/deptestdos"
]
}
13 changes: 13 additions & 0 deletions cmd/dep/testdata/harness_tests/status/case1/table/final/Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[[dependencies]]
name = "github.com/sdboyer/deptest"
version = "^0.8.0"

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[[dependencies]]
name = "github.com/sdboyer/deptest"
version = "^0.8.0"
18 changes: 18 additions & 0 deletions cmd/dep/testdata/harness_tests/status/case1/table/initial/main.go
Original file line number Diff line number Diff line change
@@ -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")
}
3 changes: 3 additions & 0 deletions cmd/dep/testdata/harness_tests/status/case1/table/stdout.txt
Original file line number Diff line number Diff line change
@@ -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
29 changes: 29 additions & 0 deletions internal/test/integration_testcase.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"regexp"
"strings"
"testing"
"unicode"
)

var (
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions internal/test/integration_testproj.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 070b761

Please sign in to comment.