/usr/share/lua/5.1/busted/context.lua is in lua-busted 2.0~rc12-1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | local tablex = require 'pl.tablex'
local function save()
local g = {}
for k,_ in next, _G, nil do
g[k] = rawget(_G, k)
end
return {
gmt = debug.getmetatable(_G),
g = g,
loaded = tablex.copy(package.loaded)
}
end
local function restore(state)
setmetatable(_G, state.gmt)
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
end
return function()
local context = {}
local data = { descriptor = 'suite', attributes = {} }
local parents = {}
local children = {}
local stack = {}
local states = {}
function context.ref()
local ref = {}
local ctx = data
local function unwrap(element, levels)
local levels = levels or 1
local parent = element
for i = 1, levels do
parent = ref.parent(parent)
if not parent then break end
end
if not element.env then element.env = {} end
setmetatable(element.env, {
__newindex = function(self, key, value)
if not parent then
_G[key] = value
else
if not parent.env then parent.env = {} end
parent.env[key] = value
end
end
})
end
local function push_state(current)
local state = false
if current.attributes.envmode == 'insulate' then
state = save()
elseif current.attributes.envmode == 'unwrap' then
unwrap(current)
elseif current.attributes.envmode == 'expose' then
unwrap(current, 2)
end
table.insert(states, state)
end
local function pop_state(current)
local state = table.remove(states)
if current.attributes.envmode == 'expose' then
states[#states] = states[#states] and save()
end
if state then
restore(state)
end
end
function ref.get(key)
if not key then return ctx end
return ctx[key]
end
function ref.set(key, value)
ctx[key] = value
end
function ref.clear()
data = { descriptor = 'suite', attributes = {} }
parents = {}
children = {}
stack = {}
states = {}
ctx = data
end
function ref.attach(child)
if not children[ctx] then children[ctx] = {} end
parents[child] = ctx
table.insert(children[ctx], child)
end
function ref.children(parent)
return children[parent] or {}
end
function ref.parent(child)
return parents[child]
end
function ref.push(current)
if not parents[current] and current ~= data then error('Detached child. Cannot push.') end
if ctx ~= current then push_state(current) end
table.insert(stack, ctx)
ctx = current
end
function ref.pop()
local current = ctx
ctx = table.remove(stack)
if ctx ~= current then pop_state(current) end
if not ctx then error('Context stack empty. Cannot pop.') end
end
return ref
end
return context
end
|