diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2023-08-21 16:35:04 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2023-08-21 17:04:21 +0300 |
commit | 215f53133e5879ff35cba142bd5c643a7ccd4249 (patch) | |
tree | c1a3002318be01aa5c5fcad5a0dbe63d8a95cc78 /internal/testhelper | |
parent | cab64a05e22215cf8dc879f9fbb125207ad38e51 (diff) |
repository: Fix ReplicateRepository test depending on host umask
One of our tests for ReplicateRepository implicitly depends on the host
umask. This is becaues we have a hardcoded vote that expects a specific
vote for the hooks vote, but that vote does in fact include permission
bits that change depending on the current umask.
Fix this by dynamically detecting the expecte mode bits such that we can
compute the expected vote instead of having to hardcode it.
Diffstat (limited to 'internal/testhelper')
-rw-r--r-- | internal/testhelper/testhelper.go | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/internal/testhelper/testhelper.go b/internal/testhelper/testhelper.go index 03869008d..24481581c 100644 --- a/internal/testhelper/testhelper.go +++ b/internal/testhelper/testhelper.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "io" + "io/fs" "math/rand" "net" "net/http" @@ -328,6 +329,22 @@ func WriteExecutable(tb testing.TB, path string, content []byte) string { return path } +// MaskedPerms determines permission bits with the current process umask applied. This is preferable over calls to +// `perm.GetUmask()` because it can be executed in parallel. Note that this function does not include non-permission +// mode bits like `fs.ModeDir` or `fs.ModeSocket`. You can OR these manually as required. +func MaskedPerms(tb testing.TB, mode fs.FileMode) fs.FileMode { + tb.Helper() + + f, err := os.OpenFile(filepath.Join(TempDir(tb), "file"), os.O_CREATE|os.O_EXCL, mode) + require.NoError(tb, err) + defer MustClose(tb, f) + + stat, err := f.Stat() + require.NoError(tb, err) + + return stat.Mode().Perm() +} + // Unsetenv unsets an environment variable. The variable will be restored after the test has // finished. func Unsetenv(tb testing.TB, key string) { |