From b475a680fb6c7aa4a9dc23d9ae026be6ba731530 Mon Sep 17 00:00:00 2001 From: Daniel Barlow Date: Sat, 24 Aug 2024 21:50:30 +0100 Subject: [PATCH] define-tests macro, evals body only when inside fennelrepl --test --- pkgs/anoia/assert.fnl | 6 +++++- pkgs/fennelrepl/default.nix | 19 +++++++++++++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/pkgs/anoia/assert.fnl b/pkgs/anoia/assert.fnl index f3dae6d..2ff18fc 100644 --- a/pkgs/anoia/assert.fnl +++ b/pkgs/anoia/assert.fnl @@ -18,4 +18,8 @@ (.. "\nexpected " ve# "\ngot " va#) )))) -{ : expect : expect= } +(fn define-tests [& body] + (when _G.RUNNING_TESTS + `(do ,(unpack body)))) + +{ : define-tests : expect : expect= } diff --git a/pkgs/fennelrepl/default.nix b/pkgs/fennelrepl/default.nix index 8320130..0f37dcc 100644 --- a/pkgs/fennelrepl/default.nix +++ b/pkgs/fennelrepl/default.nix @@ -27,17 +27,28 @@ in writeScriptBin "fennelrepl" '' package.path = ${lib.strings.escapeShellArg luapath} .. ";" .. package.path package.cpath = ${lib.strings.escapeShellArg luacpath} .. ";" .. (package.cpath or "") local fennel = require "fennel" + local specials = require("fennel.specials") table.insert(package.loaders or package.searchers,1, fennel.searcher) fennel['macro-path'] = "${anoia.dev}/share/lua/${lua.luaversion}/?.fnl;" .. fennel['macro-path'] + local function eval_as_test(f) + local g = (specials["make-compiler-env"]())._G + g["RUNNING_TESTS"] = true + return fennel.dofile(f, {correlate = true, compilerEnv = g}) + end + local more_fennel = os.getenv("FENNEL_PATH") if more_fennel then - fennel.path = more_fennel .. ";" .. fennel.path + fennel.path = more_fennel .. ";" .. fennel.path end if #arg > 0 then - script = table.remove(arg, 1) - fennel.dofile(script, {correlate = true}, arg) + if arg[1] == '--test' then + eval_as_test(arg[2]) + else + script = table.remove(arg, 1) + fennel.dofile(script, {correlate = true}, arg) + end else - fennel.repl() + fennel.repl() end ''