repositories / dotfiles
dotfiles
bugabingas dorkfiles
owned by admin
nushell/tests/markdown.test.nu
Rawuse std/assert
const markdown_module = path self ../modules/markdown.nu
const todo_module = path self ../modules/todo.nu
let temp = mktemp --directory --tmpdir markdown-test.XXXXXX
try {
let notes = $temp | path join 'notes space α'
mkdir $notes
for name in [TODO.md COMMANDS.md] {
$"# ($name)\n\nFixture text.\n" | save ($notes | path join $name)
}
let cases = [
{name: mdcat, available: [mdcat glow bat], backend: mdcat}
{name: glow, available: [glow bat], backend: glow}
{name: bat, available: [bat], backend: bat}
{name: fallback, available: [], backend: null}
]
for case in $cases {
let bin = $temp | path join $case.name
mkdir $bin
for backend in $case.available {
let executable = $bin | path join $backend
$'#!/bin/sh
printf "%s\n" "($backend)" "$@"
exit "${MD_TEST_EXIT:-0}"
' | save $executable
^chmod +x $executable
}
for invocation in [
{command: 'markdown $env.MD_TEST_FILE', file: TODO.md}
{command: todo, file: TODO.md}
{command: 'todo commands', file: COMMANDS.md}
] {
let file = $notes | path join $invocation.file
let command = if $case.backend == null { $"($invocation.command) | to json --raw" } else { $invocation.command }
let code = $"use '($markdown_module)'; use '($todo_module)'; ($command)"
let result = with-env {PATH: [$bin], NOTES: $notes, MD_TEST_FILE: $file, MD_TEST_EXIT: '0'} {
^($nu.current-exe) --no-config-file -c $code | complete
}
assert equal $result.exit_code 0 $result.stderr
let expected = if $case.backend == null {
open $file
} else {
let args = if $case.backend == bat { [bat --no-pager $file] } else { [$case.backend $file] }
($args | str join "\n") + "\n"
}
let actual = if $case.backend == null { $result.stdout | from json } else { $result.stdout }
assert equal $actual $expected $"($case.name): ($invocation.command)"
}
}
# A present but failing preferred backend must not silently fall through.
let failure = with-env {PATH: [($temp | path join mdcat)], MD_TEST_FILE: ($notes | path join TODO.md), MD_TEST_EXIT: '17'} {
^($nu.current-exe) --no-config-file -c $"use '($markdown_module)'; markdown $env.MD_TEST_FILE" | complete
}
assert equal $failure.exit_code 17
assert equal ($failure.stdout | lines | first) mdcat
rm --recursive $temp
} catch {|error|
rm --recursive $temp
error make $error
}
print 'Markdown backend precedence, fallback, exact paths, To-Do integration, and failure propagation passed'