def test_path_typed_list(self): config = self.config ctxt1 = Context(config=config) ctxt1.push_source(mozpath.join(config.topsrcdir, "foo", "moz.build")) ctxt2 = Context(config=config) ctxt2.push_source(mozpath.join(config.topsrcdir, "bar", "moz.build")) paths = [ "!../bar/qux", "!/qux/qux", "!qux", "../bar/qux", "/qux/qux", "qux", ] MyList = ContextDerivedTypedList(Path) l = MyList(ctxt1) l += paths for p_str, p_path in zip(paths, l): self.assertEqual(p_str, p_path) self.assertEqual(p_path, Path(ctxt1, p_str)) self.assertEqual(p_path.join("foo"), Path(ctxt1, mozpath.join(p_str, "foo"))) l2 = MyList(ctxt2) l2 += paths for p_str, p_path in zip(paths, l2): self.assertEqual(p_str, p_path) self.assertEqual(p_path, Path(ctxt2, p_str)) # Assigning with Paths from another context doesn't rebase them l2 = MyList(ctxt2) l2 += l for p_str, p_path in zip(paths, l2): self.assertEqual(p_str, p_path) self.assertEqual(p_path, Path(ctxt1, p_str)) MyListWithFlags = ContextDerivedTypedListWithItems( Path, StrictOrderingOnAppendListWithFlagsFactory({ "foo": bool, }), ) l = MyListWithFlags(ctxt1) l += paths for p in paths: l[p].foo = True for p_str, p_path in zip(paths, l): self.assertEqual(p_str, p_path) self.assertEqual(p_path, Path(ctxt1, p_str)) self.assertEqual(l[p_str].foo, True) self.assertEqual(l[p_path].foo, True)
def test_path_with_mixed_contexts(self): config = self.config ctxt1 = Context(config=config) ctxt1.push_source(mozpath.join(config.topsrcdir, 'foo', 'moz.build')) ctxt2 = Context(config=config) ctxt2.push_source(mozpath.join(config.topsrcdir, 'bar', 'moz.build')) path1 = Path(ctxt1, 'qux') path2 = Path(ctxt2, path1) self.assertEqual(path2, path1) self.assertEqual(path2, 'qux') self.assertEqual(path2.context, ctxt1) self.assertEqual(path2.full_path, mozpath.join(config.topsrcdir, 'foo', 'qux')) path1 = Path(ctxt1, '../bar/qux') path2 = Path(ctxt2, path1) self.assertEqual(path2, path1) self.assertEqual(path2, '../bar/qux') self.assertEqual(path2.context, ctxt1) self.assertEqual(path2.full_path, mozpath.join(config.topsrcdir, 'bar', 'qux')) path1 = Path(ctxt1, '/qux/qux') path2 = Path(ctxt2, path1) self.assertEqual(path2, path1) self.assertEqual(path2, '/qux/qux') self.assertEqual(path2.context, ctxt1) self.assertEqual(path2.full_path, mozpath.join(config.topsrcdir, 'qux', 'qux')) path1 = Path(ctxt1, '!qux') path2 = Path(ctxt2, path1) self.assertEqual(path2, path1) self.assertEqual(path2, '!qux') self.assertEqual(path2.context, ctxt1) self.assertEqual(path2.full_path, mozpath.join(config.topobjdir, 'foo', 'qux')) path1 = Path(ctxt1, '!../bar/qux') path2 = Path(ctxt2, path1) self.assertEqual(path2, path1) self.assertEqual(path2, '!../bar/qux') self.assertEqual(path2.context, ctxt1) self.assertEqual(path2.full_path, mozpath.join(config.topobjdir, 'bar', 'qux')) path1 = Path(ctxt1, '!/qux/qux') path2 = Path(ctxt2, path1) self.assertEqual(path2, path1) self.assertEqual(path2, '!/qux/qux') self.assertEqual(path2.context, ctxt1) self.assertEqual(path2.full_path, mozpath.join(config.topobjdir, 'qux', 'qux'))
def test_path_with_mixed_contexts(self): config = self.config ctxt1 = Context(config=config) ctxt1.push_source(mozpath.join(config.topsrcdir, "foo", "moz.build")) ctxt2 = Context(config=config) ctxt2.push_source(mozpath.join(config.topsrcdir, "bar", "moz.build")) path1 = Path(ctxt1, "qux") path2 = Path(ctxt2, path1) self.assertEqual(path2, path1) self.assertEqual(path2, "qux") self.assertEqual(path2.context, ctxt1) self.assertEqual(path2.full_path, mozpath.join(config.topsrcdir, "foo", "qux")) path1 = Path(ctxt1, "../bar/qux") path2 = Path(ctxt2, path1) self.assertEqual(path2, path1) self.assertEqual(path2, "../bar/qux") self.assertEqual(path2.context, ctxt1) self.assertEqual(path2.full_path, mozpath.join(config.topsrcdir, "bar", "qux")) path1 = Path(ctxt1, "/qux/qux") path2 = Path(ctxt2, path1) self.assertEqual(path2, path1) self.assertEqual(path2, "/qux/qux") self.assertEqual(path2.context, ctxt1) self.assertEqual(path2.full_path, mozpath.join(config.topsrcdir, "qux", "qux")) path1 = Path(ctxt1, "!qux") path2 = Path(ctxt2, path1) self.assertEqual(path2, path1) self.assertEqual(path2, "!qux") self.assertEqual(path2.context, ctxt1) self.assertEqual(path2.full_path, mozpath.join(config.topobjdir, "foo", "qux")) path1 = Path(ctxt1, "!../bar/qux") path2 = Path(ctxt2, path1) self.assertEqual(path2, path1) self.assertEqual(path2, "!../bar/qux") self.assertEqual(path2.context, ctxt1) self.assertEqual(path2.full_path, mozpath.join(config.topobjdir, "bar", "qux")) path1 = Path(ctxt1, "!/qux/qux") path2 = Path(ctxt2, path1) self.assertEqual(path2, path1) self.assertEqual(path2, "!/qux/qux") self.assertEqual(path2.context, ctxt1) self.assertEqual(path2.full_path, mozpath.join(config.topobjdir, "qux", "qux"))
def test_objdir_path(self): config = self.config ctxt = Context(config=config) ctxt.push_source(mozpath.join(config.topsrcdir, 'foo', 'moz.build')) path = ObjDirPath(ctxt, '!qux') self.assertEqual(path, '!qux') self.assertEqual(path.full_path, mozpath.join(config.topobjdir, 'foo', 'qux')) path = ObjDirPath(ctxt, '!../bar/qux') self.assertEqual(path, '!../bar/qux') self.assertEqual(path.full_path, mozpath.join(config.topobjdir, 'bar', 'qux')) path = ObjDirPath(ctxt, '!/qux/qux') self.assertEqual(path, '!/qux/qux') self.assertEqual(path.full_path, mozpath.join(config.topobjdir, 'qux', 'qux')) with self.assertRaises(ValueError): path = ObjDirPath(ctxt, '../bar/qux') with self.assertRaises(ValueError): path = ObjDirPath(ctxt, '/qux/qux') path = ObjDirPath(path) self.assertIsInstance(path, ObjDirPath) self.assertEqual(path, '!/qux/qux') self.assertEqual(path.full_path, mozpath.join(config.topobjdir, 'qux', 'qux')) path = Path(path) self.assertIsInstance(path, ObjDirPath)
def test_update(self): test = Context({ 'foo': (int, int, ''), 'bar': (bool, bool, ''), 'baz': (dict, list, ''), }) self.assertEqual(list(test), []) with self.assertRaises(ValueError): test.update(bar=True, foo={}) self.assertEqual(list(test), []) test.update(bar=True, foo=1) self.assertEqual(set(test.keys()), {'foo', 'bar'}) self.assertEqual(test['foo'], 1) self.assertEqual(test['bar'], True) test.update([('bar', False), ('foo', 2)]) self.assertEqual(test['foo'], 2) self.assertEqual(test['bar'], False) test.update([('foo', 0), ('baz', {'a': 1, 'b': 2})]) self.assertEqual(test['foo'], 0) self.assertEqual(test['baz'], {'a': 1, 'b': 2}) test.update([('foo', 42), ('baz', [('c', 3), ('d', 4)])]) self.assertEqual(test['foo'], 42) self.assertEqual(test['baz'], {'c': 3, 'd': 4})
def test_key_checking(self): # Checking for existence of a key should not populate the key if it # doesn't exist. g = Context(allowed_variables=VARIABLES) self.assertFalse('HOGE' in g) self.assertFalse('HOGE' in g)
def test_allowed_set(self): self.assertIn('HOGE', VARIABLES) ns = Context(allowed_variables=VARIABLES) ns['HOGE'] = 'foo' self.assertEqual(ns['HOGE'], 'foo')
def test_allowed_set(self): self.assertIn("HOGE", VARIABLES) ns = Context(allowed_variables=VARIABLES) ns["HOGE"] = "foo" self.assertEqual(ns["HOGE"], "foo")
def test_update(self): test = Context({ "foo": (int, int, ""), "bar": (bool, bool, ""), "baz": (dict, list, ""), }) self.assertEqual(list(test), []) with self.assertRaises(ValueError): test.update(bar=True, foo={}) self.assertEqual(list(test), []) test.update(bar=True, foo=1) self.assertEqual(set(test.keys()), {"foo", "bar"}) self.assertEqual(test["foo"], 1) self.assertEqual(test["bar"], True) test.update([("bar", False), ("foo", 2)]) self.assertEqual(test["foo"], 2) self.assertEqual(test["bar"], False) test.update([("foo", 0), ("baz", {"a": 1, "b": 2})]) self.assertEqual(test["foo"], 0) self.assertEqual(test["baz"], {"a": 1, "b": 2}) test.update([("foo", 42), ("baz", [("c", 3), ("d", 4)])]) self.assertEqual(test["foo"], 42) self.assertEqual(test["baz"], {"c": 3, "d": 4})
def test_defaults(self): test = Context({ 'foo': (int, int, ''), 'bar': (bool, bool, ''), 'baz': (dict, dict, ''), }) self.assertEqual(list(test), []) self.assertEqual(test['foo'], 0) self.assertEqual(set(test.keys()), {'foo'}) self.assertEqual(test['bar'], False) self.assertEqual(set(test.keys()), {'foo', 'bar'}) self.assertEqual(test['baz'], {}) self.assertEqual(set(test.keys()), {'foo', 'bar', 'baz'}) with self.assertRaises(KeyError): test['qux'] self.assertEqual(set(test.keys()), {'foo', 'bar', 'baz'})
def test_objdir_path(self): config = self.config ctxt = Context(config=config) ctxt.push_source(mozpath.join(config.topsrcdir, "foo", "moz.build")) path = ObjDirPath(ctxt, "!qux") self.assertEqual(path, "!qux") self.assertEqual(path.full_path, mozpath.join(config.topobjdir, "foo", "qux")) path = ObjDirPath(ctxt, "!../bar/qux") self.assertEqual(path, "!../bar/qux") self.assertEqual(path.full_path, mozpath.join(config.topobjdir, "bar", "qux")) path = ObjDirPath(ctxt, "!/qux/qux") self.assertEqual(path, "!/qux/qux") self.assertEqual(path.full_path, mozpath.join(config.topobjdir, "qux", "qux")) with self.assertRaises(ValueError): path = ObjDirPath(ctxt, "../bar/qux") with self.assertRaises(ValueError): path = ObjDirPath(ctxt, "/qux/qux") path = ObjDirPath(path) self.assertIsInstance(path, ObjDirPath) self.assertEqual(path, "!/qux/qux") self.assertEqual(path.full_path, mozpath.join(config.topobjdir, "qux", "qux")) path = Path(path) self.assertIsInstance(path, ObjDirPath)
def test_defaults(self): test = Context({ "foo": (int, int, ""), "bar": (bool, bool, ""), "baz": (dict, dict, ""), }) self.assertEqual(list(test), []) self.assertEqual(test["foo"], 0) self.assertEqual(set(test.keys()), {"foo"}) self.assertEqual(test["bar"], False) self.assertEqual(set(test.keys()), {"foo", "bar"}) self.assertEqual(test["baz"], {}) self.assertEqual(set(test.keys()), {"foo", "bar", "baz"}) with self.assertRaises(KeyError): test["qux"] self.assertEqual(set(test.keys()), {"foo", "bar", "baz"})
def test_exec_source_reassign_exported(self): template_sandbox = self.sandbox(data_path='templates') # Templates need to be defined in actual files because of # inspect.getsourcelines. template_sandbox.exec_file('templates.mozbuild') config = MockConfig() exports = {'DIST_SUBDIR': 'browser'} sandbox = TestedSandbox(Context(VARIABLES, config), metadata={ 'exports': exports, 'templates': template_sandbox.templates, }) self.assertEqual(sandbox['DIST_SUBDIR'], 'browser') # Templates should not interfere sandbox.exec_source('Template([])', 'foo.mozbuild') sandbox.exec_source('DIST_SUBDIR = "foo"') with self.assertRaises(SandboxExecutionError) as se: sandbox.exec_source('DIST_SUBDIR = "bar"') self.assertEqual(sandbox['DIST_SUBDIR'], 'foo') e = se.exception self.assertIsInstance(e.exc_value, KeyError) e = se.exception.exc_value self.assertEqual(e.args[0], 'global_ns') self.assertEqual(e.args[1], 'reassign') self.assertEqual(e.args[2], 'DIST_SUBDIR')
def sandbox(self): return Sandbox( Context( { "DIRS": (list, list, None), } ) )
def test_path_typed_hierarchy_list(self): config = self.config ctxt1 = Context(config=config) ctxt1.push_source(mozpath.join(config.topsrcdir, 'foo', 'moz.build')) ctxt2 = Context(config=config) ctxt2.push_source(mozpath.join(config.topsrcdir, 'bar', 'moz.build')) paths = [ '!../bar/qux', '!/qux/qux', '!qux', '../bar/qux', '/qux/qux', 'qux', ] MyList = ContextDerivedTypedHierarchicalStringList(Path) l = MyList(ctxt1) l += paths l.subdir += paths for _, files in l.walk(): for p_str, p_path in zip(paths, files): self.assertEqual(p_str, p_path) self.assertEqual(p_path, Path(ctxt1, p_str)) self.assertEqual(p_path.join('foo'), Path(ctxt1, mozpath.join(p_str, 'foo'))) l2 = MyList(ctxt2) l2 += paths l2.subdir += paths for _, files in l2.walk(): for p_str, p_path in zip(paths, files): self.assertEqual(p_str, p_path) self.assertEqual(p_path, Path(ctxt2, p_str)) # Assigning with Paths from another context doesn't rebase them l2 = MyList(ctxt2) l2 += l for _, files in l2.walk(): for p_str, p_path in zip(paths, files): self.assertEqual(p_str, p_path) self.assertEqual(p_path, Path(ctxt1, p_str))
def sandbox(self, data_path=None, metadata={}): config = None if data_path is not None: config = MockConfig(mozpath.join(test_data_path, data_path)) else: config = MockConfig() return TestedSandbox(Context(VARIABLES, config), metadata)
def test_aggregate_empty(self): c = Context({}) files = {'moz.build': Files(c, '**')} self.assertEqual(Files.aggregate(files), { 'bug_component_counts': [], 'recommended_bug_component': None, })
def test_invalid_utf8_substs(self): """Ensure invalid UTF-8 in substs is converted with an error.""" # This is really mbcs. It's a bunch of invalid UTF-8. config = MockConfig(extra_substs={'BAD_UTF8': b'\x83\x81\x83\x82\x3A'}) sandbox = MozbuildSandbox(Context(VARIABLES, config)) self.assertEqual(sandbox['CONFIG']['BAD_UTF8'], u'\ufffd\ufffd\ufffd\ufffd:')
def test_single_bug_component(self): c = Context({}) f = Files(c, '**') f['BUG_COMPONENT'] = (u'Product1', u'Component1') files = {'moz.build': f} self.assertEqual(Files.aggregate(files), { 'bug_component_counts': [((u'Product1', u'Component1'), 1)], 'recommended_bug_component': (u'Product1', u'Component1'), })
def test_absolute_path(self): config = self.config ctxt = Context(config=config) ctxt.push_source(mozpath.join(config.topsrcdir, "foo", "moz.build")) path = AbsolutePath(ctxt, "%/qux") self.assertEqual(path, "%/qux") self.assertEqual(path.full_path, "/qux") with self.assertRaises(ValueError): path = AbsolutePath(ctxt, "%qux")
def test_absolute_path(self): config = self.config ctxt = Context(config=config) ctxt.push_source(mozpath.join(config.topsrcdir, 'foo', 'moz.build')) path = AbsolutePath(ctxt, '%/qux') self.assertEqual(path, '%/qux') self.assertEqual(path.full_path, '/qux') with self.assertRaises(ValueError): path = AbsolutePath(ctxt, '%qux')
def test_aggregate_empty(self): c = Context({}) files = {"moz.build": Files(c, "**")} self.assertEqual( Files.aggregate(files), { "bug_component_counts": [], "recommended_bug_component": None, }, )
def test_value_checking(self): ns = Context(allowed_variables=VARIABLES) # Setting to a non-allowed type should not work. with self.assertRaises(ValueError) as ve: ns['HOGE'] = True e = ve.exception.args self.assertEqual(e[0], 'global_ns') self.assertEqual(e[1], 'set_type') self.assertEqual(e[2], 'HOGE') self.assertEqual(e[3], True) self.assertEqual(e[4], unicode)
def test_single_bug_component(self): c = Context({}) f = Files(c, "**") f["BUG_COMPONENT"] = ("Product1", "Component1") files = {"moz.build": f} self.assertEqual( Files.aggregate(files), { "bug_component_counts": [(("Product1", "Component1"), 1)], "recommended_bug_component": ("Product1", "Component1"), }, )
def test_value_checking(self): ns = Context(allowed_variables=VARIABLES) # Setting to a non-allowed type should not work. with self.assertRaises(ValueError) as ve: ns["HOGE"] = True e = ve.exception.args self.assertEqual(e[0], "global_ns") self.assertEqual(e[1], "set_type") self.assertEqual(e[2], "HOGE") self.assertEqual(e[3], True) self.assertEqual(e[4], six.text_type)
def test_multiple_patterns(self): c = Context({}) f1 = Files(c, 'a/**') f1['BUG_COMPONENT'] = (u'Product1', u'Component1') f2 = Files(c, 'b/**', 'a/bar') f2['BUG_COMPONENT'] = (u'Product2', u'Component2') files = {'a/foo': f1, 'a/bar': f2, 'b/foo': f2} self.assertEqual(Files.aggregate(files), { 'bug_component_counts': [ ((u'Product2', u'Component2'), 2), ((u'Product1', u'Component1'), 1), ], 'recommended_bug_component': (u'Product2', u'Component2'), })
def test_context_derived_typed_list(self): ns = Context(allowed_variables=VARIABLES) # Setting to a type that's rejected by coercion should not work. with self.assertRaises(ValueError): ns['HOGERA'] = [False] ns['HOGERA'] += ['a', 'b', 'c'] self.assertIsInstance(ns['HOGERA'], VARIABLES['HOGERA'][0]) for n in range(0, 3): self.assertIsInstance(ns['HOGERA'][n], Piyo) self.assertEqual(ns['HOGERA'][n].value, ['a', 'b', 'c'][n]) self.assertEqual(ns['HOGERA'][n].context, ns) with self.assertRaises(UnsortedError): ns['HOGERA'] += ['f', 'e', 'd']
def test_no_recommended_bug_component(self): """If there is no clear count winner, we don't recommend a bug component.""" c = Context({}) f1 = Files(c, '**') f1['BUG_COMPONENT'] = (u'Product1', u'Component1') f2 = Files(c, '**') f2['BUG_COMPONENT'] = (u'Product2', u'Component2') files = {'a': f1, 'b': f2} self.assertEqual(Files.aggregate(files), { 'bug_component_counts': [ ((u'Product1', u'Component1'), 1), ((u'Product2', u'Component2'), 1), ], 'recommended_bug_component': None, })
def test_context_derived_typed_list(self): ns = Context(allowed_variables=VARIABLES) # Setting to a type that's rejected by coercion should not work. with self.assertRaises(ValueError): ns["HOGERA"] = [False] ns["HOGERA"] += ["a", "b", "c"] self.assertIsInstance(ns["HOGERA"], VARIABLES["HOGERA"][0]) for n in range(0, 3): self.assertIsInstance(ns["HOGERA"][n], Piyo) self.assertEqual(ns["HOGERA"][n].value, ["a", "b", "c"][n]) self.assertEqual(ns["HOGERA"][n].context, ns) with self.assertRaises(UnsortedError): ns["HOGERA"] += ["f", "e", "d"]
def test_multiple_patterns(self): c = Context({}) f1 = Files(c, "a/**") f1["BUG_COMPONENT"] = ("Product1", "Component1") f2 = Files(c, "b/**", "a/bar") f2["BUG_COMPONENT"] = ("Product2", "Component2") files = {"a/foo": f1, "a/bar": f2, "b/foo": f2} self.assertEqual( Files.aggregate(files), { "bug_component_counts": [ (("Product2", "Component2"), 2), (("Product1", "Component1"), 1), ], "recommended_bug_component": ("Product2", "Component2"), }, )