Skip to content

Commit

Permalink
Add tests with different umask values
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Apr 14, 2021
1 parent 2436c92 commit 5331014
Show file tree
Hide file tree
Showing 15 changed files with 62 additions and 17 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,14 @@ jobs:
[Environment]::SetEnvironmentVariable("Path", $env:PATH, "Machine")
choco install --yes gpg4win
echo "C:\Program Files (x86)\GnuPG\bin" >> $env:GITHUB_PATH
- name: Test (umask 022)
if: runner.os != 'Windows'
run: go test -ldflags="-X github.com/twpayne/chezmoi/internal/chezmoitest.umaskStr=0o022" -race ./...
- name: Test (umask 002)
if: runner.os != 'Windows'
run: go test -ldflags="-X github.com/twpayne/chezmoi/internal/chezmoitest.umaskStr=0o002" -race ./...
- name: Test
if: runner.os == 'Windows'
run: go test -race ./...
test-release:
runs-on: ubuntu-18.04
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ run:

.PHONY: test
test:
$(GO) test ./...
$(GO) test -ldflags="-X github.com/twpayne/chezmoi/internal/chezmoitest.umaskStr=0o022" ./...
$(GO) test -ldflags="-X github.com/twpayne/chezmoi/internal/chezmoitest.umaskStr=0o002" ./...

.PHONY: generate
generate: completions assets/scripts/install.sh
Expand Down
5 changes: 4 additions & 1 deletion cmd/verifycmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ func TestVerifyCmd(t *testing.T) {
name: "file",
root: map[string]interface{}{
"/home/user": map[string]interface{}{
".bashrc": "# contents of .bashrc\n",
".bashrc": &vfst.File{
Contents: []byte("# contents of .bashrc\n"),
Perm: 0o666 &^ chezmoitest.Umask,
},
".local/share/chezmoi/dot_bashrc": "# contents of .bashrc\n",
},
},
Expand Down
11 changes: 10 additions & 1 deletion internal/chezmoitest/chezmoitest.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"path/filepath"
"regexp"
"runtime"
"strconv"
"strings"
"testing"

Expand Down Expand Up @@ -122,8 +123,16 @@ func SkipUnlessGOOS(t *testing.T, name string) {
// WithTestFS calls f with a test filesystem populated with root.
func WithTestFS(t *testing.T, root interface{}, f func(fs vfs.FS)) {
t.Helper()
fs, cleanup, err := vfst.NewTestFS(root)
fs, cleanup, err := vfst.NewTestFS(root, vfst.BuilderUmask(Umask))
require.NoError(t, err)
t.Cleanup(cleanup)
f(fs)
}

func mustParseFilemode(s string) os.FileMode {
i, err := strconv.ParseInt(s, 0, 32)
if err != nil {
panic(err)
}
return os.FileMode(i)
}
22 changes: 13 additions & 9 deletions internal/chezmoitest/chezmoitest_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@
package chezmoitest

import (
"os"
"syscall"
)

// Umask is the umask used in tests.
//
// If you change this then you will need to update the testscripts in
// testdata/scripts where permissions after applying umask are hardcoded as
// strings. Pure Go tests should use this value to ensure that they pass,
// irrespective of what it is set to. Be aware that the process's umask is a
// process-level property and cannot be locally changed within individual tests.
const Umask = os.FileMode(0o022)
var (
umaskStr = "0o022"

// Umask is the umask used in tests.
//
// If you change this then you will need to update the testscripts in
// testdata/scripts where permissions after applying umask are hardcoded as
// strings. Pure Go tests should use this value to ensure that they pass,
// irrespective of what it is set to. Be aware that the process's umask is a
// process-level property and cannot be locally changed within individual
// tests.
Umask = mustParseFilemode(umaskStr)
)

func init() {
syscall.Umask(int(Umask))
Expand Down
8 changes: 5 additions & 3 deletions internal/chezmoitest/chezmoitest_windows.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package chezmoitest

import "os"
var (
umaskStr = "0"

// Umask is the umask used in tests.
const Umask = os.FileMode(0)
// Umask is the umask used in tests.
Umask = mustParseFilemode(umaskStr)
)
10 changes: 8 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path"
"path/filepath"
"regexp"
"runtime"
"strconv"
"strings"
Expand All @@ -21,6 +22,8 @@ import (
"github.com/twpayne/chezmoi/v2/internal/chezmoitest"
)

var umaskConditionRx = regexp.MustCompile(`\Aumask:([0-7]{3})\z`)

//nolint:interfacer
func TestMain(m *testing.M) {
os.Exit(testscript.RunMain(m, map[string]func() int{
Expand Down Expand Up @@ -63,9 +66,12 @@ func TestScript(t *testing.T) {
return runtime.GOOS == "linux", nil
case "windows":
return runtime.GOOS == "windows", nil
default:
return false, fmt.Errorf("%s: unknown condition", cond)
}
if m := umaskConditionRx.FindStringSubmatch(cond); m != nil {
umask, _ := strconv.ParseInt(m[1], 8, 64)
return chezmoitest.Umask == os.FileMode(umask), nil
}
return false, fmt.Errorf("%s: unknown condition", cond)
},
Setup: setup,
})
Expand Down
2 changes: 2 additions & 0 deletions testdata/scripts/diff.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[!umask:022] skip

mkhomedir golden
mkhomedir
mksourcedir
Expand Down
2 changes: 2 additions & 0 deletions testdata/scripts/dumpjson.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[!umask:022] skip

mksourcedir

chezmoi dump --format=json
Expand Down
2 changes: 2 additions & 0 deletions testdata/scripts/dumpyaml.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[!umask:022] skip

mksourcedir

chezmoi dump --format=yaml
Expand Down
2 changes: 2 additions & 0 deletions testdata/scripts/exclude.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[!umask:022] skip

# test that chezmoi diff respsects the diff.exclude configuration variable
chezmoi diff
[!windows] cmp stdout golden/diff
Expand Down
1 change: 1 addition & 0 deletions testdata/scripts/modify.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[windows] stop 'UNIX only' # FIXME
[!exec:sed] stop
[!umask:022] skip

cp golden/.modify home/user

Expand Down
1 change: 1 addition & 0 deletions testdata/scripts/runscriptdir_unix.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[windows] skip 'UNIX only'
[!umask:022] skip

chezmoi apply
cmpenv stdout golden/apply
Expand Down
1 change: 1 addition & 0 deletions testdata/scripts/scriptsubdir_unix.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[windows] skip 'UNIX only'
[!umask:022] skip

# test that scripts in subdirectories are run in the subdirectory
chezmoi apply --force
Expand Down
2 changes: 2 additions & 0 deletions testdata/scripts/verify.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[!umask:022] skip

mkhomedir golden
mkhomedir
mksourcedir
Expand Down

0 comments on commit 5331014

Please sign in to comment.