def test_create_branch_with_patch(self): host = MockHost() host.filesystem = MockFileSystem() local_wpt = LocalWPT(host) local_wpt.fetch() local_branch_name = local_wpt.create_branch_with_patch( 'message', 'patch', 'author') self.assertEqual(local_branch_name, 'chromium-export-try') self.assertEqual( host.executive.calls, [[ 'git', 'clone', 'https://chromium.googlesource.com/external/w3c/web-platform-tests.git', '/tmp/wpt' ], ['git', 'remote'], [ 'git', 'remote', 'add', 'github', '[email protected]:w3c/web-platform-tests.git' ], ['git', 'reset', '--hard', 'HEAD'], ['git', 'clean', '-fdx'], ['git', 'checkout', 'origin/master'], ['git', 'branch', '-a'], ['git', 'branch', '-a'], ['git', 'checkout', '-b', 'chromium-export-try'], ['git', 'apply', '-'], ['git', 'commit', '--author', 'author', '-am', 'message'], ['git', 'push', 'github', 'chromium-export-try']])
def test_apply_exportable_commits_locally(self): host = MockHost() importer = TestImporter(host, wpt_github=MockWPTGitHub(pull_requests=[])) fake_commit = MockChromiumCommit( host, subject='My fake commit', patch=( 'Fake patch contents...\n' '--- a/third_party/WebKit/LayoutTests/external/wpt/css/css-ui-3/outline-004.html\n' '+++ b/third_party/WebKit/LayoutTests/external/wpt/css/css-ui-3/outline-004.html\n' '@@ -20,7 +20,7 @@\n' '...')) importer.exportable_but_not_exported_commits = lambda _: [fake_commit] applied = importer.apply_exportable_commits_locally(LocalWPT(host)) self.assertEqual(applied, [fake_commit]) self.assertEqual(host.executive.full_calls, [ MockCall( ['git', 'apply', '-'], { 'input': ( 'Fake patch contents...\n' '--- a/css/css-ui-3/outline-004.html\n' '+++ b/css/css-ui-3/outline-004.html\n' '@@ -20,7 +20,7 @@\n' '...'), 'cwd': '/tmp/wpt', 'env': None }), MockCall( ['git', 'add', '.'], kwargs={'input': None, 'cwd': '/tmp/wpt', 'env': None}), MockCall( ['git', 'commit', '--all', '-F', '-'], kwargs={'cwd': '/tmp/wpt', 'env': None}) ])
def test_create_branch_with_patch(self): host = MockHost() host.filesystem = MockFileSystem() local_wpt = LocalWPT(host, 'token') local_wpt.fetch() local_wpt.create_branch_with_patch('chromium-export-decafbad', 'message', 'patch', 'author <*****@*****.**>') self.assertEqual( host.executive.calls, [[ 'git', 'clone', 'https://[email protected]/w3c/web-platform-tests.git', '/tmp/wpt' ], ['git', 'reset', '--hard', 'HEAD'], ['git', 'clean', '-fdx'], ['git', 'checkout', 'origin/master'], ['git', 'branch', '-D', 'chromium-export-decafbad'], ['git', 'checkout', '-b', 'chromium-export-decafbad'], ['git', 'apply', '-'], ['git', 'add', '.'], [ 'git', 'commit', '--author', 'author <*****@*****.**>', '-am', 'message' ], ['git', 'push', 'origin', 'chromium-export-decafbad']])
def main(self, argv=None): """Creates PRs for in-flight CLs and merges changes that land on master. Returns: A boolean: True if success, False if there were any patch failures. """ args = self.parse_args(argv) self.dry_run = args.dry_run configure_logging(logging_level=logging.INFO, include_time=True) credentials = read_credentials(self.host, args.credentials_json) if not (credentials['GH_USER'] and credentials['GH_TOKEN']): _log.error('Must provide both user and token for GitHub.') return False self.wpt_github = self.wpt_github or WPTGitHub(self.host, credentials['GH_USER'], credentials['GH_TOKEN']) self.gerrit = self.gerrit or GerritAPI(self.host, credentials['GERRIT_USER'], credentials['GERRIT_TOKEN']) self.local_wpt = self.local_wpt or LocalWPT(self.host, credentials['GH_TOKEN']) self.local_wpt.fetch() open_gerrit_cls = self.gerrit.query_exportable_open_cls() self.process_gerrit_cls(open_gerrit_cls) exportable_commits, errors = self.get_exportable_commits() for error in errors: _log.warn(error) self.process_chromium_commits(exportable_commits) return not bool(errors)
def test_no_fetch_flag(self): host = MockHost() host.filesystem = MockFileSystem(files={'/tmp/wpt': ''}) LocalWPT(host, no_fetch=True) self.assertEqual(len(host.executive.calls), 0)
def test_seek_change_id(self): host = MockHost() local_wpt = LocalWPT(host, 'token') local_wpt.seek_change_id('Ifake-change-id') self.assertEqual( host.executive.calls, [['git', 'log', '-1', '--grep', '^Change-Id: Ifake-change-id']])
def test_fetch_when_wpt_dir_does_not_exist(self): host = MockHost() local_wpt = LocalWPT(host, 'token') local_wpt.fetch() self.assertEqual(host.executive.calls, [ ['git', 'clone', 'https://[email protected]/w3c/web-platform-tests.git', '/tmp/wpt'], ])
def test_create_branch_with_patch(self): host = MockHost() host.filesystem = MockFileSystem() local_wpt = LocalWPT(host) local_wpt.create_branch_with_patch('branch-name', 'message', 'patch') self.assertEqual(len(host.executive.calls), 9)
def test_clones_if_wpt_does_not_exist(self): host = MockHost() host.filesystem = MockFileSystem() LocalWPT(host) self.assertEqual(len(host.executive.calls), 1) self.assertEqual(host.executive.calls[0][1], 'clone')
def test_last_wpt_exported_commit_not_found(self): host = MockHost() host.executive = MockExecutive(run_command_fn=lambda _: '') host.filesystem = MockFileSystem() local_wpt = LocalWPT(host, 'token') commit = local_wpt.most_recent_chromium_commit() self.assertEqual(commit, (None, None))
def main(self, argv=None): """Creates PRs for in-flight CLs and merges changes that land on master. Returns: A boolean: True if success, False if there were any patch failures. """ options = self.parse_args(argv) self.dry_run = options.dry_run log_level = logging.DEBUG if options.verbose else logging.INFO configure_logging(logging_level=log_level, include_time=True) if options.verbose: # Print out the full output when executive.run_command fails. self.host.executive.error_output_limit = None credentials = read_credentials(self.host, options.credentials_json) if not (credentials.get('GH_USER') and credentials.get('GH_TOKEN')): _log.error('You must provide your GitHub credentials for this ' 'script to work.') _log.error('See https://chromium.googlesource.com/chromium/src' '/+/master/docs/testing/web_platform_tests.md' '#GitHub-credentials for instructions on how to set ' 'your credentials up.') return False self.wpt_github = self.wpt_github or WPTGitHub( self.host, credentials['GH_USER'], credentials['GH_TOKEN']) self.gerrit = self.gerrit or GerritAPI( self.host, credentials['GERRIT_USER'], credentials['GERRIT_TOKEN']) self.local_wpt = self.local_wpt or LocalWPT(self.host, credentials['GH_TOKEN']) self.local_wpt.fetch() _log.info('Searching for exportable in-flight CLs.') # The Gerrit search API is slow and easy to fail, so we wrap it in a try # statement to continue exporting landed commits when it fails. try: open_gerrit_cls = self.gerrit.query_exportable_open_cls() except GerritError as e: _log.info( 'In-flight CLs cannot be exported due to the following error:') _log.error(str(e)) gerrit_error = True else: self.process_gerrit_cls(open_gerrit_cls) gerrit_error = False _log.info('Searching for exportable Chromium commits.') exportable_commits, git_errors = self.get_exportable_commits() self.process_chromium_commits(exportable_commits) if git_errors: _log.info( 'Attention: The following errors have prevented some commits from being ' 'exported:') for error in git_errors: _log.error(error) return not (gerrit_error or git_errors)
def __init__(self, host, gh_user, gh_token, gerrit_user, gerrit_token, dry_run=False): self.host = host self.wpt_github = WPTGitHub(host, gh_user, gh_token, pr_history_window=PR_HISTORY_WINDOW) self.gerrit = GerritAPI(self.host, gerrit_user, gerrit_token) self.dry_run = dry_run self.local_wpt = LocalWPT(self.host, gh_token) self.local_wpt.fetch()
def test_seek_commit_position(self): host = MockHost() local_wpt = LocalWPT(host, 'token') local_wpt.seek_commit_position('refs/heads/master@{12345}') self.assertEqual(host.executive.calls, [[ 'git', 'log', '-1', '--grep', '^Cr-Commit-Position: refs/heads/master@{12345}' ]])
def test_run(self): host = MockHost() host.filesystem = MockFileSystem() local_wpt = LocalWPT(host) local_wpt.run(['echo', 'rutabaga']) self.assertEqual(len(host.executive.calls), 2) self.assertEqual(host.executive.calls[1], ['echo', 'rutabaga'])
def test_fetches_if_wpt_exists(self): host = MockHost() host.filesystem = MockFileSystem(files={'/tmp/wpt': ''}) LocalWPT(host) self.assertEqual(len(host.executive.calls), 2) self.assertEqual(host.executive.calls[0][1], 'fetch') self.assertEqual(host.executive.calls[1][1], 'checkout')
def test_fetch_when_wpt_dir_does_not_exist(self): host = MockHost() host.filesystem = MockFileSystem() local_wpt = LocalWPT(host) local_wpt.fetch() self.assertEqual(len(host.executive.calls), 3) self.assertEqual(host.executive.calls[0][1], 'clone')
def test_apply_exportable_commits_locally_returns_none_on_failure(self): host = MockHost() wpt_github = MockWPTGitHub(pull_requests=[]) importer = TestImporter(host, wpt_github=wpt_github) commit = MockChromiumCommit(host, subject='My fake commit') importer.exportable_but_not_exported_commits = lambda _: [commit] local_wpt = LocalWPT(host) local_wpt.apply_patch = lambda _: 'Failed' # Failure to apply patch. applied = importer.apply_exportable_commits_locally(local_wpt) self.assertIsNone(applied)
def test_fetch_when_wpt_dir_exists(self): host = MockHost() host.filesystem = MockFileSystem(files={'/tmp/wpt': ''}) local_wpt = LocalWPT(host, 'token') local_wpt.fetch() self.assertEqual(host.executive.calls, [ ['git', 'fetch', 'origin'], ['git', 'checkout', 'origin/master'], ])
def test_test_patch_error(self): def _run_fn(args): if args[0] == 'git' and args[1] == 'apply': raise ScriptError('MOCK failed applying patch') return '' host = MockHost() host.executive = MockExecutive(run_command_fn=_run_fn) local_wpt = LocalWPT(host, 'token') self.assertEqual(local_wpt.test_patch('dummy patch'), (False, 'MOCK failed applying patch'))
def test_test_patch_empty_diff(self): host = MockHost() host.executive = mock_git_commands({ 'apply': '', 'add': '', 'diff': '', 'reset': '', 'clean': '', 'checkout': '', }, strict=True) local_wpt = LocalWPT(host, 'token') self.assertEqual(local_wpt.test_patch('dummy patch'), (False, ''))
def test_is_commit_affecting_directory(self): host = MockHost() # return_exit_code=True is passed to run() in the method under test, # so the mock return value should be exit code instead of output. host.executive = mock_git_commands({'diff-tree': 1}, strict=True) local_wpt = LocalWPT(host, 'token') self.assertTrue(local_wpt.is_commit_affecting_directory( 'HEAD', 'css/')) self.assertEqual(host.executive.calls, [[ 'git', 'diff-tree', '--quiet', '--no-commit-id', 'HEAD', '--', 'css/' ]])
def exportable_but_not_exported_commits(self, wpt_path): """Checks for commits that might be overwritten by importing. Args: wpt_path: The path to a local checkout of web-platform-tests. Returns: A list of commits in the Chromium repo that are exportable but not yet exported to the web-platform-tests repo. """ local_wpt = LocalWPT(self.host, path=wpt_path) assert self.host.filesystem.exists(wpt_path) _, chromium_commit = local_wpt.most_recent_chromium_commit() return exportable_commits_since(chromium_commit.sha, self.host, local_wpt)
def test_last_wpt_exported_commit(self): host = MockHost() host.executive = mock_git_commands({ 'rev-list': '9ea4fc353a4b1c11c6e524270b11baa4d1ddfde8', 'footers': 'Cr-Commit-Position: 123', 'crrev-parse': 'add087a97844f4b9e307d9a216940582d96db306', }, strict=True) host.filesystem = MockFileSystem() local_wpt = LocalWPT(host, 'token') wpt_sha, chromium_commit = local_wpt.most_recent_chromium_commit() self.assertEqual(wpt_sha, '9ea4fc353a4b1c11c6e524270b11baa4d1ddfde8') self.assertEqual(chromium_commit.position, '123') self.assertEqual(chromium_commit.sha, 'add087a97844f4b9e307d9a216940582d96db306')
def test_last_wpt_exported_commit(self): host = MockHost() return_vals = [ 'deadbeefcafe', '123', '9ea4fc353a4b1c11c6e524270b11baa4d1ddfde8', ] host.executive = MockExecutive(run_command_fn=lambda _: return_vals.pop()) host.filesystem = MockFileSystem() local_wpt = LocalWPT(host) wpt_sha, chromium_commit = local_wpt.most_recent_chromium_commit() self.assertEqual(wpt_sha, '9ea4fc353a4b1c11c6e524270b11baa4d1ddfde8') self.assertEqual(chromium_commit.position, '123') self.assertEqual(chromium_commit.sha, 'deadbeefcafe')
def test_fetch_when_wpt_dir_exists(self): host = MockHost() host.filesystem = MockFileSystem(files={'/tmp/wpt': ''}) local_wpt = LocalWPT(host) local_wpt.fetch() self.assertEqual( host.executive.calls, [['git', 'fetch', '--all'], ['git', 'checkout', 'origin/master'], ['git', 'remote'], [ 'git', 'remote', 'add', 'github', '[email protected]:w3c/web-platform-tests.git' ]])
def test_commits_in_range(self): host = MockHost() host.executive = mock_git_commands( { 'rev-list': '34ab6c3f5aee8bf05207b674edbcb6affb179545 Fix presubmit errors\n' '8c596b820634a623dfd7a2b0f36007ce2f7a0c9f test\n' }, strict=True) local_wpt = LocalWPT(host, 'token') self.assertTrue(local_wpt.commits_in_range('HEAD~2', 'HEAD')) self.assertEqual( host.executive.calls, [['git', 'rev-list', '--pretty=oneline', 'HEAD~2..HEAD']])
def __init__(self, host, gh_user, gh_token, gerrit_user, gerrit_token, dry_run=False): self.host = host self.wpt_github = WPTGitHub(host, gh_user, gh_token) self.gerrit = GerritAPI(self.host, gerrit_user, gerrit_token) self.dry_run = dry_run self.local_wpt = LocalWPT(self.host, gh_token) self.local_wpt.fetch()
def main(self, argv=None): """Creates PRs for in-flight CLs and merges changes that land on master. Returns: A boolean: True if success, False if there were any patch failures. """ options = self.parse_args(argv) self.dry_run = options.dry_run log_level = logging.DEBUG if options.verbose else logging.INFO configure_logging(logging_level=log_level, include_time=True) if options.verbose: # Print out the full output when executive.run_command fails. self.host.executive.error_output_limit = None credentials = read_credentials(self.host, options.credentials_json) if not (credentials['GH_USER'] and credentials['GH_TOKEN']): _log.error('Must provide both user and token for GitHub.') return False self.wpt_github = self.wpt_github or WPTGitHub( self.host, credentials['GH_USER'], credentials['GH_TOKEN']) self.gerrit = self.gerrit or GerritAPI( self.host, credentials['GERRIT_USER'], credentials['GERRIT_TOKEN']) self.local_wpt = self.local_wpt or LocalWPT(self.host, credentials['GH_TOKEN']) self.local_wpt.fetch() # The Gerrit search API is slow and easy to fail, so we wrap it in a try # statement to continue exporting landed commits when it fails. try: open_gerrit_cls = self.gerrit.query_exportable_open_cls() except GerritError as e: _log.error(str(e)) gerrit_error = True else: self.process_gerrit_cls(open_gerrit_cls) gerrit_error = False exportable_commits, git_errors = self.get_exportable_commits() for error in git_errors: _log.error(error) self.process_chromium_commits(exportable_commits) return not (gerrit_error or git_errors)
def test_apply_exportable_commits_locally(self): # TODO(robertma): Consider using MockLocalWPT. host = MockHost() importer = TestImporter(host, wpt_github=MockWPTGitHub(pull_requests=[])) importer.wpt_git = MockGit(cwd='/tmp/wpt', executive=host.executive) fake_commit = MockChromiumCommit( host, subject='My fake commit', patch= ('Fake patch contents...\n' '--- a/third_party/WebKit/LayoutTests/external/wpt/css/css-ui-3/outline-004.html\n' '+++ b/third_party/WebKit/LayoutTests/external/wpt/css/css-ui-3/outline-004.html\n' '@@ -20,7 +20,7 @@\n' '...')) importer.exportable_but_not_exported_commits = lambda _: [fake_commit] applied = importer.apply_exportable_commits_locally(LocalWPT(host)) self.assertEqual(applied, [fake_commit]) # This assertion is implementation details of LocalWPT.apply_patch. # TODO(robertma): Move this to local_wpt_unittest.py. self.assertEqual(host.executive.full_calls, [ MockCall( ['git', 'apply', '-'], { 'input': ('Fake patch contents...\n' '--- a/css/css-ui-3/outline-004.html\n' '+++ b/css/css-ui-3/outline-004.html\n' '@@ -20,7 +20,7 @@\n' '...'), 'cwd': '/tmp/wpt', 'env': None }), MockCall(['git', 'add', '.'], kwargs={ 'input': None, 'cwd': '/tmp/wpt', 'env': None }) ]) self.assertEqual( importer.wpt_git.local_commits(), [['Applying patch 14fd77e88e42147c57935c49d9e3b2412b8491b7']])
def main(): configure_logging() options = parse_args() host = Host() github = GitHub(host) local_wpt = LocalWPT(host, no_fetch=options.no_fetch, use_github=True) chromium_wpt = ChromiumWPT(host) wpt_commit, chromium_commit = local_wpt.most_recent_chromium_commit() assert chromium_commit, 'No Chromium commit found, this is impossible' _log.info('web-platform-tests@%s (%d behind origin/master)', wpt_commit, local_wpt.commits_behind_master(wpt_commit)) _log.info('chromium@%s (%d behind origin/master)', chromium_commit.sha, chromium_commit.num_behind_master()) exportable_commits = chromium_wpt.exportable_commits_since( chromium_commit.sha) if exportable_commits: _log.info('Found %s exportable commits in chromium:', len(exportable_commits)) for commit in exportable_commits: _log.info('- %s %s', commit, chromium_wpt.subject(commit)) else: _log.info('No exportable commits found in Chromium, stopping.') return for commit in exportable_commits: _log.info('Uploading %s', chromium_wpt.subject(commit)) chromium_commit = ChromiumCommit(host, sha=commit) patch = chromium_wpt.format_patch(commit) message = chromium_wpt.message(commit) local_wpt.create_branch_with_patch(branch_name, message, patch) github.create_pr(local_branch_name='chromium-try-{}'.format(commit), desc_title=chromium_commit.subject(), body=chromium_commit.body())