From d601baae9c8df74f2531dfdca1e13c2ba8fd5eec Mon Sep 17 00:00:00 2001 From: Dmitri Shuralyov Date: Sat, 9 Apr 2016 18:14:50 -0700 Subject: [PATCH] go/vcs: apply test style improvements from cmd/go Apply style improvements to TestFromDir from golang/go@b6cd6d7d3211bd90, in order to keep them in sync. Check for error when creating a directory, its successful existence is a precondition for the test to run. Helps golang/go#11490. Change-Id: I87054114c84aead96977f603ca3bd9eccfcfbd5e Reviewed-on: https://go-review.googlesource.com/21795 Reviewed-by: Alex Brainman --- go/vcs/vcs_test.go | 45 ++++++++++++++++----------------------------- 1 file changed, 16 insertions(+), 29 deletions(-) diff --git a/go/vcs/vcs_test.go b/go/vcs/vcs_test.go index 2d4bcda3..9eab67be 100644 --- a/go/vcs/vcs_test.go +++ b/go/vcs/vcs_test.go @@ -7,7 +7,7 @@ package vcs import ( "io/ioutil" "os" - pathpkg "path" + "path" "path/filepath" "reflect" "runtime" @@ -50,45 +50,32 @@ func TestRepoRootForImportPath(t *testing.T) { // Test that FromDir correctly inspects a given directory and returns the right VCS and root. func TestFromDir(t *testing.T) { - type testStruct struct { - path string - want *RepoRoot - } - - tests := make([]testStruct, len(vcsList)) tempDir, err := ioutil.TempDir("", "vcstest") if err != nil { t.Fatal(err) } defer os.RemoveAll(tempDir) - for i, vcs := range vcsList { - tests[i] = testStruct{ - path: filepath.Join(tempDir, "example.com", vcs.Name, "."+vcs.Cmd), - want: &RepoRoot{ - VCS: vcs, - Root: pathpkg.Join("example.com", vcs.Name), - }, - } - } - - for _, test := range tests { - os.MkdirAll(test.path, 0755) - var ( - got = new(RepoRoot) - err error - ) - got.VCS, got.Root, err = FromDir(test.path, tempDir) + for _, vcs := range vcsList { + dir := filepath.Join(tempDir, "example.com", vcs.Name, "."+vcs.Cmd) + err := os.MkdirAll(dir, 0755) if err != nil { - t.Errorf("FromDir(%q, %q): %v", test.path, tempDir, err) - os.RemoveAll(test.path) + t.Fatal(err) + } + + want := RepoRoot{ + VCS: vcs, + Root: path.Join("example.com", vcs.Name), + } + var got RepoRoot + got.VCS, got.Root, err = FromDir(dir, tempDir) + if err != nil { + t.Errorf("FromDir(%q, %q): %v", dir, tempDir, err) continue } - want := test.want if got.VCS.Name != want.VCS.Name || got.Root != want.Root { - t.Errorf("FromDir(%q, %q) = VCS(%s) Root(%s), want VCS(%s) Root(%s)", test.path, tempDir, got.VCS, got.Root, want.VCS, want.Root) + t.Errorf("FromDir(%q, %q) = VCS(%s) Root(%s), want VCS(%s) Root(%s)", dir, tempDir, got.VCS, got.Root, want.VCS, want.Root) } - os.RemoveAll(test.path) } }