function TestRunner.runPlanNode(W,m,O)local function q(m,O)local q=true local c _G[RUNNING_GLOBAL]=true O=O or""local t=getfenv(m)for W,m in pairs(TestRunner.environment)do t[W]=m end t.fail=function(W)if W==nil then W="\102\097\105\108\040\041 \119\097\115 \099\097\108\108\101\100\046"end q=false c=O..(W..("\n"..debug.traceback()))end local Z=W:getContext()local l,S=xpcall(function()m(Z)end,function(W)return O..(W..("\n"..debug.traceback()))end)if not l then q=false c=S end _G[RUNNING_GLOBAL]=nil return q,c end local function c(W)for W,m in ipairs(O:getBeforeEachHooks())do local O,c=q(m,"\098\101\102\111\114\101\069\097\099\104 \104\111\111\107\058 ")if not O then return false,c end end do local m,O=q(W.callback)if not m then return false,O end end for W,m in ipairs(O:getAfterEachHooks())do local O,c=q(m,"\097\102\116\101\114\069\097\099\104 \104\111\111\107\058 ")if not O then return false,c end end return true,nil end O:pushHooksFrom(m)local t=false for m,O in ipairs(O:getBeforeAllHooks())do local c,Z=q(O,"\098\101\102\111\114\101\065\108\108 \104\111\111\107\058 ")if not c then W:addDummyError("\098\101\102\111\114\101\065\108\108",Z)t=true end end if not t then for m,q in ipairs(m.children)do W:pushNode(q)if q.type==TestEnum.NodeType.It then if W:shouldSkip()then W:setSkipped()else local m,O=c(q)if m then W:setSuccess()else W:setError(O)end end elseif q.type==TestEnum.NodeType.Describe then TestRunner.runPlanNode(W,q,O)if q.loadError then local m="\069\114\114\111\114 \100\117\114\105\110\103 \112\108\097\110\110\105\110\103\058 "..q.loadError W:setError(m)else W:setStatusFromChildren()end end W:popNode()end end for m,O in ipairs(O:getAfterAllHooks())do local c,t=q(O,"\097\102\116\101\114\065\108\108 \104\111\111\107\058 ")if not c then W:addDummyError("\097\102\116\101\114\065\108\108",t)end end O:popHooks()end return TestRunner

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
