From fc450669c048dccc1ccf3a0f198ad11ee7a03c6a Mon Sep 17 00:00:00 2001 From: Caleb Maclennan Date: Thu, 19 Nov 2020 22:38:29 +0300 Subject: [PATCH 1/4] fix(context): Restore state on its own terms, not the terms of the current env --- busted/context.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/busted/context.lua b/busted/context.lua index 2a8bc6b9..3e7e8fd1 100644 --- a/busted/context.lua +++ b/busted/context.lua @@ -17,9 +17,9 @@ local function restore(state) for k,_ in next, _G, nil do rawset(_G, k, state.g[k]) 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() From ca35d6d865b39fe48c49c42445e225aeaa23c629 Mon Sep 17 00:00:00 2001 From: Caleb Maclennan Date: Wed, 2 Dec 2020 17:11:46 +0300 Subject: [PATCH 2/4] chore(core): Add internal function for requiring 3rd party libraries --- busted/core.lua | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/busted/core.lua b/busted/core.lua index b2faeb7f..c375dd9f 100644 --- a/busted/core.lua +++ b/busted/core.lua @@ -1,3 +1,13 @@ +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 @@ -47,6 +57,8 @@ return function() local busted = {} busted.version = '2.0.0-0' + busted.require = busted_require + local root = require 'busted.context'() busted.context = root.ref() From 2c978f6dba22db6070d98edb6edca53011dd9ad5 Mon Sep 17 00:00:00 2001 From: Caleb Maclennan Date: Wed, 2 Dec 2020 17:15:48 +0300 Subject: [PATCH 3/4] fix(core): Isolate 3rd party libraries used by busted from tests --- busted/core.lua | 10 +++++----- busted/init.lua | 10 +++++----- busted/runner.lua | 11 ++++++----- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/busted/core.lua b/busted/core.lua index c375dd9f..22bd9b72 100644 --- a/busted/core.lua +++ b/busted/core.lua @@ -11,9 +11,9 @@ 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 = { @@ -52,13 +52,13 @@ local function isCallable(obj) end return function() - local mediator = require 'mediator'() - local busted = {} busted.version = '2.0.0-0' busted.require = busted_require + local mediator = busted.require 'mediator'() + local root = require 'busted.context'() busted.context = root.ref() diff --git a/busted/init.lua b/busted/init.lua index 5092960c..06023ea8 100644 --- a/busted/init.lua +++ b/busted/init.lua @@ -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' local fixture_path = require('busted.fixtures').fixture_path diff --git a/busted/runner.lua b/busted/runner.lua index 274a3c39..d852660d 100644 --- a/busted/runner.lua +++ b/busted/runner.lua @@ -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 @@ -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'() From ca8a105eeb6d077dded2d38586a6f5568ed0e2bc Mon Sep 17 00:00:00 2001 From: Caleb Maclennan Date: Mon, 7 Dec 2020 12:59:29 +0300 Subject: [PATCH 4/4] test(core): Test that Busted's 3rd party requires are not exposed --- spec/insulate-expose_spec.lua | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/spec/insulate-expose_spec.lua b/spec/insulate-expose_spec.lua index bba84bd6..3273cc10 100644 --- a/spec/insulate-expose_spec.lua +++ b/spec/insulate-expose_spec.lua @@ -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'