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) }