This commit is contained in:
Icy Avocado
2026-01-22 11:17:53 -07:00
commit 73cba30662
16 changed files with 888 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
local M = require('merged-repl-plugin')
local main = [[/home/dai/projects/reperl.nvim/test_main_repo]]
local repl = [[/home/dai/projects/reperl.nvim/test_repl_repo]]
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([[/home/dai/projects/reperl.nvim/tests/sync_result.txt]], '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([[/home/dai/projects/reperl.nvim/tests/repl_output_captured.log]], 'w')
if out_f then out_f:write(content); out_f:close() end
end
end
vim.cmd('qa!')

41
tests/_headless_test.lua Normal file
View File

@@ -0,0 +1,41 @@
-- headless test for merged-repl-plugin
local M = require('merged-repl-plugin')
local main = [[/home/dai/projects/reperl.nvim/test_main_repo]]
local repl = [[/home/dai/projects/reperl.nvim/test_repl_repo]]
local repl_file = repl .. '/repl.pl'
local sync_out = [[/home/dai/projects/reperl.nvim/tests/sync_result.txt]]
local repl_out = [[/home/dai/projects/reperl.nvim/tests/repl_output_captured.log]]
M.setup{ main_repo_path = main, repl_repo_path = repl, repl_file = repl_file, auto_start = false }
-- run sync_once and write result to file
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)
-- run the repl using plugin runner
M.run_repl()
-- wait for buffer to appear and capture its contents to repl_out
local bufname = 'REPL Output: ' .. vim.fn.fnamemodify(repl, ':t')
local ok = vim.wait(2000, function()
local bufnr = vim.fn.bufnr(bufname)
if bufnr <= 0 then return false end
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
return #lines > 0
end, 50)
local bufnr = vim.fn.bufnr(bufname)
if bufnr > 0 then
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
local f = io.open(repl_out, 'w')
f:write(table.concat(lines, '\n'))
f:close()
end
-- exit
vim.cmd('qa!')

View File

@@ -0,0 +1 @@
-- REPL Run: 2026-01-22 11:16:46

147
tests/run_single.sh Executable file
View File

@@ -0,0 +1,147 @@
#!/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."

130
tests/run_tests.sh Executable file
View File

@@ -0,0 +1,130 @@
#!/usr/bin/env bash
set -euo pipefail
# Run integration tests for merged-repl-plugin
# Usage: ./tests/run_tests.sh
# This script creates two test git repositories under the project directory,
# runs headless Neovim to exercise the plugin (sync and run), and verifies
# expected results (branch creation, repl output capture, auto-commit).
REPO_ROOT=$(cd "$(dirname "$0")/.." && pwd)
MAIN="$REPO_ROOT/test_main_repo"
REPL="$REPO_ROOT/test_repl_repo"
LUA_TMP="$REPO_ROOT/tests/_headless_test.lua"
SYNC_OUT="$REPO_ROOT/tests/sync_result.txt"
REPL_OUT="$REPO_ROOT/tests/repl_output_captured.log"
echo "Running tests in: $REPO_ROOT"
# Prepare test repos
rm -rf "$MAIN" "$REPL" "$REPL_OUT" "$SYNC_OUT"
mkdir -p "$MAIN" "$REPL" "$REPO_ROOT/tests"
git -C "$MAIN" init || true
# create initial commit in main
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"
fi
git -C "$REPL" init || 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"
fi
# Ensure template branch exists in repl repo
git -C "$REPL" checkout -B template || true
# Write a simple repl.pl
cat > "$REPL/repl.pl" <<'EOF'
print "Hello from REPL\n";
EOF
# commit repl.pl
git -C "$REPL" add repl.pl || true
git -C "$REPL" -c user.name='CI' -c user.email='ci@example.com' commit -m "add repl" || true
# Compose headless lua test file
cat > "$LUA_TMP" <<LUALIB
-- headless test for merged-repl-plugin
local M = require('merged-repl-plugin')
local main = [[${MAIN}]]
local repl = [[${REPL}]]
local repl_file = repl .. '/repl.pl'
local sync_out = [[${SYNC_OUT}]]
local repl_out = [[${REPL_OUT}]]
M.setup{ main_repo_path = main, repl_repo_path = repl, repl_file = repl_file, auto_start = false }
-- run sync_once and write result to file
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)
-- run the repl using plugin runner
M.run_repl()
-- wait for buffer to appear and capture its contents to repl_out
local bufname = 'REPL Output: ' .. vim.fn.fnamemodify(repl, ':t')
local ok = vim.wait(2000, function()
local bufnr = vim.fn.bufnr(bufname)
if bufnr <= 0 then return false end
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
return #lines > 0
end, 50)
local bufnr = vim.fn.bufnr(bufname)
if bufnr > 0 then
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
local f = io.open(repl_out, 'w')
f:write(table.concat(lines, '\n'))
f:close()
end
-- exit
vim.cmd('qa!')
LUALIB
# Run headless Neovim with the test lua file
nvim --headless -u NONE -c "lua package.path = package.path .. ';./lua/?.lua;./lua/?/init.lua'" -c "luafile ${LUA_TMP}"
# Check results
echo "--- Sync result ---"
if [ -f "$SYNC_OUT" ]; then
cat "$SYNC_OUT"
else
echo "No sync output file found"
fi
echo "--- Repl output ---"
if [ -f "$REPL_OUT" ]; then
cat "$REPL_OUT"
else
echo "No repl output captured"
fi
# Verify repl repo got branch created if main branch changed
# create a new branch in main to test branch mirroring
git -C "$MAIN" checkout -B feature-test
# run sync_once again
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 "--- Branches in repl repo ---"
git -C "$REPL" branch --list
# Test auto-commit: modify repl.pl and run sync_once to ensure commit
echo 'print "Modified\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 commit message in repl repo ---"
git -C "$REPL" log -1 --pretty=%B
echo "Tests finished."

5
tests/sync_result.txt Normal file
View File

@@ -0,0 +1,5 @@
nil
{
branch = "master",
changed = true
}