148 lines
5.1 KiB
Bash
Executable File
148 lines
5.1 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
# Single-run integration test for merged-repl-plugin
|
|
# Usage: ./tests/run_single.sh
|
|
# Requirements: bash, git, nvim (headless) available in PATH
|
|
|
|
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
|
|
MAIN="$ROOT/test_main_repo"
|
|
REPL="$ROOT/test_repl_repo"
|
|
LUA_TEST="$ROOT/tests/_headless_single.lua"
|
|
SYNC_OUT="$ROOT/tests/sync_result.txt"
|
|
REPL_OUT="$ROOT/tests/repl_output_captured.log"
|
|
TMP_CLEANUP=()
|
|
|
|
fail() { echo "ERROR: $*" >&2; exit 1; }
|
|
|
|
# ensure nvim not running (we'll be conservative)
|
|
if pgrep -a nvim > /dev/null; then
|
|
echo "Warning: nvim process found. Please close running nvim instances to avoid interference."
|
|
fi
|
|
|
|
echo "Preparing test repos in: $ROOT"
|
|
rm -rf "$MAIN" "$REPL" "$SYNC_OUT" "$REPL_OUT" "$LUA_TEST"
|
|
mkdir -p "$MAIN" "$REPL" "$ROOT/tests"
|
|
|
|
# init main
|
|
git -C "$MAIN" init >/dev/null 2>&1 || true
|
|
if [ -z "$(git -C "$MAIN" rev-parse --verify HEAD 2>/dev/null || true)" ]; then
|
|
touch "$MAIN/README"
|
|
git -C "$MAIN" add README
|
|
git -C "$MAIN" -c user.name='CI' -c user.email='ci@example.com' commit -m "init" >/dev/null
|
|
fi
|
|
|
|
# init repl
|
|
git -C "$REPL" init >/dev/null 2>&1 || true
|
|
if [ -z "$(git -C "$REPL" rev-parse --verify HEAD 2>/dev/null || true)" ]; then
|
|
touch "$REPL/README"
|
|
git -C "$REPL" add README
|
|
git -C "$REPL" -c user.name='CI' -c user.email='ci@example.com' commit -m "init" >/dev/null
|
|
fi
|
|
|
|
# ensure template branch exists in repl
|
|
git -C "$REPL" checkout -B template >/dev/null 2>&1 || true
|
|
|
|
# create a simple repl.pl and commit
|
|
cat > "$REPL/repl.pl" <<'EOF'
|
|
print "Hello from REPL\n";
|
|
EOF
|
|
git -C "$REPL" add repl.pl >/dev/null 2>&1 || true
|
|
git -C "$REPL" -c user.name='CI' -c user.email='ci@example.com' commit -m "add repl" >/dev/null 2>&1 || true
|
|
|
|
# headless lua script that triggers sync_once and run_repl and dumps file output to a file
|
|
cat > "$LUA_TEST" <<LUA
|
|
local M = require('merged-repl-plugin')
|
|
local main = [[${MAIN}]]
|
|
local repl = [[${REPL}]]
|
|
local rf = repl .. '/repl.pl'
|
|
M.setup{ main_repo_path = main, repl_repo_path = repl, repl_file = rf, auto_start = false, output_target = 'file' }
|
|
-- run one sync and capture result
|
|
M.sync_once(function(err, res)
|
|
local f = io.open([[${SYNC_OUT}]], 'w')
|
|
f:write(tostring(err) .. "\n")
|
|
if res then f:write(vim.inspect(res) .. "\n") end
|
|
f:close()
|
|
end)
|
|
-- ensure repl file exists (writing fresh, since sync may change working tree)
|
|
local fh = io.open(rf, 'w')
|
|
if fh then fh:write('print "Hello from REPL\\n";') fh:close() end
|
|
|
|
-- run the repl; runner will write repl_output.log in the repl repo
|
|
M.run_repl()
|
|
-- wait up to 2s for the output file to appear
|
|
local out_path = repl .. '/repl_output.log'
|
|
local ok = vim.wait(2000, function()
|
|
return vim.loop.fs_stat(out_path) ~= nil
|
|
end, 50)
|
|
if ok then
|
|
-- copy to tests location for inspection
|
|
local in_f = io.open(out_path, 'r')
|
|
if in_f then
|
|
local content = in_f:read('*a')
|
|
in_f:close()
|
|
local out_f = io.open([[${REPL_OUT}]], 'w')
|
|
if out_f then out_f:write(content); out_f:close() end
|
|
end
|
|
end
|
|
|
|
vim.cmd('qa!')
|
|
LUA
|
|
|
|
# run headless nvim to execute the lua test
|
|
nvim --headless -u NONE -c "lua package.path = package.path .. ';./lua/?.lua;./lua/?/init.lua'" -c "luafile $LUA_TEST"
|
|
|
|
# wait for repl_output.log to appear (up to 5s)
|
|
out_file="$REPL/repl_output.log"
|
|
max_wait=25 # 25 * 0.2s = 5s
|
|
i=0
|
|
while [ $i -lt $max_wait ]; do
|
|
if [ -f "$out_file" ]; then break; fi
|
|
sleep 0.2
|
|
i=$((i+1))
|
|
done
|
|
|
|
|
|
# dump sync output and repl output
|
|
|
|
echo "=== sync result ==="
|
|
if [ -f "$SYNC_OUT" ]; then cat "$SYNC_OUT"; else echo "(no sync output)"; fi
|
|
|
|
echo "=== repl output ==="
|
|
if [ -f "$REPL_OUT" ]; then cat "$REPL_OUT"; else echo "(no repl output captured)"; fi
|
|
|
|
# show repl_output.log content for debugging if present
|
|
if [ -f "$out_file" ]; then
|
|
echo "--- $out_file contents ---"
|
|
sed -n '1,200p' "$out_file"
|
|
fi
|
|
|
|
|
|
# strict checks
|
|
# ensure repl output file exists and contains expected text
|
|
if [ ! -f "$REPL/repl_output.log" ]; then
|
|
echo "ERROR: repl_output.log missing" >&2
|
|
exit 2
|
|
fi
|
|
if ! grep -q "Hello from REPL" "$REPL/repl_output.log"; then
|
|
echo "ERROR: expected REPL output missing" >&2
|
|
cat "$REPL/repl_output.log" || true
|
|
exit 3
|
|
fi
|
|
|
|
# verify branch mirroring (create branch in main and run sync_once again)
|
|
git -C "$MAIN" checkout -B feature-single || fail "failed to create test branch in main"
|
|
nvim --headless -u NONE -c "lua package.path = package.path .. ';./lua/?.lua;./lua/?/init.lua'" -c "lua require('merged-repl-plugin').setup{ main_repo_path='${MAIN}', repl_repo_path='${REPL}', repl_file='${REPL}/repl.pl', auto_start=false } require('merged-repl-plugin').sync_once()" -c "qa!"
|
|
|
|
echo "=== repl branches ==="
|
|
git -C "$REPL" branch --list
|
|
|
|
# test auto-commit: append to repl.pl and run sync_once; then show latest commit message
|
|
echo 'print "Modified for single test\\n";' >> "$REPL/repl.pl"
|
|
nvim --headless -u NONE -c "lua package.path = package.path .. ';./lua/?.lua;./lua/?/init.lua'" -c "lua require('merged-repl-plugin').setup{ main_repo_path='${MAIN}', repl_repo_path='${REPL}', repl_file='${REPL}/repl.pl', auto_start=false } require('merged-repl-plugin').sync_once()" -c "qa!"
|
|
|
|
echo "=== latest repl commit message ==="
|
|
git -C "$REPL" log -1 --pretty=%B || true
|
|
|
|
echo "Single run complete."
|