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
Пример #2
0
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
Пример #4
0
 def spyRun(*, grepArgs, projectDir, reporter, silent):
     nonlocal spy
     spy = o(grepArgs=grepArgs,
             projectDir=projectDir,
             reporter=reporter,
             silent=silent)
     return 0
Пример #5
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)
Пример #6
0
    def return_(truthyResult):
        def otherwise(falseyResult):
            if condition:
                return truthyResult
            else:
                return falseyResult

        return o(otherwise=otherwise)
Пример #7
0
def createASuite(label, *, succeeded, parentSuite):
    return o(
        label=label,
        fn=noop,
        tests=[],
        suites=[],
        parentSuite=parentSuite,
        succeeded=succeeded,
    )
Пример #8
0
def _getCommon(*, label, fn, parentSuite, rootState, after, before):
    return o(
        label=label,
        fn=fn,
        parentSuite=parentSuite,
        rootState=rootState,
        after=after,
        before=before,
    )
Пример #9
0
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
Пример #10
0
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)
Пример #11
0
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
Пример #12
0
def whenTruthy(condition):
    def return_(truthyResult):
        def otherwise(falseyResult):
            if condition:
                return truthyResult
            else:
                return falseyResult

        return o(otherwise=otherwise)

    return o(return_=return_)
Пример #13
0
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
Пример #14
0
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
Пример #15
0
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
Пример #16
0
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
Пример #17
0
    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
Пример #18
0
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
Пример #19
0
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
Пример #20
0
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
Пример #21
0
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'
    }
  ]
Пример #22
0
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)
Пример #24
0
def report(state):
    global lastCalledState
    lastCalledState = o(testState=state, wasCalled=True)
Пример #25
0
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)
Пример #26
0
def spySubprocessRun(*args, **kwargs):
    global spyResult
    spyResult = o(args=args, kwargs=kwargs)
    return o(returncode=0, stdout="success")
Пример #27
0
# ---- #
# 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)
Пример #28
0
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
Пример #29
0
def makeDefaultCtxProps():
    return o(inArray=False, indentLevel=0, hasKeys=False, result=LinkedList())
Пример #30
0
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),
    ],
)