#!/usr/bin/env nu # Comprehensive test suite for shim module # Run with: nu test_shim_edge_cases.nu use ./modules/shim.nu print "=== SHIM MODULE EDGE CASE TESTS ===" print "" # Helper to check if path is file def check_is_file [] { let p = $in try { ($p | path type) == file } catch { false } } # Helper to check if path is symlink def check_is_symlink [] { let p = $in try { ($p | path type) == symlink } catch { false } } # Clean up helper def cleanup [] { do --ignore-errors { shim unlink "/home/oli/Workspace/dotfiles/nushell/test_shim/file1.txt" } do --ignore-errors { shim unlink "/home/oli/Workspace/dotfiles/nushell/test_shim/file2.txt" } do --ignore-errors { rm "/home/oli/Workspace/dotfiles/nushell/test_shim/file1.txt" } do --ignore-errors { rm "/home/oli/Workspace/dotfiles/nushell/test_shim/file2.txt" } do --ignore-errors { rm "/home/oli/Workspace/dotfiles/nushell/test_shim/moved1.txt" } do --ignore-errors { rm "/home/oli/Workspace/dotfiles/nushell/test_shim/moved2.txt" } do --ignore-errors { rm "/home/oli/Workspace/dotfiles/nushell/test_shim/subdir/file2.txt" } do --ignore-errors { rmdir "/home/oli/Workspace/dotfiles/nushell/test_shim/subdir" } } # Initial cleanup cleanup print "" print "== Test 1: Basic link and unlink ==" echo "test1 content" | save --force "/home/oli/Workspace/dotfiles/nushell/test_shim/file1.txt" shim link "/home/oli/Workspace/dotfiles/nushell/test_shim/file1.txt" "/home/oli/Workspace/dotfiles/nushell/test_shim/moved1.txt" let t1_is_symlink = ("/home/oli/Workspace/dotfiles/nushell/test_shim/file1.txt" | check_is_symlink) let t1_content = (cat "/home/oli/Workspace/dotfiles/nushell/test_shim/file1.txt" | str trim) shim unlink "/home/oli/Workspace/dotfiles/nushell/test_shim/file1.txt" let t1_is_file = ("/home/oli/Workspace/dotfiles/nushell/test_shim/file1.txt" | check_is_file) let t1_restored = (cat "/home/oli/Workspace/dotfiles/nushell/test_shim/file1.txt" | str trim) if $t1_is_symlink and $t1_content == "test1 content" and $t1_is_file and $t1_restored == "test1 content" { print " [PASS]" } else { print " [FAIL]" print $" symlink: ($t1_is_symlink), content: ($t1_content), restored: ($t1_is_file), restored_content: ($t1_restored)" } cleanup print "" print "== Test 2: Source doesn't exist ==" let t2_pass = try { shim link /nonexistent/file.txt /tmp/dest; false } catch { true } if $t2_pass { print " [PASS]" } else { print " [FAIL]" } print " Expected error when source doesn't exist" cleanup print "" print "== Test 3: Source is already a symlink ==" echo "content" | save --force "/home/oli/Workspace/dotfiles/nushell/test_shim/file2.txt" shim link "/home/oli/Workspace/dotfiles/nushell/test_shim/file2.txt" "/home/oli/Workspace/dotfiles/nushell/test_shim/moved2.txt" let t3_pass = try { shim link "/home/oli/Workspace/dotfiles/nushell/test_shim/file2.txt" "/tmp/another.txt"; false } catch { true } shim unlink "/home/oli/Workspace/dotfiles/nushell/test_shim/file2.txt" if $t3_pass { print " [PASS]" } else { print " [FAIL]" } print " Expected error when linking already-linked path" cleanup print "" print "== Test 4: Move into existing directory ==" mkdir "/home/oli/Workspace/dotfiles/nushell/test_shim/subdir" echo "content" | save --force "/home/oli/Workspace/dotfiles/nushell/test_shim/file2.txt" shim link "/home/oli/Workspace/dotfiles/nushell/test_shim/file2.txt" "/home/oli/Workspace/dotfiles/nushell/test_shim/subdir" let t4_in_subdir = (("/home/oli/Workspace/dotfiles/nushell/test_shim/subdir/file2.txt" | check_is_file)) let t4_is_symlink = ("/home/oli/Workspace/dotfiles/nushell/test_shim/file2.txt" | check_is_symlink) if $t4_in_subdir and $t4_is_symlink { print " [PASS]" } else { print " [FAIL]" print $" file in subdir: ($t4_in_subdir), symlink at original: ($t4_is_symlink)" } shim unlink "/home/oli/Workspace/dotfiles/nushell/test_shim/file2.txt" do --ignore-errors { rm "/home/oli/Workspace/dotfiles/nushell/test_shim/subdir/file2.txt" } do --ignore-errors { rmdir "/home/oli/Workspace/dotfiles/nushell/test_shim/subdir" } cleanup print "" print "== Test 5: Relative symlinks ==" echo "relcontent" | save --force "/home/oli/Workspace/dotfiles/nushell/test_shim/file2.txt" shim link "/home/oli/Workspace/dotfiles/nushell/test_shim/file2.txt" "/home/oli/Workspace/dotfiles/nushell/test_shim/moved2.txt" --relative let t5_target = (^readlink "/home/oli/Workspace/dotfiles/nushell/test_shim/file2.txt") let t5_is_rel = not ($t5_target | str starts-with "/") let t5_content = (cat "/home/oli/Workspace/dotfiles/nushell/test_shim/file2.txt" | str trim) shim unlink "/home/oli/Workspace/dotfiles/nushell/test_shim/file2.txt" if $t5_is_rel and $t5_content == "relcontent" { print " [PASS]" } else { print " [FAIL]" print $" target: ($t5_target), is_relative: ($t5_is_rel), content: ($t5_content)" } cleanup print "" print "== Test 6: Dry-run mode ==" echo "dryrun" | save --force "/home/oli/Workspace/dotfiles/nushell/test_shim/file2.txt" shim link "/home/oli/Workspace/dotfiles/nushell/test_shim/file2.txt" "/tmp/dryrun.txt" --dry-run let t6_is_file = ("/home/oli/Workspace/dotfiles/nushell/test_shim/file2.txt" | check_is_file) if $t6_is_file { print " [PASS]" } else { print " [FAIL]" } print " File unchanged after dry-run" cleanup print "" print "== Test 7: Quiet mode ==" echo "quiet" | save --force "/home/oli/Workspace/dotfiles/nushell/test_shim/file2.txt" let t7_out = (shim link "/home/oli/Workspace/dotfiles/nushell/test_shim/file2.txt" "/home/oli/Workspace/dotfiles/nushell/test_shim/moved2.txt" --quiet) let t7_empty = ($t7_out | is-empty) or ($t7_out | describe) == "nothing" shim unlink "/home/oli/Workspace/dotfiles/nushell/test_shim/file2.txt" if $t7_empty { print " [PASS]" } else { print " [FAIL]" } print " No output from link command with --quiet" cleanup print "" print "== Test 8: Cross-filesystem relative ==" echo "crossfs" | save --force "/home/oli/Workspace/dotfiles/nushell/test_shim/file2.txt" let t8_pass = try { shim link "/home/oli/Workspace/dotfiles/nushell/test_shim/file2.txt" "/tmp/crossfs.txt" --relative; false } catch { true } cleanup if $t8_pass { print " [PASS]" } else { print " [FAIL]" } print " Expected error for cross-filesystem relative path" print "" print "=== ALL TESTS COMPLETE ==="