Files
rosetta/tools/test.sh
T

70 lines
3.9 KiB
Bash

#!/usr/bin/env bash
# Tests (bash tools/test.sh [filter]): generate the fixtures (tools/fixtures.py -> tests/gen), regenerate the registry,
# build both targets (tools/build.py: the module trees in build/_modules and the bundles build/Rosetta.luau,
# build/rosetta.lua), then run every tests/**/*.luau twice: under `luau --codegen -O2` (the source, through the
# .luaurc aliases) and under `luajit` (build/_modules/luajit/tests/**/*.lua, with the compat prelude). The api test and
# the docs / example tests run once more against the bundles (build/Rosetta.luau under luau, build/rosetta.lua under
# LuaJIT; tools/bundle.py --stage-tests) and, under luau, against the chunked build (build/Rosetta.rbxm's ModuleScripts
# as files, build/_modules/chunked). Then the lint, lune / rojo reading the rbxm (tools/bundle.py --check-rbxm), and a
# check that the committed bundles are up to date.
# A filter runs only the tests whose path contains it (and the fixture generators whose name contains it), and skips
# the lint and the bundle check. RUNTIMES="luau" or "luajit" runs one runtime only.
cd "$(dirname "$0")/.." || exit 1
LUAU=${LUAU:-$(command -v luau || nix shell nixpkgs#luau -c sh -c 'command -v luau')}
LUAJIT=${LUAJIT:-$(command -v luajit || nix shell nixpkgs#luajit -c sh -c 'command -v luajit')}
export LUAJIT
RUNTIMES=${RUNTIMES:-"luau luajit"}
LJPATH='package.path = "build/_modules/luajit/?.lua;build/_modules/luajit/?/init.lua;" .. package.path'
# the bundle first: rosetta and rosetta._compat come from build/rosetta.lua, tests.* from the module build
LJBUNDLE='package.path = "build/?.lua;build/_modules/luajit/?.lua;build/_modules/luajit/?/init.lua;" .. package.path; require("rosetta")'
BUNDLES="build/Rosetta.luau build/rosetta.lua build/Rosetta.luau.linemap.json build/rosetta.lua.linemap.json build/Rosetta.rbxm build/Rosetta.rbxmx build/Rosetta.rbxm.linemap.json"
fail=0
python3 -I tools/fixtures.py $1 || { echo "FAILED: fixtures"; exit 1; }
python3 -I tools/gen_registry.py > /dev/null || { echo "FAILED: gen_registry"; exit 1; }
python3 -I tools/build.py || { echo "FAILED: build"; exit 1; }
python3 -I tools/bundle.py --stage-tests > /dev/null || { echo "FAILED: bundle --stage-tests"; exit 1; }
passed=0
run() { # run <test label> <runtime> <command...>
local t=$1 rt=$2
shift 2
echo "== $t ($rt)"
if "$@"; then passed=$((passed + 1)); else echo "FAILED: $t ($rt)"; fail=1; fi
}
for t in $(find tests -name '*.luau' -not -path 'tests/gen/*' -not -path 'tests/fixtures/*' -not -name 'T.luau' -not -name '_*' | sort); do
[ -n "$1" ] && [[ "$t" != *$1* ]] && continue
for rt in $RUNTIMES; do
if [ "$rt" = luau ]; then
run "$t" luau "$LUAU" --codegen -O2 "$t"
else
run "$t" luajit "$LUAJIT" -e "$LJPATH" "build/_modules/luajit/${t%.luau}.lua"
fi
done
done
for t in $(cd build/_modules/bundle_tests/luau && find . -name '*.luau' | sed 's#^\./##' | sort); do
[ -n "$1" ] && [[ "tests/$t" != *$1* ]] && continue
for rt in $RUNTIMES; do
if [ "$rt" = luau ]; then
run "tests/$t" "luau, build/Rosetta.luau" "$LUAU" --codegen -O2 "build/_modules/bundle_tests/luau/$t"
else
run "tests/$t" "luajit, build/rosetta.lua" "$LUAJIT" -e "$LJBUNDLE" "build/_modules/bundle_tests/luajit/${t%.luau}.lua"
fi
done
done
if [[ " $RUNTIMES " == *" luau "* ]]; then
for t in $(cd build/_modules/bundle_tests/luau_chunked && find . -name '*.luau' | sed 's#^\./##' | sort); do
[ -n "$1" ] && [[ "tests/$t" != *$1* ]] && continue
run "tests/$t" "luau, build/Rosetta.rbxm" "$LUAU" --codegen -O2 "build/_modules/bundle_tests/luau_chunked/$t"
done
fi
if [ -z "$1" ]; then
bash tools/lint.sh || fail=1
python3 -I tools/bundle.py --check-rbxm || { echo "FAILED: check-rbxm"; fail=1; }
if [ -n "$(git status --porcelain -- $BUNDLES)" ]; then
git status --short -- $BUNDLES
echo "FAILED: the bundles in build/ differ from the committed ones (tools/build.py rewrote them): commit them"
fail=1
fi
fi
[ $fail -eq 0 ] && echo "rosetta: all tests passed ($passed runs)" || echo "rosetta: FAILURES"
exit $fail