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

Commit c29d650

Browse files
authored
Merge pull request #402 from domgreen/test_harness_errors
Test harness can now assert if errors have been raised correctly
2 parents b4f12ee + 2bca729 commit c29d650

File tree

8 files changed

+49
-9
lines changed

8 files changed

+49
-9
lines changed

cmd/dep/init.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
7474
return err
7575
}
7676
if mok {
77-
return errors.Errorf("manifest file %q already exists", mf)
77+
return errors.Errorf("manifest already exists: %s", mf)
7878
}
7979
// Manifest file does not exist.
8080

cmd/dep/integration_test.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ func TestIntegration(t *testing.T) {
1919
test.NeedsGit(t)
2020

2121
filepath.Walk(filepath.Join("testdata", "harness_tests"), func(path string, info os.FileInfo, err error) error {
22+
if err != nil {
23+
t.Fatal("error walking filepath")
24+
}
25+
2226
wd, err := os.Getwd()
2327
if err != nil {
2428
panic(err)
@@ -51,13 +55,18 @@ func TestIntegration(t *testing.T) {
5155

5256
// Run commands
5357
testProj.RecordImportPaths()
54-
for _, args := range testCase.Commands {
58+
59+
var err error
60+
for i, args := range testCase.Commands {
5561
err = testProj.DoRun(args)
56-
if err != nil {
57-
t.Fatalf("%v", err)
62+
if err != nil && i < len(testCase.Commands)-1 {
63+
t.Fatalf("cmd %s raised an unexpected error: %s", args[0], err.Error())
5864
}
5965
}
6066

67+
// Check error raised in final command
68+
testCase.CompareError(err, testProj.GetStderr())
69+
6170
// Check final manifest and lock
6271
testCase.CompareFile(dep.ManifestName, testProj.ProjPath(dep.ManifestName))
6372
testCase.CompareFile(dep.LockName, testProj.ProjPath(dep.LockName))

cmd/dep/testdata/harness_tests/README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ The `testcase.json` file has the following format:
5858
"github.com/sdboyer/deptestdos",
5959
"github.com/sdboyer/deptesttres",
6060
"github.com/sdboyer/deptestquatro"
61-
]
61+
],
62+
"error-expected": "something went wrong"
6263
}
6364

6465
All of the categories are optional - if the `imports` list for a test is empty,
@@ -72,9 +73,10 @@ The test procedure is as follows:
7273
4. Fetch the repos and versions in `gopath-initial` into `$TMPDIR/src` directory
7374
5. Fetch the repos and versions in `vendor-initial` to the project's `vendor` directory
7475
6. Run `commands` on the project, in declaration order
75-
7. Check the resulting files against those in the `final` input directory
76-
8. Check the `vendor` directory for the projects listed under `vendor-final`
77-
9. Check that there were no changes to `src` listings
78-
10. Clean up
76+
7. Ensure that, if any errors are raised, it is only by the final command and their string output matches `error-expected`
77+
8. Check the resulting files against those in the `final` input directory
78+
9. Check the `vendor` directory for the projects listed under `vendor-final`
79+
10. Check that there were no changes to `src` listings
80+
11. Clean up
7981

8082
Note that for the remote fetches, only git repos are currently supported.

cmd/dep/testdata/harness_tests/init/manifest-exists/final/Gopkg.toml

Whitespace-only changes.

cmd/dep/testdata/harness_tests/init/manifest-exists/initial/Gopkg.toml

Whitespace-only changes.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"commands": [
3+
["init"]
4+
],
5+
"error-expected" : "manifest already exists:"
6+
}

test/integration_testcase.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"os"
1313
"path/filepath"
1414
"regexp"
15+
"strings"
1516
"testing"
1617
)
1718

@@ -27,6 +28,7 @@ type IntegrationTestCase struct {
2728
initialPath string
2829
finalPath string
2930
Commands [][]string `json:"commands"`
31+
ErrorExpected string `json:"error-expected"`
3032
GopathInitial map[string]string `json:"gopath-initial"`
3133
VendorInitial map[string]string `json:"vendor-initial"`
3234
VendorFinal []string `json:"vendor-final"`
@@ -126,6 +128,22 @@ func (tc *IntegrationTestCase) CompareFile(goldenPath, working string) {
126128
}
127129
}
128130

131+
// CompareError compares exected and actual error
132+
func (tc *IntegrationTestCase) CompareError(err error, stderr string) {
133+
wantExists, want := tc.ErrorExpected != "", tc.ErrorExpected
134+
gotExists, got := stderr != "" && err != nil, stderr
135+
136+
if wantExists && gotExists {
137+
if !strings.Contains(got, want) {
138+
tc.t.Errorf("expected error containing %s, got error %s", want, got)
139+
}
140+
} else if !wantExists && gotExists {
141+
tc.t.Fatal("error raised where none was expected")
142+
} else if wantExists && !gotExists {
143+
tc.t.Error("error not raised where one was expected")
144+
}
145+
}
146+
129147
func (tc *IntegrationTestCase) CompareVendorPaths(gotVendorPaths []string) {
130148
if *UpdateGolden {
131149
tc.VendorFinal = gotVendorPaths

test/integration_testproj.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,11 @@ func (p *IntegrationTestProject) RunGit(dir string, args ...string) {
139139
}
140140
}
141141

142+
// GetStderr gets the Stderr output from test run
143+
func (p *IntegrationTestProject) GetStderr() string {
144+
return p.stderr.String()
145+
}
146+
142147
func (p *IntegrationTestProject) GetVendorGit(ip string) {
143148
parse := strings.Split(ip, "/")
144149
gitDir := strings.Join(parse[:len(parse)-1], string(filepath.Separator))

0 commit comments

Comments
 (0)