Skip to content

Commit

Permalink
chore: Fix umask tests
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Jan 15, 2023
1 parent 76384d5 commit a3f041c
Show file tree
Hide file tree
Showing 12 changed files with 115 additions and 62 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -267,13 +267,13 @@ jobs:
env:
CHEZMOI_GITHUB_TOKEN: ${{ secrets.CHEZMOI_GITHUB_TOKEN }}
run: |
go test -cover -ldflags="-X github.com/twpayne/chezmoi/pkg/chezmoitest.umaskStr=0o022" -race ./...
go test -cover -ldflags="-X github.com/twpayne/chezmoi/v2/pkg/chezmoitest.umaskStr=0o022" -race ./...
- name: test-umask-002
if: github.event_name == 'push' || needs.changes.outputs.code == 'true'
env:
CHEZMOI_GITHUB_TOKEN: ${{ secrets.CHEZMOI_GITHUB_TOKEN }}
run: |
go test -cover -ldflags="-X github.com/twpayne/chezmoi/pkg/chezmoitest.umaskStr=0o002" -race ./...
go test -cover -ldflags="-X github.com/twpayne/chezmoi/v2/pkg/chezmoitest.umaskStr=0o002" -race ./...
- name: test-install.sh
if: github.event_name == 'push' || needs.changes.outputs.code == 'true'
run: |
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ rm-dist:

.PHONY: test
test:
${GO} test -ldflags="-X github.com/twpayne/chezmoi/pkg/chezmoitest.umaskStr=0o022" ./...
${GO} test -ldflags="-X github.com/twpayne/chezmoi/pkg/chezmoitest.umaskStr=0o002" ./...
${GO} test -ldflags="-X github.com/twpayne/chezmoi/v2/pkg/chezmoitest.umaskStr=0o022" ./...
${GO} test -ldflags="-X github.com/twpayne/chezmoi/v2/pkg/chezmoitest.umaskStr=0o002" ./...

