From 68f4b6f1490382ff6bfa915e23342933d48a5a32 Mon Sep 17 00:00:00 2001 From: Steap2448 Date: Wed, 2 Jun 2021 17:48:05 +0300 Subject: [PATCH] Naive attempt --- luatest/runner.lua | 33 ++++++++++++++++++++++++++++++++- test/fixtures/mock.lua | 16 ++++++++++++++++ test/runner_test.lua | 5 +++++ 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/mock.lua diff --git a/luatest/runner.lua b/luatest/runner.lua index cece331f..92d5a2c2 100644 --- a/luatest/runner.lua +++ b/luatest/runner.lua @@ -75,10 +75,11 @@ Options: -v, --verbose: Increase verbosity -q, --quiet: Set verbosity to minimum -c Disable capture + --sandbox Sandbox group of tests -b Print full backtrace (don't remove luatest frames) -e, --error: Stop on first error -f, --failure: Stop on first failure or error - --shuffle VALUE: Set execution order: + -s, --shuffle VALUE: Set execution order: - group[:seed] - shuffle tests within group - all[:seed] - shuffle all tests - none - sort tests within group by line number (default) @@ -148,6 +149,8 @@ function Runner.parse_cmd_line(args) result.full_backtrace = true elseif arg == '-c' then result.enable_capture = false + elseif arg == '--sandbox' then + result.enable_sandbox = true elseif arg == '--coverage' then result.coverage_report = true elseif arg:sub(1,1) == '-' then @@ -280,7 +283,32 @@ function Runner.mt:start_suite(selected_count, not_selected_count) self.output:start_suite() end +function Runner.mt:enter_sandbox() + assert(self.packages_snap == nil, 'Already in sandbox') + + self.packages_snap = table.deepcopy(package.loaded) +end + +function Runner.mt:exit_sandbox() + assert(self.packages_snap, 'Not in a sandbox') + + for name, _ in pairs(package.loaded) do + if self.packages_snap[name] == nil then + package.loaded[name] = nil + end + end + + for name, content in pairs(self.packages_snap) do + package.loaded[name] = content + end + + self.packages_snap = nil +end + function Runner.mt:start_group(group) + if self.enable_sandbox then + self:enter_sandbox() + end self.output:start_group(group) end @@ -320,6 +348,9 @@ end function Runner.mt:end_group(group) self.output:end_group(group) + if self.enable_sandbox then + self:exit_sandbox() + end end function Runner.mt:end_suite() diff --git a/test/fixtures/mock.lua b/test/fixtures/mock.lua new file mode 100644 index 00000000..85ae7fc3 --- /dev/null +++ b/test/fixtures/mock.lua @@ -0,0 +1,16 @@ +local t = require('luatest') +local g1 = t.group('first_group') +local g2 = t.group('second_group') + +function g1.test_make_mock() + package.loaded.math.pi = 3 + t.assert_not_almost_equals(package.loaded.math.pi, 3.14, 0.01) + + package.loaded.new_package = 'sup' +end + +function g2.test_check_mock() + t.assert_almost_equals(package.loaded.math.pi, 3.14, 0.01) + t.assert_equals(package.loaded.new_package, nil) +end + diff --git a/test/runner_test.lua b/test/runner_test.lua index b148f7ca..0b5bc993 100644 --- a/test/runner_test.lua +++ b/test/runner_test.lua @@ -189,3 +189,8 @@ g.test_show_help = function() local captured = capture:flush() t.assert_str_contains(captured.stdout, 'Usage: luatest') end + +g.test_sandbox = function() + local status = os.execute('bin/luatest test/fixtures/mock.lua --sandbox') + t.assert_equals(status, 0) +end