# test that chezmoi init creates a config file and updates the state
exec chezmoi init
cmp $CHEZMOICONFIGDIR/chezmoi.toml golden/chezmoi.toml
exec chezmoi state dump --format=yaml
cmp stdout golden/state-dump.yaml
! stderr .

# test that chezmoi apply succeeds
exec chezmoi apply
! stderr .

# test that chezmoi apply prints a warning if the config file template has been changed
cp golden/.chezmoi.toml.tmpl $CHEZMOISOURCEDIR/.chezmoi.toml.tmpl
exec chezmoi apply
stderr 'warning: config file template has changed'

# test that chezmoi apply does not print the warning if it is suppressed
appendline $CHEZMOICONFIGDIR/chezmoi.toml '[warnings]'
appendline $CHEZMOICONFIGDIR/chezmoi.toml '  configFileTemplateHasChanged = false'
exec chezmoi apply
! stderr .
cp golden/chezmoi.toml $CHEZMOICONFIGDIR

# test that chezmoi init re-generates the config file
exec chezmoi init
cmp $CHEZMOICONFIGDIR/chezmoi.toml golden/chezmoi.toml
! stderr .

# test that chezmoi apply no longer prints a warning after the config file is regenerated
exec chezmoi apply
! stderr .

# test that chezmoi apply --force ignores config file changes and updates the state
edit $CHEZMOISOURCEDIR/.chezmoi.toml.tmpl
grep '# edited' $CHEZMOISOURCEDIR/.chezmoi.toml.tmpl
exec chezmoi apply --force
! stderr .
! grep '# edited' $CHEZMOICONFIGDIR/chezmoi.toml

chhome home2/user

# test that chezmoi diff prints a warning when a config file template is added
exec chezmoi diff
! stderr .
cp golden/chezmoi.toml $CHEZMOISOURCEDIR/.chezmoi.toml.tmpl
exec chezmoi diff
stderr 'warning: config file template has changed'

# test that chezmoi diff does not print a warning when the config file template is removed
rm $CHEZMOISOURCEDIR/.chezmoi.toml.tmpl
exec chezmoi diff
! stderr .

-- golden/.chezmoi.toml.tmpl --
# chezmoi:template:left-delimiter="((" right-delimiter="))"
(( $email := get . "email" -))
(( if not $email -))
((   $email = promptString "email" -))
(( end -))
[data]
    email = (( $email | quote ))
-- golden/chezmoi.toml --
[data]
    email = "me@home.org"
-- golden/state-dump.yaml --
configState:
    configState:
        configTemplateContentsSHA256: af43121a524340707b84e390f510c949731177e6f2a25b3b6b11b2fc656cf8f2
entryState: {}
gitRepoExternalState: {}
scriptState: {}
-- home/user/.local/share/chezmoi/.chezmoi.toml.tmpl --
[data]
    email = "me@home.org"
-- home/user/.local/share/chezmoi/.git/.keep --
-- home2/user/.local/share/chezmoi/.keep --

