first commit

This commit is contained in:
afoucaultc 2026-06-05 13:11:08 +02:00
commit 205faf4224
5471 changed files with 973850 additions and 0 deletions

View file

@ -0,0 +1,35 @@
#!/usr/bin/env zsh
# Clear any existing env vars that can cause conflicts
unset YSU_MESSAGE_POSITION
unset YSU_HARDCORE_ALIASES
unset YSU_HARDCORE
# Create isolated git environment using only GIT_CONFIG_GLOBAL
export GIT_CONFIG_GLOBAL="$(mktemp)"
export GIT_CONFIG_NOSYSTEM=1
# Simplify format for tests
export YSU_MESSAGE_FORMAT='Found existing %alias_type for "%command". You should use: "%alias"'
# Exit code for hardcore mode
export HARDCORE_EXIT_CODE=130
# Mock the kill command to avoid killing the test process
function kill() {
echo "kill called with: $*"
return $HARDCORE_EXIT_CODE
}
# Source the plugin
source "$PWD/you-should-use.plugin.zsh"
function cleanup() {
# Ensure the temporary git config file is removed
if [[ -n "$GIT_CONFIG_GLOBAL" && -f "$GIT_CONFIG_GLOBAL" ]]; then
echo "Removing temporary git config file:"
rm -v -f "$GIT_CONFIG_GLOBAL"
fi
}
trap cleanup EXIT

View file

@ -0,0 +1,160 @@
#!/usr/bin/env zunit
@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,51 @@
#!/usr/bin/env zunit
@setup {
_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,65 @@
#!/usr/bin/env zunit
@test 'git aliases not triggered by aliased git commands' {
git config --global alias.co checkout
run _check_git_aliases "gco" "git checkout"
assert "$output" is_empty
assert $state equals 0
}
@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 "$output" is_empty
assert $state equals 0
}
@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,55 @@
#!/usr/bin/env zunit
@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"'
# hardcore mode is not enabled and should not show up
assert "$output" does_not_contain 'hardcore mode enabled'
assert $state equals 0
}
@test 'global aliases match - with hardcore mode enabled' {
alias -g NE="2>/dev/null"
export YSU_HARDCORE=1
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 "$output" contains 'hardcore mode enabled'
assert $state equals $HARDCORE_EXIT_CODE
}
@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,84 @@
#!/usr/bin/env zunit
@teardown {
unset YSU_HARDCORE_ALIASES
unset YSU_HARDCORE
}
@test 'hardcore aliases - triggers hardcore message when alias is in list' {
export YSU_HARDCORE_ALIASES=("gs" "ll")
alias gs="git status"
alias ll="ls -l"
run _check_ysu_hardcore "gs"
assert $state equals $HARDCORE_EXIT_CODE
assert "$output" contains "You Should Use hardcore mode enabled"
}
@test 'hardcore aliases - does not kill when alias not in hardcore list' {
export YSU_HARDCORE_ALIASES=("gs" "ll")
alias gs="git status"
alias gco="git checkout"
run _check_ysu_hardcore "gco"
assert $state equals 0
assert "$output" is_empty
}
@test 'hardcore aliases - does not activate when YSU_HARDCORE is set' {
export YSU_HARDCORE=1
export YSU_HARDCORE_ALIASES=("gs")
alias gs="git status"
# YSU_HARDCORE takes precedence, should still kill but with general message
run _check_ysu_hardcore "gs"
assert $state equals $HARDCORE_EXIT_CODE # SIGINT exit code
}
@test 'hardcore aliases - integration with check_aliases in BESTMATCH mode' {
export YSU_HARDCORE_ALIASES=("gs")
alias gs="git status"
alias g="git"
# This should find gs alias and trigger hardcore mode
run _check_aliases "git status"
assert $state equals $HARDCORE_EXIT_CODE # SIGINT exit code
}
@test 'hardcore aliases - integration with check_aliases in ALL mode' {
export YSU_MODE="ALL"
export YSU_HARDCORE_ALIASES=("gs")
alias gs="git status"
alias g="git"
# This should find both aliases but only kill for gs
run _check_aliases "git status"
assert $state equals $HARDCORE_EXIT_CODE # SIGINT exit code
}
@test 'hardcore aliases - does not trigger when hardcore alias not found' {
export YSU_HARDCORE_ALIASES=("gco")
alias gs="git status"
alias g="git"
# Should show normal reminder without killing
run _check_aliases "git status"
assert $state equals 0
assert "$output" contains 'Found existing alias for "git status". You should use: "gs"'
}
@test 'hardcore aliases - empty array does not trigger' {
export YSU_HARDCORE_ALIASES=()
alias gs="git status"
run _check_ysu_hardcore "gs"
assert $state equals 0
assert "$output" is_empty
}

View file

@ -0,0 +1,94 @@
#!/usr/bin/env zunit
@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"
}