feat: add plugins for zsh

This commit is contained in:
2024-09-22 20:14:22 +04:00
parent f4c1c54e72
commit 6c85cc3b71
515 changed files with 30838 additions and 0 deletions

View File

@@ -0,0 +1,170 @@
#!/usr/bin/env zunit
@setup {
load "../you-should-use.plugin.zsh"
# Simplify format for tests
YSU_MESSAGE_FORMAT='Found existing %alias_type for "%command". You should use: "%alias"'
unset YSU_MESSAGE_POSITION
}
@teardown {
}
@test 'aliases no match' {
export YSU_MODE="ALL"
alias ls="less -R"
run _check_aliases "less"
assert "$output" is_empty
assert $state equals 0
}
@test 'aliases longer or the same length as their commands are ignored' {
export YSU_MODE="ALL"
alias longalias='ls -lA'
alias ls_-lA='ls -lA'
run _check_aliases "ls -lA"
assert "$output" is_empty
assert $state equals 0
}
@test 'aliases match - all with alphabetical ordering' {
export YSU_MODE="ALL"
alias c="git status -v"
alias a="git status"
alias b="git"
run _check_aliases "git status -v"
assert $lines[1] contains 'Found existing alias for "git status". You should use: "a"'
assert $lines[2] contains 'Found existing alias for "git". You should use: "b"'
assert $lines[3] contains 'Found existing alias for "git status -v". You should use: "c"'
assert $state equals 0
}
@test 'aliases bestmatch default' {
unset YSU_MODE
alias gs="git status"
alias g="git"
run _check_aliases "git status -v"
assert "$output" contains 'Found existing alias for "git status". You should use: "gs"'
assert $state equals 0
}
@test 'aliases bestmatch - chooses longest matching alias value' {
export YSU_MODE="BESTMATCH"
alias gs="git status"
alias g="git"
alias gsv="git status -v"
run _check_aliases "git status -v"
assert "$output" contains 'Found existing alias for "git status -v". You should use: "gsv"'
assert $state equals 0
}
@test 'aliases bestmatch - chooses longest matching alias value case 2' {
export YSU_MODE="BESTMATCH"
alias gco="git checkout"
alias gcom="git checkout master"
run _check_aliases "git checkout master"
assert "$output" contains 'Found existing alias for "git checkout master". You should use: "gcom"'
assert $state equals 0
}
@test 'aliases bestmatch - chooses shortest alias key on equal value lengths' {
export YSU_MODE="BESTMATCH"
alias v="vim"
alias nvim="vim"
run _check_aliases "vim"
assert "$output" contains 'Found existing alias for "vim". You should use: "v"'
assert $state equals 0
}
@test 'aliases ignore if sudo' {
unset YSU_MODE
alias gs="git status"
run _check_aliases "sudo git status -v"
assert "$output" is_empty
assert $state equals 0
}
@test 'aliases exact match' {
export YSU_MODE="ALL"
alias hu='hg update'
run _check_aliases 'hg update'
assert "$output" contains 'Found existing alias for "hg update". You should use: "hu"'
assert $state equals 0
}
@test 'aliases substring match' {
export YSU_MODE="ALL"
alias hu='hg update'
run _check_aliases 'hg update -all'
assert "$output" contains 'Found existing alias for "hg update". You should use: "hu"'
assert $state equals 0
}
@test 'aliases ignores - does not report an ignored alias' {
export YSU_MODE="ALL"
export YSU_IGNORED_ALIASES=("something" "e" "else")
alias e="echo"
alias l="ls"
run _check_aliases "echo hello"
assert $output is_empty
assert $state equals 0
}
@test 'aliases ignores only - only ignores specified aliases' {
export YSU_MODE="ALL"
export YSU_IGNORED_ALIASES=("something" "else")
alias e="echo"
alias l="ls"
run _check_aliases "echo hello"
assert $output contains 'Found existing alias for "echo"'
assert $state equals 0
}
@test 'dont display recommendations for an alias if it is used' {
alias hu='hg update'
run _check_aliases 'hu' 'hg update'
assert "$output" is_empty
assert $state equals 0
}
@test 'dont display recommendations for an alias if it is used - 2' {
alias eh='echo hello'
run _check_aliases 'eh world' 'echo hello world'
assert "$output" is_empty
assert $state equals 0
}
@test 'dont display recommendations for an alias if it is used - 4' {
alias ..='cd ..'
alias cd..='cd ..'
run _check_aliases '..' 'cd ..'
assert "$output" is_empty
assert $state equals 0
}
@test 'dont display recommendations for an alias if it is used - 3' {
alias ls='ls --color=auto'
alias ll='ls -lh --group-directories-first'
run _check_aliases 'll' 'ls --color=auto -lh --group-directories-first'
assert "$output" is_empty
assert $state equals 0
}

