Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run tests in individual sandbox #135

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion luatest/runner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand Down
16 changes: 16 additions & 0 deletions test/fixtures/mock.lua
Original file line number Diff line number Diff line change
@@ -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

5 changes: 5 additions & 0 deletions test/runner_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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