#!/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 set --prepend fish_function_path (status dirname)/../functions # 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 creates component directories @test "--new creates component directories" ( set --global --export SCRIPTURA_ROOT $test_root set --global --export SCRIPTURA_EDITOR "touch" scriptura --new foo bar baz path is --type dir "$test_root/foo/bar" path is --type file --perm exec "$test_root/foo/bar/baz" ) $status -eq 0 # 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: --new with -- creates script with remaining arguments @test "--new with -- creates script with remaining arguments" ( set --global --export SCRIPTURA_ROOT $test_root scriptura --new new_script -- echo foo path is --type file --perm exec "$test_root/new_script" tail -n 1 "$test_root/new_script" ) = "echo foo" # 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: directory listing with argument shows children @test "directory listing with argument shows children" -n ( set --global --export SCRIPTURA_ROOT $test_root mkdir -p "$test_root/test_dir" printf "#!/usr/bin/env fish\n# Test script\n" > "$test_root/test_dir/test_script" chmod +x "$test_root/test_dir/test_script" scriptura test_dir 2>&1 | string match "test_script*" ) # 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 "script*" ) # 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" "$( set --global --export SCRIPTURA_ROOT $test_root scriptura nonexistent_target 2>&1 )" = 'scriptura: don\'t know what to do with target "nonexistent_target"' # 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