View File

@@ -0,0 +1,53 @@
#!/usr/bin/env zunit
@setup {
load "../you-should-use.plugin.zsh"
unset YSU_MESSAGE_POSITION
_YSU_BUFFER=""
}
@teardown {
rm -f output.txt
}
# We work around not being able to use `run` AND test variable values by redirecting
# all output to a temporary file from which we can read.
@test 'ysu - _write_ysu_buffer before' {
YSU_MESSAGE_POSITION="before"
export _YSU_BUFFER
_write_ysu_buffer "hello world" 2> output.txt
assert $state equals 0
assert "$(< output.txt)" same_as "hello world"
assert "$_YSU_BUFFER" is_empty
}
@test 'ysu - _write_ysu_buffer after' {
YSU_MESSAGE_POSITION="after"
export _YSU_BUFFER
_write_ysu_buffer "hello world" 2> output.txt
assert $state equals 0
assert "$(< output.txt)" is_empty
assert "$_YSU_BUFFER" same_as "hello world"
}
@test 'ysu - _write_ysu_buffer invalid' {
YSU_MESSAGE_POSITION="invalid"
export _YSU_BUFFER
_write_ysu_buffer "" 2> output.txt
assert $state equals 0
expected="$(tput setaf 1)$(tput bold)Unknown value for YSU_MESSAGE_POSITION 'invalid'. "
expected+="Expected value 'before' or 'after'$(tput sgr0)\n"
assert "$(< output.txt)" same_as "$expected"
assert "$_YSU_BUFFER" is_empty
}

View File

@@ -0,0 +1,83 @@
#!/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
}

View File

@@ -0,0 +1,53 @@
#!/usr/bin/env zunit
@setup {
load "../you-should-use.plugin.zsh"
# Simplify format for tests
YSU_MESSAGE_FORMAT='Found existing %alias_type for "%command". You should use: "%alias"'
unset YSU_MESSAGE_POSITION
}
@teardown {
}
@test 'global aliases no match' {
alias -g hw="Hello World!"
run _check_global_aliases "echo 'hello'"
assert "$output" is_empty
assert $state equals 0
}
@test 'global aliases match' {
alias -g NE="2>/dev/null"
run _check_global_aliases "echo 'hello' 2>/dev/null"
assert "$output" contains 'Found existing global alias for "2>/dev/null". You should use: "NE"'
assert $state equals 0
}
@test 'test doesnt match substrings' {
alias -g n="nvim"
run _check_global_aliases "nvimkaowjqwe"
assert "$output" is_empty
assert $state equals 0
}
@test 'global aliases ignore on sudo' {
alias -g NE="2>/dev/null"
run _check_global_aliases "sudo echo 'hello' 2>/dev/null"
assert "$output" is_empty
assert $state equals 0
}
@test 'global aliases - does not report an ignored alias' {
export YSU_MODE="ALL"
export YSU_IGNORED_GLOBAL_ALIASES=("..." "NE")
alias -g ...="../.."
run _check_global_aliases "cd ../.."
assert $output is_empty
assert $state equals 0
}

