def test_write_pot(self): s = ConfigSettings() s.register_provider(Provider1) s.register_provider(Provider2) # Just a basic sanity test. temp = NamedTemporaryFile('wt') s.write_pot(temp) temp.flush()
def test_file_reading_single(self): temp = NamedTemporaryFile(mode='wt') temp.write(CONFIG1) temp.flush() s = ConfigSettings() s.register_provider(Provider1) s.load_file(temp.name) self.assertEqual(s.foo.bar, 'bar_value')
def test_hash_file_known_hash(self): """Ensure a known hash value is recreated.""" data = b'The quick brown fox jumps over the lazy cog' expected = 'de9f2c7fd25e1b3afad3e85a0bd17d9b100db4b3' temp = NamedTemporaryFile() temp.write(data) temp.flush() actual = hash_file(temp.name) self.assertEqual(actual, expected)
def test_pruning(self): """Ensure old warnings are removed from database appropriately.""" db = WarningsDatabase() source_files = [] for i in range(1, 21): temp = NamedTemporaryFile(mode='wt') temp.write('x' * (100 * i)) temp.flush() # Keep reference so it doesn't get GC'd and deleted. source_files.append(temp) w = CompilerWarning() w['filename'] = temp.name w['line'] = 1 w['column'] = i * 10 w['message'] = 'irrelevant' db.insert(w) self.assertEqual(len(db), 20) # If we change a source file, inserting a new warning should nuke the # old one. source_files[0].write('extra') source_files[0].flush() w = CompilerWarning() w['filename'] = source_files[0].name w['line'] = 1 w['column'] = 50 w['message'] = 'replaced' db.insert(w) self.assertEqual(len(db), 20) warnings = list(db.warnings_for_file(source_files[0].name)) self.assertEqual(len(warnings), 1) self.assertEqual(warnings[0]['column'], w['column']) # If we delete the source file, calling prune should cause the warnings # to go away. old_filename = source_files[0].name del source_files[0] self.assertFalse(os.path.exists(old_filename)) db.prune() self.assertEqual(len(db), 19)
def test_hash_file_large(self): """Ensure that hash_file seems to work with a large file.""" data = b'x' * 1048576 hasher = hashlib.sha1() hasher.update(data) expected = hasher.hexdigest() temp = NamedTemporaryFile() temp.write(data) temp.flush() actual = hash_file(temp.name) self.assertEqual(actual, expected)
def test_file_reading_multiple(self): """Loading multiple files has proper overwrite behavior.""" temp1 = NamedTemporaryFile(mode='wt') temp1.write(CONFIG1) temp1.flush() temp2 = NamedTemporaryFile(mode='wt') temp2.write(CONFIG2) temp2.flush() s = ConfigSettings() s.register_provider(Provider1) s.load_files([temp1.name, temp2.name]) self.assertEqual(s.foo.bar, 'value2')
def test_basic(self): """ Test that writing to a file produces correct output. """ c = self._config( dict( OS_TARGET='WINNT', TARGET_CPU='i386', MOZ_WIDGET_TOOLKIT='windows', )) tempdir = tempfile.tempdir c.topsrcdir = tempdir with NamedTemporaryFile(dir=os.path.normpath(c.topsrcdir), mode='wt') as mozconfig: mozconfig.write('unused contents') mozconfig.flush() c.mozconfig = mozconfig.name write_mozinfo(self.f, c) with open(self.f) as f: d = json.load(f) self.assertEqual('win', d['os']) self.assertEqual('x86', d['processor']) self.assertEqual('windows', d['toolkit']) self.assertEqual(tempdir, d['topsrcdir']) self.assertEqual(mozconfig.name, d['mozconfig']) self.assertEqual(32, d['bits'])
def test_basic(self): """ Test that writing to a file produces correct output. """ c = self._config( dict( OS_TARGET="WINNT", TARGET_CPU="i386", MOZ_WIDGET_TOOLKIT="windows", ) ) tempdir = tempfile.tempdir c.topsrcdir = tempdir with NamedTemporaryFile( dir=os.path.normpath(c.topsrcdir), mode="wt" ) as mozconfig: mozconfig.write("unused contents") mozconfig.flush() c.mozconfig = mozconfig.name write_mozinfo(self.f, c) with open(self.f) as f: d = json.load(f) self.assertEqual("win", d["os"]) self.assertEqual("x86", d["processor"]) self.assertEqual("windows", d["toolkit"]) self.assertEqual(tempdir, d["topsrcdir"]) self.assertEqual(mozconfig.name, d["mozconfig"]) self.assertEqual(32, d["bits"])
def _test_one(self, name): with TemporaryDirectory() as tmpdir: with NamedTemporaryFile(mode='r+') as temp: srcdir = os.path.join(test_data_path, name) generate_browsersearch.main( ['--silent', '--srcdir', srcdir, temp.name]) return json.load(temp)
def test_file_writing(self): s = ConfigSettings() s.register_provider(Provider2) s.a.string = 'foo' s.a.boolean = False temp = NamedTemporaryFile('wt') s.write(temp) temp.flush() s2 = ConfigSettings() s2.register_provider(Provider2) s2.load_file(temp.name) self.assertEqual(s.a.string, s2.a.string) self.assertEqual(s.a.boolean, s2.a.boolean)
def test_read_exported_variables(self): """Exported variables are caught as new variables.""" with NamedTemporaryFile(mode='w') as mozconfig: mozconfig.write('export MY_EXPORTED=woot\n') mozconfig.flush() result = self.get_loader().read_mozconfig(mozconfig.name) self.assertEqual(result['env']['added'], {'MY_EXPORTED': 'woot'})
def test_read_capture_mk_options_objdir_environ(self): """Ensures mk_add_options calls are captured and override the environ.""" os.environ['MOZ_OBJDIR'] = 'obj-firefox' with NamedTemporaryFile(mode='w') as mozconfig: mozconfig.write('mk_add_options MOZ_OBJDIR=/foo/bar\n') mozconfig.flush() result = self.get_loader().read_mozconfig(mozconfig.name) self.assertEqual(result['topobjdir'], '/foo/bar')
def test_read_ac_options_substitution(self): """Ensure ac_add_options values are substituted.""" with NamedTemporaryFile(mode='w') as mozconfig: mozconfig.write('ac_add_options --foo=@TOPSRCDIR@\n') mozconfig.flush() loader = self.get_loader() result = loader.read_mozconfig(mozconfig.name) self.assertEqual(result['configure_args'], ['--foo=%s' % loader.topsrcdir])
def test_read_empty_variable_value(self): """Ensure empty variable values are parsed properly.""" with NamedTemporaryFile(mode='w') as mozconfig: mozconfig.write('EMPTY=\n') mozconfig.flush() result = self.get_loader().read_mozconfig(mozconfig.name) self.assertIn('EMPTY', result['env']['added']) self.assertEqual(result['env']['added']['EMPTY'], '')
def test_objdir_config_guess(self): base = self.get_base() with NamedTemporaryFile() as mozconfig: os.environ[b'MOZCONFIG'] = mozconfig.name self.assertIsNotNone(base.topobjdir) self.assertEqual(len(base.topobjdir.split()), 1) self.assertTrue(base.topobjdir.endswith(base._config_guess)) self.assertTrue(os.path.isabs(base.topobjdir)) self.assertTrue(base.topobjdir.startswith(topsrcdir))
def test_objdir_trailing_slash(self): """Trailing slashes in topobjdir should be removed.""" base = self.get_base() with NamedTemporaryFile(mode="wt") as mozconfig: mozconfig.write("mk_add_options MOZ_OBJDIR=@TOPSRCDIR@/foo/") mozconfig.flush() os.environ["MOZCONFIG"] = mozconfig.name self.assertEqual(base.topobjdir, mozpath.join(base.topsrcdir, "foo")) self.assertTrue(base.topobjdir.endswith("foo"))
def test_read_topsrcdir_defined(self): """Ensure $topsrcdir references work as expected.""" with NamedTemporaryFile(mode='w') as mozconfig: mozconfig.write('TEST=$topsrcdir') mozconfig.flush() loader = self.get_loader() result = loader.read_mozconfig(mozconfig.name) self.assertEqual(result['env']['added']['TEST'], loader.topsrcdir.replace(os.sep, '/'))
def test_read_moz_objdir_substitution(self): """Ensure @TOPSRCDIR@ substitution is recognized in MOZ_OBJDIR.""" with NamedTemporaryFile(mode='w') as mozconfig: mozconfig.write('mk_add_options MOZ_OBJDIR=@TOPSRCDIR@/some-objdir') mozconfig.flush() loader = self.get_loader() result = loader.read_mozconfig(mozconfig.name) self.assertEqual(result['topobjdir'], '%s/some-objdir' % loader.topsrcdir)
def test_read_capture_ac_options(self): """Ensures ac_add_options calls are captured.""" with NamedTemporaryFile(mode='w') as mozconfig: mozconfig.write('ac_add_options --enable-debug\n') mozconfig.write('ac_add_options --disable-tests --enable-foo\n') mozconfig.write('ac_add_options --foo="bar baz"\n') mozconfig.flush() result = self.get_loader().read_mozconfig(mozconfig.name) self.assertEqual(result['configure_args'], [ '--enable-debug', '--disable-tests', '--enable-foo', '--foo=bar baz'])
def test_read_unmodified_variables(self): """Variables modified by mozconfig are detected.""" cc_path = os.path.realpath('/usr/bin/gcc') os.environ['CC'] = cc_path with NamedTemporaryFile(mode='w') as mozconfig: mozconfig.flush() result = self.get_loader().read_mozconfig(mozconfig.name) self.assertEqual(result['vars']['unmodified'], {}) self.assertEqual(result['env']['unmodified'], {'CC': cc_path})
def test_hashing(self): """Ensure that hashing files on insert works.""" db = WarningsDatabase() temp = NamedTemporaryFile(mode='wt') temp.write('x' * 100) temp.flush() w = CompilerWarning() w['filename'] = temp.name w['line'] = 1 w['column'] = 4 w['message'] = 'foo bar' # Should not throw. db.insert(w) w['filename'] = 'DOES_NOT_EXIST' with self.assertRaises(Exception): db.insert(w)
def test_read_new_variables(self): """New variables declared in mozconfig file are detected.""" with NamedTemporaryFile(mode='w') as mozconfig: mozconfig.write('CC=/usr/local/bin/clang\n') mozconfig.write('CXX=/usr/local/bin/clang++\n') mozconfig.flush() result = self.get_loader().read_mozconfig(mozconfig.name) self.assertEqual(result['env']['added'], { 'CC': '/usr/local/bin/clang', 'CXX': '/usr/local/bin/clang++'})
def test_read_removed_variables(self): """Variables unset by the mozconfig are detected.""" os.environ[b'CC'] = b'/usr/bin/clang' with NamedTemporaryFile(mode='w') as mozconfig: mozconfig.write('unset CC\n') mozconfig.flush() result = self.get_loader().read_mozconfig(mozconfig.name) self.assertEqual(result['env']['removed'], {'CC': '/usr/bin/clang'})
def test_read_modify_variables(self): """Variables modified by mozconfig are detected.""" os.environ[b'CC'] = b'/usr/bin/gcc' with NamedTemporaryFile(mode='w') as mozconfig: mozconfig.write('CC=/usr/local/bin/clang\n') mozconfig.flush() result = self.get_loader().read_mozconfig(mozconfig.name) self.assertEqual(result['env']['modified'], {'CC': ('/usr/bin/gcc', '/usr/local/bin/clang')})
def test_read_multiline_variables(self): """Ensure multi-line variables are captured properly.""" with NamedTemporaryFile(mode='w') as mozconfig: mozconfig.write('multi="foo\nbar"\n') mozconfig.write('single=1\n') mozconfig.flush() result = self.get_loader().read_mozconfig(mozconfig.name) self.assertEqual(result['env']['added'], { 'multi': 'foo\nbar', 'single': '1' })
def test_read_capture_mk_options(self): """Ensures mk_add_options calls are captured.""" with NamedTemporaryFile(mode='w') as mozconfig: mozconfig.write('mk_add_options MOZ_OBJDIR=/foo/bar\n') mozconfig.write('mk_add_options MOZ_MAKE_FLAGS=-j8\n') mozconfig.write('mk_add_options FOO="BAR BAZ"\n') mozconfig.write('mk_add_options BIZ=1\n') mozconfig.flush() result = self.get_loader().read_mozconfig(mozconfig.name) self.assertEqual(result['topobjdir'], '/foo/bar') self.assertEqual(result['make_flags'], '-j8') self.assertEqual(result['make_extra'], ['FOO=BAR BAZ', 'BIZ=1'])
def _test_one(self, name): with TemporaryDirectory() as tmpdir: with NamedTemporaryFile(mode='r+') as temp: srcdir = os.path.join(test_data_path, name) with FileAvoidWrite(temp.name) as faw: generate_browsersearch.main( faw, '--silent', '--fallback', mozpath.join(srcdir, 'region.properties'), ) return json.load(temp)
def test_read_empty_mozconfig(self): with NamedTemporaryFile(mode='w') as mozconfig: result = self.get_loader().read_mozconfig(mozconfig.name) self.assertEqual(result['path'], mozconfig.name) self.assertIsNone(result['topobjdir']) self.assertEqual(result['configure_args'], []) self.assertEqual(result['make_flags'], []) self.assertEqual(result['make_extra'], []) for f in ('added', 'removed', 'modified'): self.assertEqual(len(result['env'][f]), 0) self.assertGreater(len(result['env']['unmodified']), 0)
def test_read_load_exception(self): """Ensure non-0 exit codes in mozconfigs are handled properly.""" with NamedTemporaryFile(mode='w') as mozconfig: mozconfig.write('echo "hello world"\n') mozconfig.write('exit 1\n') mozconfig.flush() with self.assertRaises(MozconfigLoadException) as e: self.get_loader().read_mozconfig(mozconfig.name) self.assertIn('Evaluation of your mozconfig exited with an error', str(e.exception)) self.assertEquals(e.exception.path, mozconfig.name.replace(os.sep, '/')) self.assertEquals(e.exception.output, ['hello world'])
def test_read_modify_variables(self): """Variables modified by mozconfig are detected.""" old_path = os.path.realpath('/usr/bin/gcc') new_path = os.path.realpath('/usr/local/bin/clang') os.environ['CC'] = old_path with NamedTemporaryFile(mode='w') as mozconfig: mozconfig.write('CC="%s"\n' % new_path) mozconfig.flush() result = self.get_loader().read_mozconfig(mozconfig.name) self.assertEqual(result['vars']['modified'], {}) self.assertEqual(result['env']['modified'], {'CC': (old_path, new_path)})
def test_read_ac_app_options(self): with NamedTemporaryFile(mode='w') as mozconfig: mozconfig.write('ac_add_options --foo=@TOPSRCDIR@\n') mozconfig.write('ac_add_app_options app1 --bar=@TOPSRCDIR@\n') mozconfig.write('ac_add_app_options app2 --bar=x\n') mozconfig.flush() loader = self.get_loader() result = loader.read_mozconfig(mozconfig.name, moz_build_app='app1') self.assertEqual(result['configure_args'], [ '--foo=%s' % loader.topsrcdir, '--bar=%s' % loader.topsrcdir]) result = loader.read_mozconfig(mozconfig.name, moz_build_app='app2') self.assertEqual(result['configure_args'], [ '--foo=%s' % loader.topsrcdir, '--bar=x'])
def test_read_capture_mk_options(self): """Ensures mk_add_options calls are captured.""" with NamedTemporaryFile(mode='w') as mozconfig: mozconfig.write('mk_add_options MOZ_OBJDIR=/foo/bar\n') mozconfig.write('mk_add_options MOZ_MAKE_FLAGS="-j8 -s"\n') mozconfig.write('mk_add_options FOO="BAR BAZ"\n') mozconfig.write('mk_add_options BIZ=1\n') mozconfig.flush() result = self.get_loader().read_mozconfig(mozconfig.name) self.assertEqual(result['topobjdir'], '/foo/bar') self.assertEqual(result['make_flags'], ['-j8', '-s']) self.assertEqual(result['make_extra'], ['FOO=BAR BAZ', 'BIZ=1']) vars = result['vars']['added'] for var in ('MOZ_OBJDIR', 'MOZ_MAKE_FLAGS', 'FOO', 'BIZ'): self.assertEqual(vars.get('%s_IS_SET' % var), '1')