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 #1078 from carolynvs/gps-test-helpers
Browse files Browse the repository at this point in the history
Split test.Helper across unit and integration tests
  • Loading branch information
sdboyer authored Sep 2, 2017
2 parents 34835a5 + 3813baf commit 876083e
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 73 deletions.
7 changes: 4 additions & 3 deletions cmd/dep/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/golang/dep"
"github.com/golang/dep/internal/test"
"github.com/golang/dep/internal/test/integration"
)

func TestIntegration(t *testing.T) {
Expand Down Expand Up @@ -87,13 +88,13 @@ func runMain(prog string, args []string, stdout, stderr io.Writer, dir string, e
}

// testIntegration runs the test specified by <wd>/<relPath>/<name>/testcase.json
func testIntegration(name, relPath, wd string, externalProc bool, run test.RunFunc) func(t *testing.T) {
func testIntegration(name, relPath, wd string, externalProc bool, run integration.RunFunc) func(t *testing.T) {
return func(t *testing.T) {
t.Parallel()

// Set up environment
testCase := test.NewTestCase(t, filepath.Join(wd, relPath), name)
testProj := test.NewTestProject(t, testCase.InitialPath(), wd, externalProc, run)
testCase := integration.NewTestCase(t, filepath.Join(wd, relPath), name)
testProj := integration.NewTestProject(t, testCase.InitialPath(), wd, externalProc, run)
defer testProj.Cleanup()

// Create and checkout the vendor revisions
Expand Down
4 changes: 1 addition & 3 deletions internal/gps/vcs_version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ import (
)

func TestVCSVersion(t *testing.T) {
test.NeedsExternalNetwork(t)
test.NeedsGit(t)

h := test.NewHelper(t)
defer h.Cleanup()
requiresBins(t, "git")

h.TempDir("src")
gopath := h.Path(".")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,22 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package test
package integration

import (
"encoding/json"
"flag"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"unicode"
)

var (
UpdateGolden *bool = flag.Bool("update", false, "update golden files")
"github.com/golang/dep/internal/test"
)

// IntegrationTestCase manages a test case directory structure and content
type IntegrationTestCase struct {
// TestCase manages a test case directory structure and content
type TestCase struct {
t *testing.T
name string
rootPath string
Expand All @@ -34,10 +31,10 @@ type IntegrationTestCase struct {
InitPath string `json:"init-path"`
}

// NewTestCase creates a new IntegrationTestCase.
func NewTestCase(t *testing.T, dir, name string) *IntegrationTestCase {
// NewTestCase creates a new TestCase.
func NewTestCase(t *testing.T, dir, name string) *TestCase {
rootPath := filepath.FromSlash(filepath.Join(dir, name))
n := &IntegrationTestCase{
n := &TestCase{
t: t,
name: name,
rootPath: rootPath,
Expand All @@ -55,12 +52,13 @@ func NewTestCase(t *testing.T, dir, name string) *IntegrationTestCase {
return n
}

func (tc *IntegrationTestCase) InitialPath() string {
// InitialPath represents the initial set of files in a project.
func (tc *TestCase) InitialPath() string {
return tc.initialPath
}

// UpdateFile updates the golden file with the working result.
func (tc *IntegrationTestCase) UpdateFile(goldenPath, workingPath string) {
func (tc *TestCase) UpdateFile(goldenPath, workingPath string) {
exists, working, err := getFile(workingPath)
if err != nil {
tc.t.Fatalf("Error reading project file %s: %s", goldenPath, err)
Expand All @@ -80,7 +78,7 @@ func (tc *IntegrationTestCase) UpdateFile(goldenPath, workingPath string) {
}

// CompareFile compares the golden file with the working result.
func (tc *IntegrationTestCase) CompareFile(goldenPath, working string) {
func (tc *TestCase) CompareFile(goldenPath, working string) {
golden := filepath.Join(tc.finalPath, goldenPath)

gotExists, got, err := getFile(working)
Expand All @@ -103,8 +101,8 @@ func (tc *IntegrationTestCase) CompareFile(goldenPath, working string) {
}
}

// CompareError compares expected and actual stdout output
func (tc *IntegrationTestCase) CompareOutput(stdout string) {
// CompareOutput compares expected and actual stdout output.
func (tc *TestCase) CompareOutput(stdout string) {
expected, err := ioutil.ReadFile(filepath.Join(tc.rootPath, "stdout.txt"))
if err != nil {
if os.IsNotExist(err) {
Expand All @@ -131,8 +129,8 @@ func normalizeLines(s string) string {
return strings.Join(lines, "\n")
}

// CompareError compares exected and actual error
func (tc *IntegrationTestCase) CompareError(err error, stderr string) {
// CompareError compares expected and actual stderr output.
func (tc *TestCase) CompareError(err error, stderr string) {
wantExists, want := tc.ErrorExpected != "", tc.ErrorExpected
gotExists, got := stderr != "" && err != nil, stderr

Expand All @@ -151,8 +149,9 @@ func (tc *IntegrationTestCase) CompareError(err error, stderr string) {
}
}

func (tc *IntegrationTestCase) CompareVendorPaths(gotVendorPaths []string) {
if *UpdateGolden {
// CompareVendorPaths validates the vendor directory contents.
func (tc *TestCase) CompareVendorPaths(gotVendorPaths []string) {
if *test.UpdateGolden {
tc.VendorFinal = gotVendorPaths
} else {
wantVendorPaths := tc.VendorFinal
Expand All @@ -167,9 +166,9 @@ func (tc *IntegrationTestCase) CompareVendorPaths(gotVendorPaths []string) {
}
}

func (tc *IntegrationTestCase) WriteFile(src string, content string) error {
err := ioutil.WriteFile(src, []byte(content), 0666)
return err
// WriteFile writes a file using the default file permissions.
func (tc *TestCase) WriteFile(src string, content string) error {
return ioutil.WriteFile(src, []byte(content), 0666)
}

func getFile(path string) (bool, string, error) {
Expand Down
Loading

0 comments on commit 876083e

Please sign in to comment.