From a54a3dead5856547f221acfa28ff1f994ca923ef Mon Sep 17 00:00:00 2001 From: Ilan Cosman Date: Fri, 8 May 2020 22:48:35 -0700 Subject: [PATCH] Add lots of tests --- dev/bench.fish | 11 ++++++ functions/lean_right_prompt.fish | 19 ++++------ install.fish | 2 +- lean_theme/functions/configure.fish | 2 +- tests/cmd_duration.fish | 15 ++++++++ tests/context.fish | 11 ++++++ tests/jobs.fish | 9 +++++ tests/pwd.fish | 58 +++++++++-------------------- tests/status.fish | 16 ++++---- 9 files changed, 81 insertions(+), 62 deletions(-) create mode 100644 dev/bench.fish create mode 100644 tests/cmd_duration.fish create mode 100644 tests/context.fish create mode 100644 tests/jobs.fish diff --git a/dev/bench.fish b/dev/bench.fish new file mode 100644 index 0000000..2d8e305 --- /dev/null +++ b/dev/bench.fish @@ -0,0 +1,11 @@ +function bench -a command times + set -l thingToRun + + for i in (seq $times) + set -a thingToRun $command + end + + eval (string join ';' $thingToRun) >>'/dev/null' + + math $CMD_DURATION/$times +end \ No newline at end of file diff --git a/functions/lean_right_prompt.fish b/functions/lean_right_prompt.fish index 5d4eebf..c1008d3 100644 --- a/functions/lean_right_prompt.fish +++ b/functions/lean_right_prompt.fish @@ -30,24 +30,19 @@ function _lean_status end function _lean_cmd_duration - set -l seconds (math --scale=$lean_cmd_duration_decimals $CMD_DURATION/1000) + if test $CMD_DURATION -gt $lean_cmd_duration_threshold + set -l seconds (math --scale=$lean_cmd_duration_decimals "$CMD_DURATION/1000" % 60) + set -l minutes (math -s0 "$seconds/60" % 60) + set -l hours (math -s0 "$minutes/60" % 60) - if test $seconds -gt $lean_cmd_duration_threshold - set -l secondsMod (math $seconds % 60) - - set -l minutes (math -s0 $seconds/60) - set -l minutesMod (math $minutes % 60) - - set -l hours (math -s0 $minutes/60) - - for time in hours minutesMod secondsMod + for time in hours minutes seconds if test $$time -eq 0 set -e $time end end - set_color $lean_cmd_duration - echo -n {$hours}'h' {$minutesMod}'m' {$gsecondsMod}'s' + set_color $lean_cmd_duration_color + echo -n {$hours}'h' {$minutes}'m' {$seconds}'s' end end diff --git a/install.fish b/install.fish index cf6ccb3..1bca249 100644 --- a/install.fish +++ b/install.fish @@ -64,7 +64,7 @@ function lean_install # --------------Cmd_Duration-------------- set -U lean_cmd_duration_color 87875F set -U lean_cmd_duration_decimals 0 - set -U lean_cmd_duration_threshold 3 + set -U lean_cmd_duration_threshold 3000 # -------------Context------------- set -U lean_context_ssh_color D7AF87 set -U lean_context_root_color D7AF00 diff --git a/lean_theme/functions/configure.fish b/lean_theme/functions/configure.fish index df1a187..6e0890d 100644 --- a/lean_theme/functions/configure.fish +++ b/lean_theme/functions/configure.fish @@ -247,7 +247,7 @@ function _displayRestartAndQuit echo -e '(q) Quit and do nothing\n' end -function _quit +function _quit --on-signal INT functions -e fish_right_prompt source $fishPrompt clear diff --git a/tests/cmd_duration.fish b/tests/cmd_duration.fish new file mode 100644 index 0000000..bcb89e3 --- /dev/null +++ b/tests/cmd_duration.fish @@ -0,0 +1,15 @@ +# Fishtape test +source "$__fish_config_dir/functions/lean_right_prompt.fish" + +function _cmd_duration -a duration threshold decimals + set -g CMD_DURATION $duration + set -g lean_cmd_duration_threshold $threshold + set -g lean_cmd_duration_decimals $decimals + + lean_decolor (_lean_cmd_duration) +end + +@test 'Less than threshold' (_cmd_duration 2000 3000 0) -z +@test 'Decimals' (_cmd_duration 4567 3000 3) = '4.567s' +@test 'Minutes' (_cmd_duration 456700 3000 0) = '7m 36s' +@test 'Hours' (_cmd_duration 4567000 3000 0) = '1h 16m 7s' \ No newline at end of file diff --git a/tests/context.fish b/tests/context.fish new file mode 100644 index 0000000..ddeebe0 --- /dev/null +++ b/tests/context.fish @@ -0,0 +1,11 @@ +# Fishtape test +set -l sourceFile "$__fish_config_dir/functions/lean_right_prompt.fish" +source $sourceFile + +function _context + lean_decolor (_lean_context) +end + +@test 'None' (_context) -z +@test 'SSH' (set -g SSH_TTY 'true'; _context) = $USER'@'(prompt_hostname) +@test 'Root' (lean_decolor (su -c "source $sourceFile;_lean_context")) = 'root@'(prompt_hostname) \ No newline at end of file diff --git a/tests/jobs.fish b/tests/jobs.fish new file mode 100644 index 0000000..a0eed43 --- /dev/null +++ b/tests/jobs.fish @@ -0,0 +1,9 @@ +# Fishtape test +source "$__fish_config_dir/functions/lean_right_prompt.fish" + +function _jobs + lean_decolor (_lean_jobs) +end + +@test 'No Jobs' (_jobs) -z +@test 'Jobs' (sleep 60 &; _jobs) = "$lean_jobs_icon" \ No newline at end of file diff --git a/tests/pwd.fish b/tests/pwd.fish index a5fd9f2..48498dc 100644 --- a/tests/pwd.fish +++ b/tests/pwd.fish @@ -1,43 +1,21 @@ -function test_pwd - set -l dB (set_color $lean_color_dark_blue) - set -l lB (set_color -o $lean_color_light_blue) - set -l l (set_color $lean_color_lilac) - set -l n (set_color normal) - - set -l unwr $dB"$lean_pwd_unwritable_icon "$n - - echo 'Please perform a visual inspection.' - echo '------------' - echo 'OUTPUT' - echo 'ANSWER' - echo '------------' - _comparePwd '/' (_joinSlash $unwr '') - _comparePwd '/usr' (_joinSlash $unwr $lB'usr') - _comparePwd '/usr/share' (_joinSlash $unwr $lB'usr' $lB'share') - _comparePwd '/usr/share/fish' (_joinSlash $unwr $lB'usr' $dB'share' $lB'fish') - - _comparePwd "$HOME" (_joinSlash $lB'~') - _comparePwd "$HOME/.config" (_joinSlash $lB'~' $lB'.config') - _comparePwd "$HOME/.config/fish" (_joinSlash $lB'~' $dB'.config' $lB'fish') - - set -l longDir "$HOME/alfa/bravo/charlie/delta/echo/foxtrot/golf/hotel/inda/juliett/kilo/lima/mike/november/oscar/papa" - set -l longDirAnswerLilac $l{'a','b','c','d','e','f'} - set -l longDirAnswerDarkBlue $dB{'golf','hotel','inda','juliett','kilo','lima','mike','november','oscar'} - set -l longDirAnswer (_joinSlash $lB'~' $longDirAnswerLilac $longDirAnswerDarkBlue $lB'papa') - if not test -e $longDir - mkdir -p $longDir - end - _comparePwd $longDir $longDirAnswer - rm -r $longDir -end - -function _comparePwd -a dir answer +# Fishtape test +function _pwd -a dir cd $dir - echo (lean_pwd) - echo $answer + lean_decolor (lean_pwd) end -function _joinSlash - set -l slash (set_color $lean_color_dark_blue)'/'(set_color normal) - string join $slash {$argv}(set_color normal) -end \ No newline at end of file +set -l unwr "$lean_pwd_unwritable_icon " + +@test '/' (_pwd '/') = $unwr'/' +@test '/usr' (_pwd '/usr') = $unwr'/usr' +@test '/usr/share' (_pwd '/usr/share') = $unwr'/usr/share' +@test '~' (_pwd "$HOME") = '~' +@test '~/.config' (_pwd "$HOME/.config") = '~/.config' +@test '~/.config/fish' (_pwd "$HOME/.config/fish") = '~/.config/fish' + +set -l longDir "$HOME/alfa/bravo/charlie/delta/echo/foxtrot/golf/hotel/inda/juliett/kilo/lima/mike/november/oscar/papa" +if not test -e $longDir + mkdir -p $longDir +end +@test 'Long dir' (_pwd "$longDir") = '~/a/b/c/d/e/f/golf/hotel/inda/juliett/kilo/lima/mike/november/oscar/papa' +rm -r $longDir \ No newline at end of file diff --git a/tests/status.fish b/tests/status.fish index 1063569..5a8ebed 100644 --- a/tests/status.fish +++ b/tests/status.fish @@ -1,15 +1,15 @@ # Fishtape test source "$__fish_config_dir/functions/lean_right_prompt.fish" -function _case +function _status set -g last_pipestatus $pipestatus set -g last_status $status - _lean_status + lean_decolor (_lean_status) end -@test 'true' (true;_case) -z -@test 'false' (false;_case) -z -@test 'true|false' (true|false;_case) = '✘ 0|1' -@test 'true|true' (true|true;_case) -z -@test 'false|true' (false|true;_case) = '✔ 1|0' -@test 'false|false' (false|false;_case) = '✘ 1|1' +@test 'true' (true; _status) -z +@test 'false' (false; _status) -z +@test 'true|false' (true|false; _status) = "$lean_status_failure_icon 0|1" +@test 'true|true' (true|true; _status) -z +@test 'false|true' (false|true; _status) = "$lean_status_success_icon 1|0" +@test 'false|false' (false|false; _status) = "$lean_status_failure_icon 1|1" \ No newline at end of file