aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoralex <alex@cloudware.io>2020-02-21 18:40:07 +0100
committeralex <alex@cloudware.io>2020-02-21 18:40:07 +0100
commitb36d6550ad073885ed82b39a70a01bf0e9a4d721 (patch)
tree92685ef48368fdfe8069cb8b51ad8d2a816c35f8
parent8083f7109f8173679f179e805c579cf1a0f5c6d2 (diff)
downloadvis-b36d6550ad073885ed82b39a70a01bf0e9a4d721.tar.gz
vis-b36d6550ad073885ed82b39a70a01bf0e9a4d721.tar.xz
vis: allow tests to have an optional lua script
The script named after <test-name>.lua, if exists, is run just before loading and executing <test-name>.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.
-rw-r--r--vis/visrc.lua9
1 files changed, 9 insertions, 0 deletions
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', '')