mksourcedir

# test that chezmoi chattr empty sets the empty attribute on a file
exists $CHEZMOISOURCEDIR/dot_file
chezmoi chattr empty $HOME${/}.file
! exists $CHEZMOISOURCEDIR/dot_file
exists $CHEZMOISOURCEDIR/empty_dot_file

# test that chezmoi attr +p sets the private attribute on a file
chezmoi chattr +p $HOME${/}.file
! exists $CHEZMOISOURCEDIR/empty_dot_file
exists $CHEZMOISOURCEDIR/private_empty_dot_file

# test that chezmoi chattr t,-e sets the template attribute and removes the empty attribute on a file
chezmoi chattr t,-e $HOME${/}.file
! exists $CHEZMOISOURCEDIR/private_empty_dot_file
exists $CHEZMOISOURCEDIR/private_dot_file.tmpl

# test that chezmoi chattr -- -e,-p,r sets the readonly attribute on a file and removes the empty and private attributes
chezmoi chattr -- -e,-p,r $HOME${/}.file
! exists $CHEZMOISOURCEDIR/private_dot_file.tmpl
exists $CHEZMOISOURCEDIR/readonly_dot_file.tmpl

# test that chezmoi chattr -- -r,-t removes the readonly and template attributes on a file
chezmoi chattr -- -r,-t $HOME${/}.file
! exists $CHEZMOISOURCEDIR/readonly_dot_file.tmpl
exists $CHEZMOISOURCEDIR/dot_file

# test that chezmoi chattr +create changes a file to be a create_ file
chezmoi chattr +create $HOME${/}.file
! exists $CHEZMOISOURCEDIR/dot_file
exists $CHEZMOISOURCEDIR/create_dot_file

# test that chezmoi chattr nomodify does not change a create_ file
chezmoi chattr nomodify $HOME${/}.file
! exists $CHEZMOISOURCEDIR/dot_file
exists $CHEZMOISOURCEDIR/create_dot_file

# test that chezmoi chattr modify,script,symlink changes a create_ file to a symlink_
chezmoi chattr modify,script,symlink $HOME${/}.file
! exists $CHEZMOISOURCEDIR/create_dot_file
exists $CHEZMOISOURCEDIR/symlink_dot_file

# test that chezmoi chattr -- -symlink changes a symlink_ to a regular file
chezmoi chattr -- -symlink $HOME${/}.file
! exists $CHEZMOISOURCEDIR/symlink_dot_file
exists $CHEZMOISOURCEDIR/dot_file

# test that chezmoi chattr nox removes the execute attribute on a file
exists $CHEZMOISOURCEDIR/executable_dot_executable
chezmoi chattr nox $HOME${/}.executable
! exists $CHEZMOISOURCEDIR/executable_dot_executable
exists $CHEZMOISOURCEDIR/dot_executable

# test that chezmoi chattr x sets the executable attribute on a file
chezmoi chattr x $HOME${/}.executable
! exists $CHEZMOISOURCEDIR/dot_executable
exists $CHEZMOISOURCEDIR/executable_dot_executable

# test that chezmoi chattr +private sets the private attribute on a create file
chezmoi chattr +private $HOME${/}.create
! exists $CHEZMOISOURCEDIR/create_dot_create
exists $CHEZMOISOURCEDIR/create_private_dot_create

# test that chezmoi chattr noprivate removes the private attribute on a create file
chezmoi chattr noprivate $HOME${/}.create
! exists $CHEZMOISOURCEDIR/create_private_dot_create
exists $CHEZMOISOURCEDIR/create_dot_create

# test that chezmoi chattr exact sets the exact attribute on a directory
exists $CHEZMOISOURCEDIR/dot_dir
chezmoi chattr exact $HOME${/}.dir
! exists $CHEZMOISOURCEDIR/dot_dir
exists $CHEZMOISOURCEDIR/exact_dot_dir

# test that chezmoi chattr readonly sets the readonly attribute on a directory
chezmoi chattr readonly $HOME${/}.dir
! exists $CHEZMOISOURCEDIR/exact_dot_dir
exists $CHEZMOISOURCEDIR/exact_readonly_dot_dir

# test that chezmoi chattr +t sets the template attribute on a symlink
exists $CHEZMOISOURCEDIR/symlink_dot_symlink
chezmoi chattr +t $HOME${/}.symlink
! exists $CHEZMOISOURCEDIR/symlink_dot_symlink
exists $CHEZMOISOURCEDIR/symlink_dot_symlink.tmpl

# test that chezmoi chattr -- -t removes the template attribute on a symlink
chezmoi chattr -- -t $HOME${/}.symlink
! exists $CHEZMOISOURCEDIR/symlink_dot_symlink.tmpl
exists $CHEZMOISOURCEDIR/symlink_dot_symlink

# test that chezmoi chattr -- before sets the before attribute on a script
chezmoi chattr -- before $HOME/script.sh
! exists $CHEZMOISOURCEDIR/run_script.sh
exists $CHEZMOISOURCEDIR/run_before_script.sh

# test that chezmoi chattr -- once sets the once attribute on a script
chezmoi chattr -- once $HOME/script.sh
! exists $CHEZMOISOURCEDIR/run_before_script.sh
exists $CHEZMOISOURCEDIR/run_once_before_script.sh

# test that chezmoi chattr -- after sets the after attribute on a script and removes the before attribute
chezmoi chattr -- after $HOME/script.sh
! exists $CHEZMOISOURCEDIR/run_once_before_script.sh
exists $CHEZMOISOURCEDIR/run_once_after_script.sh

# test that chezmoi chattr onchange sets the onchange attribute on a script and removes the only attribute
chezmoi chattr -- onchange $HOME/script.sh
! exists $CHEZMOISOURCEDIR/run_once_after_script.sh
exists $CHEZMOISOURCEDIR/run_onchange_after_script.sh

# test that chezmoi chattr -- -onchange removes the onchange attribute on a script
chezmoi chattr -- -onchange $HOME/script.sh
! exists $CHEZMOISOURCEDIR/run_onchange_after_script.sh
exists $CHEZMOISOURCEDIR/run_after_script.sh

# test that chezmoi chattr -- -a removes the after attribute on a script
chezmoi chattr -- -a $HOME/script.sh
! exists $CHEZMOISOURCEDIR/run_after_script.sh
exists $CHEZMOISOURCEDIR/run_script.sh

# test that chezmoi chattr +executable,+private,+readonly,+template sets the attributes on a modify script
chezmoi chattr +executable,+private,+readonly,+template $HOME${/}.modify
! exists $CHEZMOISOURCEDIR/modify_dot_modify
exists $CHEZMOISOURCEDIR/modify_private_readonly_executable_dot_modify.tmpl

# test that chezmoi chattr --dry-run --verbose generates a diff when a file is renamed
chezmoi chattr --dry-run --verbose +executable $HOME${/}.file
cmp stdout golden/chattr-diff

-- golden/chattr-diff --
diff --git a/dot_file b/executable_dot_file
rename from dot_file
rename to executable_dot_file
-- home/user/.local/share/chezmoi/run_script.sh --
#!/bin/sh
-- home/user/.local/share/chezmoi/modify_dot_modify --
#!/bin/sh

cat
