def runTests(r): sys.path.insert(0, success) grepKeep = o(grepTests=[], grepSuites=[], grep=["keep"]) code = "runProcess(success, 'theTests.spyReporter', 'False', grepKeep)" result = runProcess(success, "theTests.spyReporter", "False", grepKeep) testState = getTestState() passed = ( result.code == 0 and result.stdout is None and result.stderr is None and hasExpectedRootTests(testState) and hasExpectedRootSuites(testState) and spyReporter.lastCalledState.wasCalled is True and spyReporter.lastCalledState.testState is testState ) if not passed: r.addError(code) grepTwoTest = o(grepTests=["^two test$"], grepSuites=[], grep=[]) code = "runProcess(success, 'theTests.spyReporter', 'False', grepTwoTest)" result = runProcess(success, "theTests.spyReporter", "False", grepTwoTest) testState = getTestState() passed = ( result.code == 0 and result.stdout is None and result.stderr is None and hasTest2(testState) and len(testState.suites) == 0 and spyReporter.lastCalledState.wasCalled is True and spyReporter.lastCalledState.testState is testState ) if not passed: r.addError(code) grepSubSuite1 = o(grepTests=[], grepSuites=["^sub suite 1 keep$"], grep=[]) code = "runProcess(success, 'theTests.spyReporter', 'False', grepSubSuite1)" result = runProcess(success, "theTests.spyReporter", "False", grepSubSuite1) testState = getTestState() passed = ( result.code == 0 and result.stdout is None and result.stderr is None and len(testState.tests) == 0 and hasSubSuite1(testState) and spyReporter.lastCalledState.wasCalled is True and spyReporter.lastCalledState.testState is testState ) if not passed: r.addError(code) sys.path.pop(0) del sys.modules["tests"] return r
def createRootSuite(label, *, succeeded): return o(label=label, fn=noop, tests=[], suites=[], parentSuite=None, succeeded=succeeded)
def validateAndGetReportFn(reporter, silent, cliResult): validationResult = o(report=None, cliResult=cliResult, hasError=False) try: reporterModule = importlib.import_module(reporter) except: if not silent: err = "An error occurred while importing the reporter" cliResult.stderr = os.linesep + err + twoLineSeps + format_exc() cliResult.code = 2 validationResult.hasError = True return validationResult if hasattr(reporterModule, "report") and callable(reporterModule.report): validationResult.report = reporterModule.report else: if not silent: cliResult.stderr = ( os.linesep + "the reporter must expose a callable 'report'" ) cliResult.code = 2 validationResult.hasError = True return validationResult
def spyRun(*, grepArgs, projectDir, reporter, silent): nonlocal spy spy = o(grepArgs=grepArgs, projectDir=projectDir, reporter=reporter, silent=silent) return 0
def str_(someString): def wrap_endsWith(suffix): return endsWith(suffix)(someString) def wrap_startsWith(prefix): return startsWith(prefix)(someString) return o(endsWith=wrap_endsWith, startsWith=wrap_startsWith)
def return_(truthyResult): def otherwise(falseyResult): if condition: return truthyResult else: return falseyResult return o(otherwise=otherwise)
def createASuite(label, *, succeeded, parentSuite): return o( label=label, fn=noop, tests=[], suites=[], parentSuite=parentSuite, succeeded=succeeded, )
def _getCommon(*, label, fn, parentSuite, rootState, after, before): return o( label=label, fn=fn, parentSuite=parentSuite, rootState=rootState, after=after, before=before, )
def createATest(label, *, error=None, parentSuite=None, succeeded): aTest = o(fn=noop, label=label, parentSuite=parentSuite, succeeded=succeeded) if not succeeded: aTest.error = iif(error is None, Exception("the error message"), error) return aTest
def wrapWith(*, indent): wrappedIndent = indent def wrappedPprint(something, *, indent=wrappedIndent): pprint(something, indent=indent) def wrappedFormat(something, *, indent=wrappedIndent): return format(something, indent=indent) return o(pprint=wrappedPprint, format=wrappedFormat)
def runTests(r): code = "successRun(projectDir=projectDir)" result = successRun(projectDir=projectDir) if result != 0: r.addError(code) code = "failRun(projectDir=projectDir)" result = failRun(projectDir=projectDir) if result != 1: r.addError(code) code = "errorRun(projectDir=projectDir)" result = errorRun(projectDir=projectDir) if result != 2: r.addError(code) code = ( "spyRun(grepArgs=grepArgs, projectDir=projectDir" ", reporter='some_reporter', silent=True)" ) grepArgs = o( grep=["grep1", "grep2"], grepTests=["greptest1"], grepSuites=["grepsuite1"] ) expectedCliGrepArgs = [ "--grep", "grep1", "--grep", "grep2", "--grep-tests", "greptest1", "--grep-suites", "grepsuite1", ] result = spyRun( grepArgs=grepArgs, projectDir=projectDir, reporter="some_reporter", silent=True ) passed = ( result == 0 and len(spyResult.args) == 1 and spyResult.args[0] == [ sys.executable, "-m", "po_simple_test._vendor.simple_test_process", "some_reporter", "True", *expectedCliGrepArgs, ] and spyResult.kwargs == {"cwd": projectDir} ) if not passed: r.addError(code) return r
def whenTruthy(condition): def return_(truthyResult): def otherwise(falseyResult): if condition: return truthyResult else: return falseyResult return o(otherwise=otherwise) return o(return_=return_)
def getValueAtPath(aDict, pathToValue): result = o(hasValue=None, value=None) val = aDict for segment in pathToValue: if segment not in val: result.hasValue = False return result val = val[segment] result.hasValue = True result.value = val return result
def runTests(r): input = o(tabbed4spaces=True) wrapped = wrapWith(indent=4) expected = dedent("""\ { tabbed4spaces: true } """) code = "wrapped.format(input)" actual = wrapped.format(input) if actual != expected: r.addError(code) return r
def runSpySimpleTest(*args, **kwargs): spy = o() def spyRun(*, grepArgs, projectDir, reporter, silent): nonlocal spy spy = o(grepArgs=grepArgs, projectDir=projectDir, reporter=reporter, silent=silent) return 0 spy.cliResult = createRunSimpleTest(spyRun)(*args, **kwargs) return spy
def combine(primaryObj): simplePrimary = o() for k, v in primaryObj.__dict__.items(): setattr(simplePrimary, k, v) def combine_inner(secondaryObj): result = copy(simplePrimary) for k, v in secondaryObj.__dict__.items(): if k in result.__dict__: _raiseOverlappingKeysError(k) else: setattr(result, k, v) return result return combine_inner
def createCtx(currentCtx, **kwargs): props = o(**kwargs) newCtx = assignAll.simpleNamespaces( [makeDefaultCtxProps(), props, style]) if currentCtx: newCtx.refs = currentCtx.refs newCtx.indentLevel = currentCtx.indentLevel + 1 # # refPath holds an array of tuple pairs # (IsArrayIndex, key) # newCtx.refPath = currentCtx.refPath + [None] newCtx.refs = currentCtx.refs newCtx.indentStr = getIndentStr(newCtx) return newCtx
def runSimpleTest(run, args): result = o(stdout=None, stderr=None, code=None) numArgs = len(args) if numArgs == 1: if args[0] == "--help": result.stdout = usage result.code = 0 return result elif args[0] == "--version": result.stdout = version result.code = 0 return result validationResult = validateAndParseArgs(args, result) if validationResult.hasError: return validationResult.cliResult argsObj = validationResult.argsObj isSilent = argsObj.silent try: subprocessReturnCode = run( grepArgs=argsObj.grepArgs, reporter=argsObj.reporter, projectDir=argsObj.projectDir, silent=isSilent, ) result.code = subprocessReturnCode return result except: if not isSilent: result.stderr = ( "An unexpected error occurred" + (os.linesep * 2) + format_exc() ) result.code = 2 return result
def createTests(label, *, error=None, formattedException=None, parentSuite=None, succeeded): tests = [ o( label=label, fn=noop, formattedException=formattedException, parentSuite=parentSuite, succeeded=succeeded, ) ] if not succeeded: tests[0].error = error or Exception("the error message") parentSuite.tests = tests return tests
def addSuite(label, after, afterEach, before, beforeEach, fn): currentSuite = state.currentSuite parent = currentSuite or state if afterEach is noop: afterEach = copy(parent.afterEach) else: afterEach = appendOne(afterEach)(parent.afterEach) if beforeEach is noop: beforeEach = copy(parent.beforeEach) else: beforeEach = appendOne(beforeEach)(parent.beforeEach) newSuite = assign( o( tests=[], suites=[], succeeded=True, afterEach=afterEach, beforeEach=beforeEach, ) )( _getCommon( label=label, fn=fn, parentSuite=currentSuite, rootState=state, after=after, before=before, ) ) parent.suites.append(newSuite) return state
from types import SimpleNamespace as o expected = o() expected.case1 = """\ { aKey: 'a val' } """ expected.case2 = """\ [ { aKey: 'a val' } { bKey: 'b val' } ] """ expected.case3 = """\ { anArray: [ { aKey: 'a val' } { bKey: 'b val' } ]
def format(something, *, indent=defaultIndent): return bfFormat(something, o(indent=indent))
def removeSucceededTests(aSuite): tests = discardWhen(isSucceeded)(aSuite.tests) suites = map_(removeSucceededTests)(aSuite.suites) return mAssign(o(tests=tests, suites=suites))(aSuite)
def report(state): global lastCalledState lastCalledState = o(testState=state, wasCalled=True)
from copy import deepcopy from types import SimpleNamespace as o initialLastCalledState = o(wasCalled=False) lastCalledState = deepcopy(initialLastCalledState) def report(state): global lastCalledState lastCalledState = o(testState=state, wasCalled=True) def resetLastCalledState(): global lastCalledState lastCalledState = deepcopy(initialLastCalledState)
def spySubprocessRun(*args, **kwargs): global spyResult spyResult = o(args=args, kwargs=kwargs) return o(returncode=0, stdout="success")
# ---- # # Init # # ---- # def spySubprocessRun(*args, **kwargs): global spyResult spyResult = o(args=args, kwargs=kwargs) return o(returncode=0, stdout="success") currentDir = path.dirname(path.abspath(__file__)) projectDir = path.join(currentDir, "fixtures", "project") spyResult = None spyRun = createRun(spySubprocessRun) successRun = createRun(justReturn(o(returncode=0))) failRun = createRun(justReturn(o(returncode=1))) errorRun = createRun(justReturn(o(returncode=2))) def runTests(r): code = "successRun(projectDir=projectDir)" result = successRun(projectDir=projectDir) if result != 0: r.addError(code) code = "failRun(projectDir=projectDir)" result = failRun(projectDir=projectDir) if result != 1: r.addError(code)
from simple_test_process.state import _getState as getTestState from types import SimpleNamespace as o from . import spyReporter from .utils import makeGetPathToFixture, runProcess import sys # ---- # # Init # # ---- # getPathToFixture = makeGetPathToFixture("many") fail = getPathToFixture("fail") noGrepArgs = o(grepTests=[], grepSuites=[], grep=[]) # ---- # # Main # # ---- # def runTests(r): sys.path.insert(0, fail) code = "runProcess(fail, 'theTests.spyReporter', 'False', noGrepArgs)" result = runProcess(fail, "theTests.spyReporter", "False", noGrepArgs) testState = getTestState() passed = ( result.code == 1 and result.stdout is None
def makeDefaultCtxProps(): return o(inArray=False, indentLevel=0, hasKeys=False, result=LinkedList())
from types import SimpleNamespace as o from .helpers import createState, createATest onlyRootTests = o() onlyRootTests.twoSucceededRootTests = createState( succeeded=True, tests=[ createATest("success 1", succeeded=True), createATest("success 2", succeeded=True), ], ) onlyRootTests.oneFailedOneSucceededRootTests = createState( succeeded=False, tests=[ createATest("success 1", succeeded=True), createATest("fail 2", succeeded=False), ], )