function TestRunner.runPlanNode(E,e,Z)local function B(e,Z)local B=true local J _G[RUNNING_GLOBAL]=true Z=Z or""local T=getfenv(e)for E,e in pairs(TestRunner.environment)do T[E]=e end T.fail=function(E)if E==nil then E="\102\097\105\108\040\041 \119\097\115 \099\097\108\108\101\100\046"end B=false J=Z..(E..("\n"..debug.traceback()))end local H=E:getContext()local s,D=xpcall(function()e(H)end,function(E)return Z..(E..("\n"..debug.traceback()))end)if not s then B=false J=D end _G[RUNNING_GLOBAL]=nil return B,J end local function J(E)for E,e in ipairs(Z:getBeforeEachHooks())do local Z,J=B(e,"\098\101\102\111\114\101\069\097\099\104 \104\111\111\107\058 ")if not Z then return false,J end end do local e,Z=B(E.callback)if not e then return false,Z end end for E,e in ipairs(Z:getAfterEachHooks())do local Z,J=B(e,"\097\102\116\101\114\069\097\099\104 \104\111\111\107\058 ")if not Z then return false,J end end return true,nil end Z:pushHooksFrom(e)local T=false for e,Z in ipairs(Z:getBeforeAllHooks())do local J,H=B(Z,"\098\101\102\111\114\101\065\108\108 \104\111\111\107\058 ")if not J then E:addDummyError("\098\101\102\111\114\101\065\108\108",H)T=true end end if not T then for e,B in ipairs(e.children)do E:pushNode(B)if B.type==TestEnum.NodeType.It then if E:shouldSkip()then E:setSkipped()else local e,Z=J(B)if e then E:setSuccess()else E:setError(Z)end end elseif B.type==TestEnum.NodeType.Describe then TestRunner.runPlanNode(E,B,Z)if B.loadError then local e="\069\114\114\111\114 \100\117\114\105\110\103 \112\108\097\110\110\105\110\103\058 "..B.loadError E:setError(e)else E:setStatusFromChildren()end end E:popNode()end end for e,Z in ipairs(Z:getAfterAllHooks())do local J,T=B(Z,"\097\102\116\101\114\065\108\108 \104\111\111\107\058 ")if not J then E:addDummyError("\097\102\116\101\114\065\108\108",T)end end Z:popHooks()end return TestRunner
--[[
	Run the given test plan node and its descendants, using the given test
	session to store all of the results.
]]
function TestRunner.runPlanNode(session, planNode, lifecycleHooks)
	local function runCallback(callback, messagePrefix)
		local success = true
		local errorMessage
		-- Any code can check RUNNING_GLOBAL to fork behavior based on
		-- whether a test is running. We use this to avoid accessing
		-- protected APIs; it's a workaround that will go away someday.
		_G[RUNNING_GLOBAL] = true

		messagePrefix = messagePrefix or ""

		local testEnvironment = getfenv(callback)

		for key, value in pairs(TestRunner.environment) do
			testEnvironment[key] = value
		end

		testEnvironment.fail = function(message)
			if message == nil then
				message = "fail() was called."
			end

			success = false
			errorMessage = messagePrefix .. message .. "\n" .. debug.traceback()
		end

		local context = session:getContext()

		local nodeSuccess, nodeResult = xpcall(function()
			callback(context)
		end, function(message)
			return messagePrefix .. message .. "\n" .. debug.traceback()
		end)

		-- If a node threw an error, we prefer to use that message over
		-- one created by fail() if it was set.
		if not nodeSuccess then
			success = false
			errorMessage = nodeResult
		end

		_G[RUNNING_GLOBAL] = nil

		return success, errorMessage
	end

	local function runNode(childPlanNode)
		-- Errors can be set either via `error` propagating upwards or
		-- by a test calling fail([message]).

		for _, hook in ipairs(lifecycleHooks:getBeforeEachHooks()) do
			local success, errorMessage = runCallback(hook, "beforeEach hook: ")
			if not success then
				return false, errorMessage
			end
		end

		do
			local success, errorMessage = runCallback(childPlanNode.callback)
			if not success then
				return false, errorMessage
			end
		end

		for _, hook in ipairs(lifecycleHooks:getAfterEachHooks()) do
			local success, errorMessage = runCallback(hook, "afterEach hook: ")
			if not success then
				return false, errorMessage
			end
		end

		return true, nil
	end

	lifecycleHooks:pushHooksFrom(planNode)

	local halt = false
	for _, hook in ipairs(lifecycleHooks:getBeforeAllHooks()) do
		local success, errorMessage = runCallback(hook, "beforeAll hook: ")
		if not success then
			session:addDummyError("beforeAll", errorMessage)
			halt = true
		end
	end

	if not halt then
		for _, childPlanNode in ipairs(planNode.children) do
			session:pushNode(childPlanNode)

			if childPlanNode.type == TestEnum.NodeType.It then
				if session:shouldSkip() then
					session:setSkipped()
				else
					local success, errorMessage = runNode(childPlanNode)

					if success then
						session:setSuccess()
					else
						session:setError(errorMessage)
					end
				end
			elseif childPlanNode.type == TestEnum.NodeType.Describe then
				TestRunner.runPlanNode(session, childPlanNode, lifecycleHooks)

				-- Did we have an error trying build a test plan?
				if childPlanNode.loadError then
					local message = "Error during planning: " .. childPlanNode.loadError
					session:setError(message)
				else
					session:setStatusFromChildren()
				end
			end

			session:popNode()
		end
	end

	for _, hook in ipairs(lifecycleHooks:getAfterAllHooks()) do
		local success, errorMessage = runCallback(hook, "afterAll hook: ")
		if not success then
			session:addDummyError("afterAll", errorMessage)
		end
	end

	lifecycleHooks:popHooks()
end

return TestRunner
