tinyyarn

scenario testing of Unix command line tools
Log | Files | Refs | README | LICENSE

commit 37a617f5f2faa5dc934567a3124739da93711614
parent 5922bf2f21d20e4b60fb99d37b196779e0f3c2ba
Author: Richard Ipsum <richardipsum@vx21.xyz>
Date:   Sat, 30 Nov 2019 03:45:01 +0000

Sanitise test environment

Diffstat:
Mtyarn.c | 13+++++++++++--
Mtyarn.in | 29++++++++++++++++++++++++-----
2 files changed, 35 insertions(+), 7 deletions(-)

diff --git a/tyarn.c b/tyarn.c @@ -490,8 +490,17 @@ static int tyarn_parse_args(lua_State *L) lua_createtable(L, 0, envoptsn); for (size_t i = 0; i < envoptsn; i++) { - lua_pushstring(L, envopts[i]); - lua_rawseti(L, -2, i + 1); + char *p = strchr(envopts[i], '='); + if (p == NULL) { + fprintf(stderr, "%s: invalid argument to --env: %s\n", + buf[0], envopts[i]); + exit(1); + } + char *key = envopts[i]; + char *value = p + 1; + *p = '\0'; + lua_pushstring(L, value); + lua_setfield(L, -2, key); } for (int i = 0; i < bufn; i++) { diff --git a/tyarn.in b/tyarn.in @@ -270,12 +270,31 @@ function load_shell_library(path) return fh:read('*all') end +function cleanenv() + return { + ['TERM'] = 'dumb', + ['SHELL'] = '/bin/sh', + ['LC_ALL'] = 'C', + ['USER'] = 'tomjon', + ['USERNAME'] = 'tomjon', + ['LOGNAME'] = 'tomjon' + } +end + +function env_from_table(env) + local e = {} + for k, v in pairs(env) do + table.insert(e, string.format('%s=%s', k, v)) + end + return e +end + function run_step(scenario_dir, datadir, implementations, scenario_key, step, shell_prelude) success = true skip_scenario = false - env = {} + env = cleanenv() shell_script_lines = {} - table.insert(env, string.format("DATADIR=%s", datadir)) + env['DATADIR'] = datadir debug(string.format("Run step %s", step)) step_impl, step_captures = find_matching_implementation(implementations, step) @@ -288,11 +307,11 @@ function run_step(scenario_dir, datadir, implementations, scenario_key, step, sh end for n, capture in ipairs(step_captures) do - table.insert(env, string.format('MATCH_%d=%s', n, capture)) + env[string.format('MATCH_%d', n)] = capture end for k, v in pairs(parsed_env) do - table.insert(env, v) + env[k] = v end for _, impl_line in ipairs(step_impl) do @@ -322,7 +341,7 @@ function run_step(scenario_dir, datadir, implementations, scenario_key, step, sh debug('shell_script_str:', shell_script_str) cmd = {"/usr/bin/env", parsed_args["shell"] or DEFAULT_SHELL, path} - ret, exit_code, stdout, stderr = tyarn.exec(cmd, env, datadir) + ret, exit_code, stdout, stderr = tyarn.exec(cmd, env_from_table(env), datadir) if parsed_args["show_stdout"] and string.len(stdout) > 0 then io.stderr:write("STDOUT:\n" .. stdout) end