def test_checkout_remote(self): with mocks.remote.Svn(), OutputCapture(): self.assertEqual(1, program.main( args=('-C', 'https://svn.webkit.org/repository/webkit', 'checkout', '3@trunk'), path=self.path, ))
def filter_branch(self, range, identifier_template=None, environment_shell=None): # We can't effectively mock the bash script in the command, but we can mock the python code that # script calls, which is where the program logic is. head, start = range.split('...') head = self.find(head) start = self.find(start) commits_to_edit = [] for commit in reversed(self.commits[head.branch]): if commit.branch == start.branch and commit.identifier <= start.identifier: break commits_to_edit.insert(0, commit) if head.branch != self.default_branch: for commit in reversed( self.commits[self.default_branch][:head.branch_point]): if commit.identifier <= start.identifier: break commits_to_edit.insert(0, commit) stdout = StringIO() original_env = { key: os.environ.get('OLDPWD') for key in [ 'OLDPWD', 'GIT_COMMIT', 'GIT_AUTHOR_NAME', 'GIT_AUTHOR_EMAIL', 'GIT_COMMITTER_NAME', 'GIT_COMMITTER_EMAIL', ] } try: count = 0 os.environ['OLDPWD'] = self.path for commit in commits_to_edit: count += 1 os.environ['GIT_COMMIT'] = commit.hash os.environ['GIT_AUTHOR_NAME'] = commit.author.name os.environ['GIT_AUTHOR_EMAIL'] = commit.author.email os.environ['GIT_COMMITTER_NAME'] = commit.author.name os.environ['GIT_COMMITTER_EMAIL'] = commit.author.email stdout.write( 'Rewrite {hash} ({count}/{total}) (--- seconds passed, remaining --- predicted)\n' .format( hash=commit.hash, count=count, total=len(commits_to_edit), )) if identifier_template: messagefile = StringIO() messagefile.write(commit.message) messagefile.seek(0) with OutputCapture() as captured: message_main(messagefile, identifier_template) lines = captured.stdout.getvalue().splitlines() if lines[-1].startswith('git-svn-id: https://svn'): lines.pop(-1) commit.message = '\n'.join(lines) if not environment_shell: continue if re.search(r'echo "Overwriting', environment_shell): stdout.write('Overwriting {}\n'.format(commit.hash)) match = re.search(r'(?P<json>\S+\.json)', environment_shell) if match: with OutputCapture() as captured: committer_main(match.group('json')) captured.stdout.seek(0) for line in captured.stdout.readlines(): line = line.rstrip() os.environ[line.split(' ')[0]] = ' '.join( line.split(' ')[1:]) commit.author = Contributor( name=os.environ['GIT_AUTHOR_NAME'], emails=[os.environ['GIT_AUTHOR_EMAIL']]) if re.search(r'echo "\s+', environment_shell): for key in [ 'GIT_AUTHOR_NAME', 'GIT_AUTHOR_EMAIL', 'GIT_COMMITTER_NAME', 'GIT_COMMITTER_EMAIL' ]: stdout.write(' {}={}\n'.format( key, os.environ[key])) finally: for key, value in original_env.items(): if value is not None: os.environ[key] = value else: del os.environ[key] return mocks.ProcessCompletion( returncode=0, stdout=stdout.getvalue(), )
def test_invalid_shutdown(self): with OutputCapture(): with self.assertRaises(TaskPool.Exception): with TaskPool(workers=1, teardown=teardown, grace_period=1) as pool: pool.do(wait, 2)
def test_upload_json(self): runner, port = self.create_runner() port.host.filesystem.files['/mock-checkout/some.json'] = 'some content' class MockFileUploader: called = [] upload_single_text_file_throws = False upload_single_text_file_return_value = None @classmethod def reset(cls): cls.called = [] cls.upload_single_text_file_throws = False cls.upload_single_text_file_return_value = None def __init__(mock, url, timeout): self.assertEqual(url, 'https://some.host/some/path') self.assertTrue(isinstance(timeout, int) and timeout) mock.called.append('FileUploader') def upload_single_text_file(mock, filesystem, content_type, filename): self.assertEqual(filesystem, port.host.filesystem) self.assertEqual(content_type, 'application/json') self.assertEqual(filename, 'some.json') mock.called.append('upload_single_text_file') if mock.upload_single_text_file_throws: raise Exception return mock.upload_single_text_file_return_value MockFileUploader.upload_single_text_file_return_value = StringIO('OK') self.assertTrue( runner._upload_json('https://some.host', 'some.json', '/some/path', MockFileUploader)) self.assertEqual(MockFileUploader.called, ['FileUploader', 'upload_single_text_file']) MockFileUploader.reset() MockFileUploader.upload_single_text_file_return_value = StringIO('OK') self.assertTrue( runner._upload_json('some.host', 'some.json', '/some/path', MockFileUploader)) MockFileUploader.reset() MockFileUploader.upload_single_text_file_return_value = StringIO( 'Some error') with OutputCapture(level=logging.INFO) as captured: self.assertFalse( runner._upload_json('https://some.host', 'some.json', '/some/path', MockFileUploader)) self.assertEqual( captured.root.log.getvalue(), 'Uploaded JSON to https://some.host/some/path but got a bad response:\nSome error\n', ) # Throwing an exception upload_single_text_file shouldn't blow up _upload_json MockFileUploader.reset() MockFileUploader.upload_single_text_file_throws = True self.assertFalse( runner._upload_json('https://some.host', 'some.json', '/some/path', MockFileUploader)) self.assertEqual(MockFileUploader.called, ['FileUploader', 'upload_single_text_file']) MockFileUploader.reset() MockFileUploader.upload_single_text_file_return_value = StringIO( '{"status": "OK"}') self.assertTrue( runner._upload_json('https://some.host', 'some.json', '/some/path', MockFileUploader)) self.assertEqual(MockFileUploader.called, ['FileUploader', 'upload_single_text_file']) MockFileUploader.reset() MockFileUploader.upload_single_text_file_return_value = StringIO( '{"status": "SomethingHasFailed", "failureStored": false}') with OutputCapture(level=logging.INFO) as captured: self.assertFalse( runner._upload_json('https://some.host', 'some.json', '/some/path', MockFileUploader)) serialized_json = json.dumps( { 'status': 'SomethingHasFailed', 'failureStored': False }, indent=4) self.assertEqual( captured.root.log.getvalue(), 'Uploaded JSON to https://some.host/some/path but got an error:\n{}\n' .format(serialized_json), )
def test_tests_to_update(self): build = Mock() with OutputCapture(): self.command._tests_to_update(build)
def test_no_timeout(self): with mocks.Time, OutputCapture() as capturer: with Timeout(2): time.sleep(1) self.assertEqual(capturer.webkitcorepy.log.getvalue(), '')
def test_detection(self): with OutputCapture(), mocks.local.Git(self.path), mocks.local.Svn(): detect = local.Scm.from_path(self.path) self.assertEqual(detect.executable, local.Git.executable)
def test_helper_fails_to_start(self): host = MockSystemHost(MockExecutive()) port = self.make_port(host) with OutputCapture(): port.start_helper() port.stop_helper()
def test_convert_prefixed_properties(self): """ Tests convert_prefixed_properties() file that has 20 properties requiring the -webkit- prefix: 10 in one style block + 5 in another style block + 5 inline styles, including one with multiple prefixed properties. The properties in the test content are in all sorts of wack formatting. """ test_html = """<html> <style type="text/css"><![CDATA[ .block1 { width: 300px; height: 300px } .block2 { @test0@: @propvalue0@; } .block3{@test1@: @propvalue1@;} .block4 { @test2@:@propvalue2@; } .block5{ @test3@ :@propvalue3@; } #block6 { @test4@ : @propvalue4@ ; } #block7 { @test5@: @propvalue5@; } #block8 { @test6@: @propvalue6@ } #block9:pseudo { @test7@: @propvalue7@; @test8@: propvalue propvalue propvalue;; propname: @propvalue8@; } ]]></style> </head> <body> <div id="elem1" style="@test9@: @propvalue9@;"></div> <div id="elem2" style="propname: propvalue; @test10@ : @propvalue10@; propname:propvalue;"></div> <div id="elem2" style="@test11@: @propvalue11@; @test12@ : @propvalue12@; @test13@ : @propvalue13@ ;"></div> <div id="elem3" style="@test14@:@propvalue14@"></div> </body> <style type="text/css"><![CDATA[ .block10{ @test15@: @propvalue15@; } .block11{ @test16@: @propvalue16@; } .block12{ @test17@: @propvalue17@; } #block13:pseudo { @test18@: @propvalue18@; @test19@: @propvalue19@; } ]]></style> </html> """ converter = _W3CTestConverter(DUMMY_PATH, DUMMY_FILENAME, None) test_content = self.generate_test_content_properties_and_values(converter.prefixed_properties, converter.prefixed_property_values, 20, test_html) with OutputCapture(): converter.feed(test_content[2]) converter.close() converted = converter.output() self.verify_conversion_happened(converted) self.verify_prefixed_properties(converted, test_content[0]) self.verify_prefixed_property_values(converted, test_content[1])
def test_guess_reviewer_from_multipatch_bug(self): step = UpdateChangeLogsWithReviewer(MockTool(), MockOptions()) with OutputCapture(level=logging.INFO) as captured: step._guess_reviewer_from_bug(50000) self.assertEqual(captured.root.log.getvalue(), 'Guessing "Reviewer2" as reviewer from attachment 10001 on bug 50000.\n')
def test_run_timeout_context(self): with OutputCapture(), self.assertRaises(TimeoutExpired): with Timeout(1): run([sys.executable, '-c', 'import time;time.sleep(2)'])
def test_guess_reviewer_from_bug(self): step = UpdateChangeLogsWithReviewer(MockTool(), MockOptions()) with OutputCapture(level=logging.INFO) as captured: step._guess_reviewer_from_bug(50001) self.assertEqual(captured.root.log.getvalue(), 'No reviewed patches on bug 50001, cannot infer reviewer.\n')
def test_default_configuration__unknown(self): # Ignore the warning about an unknown configuration value. with OutputCapture(): self.assert_configuration('Unknown', 'Unknown')
def _assert_tool_main_outputs(self, tool, main_args, expected_stdout, expected_stderr="", expected_exit_code=0): with OutputCapture() as captured: exit_code = tool.main(main_args) self.assertEqual(captured.stdout.getvalue(), expected_stdout) self.assertEqual(captured.stderr.getvalue(), expected_stderr) self.assertEqual(exit_code, expected_exit_code)
def test_parse_output_with_subtests(self): output = DriverOutput(""" Description: this is a test description. some test:Time -> [1, 2, 3, 4, 5] ms some other test = else:Time -> [6, 7, 8, 9, 10] ms some other test = else:Malloc -> [11, 12, 13, 14, 15] bytes Array Construction, []:Time -> [11, 12, 13, 14, 15] ms Concat String:Time -> [15163, 15304, 15386, 15608, 15622] ms jQuery - addClass:Time -> [2785, 2815, 2826, 2841, 2861] ms Dojo - div:only-child:Time -> [7825, 7910, 7950, 7958, 7970] ms Dojo - div:nth-child(2n+1):Time -> [3620, 3623, 3633, 3641, 3658] ms Dojo - div > div:Time -> [10158, 10172, 10180, 10183, 10231] ms Dojo - div ~ div:Time -> [6673, 6675, 6714, 6848, 6902] ms :Time -> [1080, 1120, 1095, 1101, 1104] ms """, image=None, image_hash=None, audio=None) with OutputCapture(level=logging.INFO) as captured: test = PerfTest(MockPort(), 'some-dir/some-test', '/path/some-dir/some-test') test.run_single = lambda driver, path, time_out_ms: output self.assertTrue(test.run(10)) subtests = test._metrics self.assertEqual(list(map(lambda test: test['name'], subtests)), [ 'some test', 'some other test = else', 'Array Construction, []', 'Concat String', 'jQuery - addClass', 'Dojo - div:only-child', 'Dojo - div:nth-child(2n+1)', 'Dojo - div > div', 'Dojo - div ~ div', None ]) some_test_metrics = subtests[0]['metrics'] self.assertEqual( list(map(lambda metric: metric.name(), some_test_metrics)), ['Time']) self.assertEqual(some_test_metrics[0].path(), ['some-dir', 'some-test', 'some test']) self.assertEqual(some_test_metrics[0].flattened_iteration_values(), [1, 2, 3, 4, 5] * 4) some_other_test_metrics = subtests[1]['metrics'] self.assertEqual( list(map(lambda metric: metric.name(), some_other_test_metrics)), ['Time', 'Malloc']) self.assertEqual(some_other_test_metrics[0].path(), ['some-dir', 'some-test', 'some other test = else']) self.assertEqual( some_other_test_metrics[0].flattened_iteration_values(), [6, 7, 8, 9, 10] * 4) self.assertEqual(some_other_test_metrics[1].path(), ['some-dir', 'some-test', 'some other test = else']) self.assertEqual( some_other_test_metrics[1].flattened_iteration_values(), [11, 12, 13, 14, 15] * 4) main_metrics = subtests[len(subtests) - 1]['metrics'] self.assertEqual(list(map(lambda metric: metric.name(), main_metrics)), ['Time']) self.assertEqual(main_metrics[0].path(), ['some-dir', 'some-test']) self.assertEqual(main_metrics[0].flattened_iteration_values(), [1080, 1120, 1095, 1101, 1104] * 4) self.assertEqual(captured.stdout.getvalue(), '') self.assertEqual(captured.stderr.getvalue(), '') self.assertEqual( captured.root.log.getvalue(), """DESCRIPTION: this is a test description. RESULT some-dir: some-test: Time= 1100.0 ms median= 1101.0 ms, stdev= 13.3140211016 ms, min= 1080.0 ms, max= 1120.0 ms """)
def close(self): with OutputCapture(): self.outgoing.close() self.incoming.close() self.outgoing.join_thread() self.incoming.join_thread()
def test_parse_output_with_subtests_and_total(self): output = DriverOutput(""" :Time:Total -> [2324, 2328, 2345, 2314, 2312] ms EmberJS-TodoMVC:Time:Total -> [1462, 1473, 1490, 1465, 1458] ms EmberJS-TodoMVC/a:Time -> [1, 2, 3, 4, 5] ms BackboneJS-TodoMVC:Time -> [862, 855, 855, 849, 854] ms """, image=None, image_hash=None, audio=None) with OutputCapture(level=logging.INFO) as captured: test = PerfTest(MockPort(), 'some-dir/some-test', '/path/some-dir/some-test') test.run_single = lambda driver, path, time_out_ms: output self.assertTrue(test.run(10)) subtests = test._metrics self.assertEqual(list(map(lambda test: test['name'], subtests)), [ None, 'EmberJS-TodoMVC', 'EmberJS-TodoMVC/a', 'BackboneJS-TodoMVC' ]) main_metrics = subtests[0]['metrics'] self.assertEqual(list(map(lambda metric: metric.name(), main_metrics)), ['Time']) self.assertEqual(main_metrics[0].aggregator(), 'Total') self.assertEqual(main_metrics[0].path(), ['some-dir', 'some-test']) self.assertEqual(main_metrics[0].flattened_iteration_values(), [2324, 2328, 2345, 2314, 2312] * 4) some_test_metrics = subtests[1]['metrics'] self.assertEqual( list(map(lambda metric: metric.name(), some_test_metrics)), ['Time']) self.assertEqual(some_test_metrics[0].aggregator(), 'Total') self.assertEqual(some_test_metrics[0].path(), ['some-dir', 'some-test', 'EmberJS-TodoMVC']) self.assertEqual(some_test_metrics[0].flattened_iteration_values(), [1462, 1473, 1490, 1465, 1458] * 4) some_test_metrics = subtests[2]['metrics'] self.assertEqual( list(map(lambda metric: metric.name(), some_test_metrics)), ['Time']) self.assertEqual(some_test_metrics[0].aggregator(), None) self.assertEqual(some_test_metrics[0].path(), ['some-dir', 'some-test', 'EmberJS-TodoMVC', 'a']) self.assertEqual(some_test_metrics[0].flattened_iteration_values(), [1, 2, 3, 4, 5] * 4) some_test_metrics = subtests[3]['metrics'] self.assertEqual( list(map(lambda metric: metric.name(), some_test_metrics)), ['Time']) self.assertEqual(some_test_metrics[0].aggregator(), None) self.assertEqual(some_test_metrics[0].path(), ['some-dir', 'some-test', 'BackboneJS-TodoMVC']) self.assertEqual(some_test_metrics[0].flattened_iteration_values(), [862, 855, 855, 849, 854] * 4) self.assertEqual(captured.stdout.getvalue(), '') self.assertEqual(captured.stderr.getvalue(), '') self.assertEqual( captured.root.log.getvalue(), """RESULT some-dir: some-test: Time= 2324.6 ms median= 2324.0 ms, stdev= 12.1326007105 ms, min= 2312.0 ms, max= 2345.0 ms """)
def credentials(url, required=True, name=None, prompt=None, key_name='password'): global _cache name = name or url.split('/')[2].replace('.', '_') if _cache.get(name): return _cache.get(name) username = os.environ.get('{}_USERNAME'.format(name.upper())) key = os.environ.get('{}_{}'.format(name.upper(), name.upper())) if username and key: _cache[name] = (username, key) return username, key with OutputCapture(): try: import keyring except (CalledProcessError, ImportError): keyring = None username_prompted = False key_prompted = False if not username: try: if keyring: username = keyring.get_password(url, 'username') except RuntimeError: pass if not username and required: if not sys.stderr.isatty() or not sys.stdin.isatty(): raise OSError('No tty to prompt user for username') sys.stderr.write("Authentication required to use {}\n".format( prompt or name)) sys.stderr.write('Username: '******'No tty to prompt user for username') key = getpass.getpass('{}: '.format(key_name.capitalize())) key_prompted = True if username and key: _cache[name] = (username, key) if keyring and (username_prompted or key_prompted): sys.stderr.write( 'Store username and {} in system keyring for {}? (Y/N): '.format( key_name, url)) response = (input if sys.version_info > (3, 0) else raw_input)() if response.lower() in ['y', 'yes', 'ok']: sys.stderr.write('Storing credentials...\n') keyring.set_password(url, 'username', username) keyring.set_password(url, username, key) else: sys.stderr.write('Credentials cached in process.\n') return username, key
def test_parse_warnings_are_logged_if_not_in_lint_mode(self): with OutputCapture() as captured: self.parse_exp('-- this should be a syntax error', is_lint_mode=False) self.assertNotEquals(captured.root.log.getvalue(), '')
def test_check_httpd_success(self): port = self.make_port(executive=MockExecutive2()) port._path_to_apache = lambda: '/usr/sbin/httpd' with OutputCapture() as captured: self.assertTrue(port.check_httpd()) self.assertEqual('', captured.root.log.getvalue())