def test_load_parameters_file_yaml(self): with MockedOpen({"params.yml": "some: data\n"}): self.assertEqual(load_parameters_file("params.yml"), {"some": "data"})
def test_id_and_secret_keyfile(self): includes = ("util.configure", "checks.configure", "keyfiles.configure") config, output, status = self.get_result( "id_and_secret_keyfile('Bing API')", includes=includes) self.assertEqual(status, 0) self.assertEqual( output, textwrap.dedent("""\ checking for the Bing API key... no """), ) self.assertEqual( config, { "MOZ_BING_API_CLIENTID": "no-bing-api-clientid", "MOZ_BING_API_KEY": "no-bing-api-key", }, ) config, output, status = self.get_result( "id_and_secret_keyfile('Bing API')", args=["--with-bing-api-keyfile=/foo/bar/does/not/exist"], includes=includes, ) self.assertEqual(status, 1) self.assertEqual( output, textwrap.dedent("""\ checking for the Bing API key... no ERROR: '/foo/bar/does/not/exist': No such file or directory. """), ) self.assertEqual(config, {}) with MockedOpen({"key": ""}): config, output, status = self.get_result( "id_and_secret_keyfile('Bing API')", args=["--with-bing-api-keyfile=key"], includes=includes, ) self.assertEqual(status, 1) self.assertEqual( output, textwrap.dedent("""\ checking for the Bing API key... no ERROR: 'key' is empty. """), ) self.assertEqual(config, {}) with MockedOpen({"key": "fake-id fake-key\n"}): config, output, status = self.get_result( "id_and_secret_keyfile('Bing API')", args=["--with-bing-api-keyfile=key"], includes=includes, ) self.assertEqual(status, 0) self.assertEqual( output, textwrap.dedent("""\ checking for the Bing API key... yes """), ) self.assertEqual( config, { "MOZ_BING_API_CLIENTID": "fake-id", "MOZ_BING_API_KEY": "fake-key", }, ) with MockedOpen({"key": "fake-key\n"}): config, output, status = self.get_result( "id_and_secret_keyfile('Bing API')", args=["--with-bing-api-keyfile=key"], includes=includes, ) self.assertEqual(status, 1) self.assertEqual( output, textwrap.dedent("""\ checking for the Bing API key... no ERROR: Bing API key file has an invalid format. """), ) self.assertEqual(config, {}) with MockedOpen({"default-key": "default-id default-key\n"}): config, output, status = self.get_result( "id_and_secret_keyfile('Bing API', default='default-key')", includes=includes, ) self.assertEqual(status, 0) self.assertEqual( output, textwrap.dedent("""\ checking for the Bing API key... yes """), ) self.assertEqual( config, { "MOZ_BING_API_CLIENTID": "default-id", "MOZ_BING_API_KEY": "default-key", }, ) with MockedOpen({ "default-key": "default-id default-key\n", "key": "fake-id fake-key\n" }): config, output, status = self.get_result( "id_and_secret_keyfile('Bing API', default='default-key')", args=["--with-bing-api-keyfile=key"], includes=includes, ) self.assertEqual(status, 0) self.assertEqual( output, textwrap.dedent("""\ checking for the Bing API key... yes """), ) self.assertEqual( config, { "MOZ_BING_API_CLIENTID": "fake-id", "MOZ_BING_API_KEY": "fake-key", }, )
def test_load_parameters_file_json(self): with MockedOpen({"params.json": '{"some": "data"}'}): self.assertEqual(load_parameters_file("params.json"), {"some": "data"})
def test_command_line_literal_at(self): with MockedOpen({"@[email protected]": '@foo@\n'}): self.pp.handleCommandLine( ['-Fsubstitution', '-Dfoo=foobarbaz', '@[email protected]']) self.assertEqual(self.pp.out.getvalue(), 'foobarbaz\n')
def __call__(self, name, mode): if 'w' in mode: raise Exception, 'Unexpected open with write mode' return MockedOpen.__call__(self, name, mode)
def test_error(self): with MockedOpen({'f': '#error spit this message out\n'}): with self.assertRaises(Preprocessor.Error) as e: self.pp.do_include('f') self.assertEqual(e.args[0][-1], 'spit this message out')
def test_include_missing_file(self): with MockedOpen({'f': '#include foo\n'}): with self.assertRaises(Preprocessor.Error) as e: self.pp.do_include('f') self.assertEqual(e.key, 'FILE_NOT_FOUND')
def test_no_email_owner(self, _): self.options['owner'] = 'ffxbld' with MockedOpen({self.ttc_file: None}): params = decision.get_decision_parameters(FAKE_GRAPH_CONFIG, self.options) self.assertEqual(params['owner'], '*****@*****.**')
def test_preprocess_manifest(self): with MockedOpen({'manifest': MANIFEST}): preprocess_manifest(self.sink, 'manifest') self.assertEqual(self.sink.log, self.EXPECTED_LOG)
def test_include_when(self): with MockedOpen({ os.path.join(test_data_path, 'moz.configure'): textwrap.dedent(''' option('--with-foo', help='foo') include('always.configure', when=True) include('never.configure', when=False) include('foo.configure', when='--with-foo') set_config('FOO', foo) set_config('BAR', bar) set_config('QUX', qux) '''), os.path.join(test_data_path, 'always.configure'): textwrap.dedent(''' option('--with-bar', help='bar') @depends('--with-bar') def bar(x): if x: return 'bar' '''), os.path.join(test_data_path, 'never.configure'): textwrap.dedent(''' option('--with-qux', help='qux') @depends('--with-qux') def qux(x): if x: return 'qux' '''), os.path.join(test_data_path, 'foo.configure'): textwrap.dedent(''' option('--with-foo-really', help='really foo') @depends('--with-foo-really') def foo(x): if x: return 'foo' include('foo2.configure', when='--with-foo-really') '''), os.path.join(test_data_path, 'foo2.configure'): textwrap.dedent(''' set_config('FOO2', True) '''), }): config = self.get_config() self.assertEquals(config, {}) config = self.get_config(['--with-foo']) self.assertEquals(config, {}) config = self.get_config(['--with-bar']) self.assertEquals(config, { 'BAR': 'bar', }) with self.assertRaises(InvalidOptionError) as e: self.get_config(['--with-qux']) self.assertEquals( e.exception.message, '--with-qux is not available in this configuration') config = self.get_config(['--with-foo', '--with-foo-really']) self.assertEquals(config, { 'FOO': 'foo', 'FOO2': True, })
def test_preprocess_manifest_defines(self): with MockedOpen({'manifest': MANIFEST}): preprocess_manifest(self.sink, 'manifest', {'baz': 1, 'SUFFIX': '.exe'}) self.assertEqual(self.sink.log, self.EXPECTED_LOG + [((self.MANIFEST_PATH, 12), 'add', 'baz', 'baz.exe')])
def test_include_line(self): files = { 'srcdir/test.js': '\n'.join([ '#define foo foobarbaz', '#include @inc@', '@bar@', '', ]), 'srcdir/bar.js': '\n'.join([ '#define bar barfoobaz', '@foo@', '', ]), 'srcdir/foo.js': '\n'.join([ 'bazfoobar', '#include bar.js', 'bazbarfoo', '', ]), 'objdir/baz.js': 'baz\n', 'srcdir/f.js': '\n'.join([ '#include foo.js', '#filter substitution', '#define inc bar.js', '#include test.js', '#include ../objdir/baz.js', 'fin', '', ]), } preprocessed = ('//@line 1 "$SRCDIR/foo.js"\n' 'bazfoobar\n' '//@line 2 "$SRCDIR/bar.js"\n' '@foo@\n' '//@line 3 "$SRCDIR/foo.js"\n' 'bazbarfoo\n' '//@line 2 "$SRCDIR/bar.js"\n' 'foobarbaz\n' '//@line 3 "$SRCDIR/test.js"\n' 'barfoobaz\n' '//@line 1 "$OBJDIR/baz.js"\n' 'baz\n' '//@line 6 "$SRCDIR/f.js"\n' 'fin\n').replace('DIR/', 'DIR' + os.sep) # Try with separate srcdir/objdir with MockedOpen(files): self.pp.topsrcdir = os.path.abspath('srcdir') self.pp.topobjdir = os.path.abspath('objdir') self.pp.do_include('srcdir/f.js') self.assertEqual(self.pp.out.getvalue(), preprocessed) # Try again with relative objdir self.setUp() files['srcdir/objdir/baz.js'] = files['objdir/baz.js'] del files['objdir/baz.js'] files['srcdir/f.js'] = files['srcdir/f.js'].replace('../', '') with MockedOpen(files): self.pp.topsrcdir = os.path.abspath('srcdir') self.pp.topobjdir = os.path.abspath('srcdir/objdir') self.pp.do_include('srcdir/f.js') self.assertEqual(self.pp.out.getvalue(), preprocessed)
def __call__(self, name, mode): if "w" in mode: raise Exception, "Unexpected open with write mode" return MockedOpen.__call__(self, name, mode)
def test_mocked_open(self): # Create a temporary file on the file system. (fd, path) = mkstemp() with os.fdopen(fd, 'w') as file: file.write('foobar') self.assertFalse(os.path.exists('file1')) self.assertFalse(os.path.exists('file2')) with MockedOpen({'file1': 'content1', 'file2': 'content2'}): self.assertTrue(os.path.exists('file1')) self.assertTrue(os.path.exists('file2')) self.assertFalse(os.path.exists('foo/file1')) # Check the contents of the files given at MockedOpen creation. self.assertEqual(open('file1', 'r').read(), 'content1') self.assertEqual(open('file2', 'r').read(), 'content2') # Check that overwriting these files alters their content. with open('file1', 'w') as file: file.write('foo') self.assertTrue(os.path.exists('file1')) self.assertEqual(open('file1', 'r').read(), 'foo') # ... but not until the file is closed. file = open('file2', 'w') file.write('bar') self.assertEqual(open('file2', 'r').read(), 'content2') file.close() self.assertEqual(open('file2', 'r').read(), 'bar') # Check that appending to a file does append with open('file1', 'a') as file: file.write('bar') self.assertEqual(open('file1', 'r').read(), 'foobar') self.assertFalse(os.path.exists('file3')) # Opening a non-existing file ought to fail. self.assertRaises(IOError, open, 'file3', 'r') self.assertFalse(os.path.exists('file3')) # Check that writing a new file does create the file. with open('file3', 'w') as file: file.write('baz') self.assertEqual(open('file3', 'r').read(), 'baz') self.assertTrue(os.path.exists('file3')) # Check the content of the file created outside MockedOpen. self.assertEqual(open(path, 'r').read(), 'foobar') # Check that overwriting a file existing on the file system # does modify its content. with open(path, 'w') as file: file.write('bazqux') self.assertEqual(open(path, 'r').read(), 'bazqux') with MockedOpen(): # Check that appending to a file existing on the file system # does modify its content. with open(path, 'a') as file: file.write('bazqux') self.assertEqual(open(path, 'r').read(), 'foobarbazqux') # Check that the file was not actually modified on the file system. self.assertEqual(open(path, 'r').read(), 'foobar') os.remove(path) # Check that the file created inside MockedOpen wasn't actually # created. self.assertRaises(IOError, open, 'file3', 'r')
def test_load_parameters_file_yaml(self): with MockedOpen({"params.yml": "some: data\n"}): self.assertEqual( load_parameters_file({'parameters': 'params.yml'}), {'some': 'data'})
def test_preprocess_manifest_missing_define(self): with MockedOpen({'manifest': MANIFEST}): self.assertRaises(Preprocessor.Error, preprocess_manifest, self.sink, 'manifest', {'baz': 1})
def test_load_parameters_file_json(self): with MockedOpen({"params.json": '{"some": "data"}'}): self.assertEqual( load_parameters_file({'parameters': 'params.json'}), {'some': 'data'})
def test_no_email_owner(self, mock_get_hg_revision_branch): mock_get_hg_revision_branch.return_value = 'default' self.options['owner'] = 'ffxbld' with MockedOpen({self.ttc_file: None}): params = decision.get_decision_parameters(FAKE_GRAPH_CONFIG, self.options) self.assertEqual(params['owner'], '*****@*****.**')
def test_lineEndings(self): with MockedOpen({'f': 'first\n#literal second\n'}): self.pp.setLineEndings('cr') self.pp.do_include('f') self.assertEqual(self.pp.out.getvalue(), "first\rsecond\r")
def test_simple_keyfile(self): includes = ('util.configure', 'checks.configure', 'keyfiles.configure') config, output, status = self.get_result( "simple_keyfile('Mozilla API')", includes=includes) self.assertEqual(status, 0) self.assertEqual( output, textwrap.dedent('''\ checking for the Mozilla API key... no ''')) self.assertEqual(config, { 'MOZ_MOZILLA_API_KEY': 'no-mozilla-api-key', }) config, output, status = self.get_result( "simple_keyfile('Mozilla API')", args=['--with-mozilla-api-keyfile=/foo/bar/does/not/exist'], includes=includes) self.assertEqual(status, 1) self.assertEqual( output, textwrap.dedent('''\ checking for the Mozilla API key... no ERROR: '/foo/bar/does/not/exist': No such file or directory. ''')) self.assertEqual(config, {}) with MockedOpen({'key': ''}): config, output, status = self.get_result( "simple_keyfile('Mozilla API')", args=['--with-mozilla-api-keyfile=key'], includes=includes) self.assertEqual(status, 1) self.assertEqual( output, textwrap.dedent('''\ checking for the Mozilla API key... no ERROR: 'key' is empty. ''')) self.assertEqual(config, {}) with MockedOpen({'key': 'fake-key\n'}): config, output, status = self.get_result( "simple_keyfile('Mozilla API')", args=['--with-mozilla-api-keyfile=key'], includes=includes) self.assertEqual(status, 0) self.assertEqual( output, textwrap.dedent('''\ checking for the Mozilla API key... yes ''')) self.assertEqual(config, { 'MOZ_MOZILLA_API_KEY': 'fake-key', }) with MockedOpen({'default': 'default-key\n'}): config, output, status = self.get_result( "simple_keyfile('Mozilla API', default='default')", includes=includes) self.assertEqual(status, 0) self.assertEqual( output, textwrap.dedent('''\ checking for the Mozilla API key... yes ''')) self.assertEqual(config, { 'MOZ_MOZILLA_API_KEY': 'default-key', }) with MockedOpen({'default': 'default-key\n', 'key': 'fake-key\n'}): config, output, status = self.get_result( "simple_keyfile('Mozilla API', default='key')", includes=includes) self.assertEqual(status, 0) self.assertEqual( output, textwrap.dedent('''\ checking for the Mozilla API key... yes ''')) self.assertEqual(config, { 'MOZ_MOZILLA_API_KEY': 'fake-key', })
def test_include_undefined_variable(self): with MockedOpen({'f': '#filter substitution\n#include @foo@\n'}): with self.assertRaises(Preprocessor.Error) as e: self.pp.do_include('f') self.assertEqual(e.key, 'UNDEFINED_VAR')
def test_id_and_secret_keyfile(self): includes = ('util.configure', 'checks.configure', 'keyfiles.configure') config, output, status = self.get_result( "id_and_secret_keyfile('Bing API')", includes=includes) self.assertEqual(status, 0) self.assertEqual( output, textwrap.dedent('''\ checking for the Bing API key... no ''')) self.assertEqual( config, { 'MOZ_BING_API_CLIENTID': 'no-bing-api-clientid', 'MOZ_BING_API_KEY': 'no-bing-api-key', }) config, output, status = self.get_result( "id_and_secret_keyfile('Bing API')", args=['--with-bing-api-keyfile=/foo/bar/does/not/exist'], includes=includes) self.assertEqual(status, 1) self.assertEqual( output, textwrap.dedent('''\ checking for the Bing API key... no ERROR: '/foo/bar/does/not/exist': No such file or directory. ''')) self.assertEqual(config, {}) with MockedOpen({'key': ''}): config, output, status = self.get_result( "id_and_secret_keyfile('Bing API')", args=['--with-bing-api-keyfile=key'], includes=includes) self.assertEqual(status, 1) self.assertEqual( output, textwrap.dedent('''\ checking for the Bing API key... no ERROR: 'key' is empty. ''')) self.assertEqual(config, {}) with MockedOpen({'key': 'fake-id fake-key\n'}): config, output, status = self.get_result( "id_and_secret_keyfile('Bing API')", args=['--with-bing-api-keyfile=key'], includes=includes) self.assertEqual(status, 0) self.assertEqual( output, textwrap.dedent('''\ checking for the Bing API key... yes ''')) self.assertEqual( config, { 'MOZ_BING_API_CLIENTID': 'fake-id', 'MOZ_BING_API_KEY': 'fake-key', }) with MockedOpen({'key': 'fake-key\n'}): config, output, status = self.get_result( "id_and_secret_keyfile('Bing API')", args=['--with-bing-api-keyfile=key'], includes=includes) self.assertEqual(status, 1) self.assertEqual( output, textwrap.dedent('''\ checking for the Bing API key... no ERROR: Bing API key file has an invalid format. ''')) self.assertEqual(config, {}) with MockedOpen({'default-key': 'default-id default-key\n'}): config, output, status = self.get_result( "id_and_secret_keyfile('Bing API', default='default-key')", includes=includes) self.assertEqual(status, 0) self.assertEqual( output, textwrap.dedent('''\ checking for the Bing API key... yes ''')) self.assertEqual( config, { 'MOZ_BING_API_CLIENTID': 'default-id', 'MOZ_BING_API_KEY': 'default-key', }) with MockedOpen({ 'default-key': 'default-id default-key\n', 'key': 'fake-id fake-key\n' }): config, output, status = self.get_result( "id_and_secret_keyfile('Bing API', default='default-key')", args=['--with-bing-api-keyfile=key'], includes=includes) self.assertEqual(status, 0) self.assertEqual( output, textwrap.dedent('''\ checking for the Bing API key... yes ''')) self.assertEqual( config, { 'MOZ_BING_API_CLIENTID': 'fake-id', 'MOZ_BING_API_KEY': 'fake-key', })
def moz_configure(self, source): return MockedOpen({ os.path.join(test_data_path, 'moz.configure'): textwrap.dedent(source) })
def test_no_suffixes(self): with MockedOpen({}): combine_task_graph_files([]) self.assertRaises(Exception, open("artifacts/to-run.json"))
def test_simple_keyfile(self): includes = ("util.configure", "checks.configure", "keyfiles.configure") config, output, status = self.get_result( "simple_keyfile('Mozilla API')", includes=includes) self.assertEqual(status, 0) self.assertEqual( output, textwrap.dedent("""\ checking for the Mozilla API key... no """), ) self.assertEqual( config, { "MOZ_MOZILLA_API_KEY": "no-mozilla-api-key", }, ) config, output, status = self.get_result( "simple_keyfile('Mozilla API')", args=["--with-mozilla-api-keyfile=/foo/bar/does/not/exist"], includes=includes, ) self.assertEqual(status, 1) self.assertEqual( output, textwrap.dedent("""\ checking for the Mozilla API key... no ERROR: '/foo/bar/does/not/exist': No such file or directory. """), ) self.assertEqual(config, {}) with MockedOpen({"key": ""}): config, output, status = self.get_result( "simple_keyfile('Mozilla API')", args=["--with-mozilla-api-keyfile=key"], includes=includes, ) self.assertEqual(status, 1) self.assertEqual( output, textwrap.dedent("""\ checking for the Mozilla API key... no ERROR: 'key' is empty. """), ) self.assertEqual(config, {}) with MockedOpen({"key": "fake-key\n"}): config, output, status = self.get_result( "simple_keyfile('Mozilla API')", args=["--with-mozilla-api-keyfile=key"], includes=includes, ) self.assertEqual(status, 0) self.assertEqual( output, textwrap.dedent("""\ checking for the Mozilla API key... yes """), ) self.assertEqual( config, { "MOZ_MOZILLA_API_KEY": "fake-key", }, ) with MockedOpen({"default": "default-key\n"}): config, output, status = self.get_result( "simple_keyfile('Mozilla API', default='default')", includes=includes) self.assertEqual(status, 0) self.assertEqual( output, textwrap.dedent("""\ checking for the Mozilla API key... yes """), ) self.assertEqual( config, { "MOZ_MOZILLA_API_KEY": "default-key", }, ) with MockedOpen({"default": "default-key\n", "key": "fake-key\n"}): config, output, status = self.get_result( "simple_keyfile('Mozilla API', default='key')", includes=includes) self.assertEqual(status, 0) self.assertEqual( output, textwrap.dedent("""\ checking for the Mozilla API key... yes """), ) self.assertEqual( config, { "MOZ_MOZILLA_API_KEY": "fake-key", }, )