.PHONY: test-docker
test-docker:
Expand Down
2 changes: 1 addition & 1 deletion pkg/chezmoitest/chezmoitest_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
var (
// umaskStr is the umask used in tests represented as a string so it can be
// set with the
// -ldflags="-X github.com/twpayne/chezmoi/pkg/chezmoitest.umaskStr=..."
// -ldflags="-X github.com/twpayne/chezmoi/v2/pkg/chezmoitest.umaskStr=..."
// option to go build and go test.
umaskStr = "0o022"

Expand Down
2 changes: 1 addition & 1 deletion pkg/chezmoitest/chezmoitest_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package chezmoitest
var (
// umaskStr is the umask used in tests represented as a string so it can be
// set with the
// -ldflags="-X github.com/twpayne/chezmoi/pkg/chezmoitest.umaskStr=..."
// -ldflags="-X github.com/twpayne/chezmoi/v2/pkg/chezmoitest.umaskStr=..."
// option to go build and go test.
umaskStr = "0"

Expand Down
17 changes: 13 additions & 4 deletions pkg/cmd/testdata/scripts/applyverbose.txtar
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# test that chezmoi apply --dry-run --verbose does not show scripts when scripts are excluded from diffs
exec chezmoi apply --dry-run --verbose
[!windows] cmp stdout golden/apply.diff
[!windows] [umask:002] cmp stdout golden/apply-umask-002.diff
[!windows] [umask:022] cmp stdout golden/apply-umask-022.diff
[windows] cmp stdout golden/apply-windows.diff

chhome home2/user
Expand All @@ -9,22 +10,30 @@ chhome home2/user
exec chezmoi apply --dry-run --force --verbose
! stdout .

-- golden/apply-windows.diff --
-- golden/apply-umask-002.diff --
diff --git a/.file b/.file
new file mode 100666
new file mode 100664
index 0000000000000000000000000000000000000000..8a52cb9ce9551221716a53786ad74104c5902362
--- /dev/null
+++ b/.file
@@ -0,0 +1 @@
+# contents of .file
-- golden/apply.diff --
-- golden/apply-umask-022.diff --
diff --git a/.file b/.file
new file mode 100644
index 0000000000000000000000000000000000000000..8a52cb9ce9551221716a53786ad74104c5902362
--- /dev/null
+++ b/.file
@@ -0,0 +1 @@
+# contents of .file
-- golden/apply-windows.diff --
diff --git a/.file b/.file
new file mode 100666
index 0000000000000000000000000000000000000000..8a52cb9ce9551221716a53786ad74104c5902362
--- /dev/null
+++ b/.file
@@ -0,0 +1 @@
+# contents of .file
-- home/user/.config/chezmoi/chezmoi.toml --
[diff]
exclude = ["scripts"]
Expand Down
13 changes: 11 additions & 2 deletions pkg/cmd/testdata/scripts/diffcommand_unix.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ stdout ^${HOME@R}/\.file\s+${WORK@R}/.*/\.file$

# test that chezmoi diff --use-builtin-diff uses the builtin diff even if diff.command is configured
exec chezmoi diff --use-builtin-diff
cmp stdout golden/diff.diff
[umask:002] cmp stdout golden/diff-umask-002.diff
[umask:022] cmp stdout golden/diff-umask-022.diff

# test that chezmoi diff --reverse reverses the order of arguments
exec chezmoi diff --reverse
Expand Down Expand Up @@ -38,7 +39,15 @@ chhome home4/user
! exec chezmoi diff
stderr 'exit status 2'

-- golden/diff.diff --
-- golden/diff-umask-002.diff --
diff --git a/.file b/.file
index bd729e8ee3cc005444c67dc77eed60016886b5e0..b508963510528ab709627ec448026a10a64c72ef 100664
--- a/.file
+++ b/.file
@@ -1 +1 @@
-# destination contents of .file
+# target contents of .file
-- golden/diff-umask-022.diff --
diff --git a/.file b/.file
index bd729e8ee3cc005444c67dc77eed60016886b5e0..b508963510528ab709627ec448026a10a64c72ef 100644
--- a/.file
Expand Down
47 changes: 10 additions & 37 deletions pkg/cmd/testdata/scripts/edgecases.txtar
Original file line number Diff line number Diff line change
@@ -1,30 +1,3 @@
mkhomedir

# test that chezmoi add --dry-run does not modify anything
exec chezmoi add --dry-run $HOME${/}.file
! exists $CHEZMOICONFIGDIR/chezmoistate.boltdb
! exists $CHEZMOISOURCEDIR/dot_file

# test that chezmoi add updates the persistent state
exec chezmoi add $HOME${/}.file
exists $CHEZMOICONFIGDIR/chezmoistate.boltdb
exists $CHEZMOISOURCEDIR/dot_file
exec chezmoi state dump
stdout 634a4dd193c7b3b926d2e08026aa81a416fd41cec52854863b974af422495663 # sha256sum of "# contents of .file\n"

# test that chezmoi apply updates the state if the target and destination states match, even if the last written state does not
edit $CHEZMOISOURCEDIR/dot_file
edit $HOME/.file
cmp $HOME/.file $CHEZMOISOURCEDIR/dot_file
exec chezmoi apply --dry-run $HOME${/}.file
exec chezmoi state dump
! stdout 2e9dd6a2a8c15b20d4b0882d4c0fb8c7eea4e8ece46818090b387132f9f84c34 # sha256sum of "# contents of .file\n# edited\n"
exec chezmoi apply $HOME${/}.file
exec chezmoi state dump
stdout 2e9dd6a2a8c15b20d4b0882d4c0fb8c7eea4e8ece46818090b387132f9f84c34 # sha256sum of "# contents of .file\n# edited\n"

chhome home2/user

# test that the source directory can be a symlink to another directory
symlink $HOME/.chezmoi -> $CHEZMOISOURCEDIR
exec chezmoi apply --source=$HOME${/}.chezmoi
Expand All @@ -35,13 +8,13 @@ mkdir $HOME/.dir
exec chezmoi add $HOME${/}.dir/
! exists $CHEZMOISOURCEDIR/dot_dir/dot_dir

chhome home3/user
chhome home2/user

# test that chezmoi reports an inconsistent state error when a file should be both removed and present
! exec chezmoi apply
stderr 'chezmoi: \.file: inconsistent state'

chhome home4/user
chhome home3/user

# test that chezmoi reports an error if there is a .chezmoi* file in the .chezmoitemplates directory
! exec chezmoi status
Expand All @@ -56,24 +29,24 @@ stderr 'invalid or unsupported data format'
stderr 'invalid or unsupported data format'

skip 'FIXME make the following test pass'
chhome home6/user
chhome home5/user

# test that chezmoi reports an inconsistent state error when a file should be both removed and present, even if the file is not already present
! exec chezmoi apply
stderr 'chezmoi: \.file: inconsistent state

-- golden/.file --
# contents of .file
-- home2/user/.local/share/chezmoi/dot_file --
-- home/user/.local/share/chezmoi/dot_file --
# contents of .file
-- home3/user/.file --
-- home2/user/.file --
# contents of .file
-- home3/user/.local/share/chezmoi/.chezmoiremove --
-- home2/user/.local/share/chezmoi/.chezmoiremove --
.file
-- home3/user/.local/share/chezmoi/dot_file --
-- home2/user/.local/share/chezmoi/dot_file --
# contents of .file
-- home4/user/.local/share/chezmoi/.chezmoitemplates/.chezmoiignore --
-- home5/user/.local/share/chezmoi/.chezmoiremove --
-- home3/user/.local/share/chezmoi/.chezmoitemplates/.chezmoiignore --
-- home4/user/.local/share/chezmoi/.chezmoiremove --
.file
-- home6/user/.local/share/chezmoi/dot_file --
-- home5/user/.local/share/chezmoi/dot_file --
# contents of .file
26 changes: 26 additions & 0 deletions pkg/cmd/testdata/scripts/edgecasesumask.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[!umask:022] skip

mkhomedir

# test that chezmoi add --dry-run does not modify anything
exec chezmoi add --dry-run $HOME${/}.file
! exists $CHEZMOICONFIGDIR/chezmoistate.boltdb
! exists $CHEZMOISOURCEDIR/dot_file

# test that chezmoi add updates the persistent state
exec chezmoi add $HOME${/}.file
exists $CHEZMOICONFIGDIR/chezmoistate.boltdb
exists $CHEZMOISOURCEDIR/dot_file
exec chezmoi state dump
stdout 634a4dd193c7b3b926d2e08026aa81a416fd41cec52854863b974af422495663 # sha256sum of "# contents of .file\n"

# test that chezmoi apply updates the state if the target and destination states match, even if the last written state does not
edit $CHEZMOISOURCEDIR/dot_file
edit $HOME/.file
cmp $HOME/.file $CHEZMOISOURCEDIR/dot_file
exec chezmoi apply --dry-run $HOME${/}.file
exec chezmoi state dump
! stdout 2e9dd6a2a8c15b20d4b0882d4c0fb8c7eea4e8ece46818090b387132f9f84c34 # sha256sum of "# contents of .file\n# edited\n"
exec chezmoi apply $HOME${/}.file
exec chezmoi state dump
stdout 2e9dd6a2a8c15b20d4b0882d4c0fb8c7eea4e8ece46818090b387132f9f84c34 # sha256sum of "# contents of .file\n# edited\n"
21 changes: 17 additions & 4 deletions pkg/cmd/testdata/scripts/forget.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ mksourcedir
exec chezmoi apply --force
exists $CHEZMOISOURCEDIR/dot_file
exec chezmoi state get --bucket=entryState --key=$WORK/home/user/.dir
cmp stdout golden/state-get-dir.json
[umask:002] cmp stdout golden/state-get-dir-umask-002.json
[umask:022] cmp stdout golden/state-get-dir-umask-022.json
exec chezmoi state get --bucket=entryState --key=$WORK/home/user/.file
cmp stdout golden/state-get-file.json
[umask:002] cmp stdout golden/state-get-file-umask-002.json
[umask:022] cmp stdout golden/state-get-file-umask-022.json

# test that chezmoi forget forgets a dir
exists $CHEZMOISOURCEDIR/dot_dir
Expand All @@ -29,12 +31,23 @@ chhome home2/user
exec chezmoi forget --force $HOME${/}.file
! exists $CHEZMOISOURCEDIR/home/dot_file

-- golden/state-get-dir.json --
-- golden/state-get-dir-umask-002.json --
{
"type": "dir",
"mode": 2147484157
}
-- golden/state-get-dir-umask-022.json --
{
"type": "dir",
"mode": 2147484141
}
-- golden/state-get-file.json --
-- golden/state-get-file-umask-002.json --
{
"type": "file",
"mode": 436,
"contentsSHA256": "634a4dd193c7b3b926d2e08026aa81a416fd41cec52854863b974af422495663"
}
-- golden/state-get-file-umask-022.json --
{
"type": "file",
"mode": 420,
Expand Down
17 changes: 14 additions & 3 deletions pkg/cmd/testdata/scripts/issue2092.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,28 @@

# test that chezmoi dump warns when the config file has not been generated
exec chezmoi dump
cmp stdout golden/dump.json
[umask:002] cmp stdout golden/dump-umask-002.json
[umask:022] cmp stdout golden/dump-umask-022.json
stderr 'config file template has changed'

# test that chezmoi dump does not return an error when the config file template has been modified
exec chezmoi init
edit $CHEZMOICONFIGDIR${/}chezmoi.toml
exec chezmoi dump
cmp stdout golden/dump.json
[umask:002] cmp stdout golden/dump-umask-002.json
[umask:022] cmp stdout golden/dump-umask-022.json
! stderr .

-- golden/dump.json --
-- golden/dump-umask-002.json --
{
".file": {
"type": "file",
"name": ".file",
"contents": "# contents of .file\n",
"perm": 436
}
}
-- golden/dump-umask-022.json --
{
".file": {
"type": "file",
Expand Down
22 changes: 16 additions & 6 deletions pkg/cmd/testdata/scripts/textconv.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@

# test that chezmoi diff uses textconv
exec chezmoi diff
cmp stdout golden/diff
[umask:002] cmp stdout golden/diff-umask-002
[umask:022] cmp stdout golden/diff-umask-022

# test that chezmoi apply uses textconv
exec chezmoi apply --verbose
cmp stdout golden/diff
[umask:002] cmp stdout golden/diff-umask-002
[umask:022] cmp stdout golden/diff-umask-022
cmp $HOME/file.txt golden/file.txt

# test that chezmoi apply uses textconv in interactive diffs
Expand All @@ -17,17 +19,25 @@ exec chezmoi apply --no-tty
stdout '^ # CONTENTS OF FILE.TXT'
stdout '^-# EDITED'

-- golden/diff --
-- golden/diff-overwrite --
diff
overwrite
-- golden/diff-umask-002 --
diff --git a/file.txt b/file.txt
index b0ebb2af412bf3812b0bf8c5d7b950feb8a701be..4d977287915d918f15ef3df13146c1fa58914d30 100664
--- a/file.txt
+++ b/file.txt
@@ -1 +1 @@
-# PREVIOUS CONTENTS OF FILE.TXT
+# CONTENTS OF FILE.TXT
-- golden/diff-umask-022 --
diff --git a/file.txt b/file.txt
index b0ebb2af412bf3812b0bf8c5d7b950feb8a701be..4d977287915d918f15ef3df13146c1fa58914d30 100644
--- a/file.txt
+++ b/file.txt
@@ -1 +1 @@
-# PREVIOUS CONTENTS OF FILE.TXT
+# CONTENTS OF FILE.TXT
-- golden/diff-overwrite --
diff
overwrite
-- golden/file.txt --
# contents of file.txt
-- home/user/.config/chezmoi/chezmoi.yaml --
Expand Down
2 changes: 2 additions & 0 deletions pkg/cmd/testdata/scripts/verify.txtar
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 a3f041c

Please sign in to comment.