class AddressMapTest(unittest.TestCase): _parser = JsonParser(SymbolTable({"thing": Thing})) @contextmanager def parse_address_map(self, json): path = "/dev/null" address_map = AddressMap.parse(path, json, self._parser, BuildFilePreludeSymbols(FrozenDict())) self.assertEqual(path, address_map.path) yield address_map def test_parse(self) -> None: with self.parse_address_map( dedent(""" { "type_alias": "thing", "name": "one", "age": 42 } { "type_alias": "thing", "name": "two", "age": 37 } """)) as address_map: self.assertEqual( { "one": Thing(name="one", age=42), "two": Thing(name="two", age=37) }, address_map.objects_by_name, ) def test_not_serializable(self) -> None: with self.assertRaises(UnaddressableObjectError): with self.parse_address_map("{}"): self.fail() def test_not_named(self) -> None: with self.assertRaises(UnaddressableObjectError): with self.parse_address_map('{"type_alias": "thing"}'): self.fail() def test_duplicate_names(self) -> None: with self.assertRaises(DuplicateNameError): with self.parse_address_map( '{"type_alias": "thing", "name": "one"}' '{"type_alias": "thing", "name": "one"}'): self.fail()
def test_no_import_sideeffects(self) -> None: # A parser with no symbols registered. parser = LegacyPythonCallbacksParser( SymbolTable({}), BuildFileAliases(), build_file_imports_behavior=BuildFileImportsBehavior.warn, ) # Call to import a module should succeed. parser.parse( "/dev/null", b"""import os; os.path.join('x', 'y')""", BuildFilePreludeSymbols(FrozenDict()), ) # But the imported module should not be visible as a symbol in further parses. with self.assertRaises(NameError): parser.parse("/dev/null", b"""os.path.join('x', 'y')""", BuildFilePreludeSymbols(FrozenDict()))
def _legacy_symbol_table( build_file_aliases: BuildFileAliases, registered_target_types: RegisteredTargetTypes) -> SymbolTable: """Construct a SymbolTable for the given BuildFileAliases.""" table = { alias: _make_target_adaptor(TargetAdaptor, target_type) for alias, target_type in build_file_aliases.target_types.items() } for alias, factory in build_file_aliases.target_macro_factories.items(): # TargetMacro.Factory with more than one target type is deprecated. # For default sources, this means that TargetMacro Factories with more than one target_type # will not parse sources through the engine, and will fall back to the legacy python sources # parsing. # Conveniently, multi-target_type TargetMacro.Factory, and legacy python source parsing, are # targeted to be removed in the same version of pants. if len(factory.target_types) == 1: table[alias] = _make_target_adaptor( TargetAdaptor, tuple(factory.target_types)[0], ) # Now, register any target types only declared in V2 without a V1 equivalent. table.update({ target_type.alias: TargetAdaptor for target_type in registered_target_types.types if target_type.alias not in table }) table["python_library"] = PythonTargetAdaptor table["jvm_app"] = JvmAppAdaptor table["jvm_binary"] = JvmBinaryAdaptor table["python_app"] = PythonAppAdaptor table["python_tests"] = PythonTestsAdaptor table["python_binary"] = PythonBinaryAdaptor table["remote_sources"] = RemoteSourcesAdaptor table["page"] = PageAdaptor table["pants_plugin"] = PantsPluginAdaptor table["contrib_plugin"] = PantsPluginAdaptor return SymbolTable(table)
self.default_repo = default_repo self.repos = repos @addressable(Exactly(Struct)) def default_repo(self): """""" @addressable_dict(Exactly(Struct)) def repos(self): """""" TEST_TABLE = SymbolTable({ "ApacheThriftConfig": ApacheThriftConfiguration, "Struct": Struct, "StructWithDeps": StructWithDeps, "PublishConfig": PublishConfiguration, "Target": Target, }) class GraphTestBase(unittest.TestCase, SchedulerTestBase): def create(self, build_patterns=None, parser=None) -> SchedulerSession: address_mapper = AddressMapper(build_patterns=build_patterns, parser=parser) @rule def symbol_table_singleton() -> SymbolTable: return TEST_TABLE rules = create_fs_rules() + create_graph_rules(address_mapper) + [
execution_options=DEFAULT_EXECUTION_OPTIONS, validate=validate, ) class Target(Struct): def __init__(self, name=None, configurations=None, **kwargs): super().__init__(name=name, **kwargs) self.configurations = configurations @addressable_sequence(SubclassesOf(Struct)) def configurations(self): pass TARGET_TABLE = SymbolTable({"struct": Struct, "target": Target}) def assert_equal_with_printing( test_case, expected, actual, uniform_formatter: Optional[Callable[[str], str]] = None): """Asserts equality, but also prints the values so they can be compared on failure. Usage: class FooTest(unittest.TestCase): assert_equal_with_printing = assert_equal_with_printing def test_foo(self):