View File

@@ -0,0 +1,107 @@
#!/usr/bin/env zunit
@setup {
load "../you-should-use.plugin.zsh"
unset YSU_MESSAGE_POSITION
}
@test 'ysu version exported' {
git_version="$(git tag --list | sort | tail -1)"
git tag --list
assert "$YSU_VERSION" is_not_empty
assert "$YSU_VERSION" same_as "$git_version"
}
@test 'ysu preexec functions are loaded by default' {
assert '_check_aliases' in $preexec_functions
assert '_check_git_aliases' in $preexec_functions
assert '_check_global_aliases' in $preexec_functions
assert '_flush_ysu_buffer' in $precmd_functions
}
@test 'ysu disable/enable functions' {
disable_you_should_use
assert '_check_aliases' not_in $preexec_functions
assert '_check_git_aliases' not_in $preexec_functions
assert '_check_global_aliases' not_in $preexec_functions
assert '_flush_ysu_buffer' not_in $precmd_functions
enable_you_should_use
assert '_check_aliases' in $preexec_functions
assert '_check_git_aliases' in $preexec_functions
assert '_check_global_aliases' in $preexec_functions
assert '_flush_ysu_buffer' in $precmd_functions
}
@test 'ysu message correct output' {
unset YSU_MESSAGE_FORMAT
run ysu_message "alias" "ls -l" "ll"
assert $state equals 0
expected="$(tput bold)$(tput setaf 3)Found existing alias for $(tput setaf 5)\"ls -l\"$(tput setaf 3). "
expected+="You should use: $(tput setaf 5)\"ll\"$(tput sgr0)"
assert "$output" same_as "$expected"
}
@test 'ysu message correct output 2' {
unset YSU_MESSAGE_FORMAT
run ysu_message "foobar" "2>/dev/null" "NE"
assert $state equals 0
expected="$(tput bold)$(tput setaf 3)Found existing foobar for $(tput setaf 5)\"2>/dev/null\"$(tput setaf 3). "
expected+="You should use: $(tput setaf 5)\"NE\"$(tput sgr0)"
assert "$output" same_as "$expected"
}
@test 'escapes \ and % correctly' {
unset YSU_MESSAGE_FORMAT
run ysu_message "alias" "printf '%s\\n'" "pf"
assert $state equals 0
expected="$(tput bold)$(tput setaf 3)Found existing alias for $(tput setaf 5)\"printf '%s\\n'\"$(tput setaf 3). "
expected+="You should use: $(tput setaf 5)\"pf\"$(tput sgr0)"
assert "$output" same_as "$expected"
}
@test 'ysu - _write_ysu_buffer after' {
unset YSU_MESSAGE_FORMAT
YSU_MESSAGE_POSITION="after"
_YSU_BUFFER=""
_write_ysu_buffer "hello world"
assert $state equals 0
assert "$output" is_empty
assert "$_YSU_BUFFER" same_as "hello world"
}
@test 'ysu message - custom message' {
export YSU_MESSAGE_FORMAT="Hi there %alias_type! %command <=> %alias"
run ysu_message "git alias" "tig" "t"
assert $state equals 0
assert "$output" same_as "Hi there git alias! tig <=> t"
}
@test 'ysu message - custom message 2' {
export YSU_MESSAGE_FORMAT="%alias is a %alias_type for %command"
run ysu_message "alias" "xdg-open" "xopen"
assert $state equals 0
assert "$output" same_as "xopen is a alias for xdg-open"
}
@test 'ysu message - custom message multiple usages' {
export YSU_MESSAGE_FORMAT="%alias %alias %command %command %alias_type %alias_type"
run ysu_message 'git alias' 'xpaste' 'xp'
assert $state equals 0
assert "$output" same_as "xp xp xpaste xpaste git alias git alias"
}