Skip to content

Commit

Permalink
Merge pull request #654 from alerque/isolate-requires
Browse files Browse the repository at this point in the history
Isolate 3rd party libraries used internally by Busted from test environments
  • Loading branch information
alerque authored Nov 29, 2024
2 parents 94d0081 + 321917d commit a144124
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 18 deletions.
6 changes: 3 additions & 3 deletions busted/context.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ local function restore(state)
rawset(_G, k, v)
end
end
for k,_ in pairs(package.loaded) do
package.loaded[k] = state.loaded[k]
end
local set = function(k) package.loaded[k] = state.loaded[k] end
for k,_ in pairs(package.loaded) do set(k) end
for k,_ in pairs(state.loaded) do set(k) end
end

return function()
Expand Down
22 changes: 17 additions & 5 deletions busted/core.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
local busted_internal_loaded = {}

local busted_require = function (path)
local default_loaded = package.loaded
package.loaded = busted_internal_loaded
local pack = require(path)
package.loaded = default_loaded
return pack
end

local getfenv = require 'busted.compatibility'.getfenv
local setfenv = require 'busted.compatibility'.setfenv
local unpack = require 'busted.compatibility'.unpack
local path = require 'pl.path'
local pretty = require 'pl.pretty'
local system = require 'system'
local path = busted_require 'pl.path'
local pretty = busted_require 'pl.pretty'
local system = busted_require 'system'
local throw = error

local failureMt = {
Expand Down Expand Up @@ -42,11 +52,13 @@ local function isCallable(obj)
end

return function()
local mediator = require 'mediator'()

local busted = {}
busted.version = '2.2.0'

busted.require = busted_require

local mediator = busted.require 'mediator'()

local root = require 'busted.context'()
busted.context = root.ref()

Expand Down
10 changes: 5 additions & 5 deletions busted/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ local function init(busted)

busted.hide('file')

local assert = require 'luassert'
local spy = require 'luassert.spy'
local mock = require 'luassert.mock'
local stub = require 'luassert.stub'
local match = require 'luassert.match'
local assert = busted.require 'luassert'
local spy = busted.require 'luassert.spy'
local mock = busted.require 'luassert.mock'
local stub = busted.require 'luassert.stub'
local match = busted.require 'luassert.match'

require 'busted.fixtures' -- just load into the environment, not exposing it

Expand Down
11 changes: 6 additions & 5 deletions busted/runner.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
-- Busted command-line runner

local path = require 'pl.path'
local tablex = require 'pl.tablex'
local term = require 'term'
local utils = require 'busted.utils'
local exit = require 'busted.compatibility'.exit
local loadstring = require 'busted.compatibility'.loadstring
Expand All @@ -11,12 +8,16 @@ local loaded = false
return function(options)
if loaded then return function() end else loaded = true end

local busted = require 'busted.core'()

local path = busted.require 'pl.path'
local tablex = busted.require 'pl.tablex'
local term = busted.require 'term'

local isatty = io.type(io.stdout) == 'file' and term.isatty(io.stdout)
options = tablex.update(require 'busted.options', options or {})
options.output = options.output or (isatty and 'utfTerminal' or 'plainTerminal')

local busted = require 'busted.core'()

local cli = require 'busted.modules.cli'(options)
local filterLoader = require 'busted.modules.filter_loader'()
local helperLoader = require 'busted.modules.helper_loader'()
Expand Down
14 changes: 14 additions & 0 deletions spec/insulate-expose_spec.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
assert.is_nil(package.loaded.pl)
assert.is_nil(package.loaded['pl.file'])

describe('Isolation from Busted', function()
it('in normal blocks', function()
assert.is_nil(package.loaded['pl.path'])
end)

insulate('in insulated blocks', function()
assert.is_nil(package.loaded['pl.path'])
end)

expose('in exposed blocks', function()
assert.is_nil(package.loaded['pl.path'])
end)
end)

describe('Tests insulation', function()
insulate('environment inside insulate', function()
pl = require 'pl'
Expand Down

0 comments on commit a144124

Please sign in to comment.