all repos — scriptura @ 9b7b6f66f69aa1b6e3c1583e8dee81c403852d29

a corner in $HOME for your scripts

add tests

Alan Pearce
commit

9b7b6f66f69aa1b6e3c1583e8dee81c403852d29

parent

b4f4d20d2a08216eea33f2c9a25afda54aba5ece

2 files changed, 261 insertions(+), 18 deletions(-)

changed files
M functions/scriptura.fishfunctions/scriptura.fish
@@ -1,14 +1,13 @@
-if not set --query SCRIPTURA_ROOT - if set --query SD_ROOT +function scriptura + argparse --stop-nonopt h/help n/new e/edit w/which c/cat -- $argv + or return + + set --query SCRIPTURA_ROOT + or if set --query SD_ROOT set --function SCRIPTURA_ROOT $SD_ROOT else set --function SCRIPTURA_ROOT "$HOME/sd" end -end - -function scriptura - argparse --stop-nonopt h/help n/new e/edit w/which c/cat -- $argv - or return if not path is $SCRIPTURA_ROOT if set --query _flag_new
@@ -40,44 +39,48 @@ break
end end - set argv $argv[(math $i + 1)..-1] + set --local begin (math $i + 1) + if test $begin -gt (count $argv) + set begin 1 + end + set --local remaining $argv[$begin..-1] if set --query _flag_help __scriptura_help $target - return 0 + return $status end if set --query _flag_new __scriptura_new $target - return 0 + return $status end if set --query _flag_edit __scriptura_edit $target - return 0 + return $status end if set --query _flag_which - __scriptura_which $target $argv - return 0 + __scriptura_which $target $remaining + return $status end if set --query _flag_cat __scriptura_cat $target - return 0 + return $status end if set --query script if path is --perm exec $target - $target $argv + $target $remaining else echo "scriptura: $target is not executable" >&2 __scriptura_cat $target end - else if path is --type dir $target + else if path is --type dir $target; and not test "$target" = "$SCRIPTURA_ROOT" __scriptura_list $target else - echo "scriptura: don't know what to do with target \"$target\"" >&2 + echo "scriptura: don't know what to do with target \"$remaining\"" >&2 end end
@@ -116,7 +119,7 @@ end
end function __scriptura_desc --argument-names file - string match --groups-only --regex '^#\s*([^!].*)$' <$file + string match --groups-only --regex '^#\s*([^!].*)$' <$file | string join "\n" end function __scriptura_file_help --argument-names file
A tests/scriptura.fish
@@ -0,0 +1,240 @@
+#!/usr/bin/env fish + +# Setup test environment +set --global test_root (mktemp -d) +set --global original_scriptura_root $SCRIPTURA_ROOT +set --global original_sd_root $SD_ROOT + +# Helper function to clean up +function cleanup + rm -rf $test_root + if set --query original_scriptura_root + set --global --export SCRIPTURA_ROOT $original_scriptura_root + else + set --erase --global SCRIPTURA_ROOT + end + if set --query original_sd_root + set --global --export SD_ROOT $original_sd_root + else + set --erase --global SD_ROOT + end +end + +# Test: SCRIPTURA_ROOT defaults to ~/sd when not set +@test "SCRIPTURA_ROOT defaults to ~/sd when not set" ( + set --erase SCRIPTURA_ROOT + set --erase SD_ROOT + source $(status dirname)/../functions/scriptura.fish + scriptura +) = "\$SCRIPTURA_ROOT $HOME/sd does not exist, use `scriptura --new` to create it" + +# Test: SCRIPTURA_ROOT uses SD_ROOT when set +@test "SCRIPTURA_ROOT uses SD_ROOT when set" ( + set --erase SCRIPTURA_ROOT + set --global --export SD_ROOT "/custom/sd" + source $(status dirname)/../functions/scriptura.fish + scriptura +) = "\$SCRIPTURA_ROOT /custom/sd does not exist, use `scriptura --new` to create it" + +# Test: SCRIPTURA_ROOT uses existing value when set +@test "SCRIPTURA_ROOT uses existing value when set" ( + set --global --export SCRIPTURA_ROOT "/existing/root" + set --global --export SD_ROOT "/custom/sd" + source $(status dirname)/../functions/scriptura.fish + scriptura +) = "\$SCRIPTURA_ROOT /existing/root does not exist, use `scriptura --new` to create it" + +# Test: usage is shown when no arguments provided +@test "usage is shown when no arguments provided" -n ( + set --global --export SCRIPTURA_ROOT $test_root + mkdir -p $test_root + scriptura 2>&1 | string match --regex "Usage:" +) + +# Test: error when SCRIPTURA_ROOT doesn't exist and --new not used +@test "error when SCRIPTURA_ROOT doesn't exist and --new not used" -n ( + set --global --export SCRIPTURA_ROOT "/nonexistent/path" + scriptura 2>&1 | string match --regex "does not exist" +) + +# Test: --new creates SCRIPTURA_ROOT if it doesn't exist +@test "--new creates SCRIPTURA_ROOT if it doesn't exist" ( + set --global --export SCRIPTURA_ROOT "$test_root/new_root" + scriptura --new > /dev/null 2>&1 + path is --type dir $test_root/new_root +) $status -eq 0 + +# Test: --new creates a new script +@test "--new creates a new script" ( + set --global --export SCRIPTURA_ROOT $test_root + set --global --export SCRIPTURA_EDITOR "touch" + scriptura --new test_script + path is --type file --perm exec "$test_root/test_script" +) $status -eq 0 + +# Test: --new creates script with shebang +@test "--new creates script with shebang" ( + set --global --export SCRIPTURA_ROOT $test_root + set --global --export SCRIPTURA_EDITOR "touch" + scriptura --new shebang_test + head -n 1 "$test_root/shebang_test" +) = "#!/usr/bin/env fish" + +# Test: --new fails if script already exists +@test "--new fails if script already exists" -n ( + set --global --export SCRIPTURA_ROOT $test_root + touch "$test_root/existing_script" + scriptura --new existing_script 2>&1 | string match --regex "already exists" +) + +# Test: --which shows path for executable script +@test "--which shows path for executable script" ( + set --global --export SCRIPTURA_ROOT $test_root + printf "#!/usr/bin/env fish\necho test\n" > "$test_root/which_test" + chmod +x "$test_root/which_test" + scriptura --which which_test +) = "$test_root/which_test" + +# Test: --which fails for non-executable file +@test "--which fails for non-executable file" ( + set --global --export SCRIPTURA_ROOT $test_root + touch "$test_root/non_exec" + scriptura --which non_exec +) $status -ne 0 + +# Test: --cat displays script content +@test "--cat displays script content" ( + set --global --export SCRIPTURA_ROOT $test_root + echo "# Test content" > "$test_root/cat_test" + scriptura --cat cat_test +) = "# Test content" + +# Test: --help shows help for script +@test "--help shows help for script" ( + set --global --export SCRIPTURA_ROOT $test_root + printf "#!/usr/bin/env fish\n# This is a test script\necho hello\n" > "$test_root/help_test" + chmod +x "$test_root/help_test" + scriptura --help help_test +) = "This is a test script" + +# Test: script execution works for executable scripts +@test "script execution works for executable scripts" ( + set --global --export SCRIPTURA_ROOT $test_root + printf "#!/usr/bin/env fish\necho 'executed successfully'\n" > "$test_root/exec_test" + chmod +x "$test_root/exec_test" + scriptura exec_test +) = "executed successfully" + +# Test: non-executable script shows error and content +@test "non-executable script shows error and content" -n ( + set --global --export SCRIPTURA_ROOT $test_root + echo "# Non-executable content" > "$test_root/non_exec_test" + scriptura non_exec_test 2>&1 | string match --regex "not executable" +) + +# Test: directory listing shows directories +@test "directory listing shows directories" -n ( + set --global --export SCRIPTURA_ROOT $test_root + mkdir -p "$test_root/test_dir" + scriptura 2>&1 | string match --regex "test_dir" +) + +# Test: directory listing shows executable files +@test "directory listing shows executable files" -n ( + set --global --export SCRIPTURA_ROOT $test_root + printf "#!/usr/bin/env fish\n# Test script\n" > "$test_root/list_test" + chmod +x "$test_root/list_test" + scriptura 2>&1 | string match --regex "list_test" +) + +# Test: help file is used for directories +@test "help file is used for directories" ( + set --global --export SCRIPTURA_ROOT $test_root + mkdir -p "$test_root/help_dir" + echo "Directory help text" > "$test_root/help_dir/help" + scriptura --help help_dir +) = "Directory help text" + +# Test: .help file is used for scripts +@test ".help file is used for scripts" ( + set --global --export SCRIPTURA_ROOT $test_root + printf "#!/usr/bin/env fish\n" > "$test_root/help_file_test" + chmod +x "$test_root/help_file_test" + echo "External help text" > "$test_root/help_file_test.help" + scriptura --help help_file_test +) = "External help text" + +# Test: script description extracted from comments +@test "script description extracted from comments" ( + set --global --export SCRIPTURA_ROOT $test_root + printf "#!/usr/bin/env fish\n# This is the description\n# Another comment\n" > "$test_root/desc_test" + chmod +x "$test_root/desc_test" + scriptura --help desc_test +) = "This is the description\nAnother comment" + +# Test: nested path resolution works +@test "nested path resolution works" ( + set --global --export SCRIPTURA_ROOT $test_root + mkdir -p "$test_root/level1/level2" + printf "#!/usr/bin/env fish\necho nested\n" > "$test_root/level1/level2/nested_test" + chmod +x "$test_root/level1/level2/nested_test" + scriptura level1 level2 nested_test +) = nested + +# Test: arguments passed to executed script +@test "arguments passed to executed script" ( + set --global --export SCRIPTURA_ROOT $test_root + printf "#!/usr/bin/env fish\necho \$argv\n" > "$test_root/args_test" + chmod +x "$test_root/args_test" + scriptura args_test arg1 arg2 +) = "arg1 arg2" + +# Test: -- stops argument processing +@test "-- stops argument processing" -n ( + set --global --export SCRIPTURA_ROOT $test_root + mkdir -p "$test_root/dash_test" + printf "#!/usr/bin/env fish\necho dash_dir\n" > "$test_root/dash_test/script" + chmod +x "$test_root/dash_test/script" + scriptura dash_test -- script 2>&1 | string match --regex "^script\s+ --" +) + +# Test: SCRIPTURA_CAT is used when set +@test "SCRIPTURA_CAT is used when set" "$( + set --global --export SCRIPTURA_ROOT $test_root + set --global --export SCRIPTURA_CAT "head -n 1" + printf "line1\nline2\nline3" > "$test_root/cat_custom_test" + scriptura --cat cat_custom_test +)" = line1 + +# Test: SD_CAT is used as fallback for SCRIPTURA_CAT +@test "SD_CAT is used as fallback for SCRIPTURA_CAT" ( + set --global --export SCRIPTURA_ROOT $test_root + set --erase SCRIPTURA_CAT + set --global --export SD_CAT "head -n 1" + printf "line1\nline2\nline3" > "$test_root/sd_cat_test" + scriptura --cat sd_cat_test +) = line1 + +# Test: unknown target shows error +@test "unknown target shows error" -n "$( + set --global --export SCRIPTURA_ROOT $test_root + scriptura nonexistent_target 2>&1 | string match --regex "don't know what to do" +)" + +# Test: help for directory without help file shows default +@test "help for directory without help file shows default" ( + set --global --export SCRIPTURA_ROOT $test_root + mkdir -p "$test_root/no_help_dir" + scriptura --help no_help_dir +) = "no_help_dir commands" + +# Test: shebang lines are excluded from description +@test "shebang lines are excluded from description" ( + set --global --export SCRIPTURA_ROOT $test_root + printf "#!/usr/bin/env fish\n#! Not a shebang\n# Real description\n" > "$test_root/shebang_desc_test" + chmod +x "$test_root/shebang_desc_test" + scriptura --help shebang_desc_test +) = "Real description" + +# Cleanup +cleanup