-
Notifications
You must be signed in to change notification settings - Fork 170
/
Copy pathspec_helper.lua
151 lines (123 loc) · 3.87 KB
/
spec_helper.lua
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
--- nasty monkey patch to create new notification for the whole it block
--- busted does not have builtin way of having a hook around a test with its before/after hooks
do
local function getlocal(fn, var)
local i = 1
while true do
local name, val = debug.getlocal(fn + 1, i)
if name == var then
return val
elseif not name then
break
end
i = i + 1
end
end
local function getupvalue(fn, var)
local i = 1
while true do
local name, val = debug.getupvalue(fn, i)
if name == var then
return val
elseif not name then
break
end
i = i + 1
end
end
--- busted/runner.lua:147 is 5 stacks above
-- https://github.com/Olivine-Labs/busted/blob/v2.0.rc12-1/busted/runner.lua#L147
local busted = getlocal(5, 'busted')
--- busted/core.lua:240 has "executors" upvalue available
-- https://github.com/Olivine-Labs/busted/blob/v2.0.rc12-1/busted/core.lua#L240
local executors = getupvalue(busted.register, 'executors')
--- busted/init.lua:20 defines the "it" method we want to wrap around
-- https://github.com/Olivine-Labs/busted/blob/v2.0.rc12-1/busted/init.lua#L20
local it = executors.it
busted.register('it', function(element)
local parent = busted.context.parent(element)
if busted.safe_publish('it', { 'it', 'start' }, element, parent) then
it(element)
end
busted.safe_publish('it', { 'it', 'end' }, element, parent)
end)
end
require 'luassert_helper'
require 'ngx_helper'
require 'jwt_helper'
local busted = require('busted')
local env = require('resty.env')
local previous_env = {}
local set = env.set
local null = {'null'}
-- override resty.env.set with custom function that remembers previous values
env.set = function(name, ...)
local previous = set(name, ...)
if not previous_env[name] then
previous_env[name] = previous or null
end
return previous
end
-- so they can be reset back to the values before the test run
local function reset()
for name, value in pairs(previous_env) do
if value == null then
value = nil
end
set(name, value)
end
previous_env = {}
env.reset()
-- To make sure that we are using valid policy configs in the tests.
set('APICAST_VALIDATE_POLICY_CONFIGS', true)
end
busted.before_each(reset)
busted.after_each(reset)
local resty_proxy = require('resty.http.proxy')
busted.before_each(function()
resty_proxy:reset()
end)
local resty_resolver = require 'resty.resolver'
busted.before_each(resty_resolver.reset)
busted.subscribe({ 'file', 'start' }, function ()
require('apicast.loader')
return nil, true -- needs to return true as second return value to continue executing the chain
end)
busted.subscribe({ 'file', 'end' }, function ()
collectgarbage()
return nil, true
end)
do
-- busted does auto-insulation and tries to reload all files for every test file
-- that breaks ffi.cdef as it can't be called several times with the same argument
-- backports https://github.com/Olivine-Labs/busted/commit/db6d8b4be8fd099ab387efeb8232cfd905912abb
local ffi = require('ffi')
local cdef = ffi.cdef
local cdef_cache = {}
function ffi.cdef(def)
if not cdef_cache[def] then
cdef(def)
cdef_cache[def] = true
end
end
end
_G.fixture = function (...)
local path = require('pl.path')
local file = require('pl.file')
return file.read(path.join('spec', 'fixtures', ...)) or file.read(path.join('t', 'fixtures', ...))
end
do -- stub http_ng
local http_ng = require('resty.http_ng')
local test_backend_client = require 'resty.http_ng.backend.test'
local test_backend
local stub = require('luassert.stub')
local stubbed
busted.before_each(function()
test_backend = test_backend_client.new()
stubbed = stub(http_ng, 'backend', test_backend)
end)
busted.after_each(function()
test_backend.verify_no_outstanding_expectations()
stubbed:revert()
end)
end