Esempio n. 1
0
    def test_pr_loading_failure(self):
        url = 'https://github.com/oppia/oppia/pull/1'

        def mock_parse_url(unused_url):
            return 'oppia', 'oppia', '1'

        def mock_lookup_pr(unused_owner, unused_repo, unused_number):
            return {}

        parse_url_swap = self.swap_with_checks(check_if_pr_is_low_risk,
                                               'parse_pr_url',
                                               mock_parse_url,
                                               expected_args=[
                                                   (url, ),
                                               ])
        lookup_pr_swap = self.swap_with_checks(check_if_pr_is_low_risk,
                                               'lookup_pr',
                                               mock_lookup_pr,
                                               expected_args=[
                                                   ('oppia', 'oppia', '1'),
                                               ])

        with parse_url_swap, lookup_pr_swap:
            with self.assertRaisesRegexp(
                    RuntimeError,
                    'Failed to load PR',
            ):
                check_if_pr_is_low_risk.main(tokens=[url])
Esempio n. 2
0
    def test_diff_loading_failure(self):
        url = 'https://github.com/oppia/oppia/pull/1'
        repo_url = 'https://github.com/oppia/oppia.git'
        pr = _make_pr('oppia/oppia',
                      'translatewiki-prs',
                      'develop',
                      base_url=repo_url)

        def mock_parse_url(unused_url):
            return 'oppia', 'oppia', '1'

        def mock_lookup_pr(unused_owner, unused_repo, unused_number):
            return pr

        def mock_run_cmd(unused_tokens):
            pass

        def mock_load_diff(unused_branch):
            return [], {}

        parse_url_swap = self.swap_with_checks(check_if_pr_is_low_risk,
                                               'parse_pr_url',
                                               mock_parse_url,
                                               expected_args=[
                                                   (url, ),
                                               ])
        lookup_pr_swap = self.swap_with_checks(check_if_pr_is_low_risk,
                                               'lookup_pr',
                                               mock_lookup_pr,
                                               expected_args=[
                                                   ('oppia', 'oppia', '1'),
                                               ])
        run_cmd_swap = self.swap_with_checks(
            common,
            'run_cmd',
            mock_run_cmd,
            expected_args=[
                (['git', 'remote', 'add', 'upstream', repo_url], ),
                (['git', 'fetch', 'upstream', 'develop'], ),
            ])
        load_diff_swap = self.swap_with_checks(check_if_pr_is_low_risk,
                                               'load_diff',
                                               mock_load_diff,
                                               expected_args=[
                                                   ('develop', ),
                                               ])

        with parse_url_swap, lookup_pr_swap, load_diff_swap:
            with run_cmd_swap:
                with self.assertRaisesRegexp(
                        RuntimeError,
                        'Failed to load PR diff',
                ):
                    check_if_pr_is_low_risk.main(tokens=[url])
    def test_url_parsing_failure(self):
        url = 'https://github.com/oppia/oppia/pull/1'

        def mock_parse_url(unused_url):
            return None

        parse_url_swap = self.swap_with_checks(
            check_if_pr_is_low_risk, 'parse_pr_url', mock_parse_url,
            expected_args=[
                (url,),
            ])

        with parse_url_swap:
            with self.assertRaisesRegexp(
                RuntimeError,
                'Failed to parse PR URL %s' % url,
            ):
                check_if_pr_is_low_risk.main(tokens=[url])
Esempio n. 4
0
    def test_risky_changelog(self):
        url = 'https://github.com/oppia/oppia/pull/1'
        repo_url = 'https://github.com/oppia/oppia.git'
        pr = _make_pr('oppia/oppia',
                      'translatewiki-prs',
                      'develop',
                      base_url=repo_url)
        diff_files = [('a', 'b')]
        file_diffs = {'a': ['- b']}

        def mock_parse_url(unused_url):
            return 'oppia', 'oppia', '1'

        def mock_lookup_pr(unused_owner, unused_repo, unused_number):
            return pr

        def mock_check_if_pr_is_translation_pr(pr_to_check,
                                               diff_files_to_check,
                                               file_diffs_to_check):
            if pr_to_check != pr:
                raise AssertionError(
                    'Provided PR to checker does not match expected PR.')
            if diff_files_to_check != diff_files:
                raise AssertionError('Incorrect diff_files passed to checker.')
            if file_diffs_to_check != file_diffs:
                raise AssertionError('Incorrect file_diffs passed to checker.')
            return 'Source branch does not indicate a translatewiki PR'

        def mock_check_if_pr_is_changelog_pr(pr_to_check, diff_files_to_check,
                                             file_diffs_to_check):
            if pr_to_check != pr:
                raise AssertionError(
                    'Provided PR to checker does not match expected PR.')
            if diff_files_to_check != diff_files:
                raise AssertionError('Incorrect diff_files passed to checker.')
            if file_diffs_to_check != file_diffs:
                raise AssertionError('Incorrect file_diffs passed to checker.')
            return 'Invalid change foo'

        def mock_run_cmd(unused_tokens):
            pass

        def mock_load_diff(unused_base_branch):
            return diff_files, file_diffs

        mock_low_risk_checkers = (
            ('translatewiki', mock_check_if_pr_is_translation_pr),
            ('changelog', mock_check_if_pr_is_changelog_pr),
        )

        parse_url_swap = self.swap_with_checks(check_if_pr_is_low_risk,
                                               'parse_pr_url',
                                               mock_parse_url,
                                               expected_args=[
                                                   (url, ),
                                               ])
        lookup_pr_swap = self.swap_with_checks(check_if_pr_is_low_risk,
                                               'lookup_pr',
                                               mock_lookup_pr,
                                               expected_args=[
                                                   ('oppia', 'oppia', '1'),
                                               ])
        low_risk_checkers_swap = self.swap(check_if_pr_is_low_risk,
                                           'LOW_RISK_CHECKERS',
                                           mock_low_risk_checkers)
        print_swap = self.swap_with_checks(
            python_utils,
            'PRINT',
            python_utils.PRINT,
            expected_args=[
                ('PR is not a low-risk PR of type translatewiki '
                 'because: Source branch does not indicate a '
                 'translatewiki PR', ),
                ('PR is not a low-risk PR of type changelog '
                 'because: Invalid change foo', ),
                ('PR is not low-risk. Running all CI checks.', ),
            ])
        run_cmd_swap = self.swap_with_checks(
            common,
            'run_cmd',
            mock_run_cmd,
            expected_args=[
                (['git', 'remote', 'add', 'upstream', repo_url], ),
                (['git', 'fetch', 'upstream', 'develop'], ),
            ])
        load_diff_swap = self.swap_with_checks(check_if_pr_is_low_risk,
                                               'load_diff',
                                               mock_load_diff,
                                               expected_args=[
                                                   ('develop', ),
                                               ])

        with parse_url_swap, lookup_pr_swap, print_swap, load_diff_swap:
            with low_risk_checkers_swap, run_cmd_swap:
                code = check_if_pr_is_low_risk.main(tokens=[url])
                self.assertEqual(code, 1)