From 8e6a83911027de167d689d060ce581f653d73213 Mon Sep 17 00:00:00 2001 From: George Roman Date: Tue, 24 Jul 2018 11:09:42 +0300 Subject: Tests: add test_env.run_in_tmp utility function --- spec/util/test_env.lua | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'spec/util') diff --git a/spec/util/test_env.lua b/spec/util/test_env.lua index 0218aa53..181f4693 100644 --- a/spec/util/test_env.lua +++ b/spec/util/test_env.lua @@ -101,6 +101,25 @@ function test_env.get_tmp_path() return path end +--- Helper function that runs the given function inside +-- a temporary directory, isolating it +-- @param f function: the function to be run +function test_env.run_in_tmp(f, finally) + local olddir = lfs.currentdir() + local tmpdir = test_env.get_tmp_path() + lfs.mkdir(tmpdir) + lfs.chdir(tmpdir) + + if finally then + finally(function() + lfs.chdir(olddir) + lfs.rmdir(tmpdir) + end) + end + + f(tmpdir) +end + --- Helper function for execute_bool and execute_output -- @param command string: command to execute -- @param print_command boolean: print command if 'true' -- cgit v1.2.3-55-g6feb