diff --git a/nu/host/migrate.nu b/nu/host/migrate.nu index 8f4414d..a64519a 100644 --- a/nu/host/migrate.nu +++ b/nu/host/migrate.nu @@ -171,8 +171,9 @@ def "main migrate connections" [ # Build an include list for tar: individual files + full dir trees. let file_args = $existing_files | each {|f| ["--include" $f] } | flatten let dir_args = $existing_dirs | each {|d| ["--include" $"($d)/**"] } | flatten + let tar_args = $file_args | append $dir_args | append ["--no-recursion" "-cf" "-" "."] ( - ^tar --ignore-failed-read -C $source ($file_args | append $dir_args | append ["--no-recursion" "-cf" "-"]) . + ^tar --ignore-failed-read -C $source ...$tar_args | run-argv ($ssh_base ++ [$"mkdir -p '($guest_dest)' && tar -C '($guest_dest)' -xf -"]) ) diff --git a/nu/tests/migrate_test.nu b/nu/tests/migrate_test.nu new file mode 100644 index 0000000..ec75013 --- /dev/null +++ b/nu/tests/migrate_test.nu @@ -0,0 +1,77 @@ +use std/assert +use std/testing * +source ../lib/common.nu +source ../lib/guest.nu +source ../host/migrate.nu + +# --------------------------------------------------------------------------- +# Helpers that mirror the logic in `main migrate connections` so we can unit- +# test the tricky parts (tar arg assembly, sed expression building) without +# actually touching the filesystem or a guest. +# --------------------------------------------------------------------------- + +# Reproduce the sed-expression builder from `main migrate connections`. +def build-sed-expr [from: list, to: list] { + let last_idx = ($from | length) - 1 + (0..$last_idx + | each {|i| $"s#($from | get $i)#($to | get $i)#g" } + | str join "; ") +} + +# Reproduce the tar-args assembly (everything after `tar -C $source`). +def build-tar-args [files: list, dirs: list] { + let file_args = $files | each {|f| ["--include" $f] } | flatten + let dir_args = $dirs | each {|d| ["--include" $"($d)/**"] } | flatten + $file_args | append $dir_args | append ["--no-recursion" "-cf" "-" "."] +} + +# --------------------------------------------------------------------------- + +@test +def "sed expr substitutes each pair" [] { + let expr = build-sed-expr [host.docker.internal foo.internal] [10.77.0.1 10.0.0.1] + assert ($expr | str contains "s#host.docker.internal#10.77.0.1#g") + assert ($expr | str contains "s#foo.internal#10.0.0.1#g") +} + +@test +def "sed expr pairs are joined with semicolons" [] { + let expr = build-sed-expr [a b c] [x y z] + let parts = $expr | split row "; " + assert equal ($parts | length) 3 +} + +@test +def "tar args: flat files produce paired --include entries" [] { + let args = build-tar-args [config.yaml .env] [] + assert ("--include" in $args) + assert ("config.yaml" in $args) + assert (".env" in $args) +} + +@test +def "tar args: dirs produce glob --include entries" [] { + let args = build-tar-args [] [connections] + assert ("connections/**" in $args) +} + +@test +def "tar args: always end with --no-recursion -cf - ." [] { + let args = build-tar-args [config.yaml] [connections] + let tail = $args | last 4 + assert equal $tail ["--no-recursion" "-cf" "-" "."] +} + +@test +def "tar args: result is a flat list of strings spreadable with spread operator" [] { + let args = build-tar-args [config.yaml auth.json] [connections] + # Every element must be a string so `...$args` works with ^tar. + let all_strings = $args | all {|x| ($x | describe) == "string" } + assert $all_strings +} + +@test +def "migrate connections is registered in the command tree" [] { + let names = scope commands | where name =~ '^main' | get name + assert ("main migrate connections" in $names) +}