mkhomedir
mksourcedir golden

# test chezmoi add --create
chezmoi add --create $HOME${/}.create
cmp $CHEZMOISOURCEDIR/create_dot_create golden/create_dot_create

# test that adding a directory creates a .keep file
chezmoi add --recursive=false $HOME${/}.dir
exists $CHEZMOISOURCEDIR/dot_dir/.keep

# test adding a file in a directory
chezmoi add $HOME${/}.dir/file
cmp $CHEZMOISOURCEDIR/dot_dir/file golden/dot_dir/file

# test adding a subdirectory
chezmoi add --exact $HOME${/}.dir/subdir
cmp $CHEZMOISOURCEDIR/dot_dir/exact_subdir/file golden/dot_dir/exact_subdir/file

# test adding an empty file
chezmoi add $HOME${/}.empty
exists $CHEZMOISOURCEDIR/empty_dot_empty

# test adding an executable file
chezmoi add $HOME${/}.executable
[!windows] cmp $CHEZMOISOURCEDIR/executable_dot_executable golden/executable_dot_executable
[windows] cmp $CHEZMOISOURCEDIR/dot_executable golden/executable_dot_executable

# test adding a private file
chezmoi add $HOME${/}.private
[!windows] cmp $CHEZMOISOURCEDIR/private_dot_private $HOME/.private
[windows] cmp $CHEZMOISOURCEDIR/dot_private $HOME/.private

# test adding a symlink
chezmoi add $HOME${/}.symlink
cmp $CHEZMOISOURCEDIR/symlink_dot_symlink golden/symlink_dot_symlink

# test adding a symlink with a separator
symlink $HOME/.symlink2 -> .dir/subdir/file
chezmoi add $HOME${/}.symlink2
cmp $CHEZMOISOURCEDIR/symlink_dot_symlink2 golden/symlink_dot_symlink

# test adding a symlink with --follow
symlink $HOME${/}.symlink3 -> .file
chezmoi add --follow $HOME${/}.symlink3
cmp $CHEZMOISOURCEDIR/dot_symlink3 golden/dot_file

chhome home2/user

# test that chezmoi add only creates .keep files in empty directories
mkdir $HOME/.dir/empty_subdir
chezmoi add $HOME${/}.dir
! exists $CHEZMOISOURCEDIR/dot_dir/.keep
exists $CHEZMOISOURCEDIR/dot_dir/empty_subdir/.keep
! exists $CHEZMOISOURCEDIR/dot_dir/non_empty_subdir/.keep

chhome home3/user

# test that chezmoi add respects .chezmoiignore
chezmoi add $HOME${/}.dir
exists $CHEZMOISOURCEDIR/dot_dir/file
! exists $CHEZMOISOURCEDIR/dot_dir/ignore

chhome home4/user

# test that chezmoi add does not overwrite an already-added file
chezmoi add $HOME/.file
cmp $CHEZMOISOURCEDIR/dot_file golden/dot_file
edit $HOME/.file
cmp $CHEZMOISOURCEDIR/dot_file golden/dot_file

# test that chezmoi add --force does overwrite an already-added file
chezmoi add --force $HOME/.file
cmp $CHEZMOISOURCEDIR/dot_file golden/edited_dot_file

-- golden/edited_dot_file --
# contents of .file
# edited
-- home2/user/.dir/non_empty_subdir/file --
# contents of .dir/non_empty_subdir/file
-- home3/user/.local/share/chezmoi/.chezmoiignore --
**/ignore
-- home3/user/.dir/file --
# contents of .dir/file
-- home3/user/.dir/ignore --
# contents of .dir/ignore
-- home4/user/.file --
# contents of .file
