From b36d6550ad073885ed82b39a70a01bf0e9a4d721 Mon Sep 17 00:00:00 2001 From: alex Date: Fri, 21 Feb 2020 18:40:07 +0100 Subject: vis: allow tests to have an optional lua script The script named after .lua, if exists, is run just before loading and executing .keys. This allows tests to inject Lua code in the running vis instance to help augment the test environment. For instance, a test could listen to vis.events.FILE_SAVE_PRE events and mutate file text. --- vis/visrc.lua | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/vis/visrc.lua b/vis/visrc.lua index 2cb3f8a..da34920 100644 --- a/vis/visrc.lua +++ b/vis/visrc.lua @@ -1,12 +1,21 @@ package.path = '../../lua/?.lua;'..package.path dofile("../../lua/vis.lua") +local function run_if_exists(luafile) + local f = io.open(luafile, "r") + if f ~= nil then + f:close() + dofile(luafile) + end +end + vis.events.subscribe(vis.events.WIN_OPEN, function(win) -- test.in file passed to vis local name = win.file.name if name then -- use the corresponding test.lua file name = string.gsub(name, '%.in$', '') + run_if_exists(string.format("%s.lua", name)) local file = io.open(string.format("%s.keys", name)) local keys = file:read('*all') keys = string.gsub(keys, '%s*\n', '') -- cgit v1.2.3