From ea5db61978a6ee66a4a68930e5b5ee254ed33cf6 Mon Sep 17 00:00:00 2001 From: Martin Kasban Tange Date: Wed, 7 Feb 2018 17:35:27 +0100 Subject: [PATCH 1/2] Changed all uses of path to path/filepath to get cross-platform compatibility. Also fixed tests to work on windows as well. --- cli/exitcode_test.go | 13 +++++++---- matcher/files_test.go | 10 ++++++--- matcher/matchers.go | 25 +++++++++++++++------ osutil/osutil.go | 30 +++++++++++++++++++++++++ osutil/osutil_test.go | 25 +++++++++++++++++++++ pkgpath/packages.go | 28 ++++++++++++++---------- pkgpath/packages_test.go | 26 +++++++++++++++------- signals/signals_test.go | 20 +++++++++++++++++ specdir/node.go | 10 ++++----- specdir/spec.go | 13 ++++++----- specdir/spec_test.go | 35 ++++++++++++++++++++---------- specdir/specdir.go | 8 +++---- specdir/specdir_test.go | 15 ++++++++----- tlsconfig/tlsconfig_client_test.go | 5 +++-- 14 files changed, 195 insertions(+), 68 deletions(-) create mode 100644 osutil/osutil.go create mode 100644 osutil/osutil_test.go diff --git a/cli/exitcode_test.go b/cli/exitcode_test.go index 7ace6a5b..b77056be 100644 --- a/cli/exitcode_test.go +++ b/cli/exitcode_test.go @@ -9,7 +9,8 @@ import ( "io/ioutil" "os" "os/exec" - "path" + "path/filepath" + "runtime" "syscall" "testing" @@ -66,14 +67,18 @@ func runGoFile(t *testing.T, src string) ([]byte, error) { } }() - err = ioutil.WriteFile(path.Join(tmpDir, "test_cli.go"), []byte(src), 0644) + err = ioutil.WriteFile(filepath.Join(tmpDir, "test_cli.go"), []byte(src), 0644) require.NoError(t, err) - buildCmd := exec.Command("go", "build", "-o", "test-cli", ".") + binName := "test-cli" + if runtime.GOOS == "windows" { + binName += ".exe" + } + buildCmd := exec.Command("go", "build", "-o", binName, ".") buildCmd.Dir = tmpDir output, err := buildCmd.CombinedOutput() require.NoError(t, err, "%v failed: %s", buildCmd.Args, string(output)) - testCLICmd := exec.Command(path.Join(tmpDir, "test-cli")) + testCLICmd := exec.Command(filepath.Join(tmpDir, binName)) return testCLICmd.CombinedOutput() } diff --git a/matcher/files_test.go b/matcher/files_test.go index b556afb0..480136be 100644 --- a/matcher/files_test.go +++ b/matcher/files_test.go @@ -7,7 +7,7 @@ package matcher_test import ( "io/ioutil" "os" - "path" + "path/filepath" "testing" "github.com/nmiyake/pkg/dirs" @@ -67,6 +67,10 @@ func TestListFiles(t *testing.T) { got, err := matcher.ListFiles(currCaseTmpDir, currCase.include, currCase.exclude) require.NoError(t, err, "Case %d", i) + for i, want := range currCase.want { + currCase.want[i] = filepath.FromSlash(want) + } + assert.Equal(t, currCase.want, got, "Case %d", i) } } @@ -76,10 +80,10 @@ func createFiles(t *testing.T, tmpDir string, files map[string]string) string { require.NoError(t, err) for currFile, currContent := range files { - err := os.MkdirAll(path.Join(currCaseTmpDir, path.Dir(currFile)), 0755) + err := os.MkdirAll(filepath.Join(currCaseTmpDir, filepath.Dir(currFile)), 0755) require.NoError(t, err) - err = ioutil.WriteFile(path.Join(currCaseTmpDir, currFile), []byte(currContent), 0644) + err = ioutil.WriteFile(filepath.Join(currCaseTmpDir, currFile), []byte(currContent), 0644) require.NoError(t, err) } diff --git a/matcher/matchers.go b/matcher/matchers.go index 7329af2d..81b23857 100644 --- a/matcher/matchers.go +++ b/matcher/matchers.go @@ -9,6 +9,7 @@ import ( "path" "path/filepath" "regexp" + "runtime" ) type Matcher interface { @@ -88,11 +89,12 @@ type nameMatcher []*regexp.Regexp func (m nameMatcher) Match(inputRelPath string) bool { for _, currSubpath := range allSubpaths(inputRelPath) { - currName := path.Base(currSubpath) + currName := filepath.Base(currSubpath) // do not match relative path components if currName == ".." { continue } + for _, currRegExp := range []*regexp.Regexp(m) { matchLoc := currRegExp.FindStringIndex(currName) if len(matchLoc) > 0 && matchLoc[0] == 0 && matchLoc[1] == len(currName) { @@ -109,13 +111,22 @@ func (m nameMatcher) Match(inputRelPath string) bool { // filepath.Match). However, unlike filepath.Match, subpath matches will match all of the sub-paths of a given match as // well (for example, the pattern "foo/*/bar" matches "foo/*/bar/baz"). func Path(paths ...string) Matcher { - return &pathMatcher{paths: paths, glob: true} + return newPathMatcher(paths, true) } // PathLiteral returns a Matcher that is equivalent to that returned by Paths except that matches are done using string // equality rather than using glob matching. func PathLiteral(paths ...string) Matcher { - return &pathMatcher{paths: paths, glob: false} + return newPathMatcher(paths, false) +} + +func newPathMatcher(paths []string, glob bool) Matcher { + if runtime.GOOS == "windows" { + for i, p := range paths { + paths[i] = filepath.ToSlash(p) + } + } + return &pathMatcher{paths: paths, glob: glob} } type pathMatcher struct { @@ -130,7 +141,7 @@ func (m *pathMatcher) Match(inputRelPath string) bool { var match bool if m.glob { var err error - match, err = filepath.Match(currMatcherPath, currSubpath) + match, err = path.Match(currMatcherPath, currSubpath) if err != nil { // only possible error is bad pattern panic(fmt.Sprintf("filepath: Match(%q): %v", currMatcherPath, err)) @@ -151,12 +162,12 @@ func (m *pathMatcher) Match(inputRelPath string) bool { // paths as well: a path of the form "../foo/bar/baz.txt" returns [../foo/bar/baz.txt ../foo/bar ../foo ..]. Returns nil // if the input path is not a relative path. func allSubpaths(relPath string) []string { - if path.IsAbs(relPath) { + if filepath.IsAbs(relPath) { return nil } var subpaths []string - for currRelPath := relPath; currRelPath != "."; currRelPath = path.Dir(currRelPath) { - subpaths = append(subpaths, currRelPath) + for currRelPath := relPath; currRelPath != "." && currRelPath != string(filepath.Separator); currRelPath = filepath.Dir(currRelPath) { + subpaths = append(subpaths, filepath.ToSlash(currRelPath)) } return subpaths } diff --git a/osutil/osutil.go b/osutil/osutil.go new file mode 100644 index 00000000..7566f3bd --- /dev/null +++ b/osutil/osutil.go @@ -0,0 +1,30 @@ +package osutil + +import ( + "path/filepath" + "runtime" + "strings" +) + +// MakeValidRegexPath takes a path and makes it into a valid regex string +func MakeValidRegexPath(path string) string { + return strings.Replace(filepath.FromSlash(path), "\\", "\\\\", -1) +} + +// GetNotADirErrorMsg returns the error message given if an action is +// performed on a non-existant directory. +func GetNotADirErrorMsg() string { + if runtime.GOOS == "windows" { + return "The system cannot find the path specified." + } + return "not a directory" +} + +// GetNoSuchFileOrDirErrorMsg returns the error message given if an action is +// performed on a non-existant file or directory. +func GetNoSuchFileOrDirErrorMsg() string { + if runtime.GOOS == "windows" { + return "The system cannot find the file specified." + } + return "no such file or directory" +} diff --git a/osutil/osutil_test.go b/osutil/osutil_test.go new file mode 100644 index 00000000..e0c87d83 --- /dev/null +++ b/osutil/osutil_test.go @@ -0,0 +1,25 @@ +package osutil_test + +import ( + "fmt" + "path/filepath" + "regexp" + "testing" + + "github.com/palantir/pkg/osutil" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestValidRegexPath(t *testing.T) { + currentAbs, err := filepath.Abs(".") + require.NoError(t, err) + + fmtString := `here is a path: %s with some text after` + myRegexDef := fmt.Sprintf( + "^"+fmtString+"$", + osutil.MakeValidRegexPath(currentAbs), + ) + + assert.Regexp(t, regexp.MustCompile(myRegexDef), fmt.Sprintf(fmtString, currentAbs)) +} diff --git a/pkgpath/packages.go b/pkgpath/packages.go index 94111e97..b4596b78 100644 --- a/pkgpath/packages.go +++ b/pkgpath/packages.go @@ -10,7 +10,6 @@ import ( "go/parser" "go/token" "os" - "path" "path/filepath" "sort" "strings" @@ -89,18 +88,22 @@ type pkgPath struct { func (p *pkgPath) Abs() string { switch p.pathType { case Absolute: - return p.path + absPath, err := filepath.Abs(p.path) + if err != nil { + panic(err) + } + return absPath case GoPathSrcRelative: - return path.Join(os.Getenv("GOPATH"), "src", p.path) + return filepath.Join(os.Getenv("GOPATH"), "src", p.path) case Relative: - return path.Join(p.baseDir, p.path) + return filepath.Join(p.baseDir, p.path) default: panic(fmt.Sprintf("unhandled case: %v", p.path)) } } func (p *pkgPath) GoPathSrcRel() (string, error) { - return relPathNoParentDir(p.Abs(), path.Join(os.Getenv("GOPATH"), "src"), "") + return relPathNoParentDir(p.Abs(), filepath.Join(os.Getenv("GOPATH"), "src"), "") } func (p *pkgPath) Rel(baseDir string) (string, error) { @@ -113,9 +116,10 @@ func relPathNoParentDir(absPath, baseDir, prepend string) (string, error) { if err != nil { return "", err } - if strings.HasPrefix(relPath, parentDirPath) { + if filepath.HasPrefix(relPath, parentDirPath) { return "", fmt.Errorf("resolving %s against base %s produced relative path starting with %s: %s", absPath, baseDir, parentDirPath, relPath) } + relPath = filepath.ToSlash(relPath) return prepend + relPath, nil } @@ -142,7 +146,7 @@ func (p *packages) Filter(exclude matcher.Matcher) (Packages, error) { filteredAbsPathPkgs := make(map[string]string) for currPkgRelPath, currPkg := range allPkgsRelPaths { if exclude == nil || !exclude.Match(currPkgRelPath) { - filteredAbsPathPkgs[path.Join(p.rootDir, currPkgRelPath)] = currPkg + filteredAbsPathPkgs[filepath.Join(p.rootDir, currPkgRelPath)] = currPkg } } @@ -214,7 +218,7 @@ func PackagesFromPaths(rootDir string, relPaths []string) (Packages, error) { pkgs := make(map[string]string, len(expandedRelPaths)) for _, currPath := range expandedRelPaths { - currAbsPath := path.Join(absoluteRoot, currPath) + currAbsPath := filepath.Join(absoluteRoot, currPath) currPkg, err := getPrimaryPkgForDir(currAbsPath, nil) if err != nil { return nil, fmt.Errorf("unable to determine package for directory %s: %v", currAbsPath, err) @@ -257,7 +261,7 @@ func PackagesInDir(rootDir string, exclude matcher.Matcher) (Packages, error) { // create a filter for processing package files that only passes if it does not match an exclude filter := func(info os.FileInfo) bool { // if exclude exists and matches the file, skip it - if exclude != nil && exclude.Match(path.Join(currRelPath, info.Name())) { + if exclude != nil && exclude.Match(filepath.Join(currRelPath, info.Name())) { return false } // process file if it would be included in build context (handles things like build tags) @@ -283,12 +287,12 @@ func PackagesInDir(rootDir string, exclude matcher.Matcher) (Packages, error) { } func createPkgsWithValidation(rootDir string, pkgs map[string]string) (*packages, error) { - if !path.IsAbs(rootDir) { + if !filepath.IsAbs(rootDir) { return nil, fmt.Errorf("rootDir %s is not an absolute path", rootDir) } for currAbsPkgPath := range pkgs { - if !path.IsAbs(currAbsPkgPath) { + if !filepath.IsAbs(currAbsPkgPath) { return nil, fmt.Errorf("package %s in packages %v is not an absolute path", currAbsPkgPath, pkgs) } } @@ -305,7 +309,7 @@ func expandPaths(rootDir string, relPaths []string) ([]string, error) { if strings.HasSuffix(currRelPath, "/...") { // expand splatted paths splatBaseDir := currRelPath[:len(currRelPath)-len("/...")] - baseDirAbsPath := path.Join(rootDir, splatBaseDir) + baseDirAbsPath := filepath.Join(rootDir, splatBaseDir) err := filepath.Walk(baseDirAbsPath, func(path string, info os.FileInfo, err error) error { if err != nil { return err diff --git a/pkgpath/packages_test.go b/pkgpath/packages_test.go index c4195d81..2c662ee1 100644 --- a/pkgpath/packages_test.go +++ b/pkgpath/packages_test.go @@ -8,9 +8,10 @@ import ( "fmt" "io/ioutil" "os" - "path" "path/filepath" "regexp" + "runtime" + "strings" "testing" "github.com/nmiyake/pkg/dirs" @@ -85,7 +86,7 @@ func TestListPackages(t *testing.T) { wd, err := os.Getwd() require.NoError(t, err) - testPkgPath, err := filepath.Rel(path.Join(os.Getenv("GOPATH"), "src"), wd) + testPkgPath, err := filepath.Rel(filepath.Join(os.Getenv("GOPATH"), "src"), wd) require.NoError(t, err) tmpDir, cleanup, err := dirs.TempDir(wd, "") @@ -226,9 +227,9 @@ package different`}, case pkgpath.Relative: k = "./" + k case pkgpath.GoPathSrcRelative: - k = path.Join(testPkgPath, currCaseDirRelPath, k) + k = filepath.ToSlash(filepath.Join(testPkgPath, currCaseDirRelPath, k)) case pkgpath.Absolute: - k = path.Join(wd, currCaseDirRelPath, k) + k = filepath.Join(wd, currCaseDirRelPath, k) default: require.Fail(t, "Unhandled case: %v", mode) } @@ -299,7 +300,7 @@ func TestListPackagesSetGoPath(t *testing.T) { err = os.Setenv("GOPATH", tmpDir) require.NoError(t, err) - projectDir := path.Join(tmpDir, "src", "github.com", "test") + projectDir := filepath.Join(tmpDir, "src", "github.com", "test") err = os.MkdirAll(projectDir, 0755) require.NoError(t, err) @@ -323,9 +324,18 @@ func TestListPackagesSetGoPath(t *testing.T) { } func TestPkgPathOutsideGoPathFails(t *testing.T) { - goPathSrc := path.Join(os.Getenv("GOPATH"), "src") - msg := fmt.Sprintf(`^resolving /foo against base %s produced relative path starting with ../: .+/foo$`, goPathSrc) + goPathSrc := filepath.Join(os.Getenv("GOPATH"), "src") + fooPath := string(filepath.Separator) + "foo" + absPath, err := filepath.Abs(fooPath) + require.NoError(t, err) + + msg := fmt.Sprintf(`^resolving %s against base %s produced relative path starting with %s: .+%s$`, + absPath, goPathSrc, ".."+string(filepath.Separator), fooPath) + + if runtime.GOOS == "windows" { + msg = strings.Replace(msg, "\\", "\\\\", -1) + } - _, err := pkgpath.NewAbsPkgPath("/foo").GoPathSrcRel() + _, err = pkgpath.NewAbsPkgPath(absPath).GoPathSrcRel() require.Regexp(t, msg, err.Error()) } diff --git a/signals/signals_test.go b/signals/signals_test.go index 5a1a7c7c..db2b1d32 100644 --- a/signals/signals_test.go +++ b/signals/signals_test.go @@ -9,6 +9,7 @@ import ( "context" "fmt" "os" + "runtime" "syscall" "testing" "time" @@ -20,6 +21,9 @@ import ( ) func TestCancelOnSignalsContext(t *testing.T) { + if runtime.GOOS == "windows" { + return + } ctx, _ := signals.CancelOnSignalsContext(context.Background(), syscall.SIGHUP) sendSignalToCurrProcess(t, syscall.SIGHUP) @@ -35,6 +39,10 @@ func TestCancelOnSignalsContext(t *testing.T) { } func TestRegisterStackTraceWriterOnSignals(t *testing.T) { + if runtime.GOOS == "windows" { + return + } + out := &bytes.Buffer{} signals.RegisterStackTraceWriterOnSignals(out, nil, syscall.SIGHUP) @@ -51,6 +59,10 @@ func (w errWriter) Write(p []byte) (n int, err error) { } func TestRegisterStackTraceWriterErrorHandler(t *testing.T) { + if runtime.GOOS == "windows" { + return + } + out := errWriter{} var handlerErr error errHandler := func(err error) { @@ -72,6 +84,10 @@ func TestRegisterStackTraceWriterErrorHandler(t *testing.T) { } func TestUnregisterStackTraceWriterOnSignals(t *testing.T) { + if runtime.GOOS == "windows" { + return + } + out := &bytes.Buffer{} unregister := signals.RegisterStackTraceWriterOnSignals(out, nil, syscall.SIGHUP) unregister() @@ -83,6 +99,10 @@ func TestUnregisterStackTraceWriterOnSignals(t *testing.T) { } func TestNewSignalReceiver(t *testing.T) { + if runtime.GOOS == "windows" { + return + } + c := signals.NewSignalReceiver(syscall.SIGHUP) sendSignalToCurrProcess(t, syscall.SIGHUP) diff --git a/specdir/node.go b/specdir/node.go index 0d16755b..2cd081dc 100644 --- a/specdir/node.go +++ b/specdir/node.go @@ -7,7 +7,7 @@ package specdir import ( "fmt" "os" - "path" + "path/filepath" "sort" ) @@ -71,7 +71,7 @@ func (n *fileNode) fileNode() *fileNode { func (n *fileNode) createDirectoryStructure(parentDir string, values TemplateValues, includeOptional bool) error { if n.pathType == DirPath && (!n.optional || includeOptional) { - currPath := path.Join(parentDir, n.name.name(values)) + currPath := filepath.Join(parentDir, n.name.name(values)) if err := os.MkdirAll(currPath, 0755); err != nil { return fmt.Errorf("failed to create directory %s: %v", currPath, err) } @@ -93,7 +93,7 @@ func (p fileNodePath) getPath(templateValues map[string]string) string { for _, node := range p { parts = append(parts, node.name.name(templateValues)) } - return path.Join(parts...) + return filepath.Join(parts...) } // getTemplateNames returns the names of all of the templates in the given path @@ -110,7 +110,7 @@ func (n *fileNode) paths(parentPath string, templateValues map[string]string, in // only add node and children if it is not optional or if optional paths are being included if !n.optional || includeOptional { - currPath := path.Join(parentPath, n.name.name(templateValues)) + currPath := filepath.Join(parentPath, n.name.name(templateValues)) // add current node paths = append(paths, currPath) @@ -174,7 +174,7 @@ func (n *fileNode) validate(rootDir, pathFromRoot string, values TemplateValues) func (n *fileNode) verifyLayoutForDir(rootDir, pathFromRoot string, values TemplateValues) error { if n.pathType == DirPath { for _, c := range n.children { - currPath := path.Join(pathFromRoot, c.fileNode().name.name(values)) + currPath := filepath.Join(pathFromRoot, c.fileNode().name.name(values)) if err := c.fileNode().validate(rootDir, currPath, values); err != nil { return err } diff --git a/specdir/spec.go b/specdir/spec.go index 257a822d..2130d5d3 100644 --- a/specdir/spec.go +++ b/specdir/spec.go @@ -7,7 +7,7 @@ package specdir import ( "fmt" "os" - "path" + "path/filepath" ) type PathType bool @@ -170,22 +170,23 @@ func getTemplateKeysFromName(n NodeName) []string { } func verifyPath(rootDirPath, pathFromRootDir, expectedName string, pathType PathType, optional bool) error { - if path.Base(pathFromRootDir) != expectedName { + if filepath.Base(pathFromRootDir) != expectedName { return fmt.Errorf("%s is not a path to %s", pathFromRootDir, expectedName) } - pathInfo, err := os.Stat(path.Join(rootDirPath, pathFromRootDir)) + pathInfo, err := os.Stat(filepath.Join(rootDirPath, pathFromRootDir)) if err != nil { if os.IsNotExist(err) { if !optional { - return fmt.Errorf("%s does not exist", path.Join(path.Base(rootDirPath), pathFromRootDir)) + return fmt.Errorf("%s does not exist", filepath.Join(filepath.Base(rootDirPath), pathFromRootDir)) } // path does not exist, but it is optional so is okay return nil } - return fmt.Errorf("failed to stat %s", path.Join(path.Base(rootDirPath), pathFromRootDir)) + return fmt.Errorf("failed to stat %s", filepath.Join(filepath.Base(rootDirPath), pathFromRootDir)) } else if currIsDir := pathInfo.IsDir(); currIsDir == bool(pathType) { - return fmt.Errorf("isDir for %s returned wrong value: expected %v, was %v", path.Join(path.Base(rootDirPath), pathFromRootDir), !pathType, currIsDir) + return fmt.Errorf("isDir for %s returned wrong value: expected %v, was %v", + filepath.Join(filepath.Base(rootDirPath), pathFromRootDir), !pathType, currIsDir) } return nil diff --git a/specdir/spec_test.go b/specdir/spec_test.go index 1eef0b79..6d8e9c1d 100644 --- a/specdir/spec_test.go +++ b/specdir/spec_test.go @@ -5,9 +5,10 @@ package specdir_test import ( + "fmt" "io/ioutil" "os" - "path" + "path/filepath" "regexp" "testing" @@ -15,6 +16,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "github.com/palantir/pkg/osutil" "github.com/palantir/pkg/specdir" ) @@ -40,7 +42,7 @@ func TestValidateSpec(t *testing.T) { { dirToValidate: "root", spec: specdir.NewLayoutSpec(specdir.Dir(specdir.LiteralName("root"), ""), true), - expectedError: `^.+/root does not exist$`, + expectedError: fmt.Sprintf(`^.+%s does not exist$`, osutil.MakeValidRegexPath("/root")), }, { dirToValidate: "rootNotPartOfSpec", @@ -90,7 +92,10 @@ func TestValidateSpec(t *testing.T) { "dirWithWrongChildType": specdir.DirPath, "dirWithWrongChildType/child": specdir.FilePath, }, - expectedError: `^isDir for dirWithWrongChildType/child returned wrong value: expected true, was false$`, + expectedError: fmt.Sprintf( + `^isDir for %s returned wrong value: expected true, was false$`, + osutil.MakeValidRegexPath("dirWithWrongChildType/child"), + ), }, { dirToValidate: "templateKeyName", @@ -124,7 +129,7 @@ func TestValidateSpec(t *testing.T) { require.NoError(t, err) createDirectoryStructure(t, currCaseTmpDir, currCase.pathsToCreate) - err = currCase.spec.Validate(path.Join(currCaseTmpDir, currCase.dirToValidate), currCase.values) + err = currCase.spec.Validate(filepath.Join(currCaseTmpDir, currCase.dirToValidate), currCase.values) if currCase.expectedError == "" { assert.NoError(t, err, "Case %d", i) } else { @@ -160,7 +165,10 @@ func TestCreateDirectoryStructure(t *testing.T) { expectedPaths: map[string]specdir.PathType{ "root": specdir.DirPath, }, - expectedError: `^.+/wrongName is not a path to root$`, + expectedError: fmt.Sprintf( + `^.+%s is not a path to root$`, + osutil.MakeValidRegexPath("/wrongName"), + ), }, { rootDirForCreation: "rootNotPartOfSpec", @@ -214,7 +222,12 @@ func TestCreateDirectoryStructure(t *testing.T) { specdir.Dir(specdir.LiteralName("child"), ""), ), true), includeOptional: true, - expectedError: `^failed to create directory .+/failIfFileExistsWhereDirToBeCreated/child: mkdir .*/failIfFileExistsWhereDirToBeCreated/child: not a directory$`, + expectedError: fmt.Sprintf( + `^failed to create directory .+%s: mkdir .*%s: %s$`, + osutil.MakeValidRegexPath("/failIfFileExistsWhereDirToBeCreated/child"), + osutil.MakeValidRegexPath("/failIfFileExistsWhereDirToBeCreated/child"), + osutil.GetNotADirErrorMsg(), + ), }, { rootDirForCreation: "okIfDirAlreadyExists", @@ -230,7 +243,7 @@ func TestCreateDirectoryStructure(t *testing.T) { currCaseTmpDir, err := ioutil.TempDir(tmpDir, "") require.NoError(t, err) - rootForCreation := path.Join(currCaseTmpDir, currCase.rootDirForCreation) + rootForCreation := filepath.Join(currCaseTmpDir, currCase.rootDirForCreation) err = os.Mkdir(rootForCreation, 0755) require.NoError(t, err) @@ -241,13 +254,13 @@ func TestCreateDirectoryStructure(t *testing.T) { assert.NoError(t, err, "Case %d", i) for currPath, pathType := range currCase.expectedPaths { - info, err := os.Stat(path.Join(currCaseTmpDir, currPath)) + info, err := os.Stat(filepath.Join(currCaseTmpDir, currPath)) assert.NoError(t, err, "Case %d", i) assert.Equal(t, bool(pathType), !info.IsDir(), "Case %d", i) } for _, currPath := range currCase.unexpectedPaths { - _, err = os.Stat(path.Join(currCaseTmpDir, currPath)) + _, err = os.Stat(filepath.Join(currCaseTmpDir, currPath)) assert.True(t, os.IsNotExist(err), "Case %d") } } else { @@ -258,11 +271,11 @@ func TestCreateDirectoryStructure(t *testing.T) { func createDirectoryStructure(t *testing.T, tmpDir string, paths map[string]specdir.PathType) { for currPath, pathType := range paths { - currPath = path.Join(tmpDir, currPath) + currPath = filepath.Join(tmpDir, currPath) dirToCreate := currPath if pathType == specdir.FilePath { - dirToCreate = path.Dir(currPath) + dirToCreate = filepath.Dir(currPath) } err := os.MkdirAll(dirToCreate, 0755) require.NoError(t, err) diff --git a/specdir/specdir.go b/specdir/specdir.go index fde748c2..717f9ccb 100644 --- a/specdir/specdir.go +++ b/specdir/specdir.go @@ -7,7 +7,7 @@ package specdir import ( "fmt" "os" - "path" + "path/filepath" "sort" ) @@ -42,9 +42,9 @@ func (s *specDirStruct) Path(name string) string { if value, ok := s.aliasValues[name]; ok { pathRoot := s.rootDir if s.spec.rootIsPartOfSpec() { - pathRoot = path.Dir(s.rootDir) + pathRoot = filepath.Dir(s.rootDir) } - return path.Join(pathRoot, value) + return filepath.Join(pathRoot, value) } return "" } @@ -78,7 +78,7 @@ func New(rootDir string, spec LayoutSpec, values TemplateValues, mode Mode) (Spe // if the root directory is part of the specification, verify that the name matches the required name if spec.rootIsPartOfSpec() { expectedRootDirName := spec.rootNode().name.name(values) - if path.Base(rootDir) != expectedRootDirName { + if filepath.Base(rootDir) != expectedRootDirName { return nil, fmt.Errorf("root directory name %s does not match name required by specification: %v", rootDir, expectedRootDirName) } } diff --git a/specdir/specdir_test.go b/specdir/specdir_test.go index 870c306c..dd3cec39 100644 --- a/specdir/specdir_test.go +++ b/specdir/specdir_test.go @@ -5,12 +5,15 @@ package specdir_test import ( + "fmt" "io/ioutil" "os" - "path" + "path/filepath" "regexp" "testing" + "github.com/palantir/pkg/osutil" + "github.com/nmiyake/pkg/dirs" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -39,13 +42,13 @@ func TestSpecDirConstruction(t *testing.T) { { rootDir: "missing", spec: specdir.NewLayoutSpec(specdir.Dir(specdir.LiteralName("root"), ""), true), - expectedError: `^.+/missing is not a path to root$`, + expectedError: fmt.Sprintf(`^.+%s is not a path to root$`, osutil.MakeValidRegexPath("/missing")), }, } { currCaseTmpDir, err := ioutil.TempDir(tmpDir, "") require.NoError(t, err) - rootForCreation := path.Join(currCaseTmpDir, currCase.rootDir) + rootForCreation := filepath.Join(currCaseTmpDir, currCase.rootDir) err = os.Mkdir(rootForCreation, 0755) require.NoError(t, err) @@ -69,7 +72,7 @@ func TestSpecDirCreateMode(t *testing.T) { specdir.Dir(specdir.LiteralName("root"), "", specdir.Dir(specdir.LiteralName("child"), ""), ), true) - rootForCreation := path.Join(tmpDir, "root") + rootForCreation := filepath.Join(tmpDir, "root") _, err = specdir.New(rootForCreation, spec, nil, specdir.Create) require.NoError(t, err) @@ -118,7 +121,7 @@ func TestSpecDirGetPath(t *testing.T) { currCaseTmpDir, err := ioutil.TempDir(tmpDir, "") require.NoError(t, err) - rootForCreation := path.Join(currCaseTmpDir, currCase.rootDir) + rootForCreation := filepath.Join(currCaseTmpDir, currCase.rootDir) err = os.Mkdir(rootForCreation, 0755) require.NoError(t, err) @@ -129,6 +132,6 @@ func TestSpecDirGetPath(t *testing.T) { actualPath := specDir.Path("VeryInnerDir") - assert.Equal(t, path.Join(currCaseTmpDir, currCase.expectedPath), actualPath, "Case %d", i) + assert.Equal(t, filepath.Join(currCaseTmpDir, currCase.expectedPath), actualPath, "Case %d", i) } } diff --git a/tlsconfig/tlsconfig_client_test.go b/tlsconfig/tlsconfig_client_test.go index 105ba87c..89effff6 100644 --- a/tlsconfig/tlsconfig_client_test.go +++ b/tlsconfig/tlsconfig_client_test.go @@ -9,6 +9,7 @@ import ( "fmt" "testing" + "github.com/palantir/pkg/osutil" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -57,12 +58,12 @@ func TestNewClientConfigErrors(t *testing.T) { { name: "missing certificate file", keyFile: clientKeyFile, - wantError: "failed to load TLS certificate: open : no such file or directory", + wantError: "failed to load TLS certificate: open : " + osutil.GetNoSuchFileOrDirErrorMsg(), }, { name: "missing key file", certFile: clientCertFile, - wantError: "failed to load TLS certificate: open : no such file or directory", + wantError: "failed to load TLS certificate: open : " + osutil.GetNoSuchFileOrDirErrorMsg(), }, { name: "invalid CA file", From 4ad28d2a807272075b1b0b6d961f4bb5bf8fa57a Mon Sep 17 00:00:00 2001 From: Martin Kasban Tange Date: Sat, 10 Mar 2018 18:39:09 +0100 Subject: [PATCH 2/2] Changed window signal_test ignores to build flag --- signals/signals_test.go | 22 ++-------------------- 1 file changed, 2 insertions(+), 20 deletions(-) diff --git a/signals/signals_test.go b/signals/signals_test.go index db2b1d32..c6cb3eba 100644 --- a/signals/signals_test.go +++ b/signals/signals_test.go @@ -1,3 +1,5 @@ +// +build !windows + // Copyright 2016 Palantir Technologies. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. @@ -9,7 +11,6 @@ import ( "context" "fmt" "os" - "runtime" "syscall" "testing" "time" @@ -21,9 +22,6 @@ import ( ) func TestCancelOnSignalsContext(t *testing.T) { - if runtime.GOOS == "windows" { - return - } ctx, _ := signals.CancelOnSignalsContext(context.Background(), syscall.SIGHUP) sendSignalToCurrProcess(t, syscall.SIGHUP) @@ -39,10 +37,6 @@ func TestCancelOnSignalsContext(t *testing.T) { } func TestRegisterStackTraceWriterOnSignals(t *testing.T) { - if runtime.GOOS == "windows" { - return - } - out := &bytes.Buffer{} signals.RegisterStackTraceWriterOnSignals(out, nil, syscall.SIGHUP) @@ -59,10 +53,6 @@ func (w errWriter) Write(p []byte) (n int, err error) { } func TestRegisterStackTraceWriterErrorHandler(t *testing.T) { - if runtime.GOOS == "windows" { - return - } - out := errWriter{} var handlerErr error errHandler := func(err error) { @@ -84,10 +74,6 @@ func TestRegisterStackTraceWriterErrorHandler(t *testing.T) { } func TestUnregisterStackTraceWriterOnSignals(t *testing.T) { - if runtime.GOOS == "windows" { - return - } - out := &bytes.Buffer{} unregister := signals.RegisterStackTraceWriterOnSignals(out, nil, syscall.SIGHUP) unregister() @@ -99,10 +85,6 @@ func TestUnregisterStackTraceWriterOnSignals(t *testing.T) { } func TestNewSignalReceiver(t *testing.T) { - if runtime.GOOS == "windows" { - return - } - c := signals.NewSignalReceiver(syscall.SIGHUP) sendSignalToCurrProcess(t, syscall.SIGHUP)