#!/usr/bin/env zunit

@setup {
    load "../you-should-use.plugin.zsh"
    YSU_MESSAGE_FORMAT='Found existing %alias_type for "%command". You should use: "%alias"'
    unset YSU_MESSAGE_POSITION

    if [[ ! -f ~/.gitconfig ]]; then
        touch ~/.gitconfig
    fi

    # Remove use git configuration which may interfere with tests
    mv ~/.gitconfig ~/.gitconfig.bak
    touch ~/.gitconfig
}

@teardown {
    # Restore users git configuration
    mv ~/.gitconfig.bak ~/.gitconfig
}

@test 'git aliases not triggered by aliased git commands' {
   git config --global alias.co checkout
   run _check_git_aliases "gco" "git checkout"

   assert $state equals 0
   assert "$output" is_empty
}

@test 'git aliases not triggred by non git commands' {
   git config --global alias.cp cherry-pick
   run _check_git_aliases "ls -l" "ls -l"

   assert $state equals 0
   assert "$output" is_empty
}

@test 'git aliases no match' {
   run _check_git_aliases "git config --list" "git config --list"

   assert "$output" is_empty
   assert $state equals 0
}

@test 'git aliases substring match' {
   git config --global alias.cfg config
   run _check_git_aliases "git config --list" "git config --list"

   assert "$output" contains 'Found existing git alias for "config". You should use: "git cfg"'
   assert $state equals 0
}

@test 'git aliases ignore on sudo' {
   git config --global alias.cfg config
   run _check_git_aliases "sudo git config --list" "sudo git config --list"

   assert "$output" is_empty
   assert $state equals 0
}

@test 'git aliases exact match' {
   git config --global alias.st status
   run _check_git_aliases "git status" "git status"

   assert "$output" contains 'Found existing git alias for "status". You should use: "git st"'
   assert $state equals 0
}

@test 'git aliases match parameters' {
   git config --global alias.recommit "commit --amend --reuse-message=HEAD"
   run _check_git_aliases "git commit --amend --reuse-message=HEAD" "git commit --amend --reuse-message=HEAD"

   assert "$output" contains 'Found existing git alias for "commit --amend --reuse-message=HEAD". You should use: "git recommit"'
   assert $state equals 0
}

@test 'git aliases not triggered when parameters are different' {
   git config --global alias.recommit "commit --amend --reuse-message=HEAD"
   run _check_git_aliases "git commit" "git commit"

   assert "$output" is_empty
   assert $state equals 0
}
