def _legacy_symbol_table(build_file_aliases): """Construct a SymbolTable for the given BuildFileAliases. :param build_file_aliases: BuildFileAliases to register. :type build_file_aliases: :class:`pants.build_graph.build_file_aliases.BuildFileAliases` :returns: A SymbolTable. """ 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], ) # TODO: The alias replacement here is to avoid elevating "TargetAdaptors" into the public # API until after https://github.com/pantsbuild/pants/issues/3560 has been completed. # These should likely move onto Target subclasses as the engine gets deeper into beta # territory. 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 # Note that these don't call _make_target_adaptor because we don't have a handy reference to the # types being constructed. They don't have any default_sources behavior, so this should be ok, # but if we end up doing more things in _make_target_adaptor, we should make sure they're # applied here too. table['pants_plugin'] = PantsPluginAdaptor table['contrib_plugin'] = PantsPluginAdaptor return SymbolTable(table)
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) self.assertEqual(path, address_map.path) yield address_map def test_parse(self): 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): with self.assertRaises(UnaddressableObjectError): with self.parse_address_map('{}'): self.fail() def test_not_named(self): with self.assertRaises(UnaddressableObjectError): with self.parse_address_map('{"type_alias": "thing"}'): self.fail() def test_duplicate_names(self): with self.assertRaises(DuplicateNameError): with self.parse_address_map( '{"type_alias": "thing", "name": "one"}' '{"type_alias": "thing", "name": "one"}'): self.fail()
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)
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_list(SubclassesOf(Struct)) def configurations(self): pass TARGET_TABLE = SymbolTable({'struct': Struct, 'target': Target}) def assert_equal_with_printing(test_case, expected, actual): """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): self.assert_equal_with_printing("a", "b") """ str_actual = str(actual) print('Expected:')
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):
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): address_mapper = AddressMapper(build_patterns=build_patterns, parser=parser) @rule(SymbolTable, []) def symbol_table_singleton(): return TEST_TABLE rules = create_fs_rules() + create_graph_rules(address_mapper) + [
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) + [