diff options
| author | Hiroshi Ioka <hirochachacha@gmail.com> | 2016-08-18 19:07:59 +0900 |
|---|---|---|
| committer | Brad Fitzpatrick <bradfitz@golang.org> | 2016-08-23 05:35:06 +0000 |
| commit | 8a9b96ace4c0064d3c06cd483368bd655ad43d87 (patch) | |
| tree | d6c07424113035d88be778138e7d23ab51a44004 /src/internal/testenv | |
| parent | 0b5f2f0d1149bcff3c6b08458d7ffdd96970235c (diff) | |
| download | go-8a9b96ace4c0064d3c06cd483368bd655ad43d87.tar.xz | |
internal/testenv: add HasSymlink/MustHaveSymlink
os package and path/filepath package have duplicated code for
checking symlink supports in test code.
This CL tries to simplify such test code.
Change-Id: I0371488337f5e951eca699852daab9ccb16ddd62
Reviewed-on: https://go-review.googlesource.com/27331
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Diffstat (limited to 'src/internal/testenv')
| -rw-r--r-- | src/internal/testenv/testenv.go | 13 | ||||
| -rw-r--r-- | src/internal/testenv/testenv_notwin.go | 20 | ||||
| -rw-r--r-- | src/internal/testenv/testenv_windows.go | 40 |
3 files changed, 73 insertions, 0 deletions
diff --git a/src/internal/testenv/testenv.go b/src/internal/testenv/testenv.go index f134f6b04a..7aff89928e 100644 --- a/src/internal/testenv/testenv.go +++ b/src/internal/testenv/testenv.go @@ -127,6 +127,19 @@ func MustHaveExternalNetwork(t *testing.T) { } } +// HasSymlink reports whether the current system can use os.Symlink. +func HasSymlink() bool { + return hasSymlink() +} + +// MustHaveSymlink reports whether the current system can use os.Symlink. +// If not, MustHaveSymlink calls t.Skip with an explanation. +func MustHaveSymlink(t *testing.T) { + if !HasSymlink() { + t.Skipf("skipping test: cannot make symlinks on %s/%s", runtime.GOOS, runtime.GOARCH) + } +} + var flaky = flag.Bool("flaky", false, "run known-flaky tests too") func SkipFlaky(t *testing.T, issue int) { diff --git a/src/internal/testenv/testenv_notwin.go b/src/internal/testenv/testenv_notwin.go new file mode 100644 index 0000000000..16673029aa --- /dev/null +++ b/src/internal/testenv/testenv_notwin.go @@ -0,0 +1,20 @@ +// 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. + +// +build !windows + +package testenv + +import ( + "runtime" +) + +func hasSymlink() bool { + switch runtime.GOOS { + case "android", "nacl", "plan9": + return false + } + + return true +} diff --git a/src/internal/testenv/testenv_windows.go b/src/internal/testenv/testenv_windows.go new file mode 100644 index 0000000000..042c0f2867 --- /dev/null +++ b/src/internal/testenv/testenv_windows.go @@ -0,0 +1,40 @@ +// 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 testenv + +import ( + "io/ioutil" + "os" + "path/filepath" + "sync" + "syscall" +) + +var symlinkOnce sync.Once +var winHasSymlink = true + +func initWinHasSymlink() { + tmpdir, err := ioutil.TempDir("", "symtest") + if err != nil { + panic("failed to create temp directory: " + err.Error()) + } + defer os.RemoveAll(tmpdir) + + err = os.Symlink("target", filepath.Join(tmpdir, "symlink")) + if err != nil { + err = err.(*os.LinkError).Err + switch err { + case syscall.EWINDOWS, syscall.ERROR_PRIVILEGE_NOT_HELD: + winHasSymlink = false + } + } + os.Remove("target") +} + +func hasSymlink() bool { + symlinkOnce.Do(initWinHasSymlink) + + return winHasSymlink +} |
