Beispiel #1
0
def handle_worker(config):
    """usage: cosmic-ray worker [options] <module> <operator> <occurrence> <test-runner> [-- <test-args> ...]

Run a worker process which performs a single mutation and test run. Each
worker does a minimal, isolated chunk of work: it mutates the <occurence>-th
instance of <operator> in <module>, runs the test suite defined by
<test-runner> and <test-args>, prints the results, and exits.

Normally you won't run this directly. Rather, it will be launched by celery
worker tasks.

options:
  --no-local-import   Disallow importing module from the current directory
  --keep-stdout       Do not squelch stdout
"""
    if not config['--no-local-import']:
        sys.path.insert(0, '')

    operator = cosmic_ray.plugins.get_operator(config['<operator>'])
    test_runner = cosmic_ray.plugins.get_test_runner(config['<test-runner>'],
                                                     config['<test-args>'])

    with open(os.devnull, 'w') as devnull,\
        redirect_stdout(sys.stdout if config['--keep-stdout'] else devnull):
        work_record = cosmic_ray.worker.worker(config['<module>'], operator,
                                               int(config['<occurrence>']),
                                               test_runner)

    sys.stdout.write(json.dumps(work_record))
Beispiel #2
0
def handle_worker(args):
    """usage: {program} worker \
    [options] <module> <operator> <occurrence> [<config-file>]

    Run a worker process which performs a single mutation and test run.
    Each worker does a minimal, isolated chunk of work: it mutates the
    <occurence>-th instance of <operator> in <module>, runs the test
    suite defined in the configuration, prints the results, and exits.

    Normally you won't run this directly. Rather, it will be launched
    by an execution engine. However, it can be useful to run this on
    its own for testing and debugging purposes.

    options:
      --keep-stdout       Do not squelch stdout

    """
    config = load_config(args.get('<config-file>'))

    if config.get('local-imports', True):
        sys.path.insert(0, '')

    operator = cosmic_ray.plugins.get_operator(args['<operator>'])
    test_runner = cosmic_ray.plugins.get_test_runner(
        config['test-runner']['name'], config['test-runner']['args'])

    with open(os.devnull, 'w') as devnull:
        with redirect_stdout(sys.stdout if args['--keep-stdout'] else devnull):
            work_item = cosmic_ray.worker.worker(args['<module>'], operator,
                                                 int(args['<occurrence>']),
                                                 test_runner)

    sys.stdout.write(json.dumps(work_item))

    return os.EX_OK
Beispiel #3
0
def handle_worker(config):
    """usage: cosmic-ray worker [options] <module> <operator> <occurrence> <test-runner> [-- <test-args> ...]

Run a worker process which performs a single mutation and test run. Each
worker does a minimal, isolated chunk of work: it mutates the <occurence>-th
instance of <operator> in <module>, runs the test suite defined by
<test-runner> and <test-args>, prints the results, and exits.

Normally you won't run this directly. Rather, it will be launched by celery
worker tasks.

options:
  --no-local-import   Disallow importing module from the current directory
  --keep-stdout       Do not squelch stdout
"""
    if not config['--no-local-import']:
        sys.path.insert(0, '')

    operator = cosmic_ray.plugins.get_operator(config['<operator>'])
    test_runner = cosmic_ray.plugins.get_test_runner(
        config['<test-runner>'],
        config['<test-args>'])

    with open(os.devnull, 'w') as devnull,\
        redirect_stdout(sys.stdout if config['--keep-stdout'] else devnull):
        result_type, data = cosmic_ray.worker.worker(
            config['<module>'],
            operator,
            int(config['<occurrence>']),
            test_runner)

    sys.stdout.write(
        json.dumps((result_type, data),
                   cls=cosmic_ray.json_util.JSONEncoder))
    def _run(self):
        collector = ResultCollector()

        with open(os.devnull, 'w') as devnull, redirect_stdout(devnull):
            pytest.main(list(self.test_args), plugins=[collector])

        return (all(not r.failed for r in collector.reports),
                [repr(r) for r in collector.reports if r.failed])
Beispiel #5
0
    def _run(self):
        collector = ResultCollector()

        with open(os.devnull, 'w') as devnull, redirect_stdout(devnull):
            pytest.main(list(self.test_args),
                        plugins=[collector])

        return (
            all(not r.failed for r in collector.reports),
            [repr(r) for r in collector.reports if r.failed])
    def _run(self):
        argv = ['', '--with-cosmic_ray']
        argv += list(self.test_args)
        collector = NoseResultsCollector()

        with open(os.devnull, 'w') as devnull, \
            redirect_stdout(devnull), redirect_stderr(devnull):
            nose.run(argv=argv, plugins=[collector])
        return (collector.result.wasSuccessful(), [
            r[1] for r in collector.result.errors + collector.result.failures
        ])
Beispiel #7
0
    def _run(self):
        argv = ['', '--with-cosmic_ray']
        argv += list(self.test_args)
        collector = NoseResultsCollector()

        with open(os.devnull, 'w') as devnull, \
            redirect_stdout(devnull), redirect_stderr(devnull):
            nose.run(argv=argv, plugins=[collector])
        return (collector.result.wasSuccessful(),
                [r[1] for r in collector.result.errors +
                               collector.result.failures])
Beispiel #8
0
    def _run(self):
        collector = ResultCollector()

        args = self.test_args
        if args:
            args = args.split()
        else:
            args = []
        with open(os.devnull, 'w') as devnull, redirect_stdout(devnull):
            pytest.main(args, plugins=[collector])

        return (all(not r.failed for r in collector.reports), [
            (repr(r), r.longreprtext) for r in collector.reports if r.failed
        ])
Beispiel #9
0
    def _run(self):
        argv = ['']
        argv += self.test_args.split()
        collector = Nose2ResultsCollector()

        with open(os.devnull, 'w') as devnull:
            with redirect_stdout(devnull):
                with redirect_stderr(devnull):
                    nose2.discover(argv=argv,
                                   extraHooks=[('testOutcome', collector)],
                                   exit=False)
        failures = [x for x in collector.events if x.outcome != 'passed']

        return (not failures, [(str(r.test),
                                traceback.format_exception(*r.exc_info))
                               for r in failures])
Beispiel #10
0
    def _run(self):
        collector = ResultCollector()

        args = self.test_args
        if args:
            args = args.split()
        else:
            args = []

        with StringIO() as stdout:
            with redirect_stdout(stdout):
                exit_code = pytest.main(args, plugins=[collector])

            # pytest exit codes:
            # 0: All tests were collected and passed successfully
            # 1: Tests were collected and run but some of the tests failed
            # 2: Test execution was interrupted by the user
            #    This includes:
            #    - early abortion when using -x
            #    - errors during collection (https://github.com/pytest-dev/pytest/issues/2950)
            # 3: Internal error happened while executing tests
            # 4: pytest command line usage error
            # 5: No tests were collected

            if exit_code == 0:
                return (True, ())
            if exit_code == 1:
                return (False, [(repr(r), r.longreprtext)
                                for r in collector.reports if r.failed])
            if exit_code == 2:
                return (False, [(repr(r), r.longreprtext)
                                for r in collector.reports if r.failed])
            stdout.seek(0)
            output = stdout.read()
            raise TestRunnerFailure('pytest exited non-zero', exit_code,
                                    output)