pkg/testutil: Add utility for setting umask during tests.

This commit is contained in:
Qi Xiao 2021-09-12 14:42:06 +01:00
parent e0b824bbdf
commit be56f72729
6 changed files with 19 additions and 11 deletions

View File

@ -5,7 +5,6 @@ package lscolors
import ( import (
"errors" "errors"
"testing"
) )
var errNotSupportedOnNonUNIX = errors.New("not supported on non-UNIX OS") var errNotSupportedOnNonUNIX = errors.New("not supported on non-UNIX OS")
@ -13,5 +12,3 @@ var errNotSupportedOnNonUNIX = errors.New("not supported on non-UNIX OS")
func createNamedPipe(fname string) error { func createNamedPipe(fname string) error {
return errNotSupportedOnNonUNIX return errNotSupportedOnNonUNIX
} }
func setUmask(t *testing.T, m int) {}

View File

@ -18,7 +18,7 @@ type opt struct {
func TestDetermineFeature(t *testing.T) { func TestDetermineFeature(t *testing.T) {
testutil.InTempDir(t) testutil.InTempDir(t)
setUmask(t, 0) testutil.Umask(t, 0)
test := func(name, fname string, wantFeature feature, o opt) { test := func(name, fname string, wantFeature feature, o opt) {
t.Helper() t.Helper()

View File

@ -4,16 +4,9 @@
package lscolors package lscolors
import ( import (
"testing"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
) )
func createNamedPipe(fname string) error { func createNamedPipe(fname string) error {
return unix.Mkfifo(fname, 0600) return unix.Mkfifo(fname, 0600)
} }
func setUmask(t *testing.T, m int) {
save := unix.Umask(m)
t.Cleanup(func() { unix.Umask(save) })
}

7
pkg/testutil/umask.go Normal file
View File

@ -0,0 +1,7 @@
package testutil
// Umask sets the umask for the duration of the test, and restores it afterwards.
func Umask(c Cleanuper, m int) {
save := umask(m)
c.Cleanup(func() { umask(save) })
}

View File

@ -0,0 +1,8 @@
//go:build !windows && !plan9 && !js
// +build !windows,!plan9,!js
package testutil
import "golang.org/x/sys/unix"
var umask = unix.Umask

View File

@ -0,0 +1,3 @@
package testutil
func umask(int) int { return 0 }