[windows] skip 'UNIX only'
[!exec:sed] skip 'sed not found in $PATH'
[!umask:022] skip 'unsupported umask'

cp golden/.modify home/user

# test that chezmoi cat prints the modified contents without modifying the file
chezmoi cat $HOME${/}.modify
cmp stdout golden/.modified
cmp home/user/.modify golden/.modify

# test that chezmoi diff prints the diff without modifying the file
chezmoi diff
cmp stdout golden/diff.diff
cmp home/user/.modify golden/.modify

# test that chezmoi archive includes the modified file
chezmoi archive --output=archive.tar
exec tar xf archive.tar
cmp .modify golden/.modified
cmp home/user/.modify golden/.modify

# test that chezmoi apply modifies the file
chezmoi apply --force
cmp home/user/.modify golden/.modified

chhome home2/user

# test that chezmoi cat does not fail or generate output when the target does not exist
chezmoi cat $HOME${/}.not_exist
! stdout .

# test that chezmoi cat exits with an error when the modify script fails
! chezmoi cat $HOME${/}.error
stderr error

# test that chezmoi apply updates file permissions
cmpmod 666 $HOME/.file
chezmoi apply $HOME${/}.file
cmpmod 700 $HOME/.file

chhome home3/user

# test that chezmoi apply always overwrites modified files without --force
chezmoi add $HOME${/}.modify
chezmoi apply
edit $HOME${/}.modify
rm $CHEZMOISOURCEDIR/dot_modify
cp home/user/.local/share/chezmoi/modify_dot_modify $CHEZMOISOURCEDIR
chezmoi apply
cmp $HOME${/}.modify golden/.edited-and-modified

chhome home4/user

# test that modify scripts can be templates
chezmoi cat $HOME${/}.modify
cmp stdout golden/.modified

-- golden/.edited-and-modified --
beginning
modified-middle
end
# edited
-- golden/.modified --
beginning
modified-middle
end
-- golden/.modify --
beginning
middle
end
-- golden/diff.diff --
diff --git a/.modify b/.modify
index f91830d4ecd80adfe9a6aea9dca579397aa86921..6b6d41aae5e8d64a54afd8b8ad5a38a3de1e1e35 100644
--- a/.modify
+++ b/.modify
@@ -1,3 +1,3 @@
 beginning
-middle
+modified-middle
 end
-- home/user/.local/share/chezmoi/modify_dot_modify --
#!/bin/sh

sed 's/middle/modified-middle/g'
-- home2/user/.file --
# contents of .file
-- home2/user/.local/share/chezmoi/modify_dot_not_exist --
#!/bin/sh

cat
-- home2/user/.local/share/chezmoi/modify_dot_error --
#!/bin/sh

echo error >2
exit 1
-- home2/user/.local/share/chezmoi/modify_private_executable_dot_file --
#!/bin/sh

cat
-- home3/user/.modify --
beginning
middle
end
-- home4/user/.modify --
beginning
middle
end
-- home4/user/.local/share/chezmoi/modify_dot_modify.tmpl --
#!/bin/sh

{{ "sed 's/middle/modified-middle/g'" }}
