fixing migration bugs + tests

This commit is contained in:
ari committed 2026-09-17 15:46:19 -04:00
1 parent 2d35fd973c
commit 27b713affa
2 files changed
+79 -1

No files matched your search

+2 -1
View File
@@ -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 -"])
)
+77
View File
@@ -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<string>, to: list<string>] {
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<string>, dirs: list<string>] {
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)
}