Skip to content

Commit

Permalink
internal/testenv: simplify hasSymlink for windows
Browse files Browse the repository at this point in the history
1. Combine two functions into one.

2. Use errors.Is to check for wrapped errors.

3. Use sync.OnceValues.

Change-Id: I25f55d31bb658ff08da209b1740e9dff579cca69
Reviewed-on: https://go-review.googlesource.com/c/go/+/609797
Reviewed-by: Ian Lance Taylor <[email protected]>
Auto-Submit: Ian Lance Taylor <[email protected]>
Reviewed-by: Alan Donovan <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
Commit-Queue: Ian Lance Taylor <[email protected]>
  • Loading branch information
kolyshkin authored and gopherbot committed Aug 30, 2024
1 parent b648cd6 commit 7300b9e
Showing 1 changed file with 7 additions and 22 deletions.
29 changes: 7 additions & 22 deletions src/internal/testenv/testenv_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,28 @@
package testenv

import (
"errors"
"os"
"path/filepath"
"sync"
"syscall"
)

var symlinkOnce sync.Once
var winSymlinkErr error

func initWinHasSymlink() {
var hasSymlink = sync.OnceValues(func() (bool, string) {
tmpdir, err := os.MkdirTemp("", "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:
winSymlinkErr = err
}
}
}

func hasSymlink() (ok bool, reason string) {
symlinkOnce.Do(initWinHasSymlink)

switch winSymlinkErr {
case nil:
switch {
case err == nil:
return true, ""
case syscall.EWINDOWS:
case errors.Is(err, syscall.EWINDOWS):
return false, ": symlinks are not supported on your version of Windows"
case syscall.ERROR_PRIVILEGE_NOT_HELD:
case errors.Is(err, syscall.ERROR_PRIVILEGE_NOT_HELD):
return false, ": you don't have enough privileges to create symlinks"
}

return false, ""
}
})

0 comments on commit 7300b9e

Please sign in to comment.