Esempio n. 1
0
def run_tests(tests):
    import eventhandler

    failed = 0
    for test in tests:
        eventhandler.reset_test_state()

        try:
            payload = get_payload(test['filename'])['payload']
            initial = test['initial']
            api = TestAPIProvider(payload, 'highfive',
                                  initial['new_contributor'],
                                  initial['labels'], initial['assignee'],
                                  initial['diff'])
            handle_payload(api, payload)
            expected = test['expected']
            if 'comments' in expected:
                assert len(api.comments_posted
                           ) == expected['comments'], "%d == %d" % (len(
                               api.comments_posted), expected['comments'])
            if 'labels' in expected:
                assert api.labels == expected['labels'], "%s == %s" % (
                    api.labels, expected['labels'])
            if 'assignee' in expected:
                assert api.assignee == expected['assignee'], "%s == %s" % (
                    api.assignee, expected['assignee'])
        except AssertionError, error:
            _, _, tb = sys.exc_info()
            traceback.print_tb(tb)  # Fixed format
            tb_info = traceback.extract_tb(tb)
            filename, line, func, text = tb_info[-1]
            print('{}: An error occurred on line {} in statement {}'.format(
                test['filename'], line, text))
            print(error)
            failed += 1
Esempio n. 2
0
def run_tests(tests):
    failed = 0
    for test in tests:
        try:
            payload = get_payload(test['filename'])
            initial = test['initial']
            api = TestAPIProvider(payload, 'highfive',
                                  initial['new_contributor'],
                                  initial['labels'], initial['assignee'],
                                  initial['diff'])
            handle_payload(api, payload)
            expected = test['expected']
            assert len(api.comments_posted) == expected['comments']
            assert api.labels == expected['labels']
            assert api.assignee == expected['assignee']
        except AssertionError:
            _, _, tb = sys.exc_info()
            traceback.print_tb(tb)  # Fixed format
            tb_info = traceback.extract_tb(tb)
            filename, line, func, text = tb_info[-1]
            print('{}: An error occurred on line {} in statement {}'.format(
                test['filename'], line, text))
            failed += 1

    possible_tests = [f for f in os.listdir('.') if f.endswith('.json')]
    test_files = set([test['filename'] for test in tests])
    if len(possible_tests) != len(test_files):
        print 'Found unused JSON test data: %s' % ', '.join(
            filter(lambda x: x not in test_files, possible_tests))
        sys.exit(1)
    print 'Ran %d tests, %d failed' % (len(tests), failed)

    if failed:
        sys.exit(1)
Esempio n. 3
0
File: test.py Progetto: zwn/highfive
def run_tests(tests):
    import eventhandler

    failed = 0
    for test in tests:
        eventhandler.reset_test_state()

        try:
            payload = get_payload(test['filename'])['payload']
            initial = test['initial']
            api = TestAPIProvider(payload, 'highfive', initial['new_contributor'], initial['labels'],
                                  initial['assignee'], initial['diff'], initial['pull_request'])
            handle_payload(api, payload)
            expected = test['expected']
            if 'comments' in expected:
                assert len(api.comments_posted) == expected['comments'], "%d == %d" % (len(api.comments_posted), expected['comments'])
            if 'labels' in expected:
                assert api.labels == expected['labels'], "%s == %s" % (api.labels, expected['labels'])
            if 'assignee' in expected:
                assert api.assignee == expected['assignee'], "%s == %s" % (api.assignee, expected['assignee'])
        except AssertionError, error:
            _, _, tb = sys.exc_info()
            traceback.print_tb(tb) # Fixed format
            tb_info = traceback.extract_tb(tb)
            filename, line, func, text = tb_info[-1]
            print('{}: An error occurred on line {} in statement {}'.format(test['filename'], line, text))
            print(error)
            failed += 1
Esempio n. 4
0
def run_tests(tests, warn=True, overwrite=False):
    failed, dirty = 0, 0
    for handler, test in tests:
        eventhandler.reset_test_state()

        try:
            initial, expected, = test['initial'], test['expected']
            wrapper = test['wrapper']
            payload = wrapper.json['payload']
            api = TestAPIProvider(payload,
                                  'highfive',
                                  initial['new_contributor'],
                                  initial['labels'],
                                  initial['assignee'],
                                  initial['diff'],
                                  initial['pull_request'])
            handle_payload(api, payload, [handler])

            if 'comments' in expected:
                assert len(api.comments_posted) == expected['comments'], \
                    "%d == %d" % (len(api.comments_posted),
                                  expected['comments'])
            if 'labels' in expected:
                assert api.labels == expected['labels'], \
                    "%s == %s" % (api.labels, expected['labels'])
            if 'assignee' in expected:
                assert api.assignee == expected['assignee'], \
                    "%s == %s" % (api.assignee, expected['assignee'])

            # If this is the last test in the file, then it's time for cleanup
            if test['clean']:
                cleaned = wrapper.clean(warn)

                if wrapper.unused and not overwrite:
                    error = '\033[91m%s\033[0m: The file has %s unused nodes'
                    print(error % (test['filename'], wrapper.unused))
                    dirty += 1

                if overwrite:   # useful for cleaning up the tests locally
                    clean_dict = test['dict']
                    clean_dict['payload'] = cleaned['payload']
                    with open(test['filename'], 'w') as fd:
                        json.dump(clean_dict, fd, indent=2)
                    error = '\033[91m%s\033[0m: Rewrote the JSON file'
                    print(error % test['filename'])

        except AssertionError as error:
            _, _, tb = sys.exc_info()
            traceback.print_tb(tb)      # Fixed format
            tb_info = traceback.extract_tb(tb)
            filename, line, func, text = tb_info[-1]
            error_template = '\033[91m{}\033[0m: An error occurred on ' + \
                             'line {} in statement {}'
            print(error_template.format(test['filename'], line, text))
            print(error)
            failed += 1

    return failed, dirty
Esempio n. 5
0
    def webhook():
        try:
            raw_data = flask.request.get_data()

            # Load all the headers
            try:
                event = str(flask.request.headers['X-GitHub-Event'])
                delivery = str(flask.request.headers['X-GitHub-Delivery'])
                signature = str(flask.request.headers['X-Hub-Signature'])
            except KeyError:
                return 'Error: some required webhook headers are missing\n', 400

            if 'payload' in flask.request.form:
                expected = hmac.new(webhook_secret.encode('utf8'), digestmod=hashlib.sha1)
                expected.update(raw_data)
                expected = expected.hexdigest()
                if not hmac.compare_digest('sha1=' + expected, signature):
                    return 'Error: invalid signature\n', 403

            try:
                payload = json.loads(flask.request.form['payload'], strict=False)
            except (KeyError, ValueError):
                return 'Error: missing or invalid payload\n', 400

            try:
                api_provider = GithubAPIProvider(payload, user, token)
                api_provider.extract_globals(payload)
                handle_payload(api_provider, payload, [WelcomeUserHandler(), ClientIssuesHandler()])
                return 'OK\n', 200
            except Exception as e:
                app.logger.error('An exception occurred while processing a web hook. Delivery id: {}, event name: {}'
                                 .format(delivery, event))
                app.log_exception(e)
                return 'Internal server error\n', 500
        except Exception as e:
            app.log_exception(e)
            return 'Something went wrong\n', 400