def test_address_family_duplicate_names() -> None: with pytest.raises(DuplicateNameError): AddressFamily.create( "name/space", [ AddressMap( "name/space/0", {"one": TargetAdaptor(type_alias="thing", name="one", age=42)} ), AddressMap( "name/space/1", {"one": TargetAdaptor(type_alias="thing", name="one", age=37)} ), ], )
def test_address_map_parse() -> None: address_map = parse_address_map( dedent(""" thing( name="one", age=42, ) thing( name="two", age=37, ) """)) assert { "one": TargetAdaptor(type_alias="thing", name="one", age=42), "two": TargetAdaptor(type_alias="thing", name="two", age=37), } == address_map.name_to_target_adaptor
def test_address_family_create_multiple() -> None: address_family = AddressFamily.create( "name/space", [ AddressMap( "name/space/0", {"one": TargetAdaptor(type_alias="thing", name="one", age=42)} ), AddressMap( "name/space/1", {"two": TargetAdaptor(type_alias="thing", name="two", age=37)} ), ], ) assert "name/space" == address_family.namespace assert { Address.parse("name/space:one"): TargetAdaptor(type_alias="thing", name="one", age=42), Address.parse("name/space:two"): TargetAdaptor(type_alias="thing", name="two", age=37), } == dict(address_family.addresses_to_target_adaptors.items())
def test_address_family_create_single() -> None: address_family = AddressFamily.create( "", [ AddressMap( "0", { "one": TargetAdaptor(type_alias="thing", name="one", age=42), "two": TargetAdaptor(type_alias="thing", name="two", age=37), }, ) ], ) assert "" == address_family.namespace assert { Address.parse("//:one"): TargetAdaptor(type_alias="thing", name="one", age=42), Address.parse("//:two"): TargetAdaptor(type_alias="thing", name="two", age=37), } == dict(address_family.addresses_to_target_adaptors.items())
def test_address_specs_exclude_pattern_with_single_address() -> None: """Test that single address targets are filtered based on exclude patterns.""" address_specs = AddressSpecs([SingleAddress("root", "not_me")], filter_by_global_options=True) address_family = AddressFamily("root", { "not_me": ("root/BUILD", TargetAdaptor(type_alias="", name="not_me")) }) assert not resolve_addresses_with_origins_from_address_specs( address_specs, address_family, exclude_patterns=["root.*"])
def test_address_specs_exclude_pattern() -> None: """Test that targets are filtered based on exclude patterns.""" address_specs = AddressSpecs([SiblingAddresses("root")], filter_by_global_options=True) address_family = AddressFamily( "root", { "exclude_me": ("root/BUILD", TargetAdaptor(type_alias="", name="exclude_me")), "not_me": ("root/BUILD", TargetAdaptor(type_alias="", name="not_me")), }, ) addresses_with_origins = resolve_addresses_with_origins_from_address_specs( address_specs, address_family, exclude_patterns=[".exclude*"]) assert len(addresses_with_origins) == 1 awo = addresses_with_origins[0] assert str(awo.address) == "root:not_me" assert awo.origin == SiblingAddresses("root")
def __call__(self, **kwargs: Any) -> TargetAdaptor: # Target names default to the name of the directory their BUILD file is in # (as long as it's not the root directory). if "name" not in kwargs: if not parse_state.rel_path(): raise UnaddressableObjectError( "Targets in root-level BUILD files must be named explicitly." ) kwargs["name"] = None target_adaptor = TargetAdaptor(self._type_alias, **kwargs) parse_state.add(target_adaptor) return target_adaptor
def __call__(self, *args, **kwargs): # Target names default to the name of the directory their BUILD file is in # (as long as it's not the root directory). if "name" not in kwargs: dirname = os.path.basename(self._parse_context.rel_path) if not dirname: raise UnaddressableObjectError( "Targets in root-level BUILD files must be named explicitly." ) kwargs["name"] = dirname kwargs.setdefault("type_alias", self._type_alias) target_adaptor = TargetAdaptor(**kwargs) self._parse_context._storage.add(target_adaptor) return target_adaptor
def test_address_specs_duplicated() -> None: """Test that matching the same AddressSpec twice succeeds.""" address_spec = SingleAddress("root", "root") address_family = AddressFamily( "root", {"root": ("root/BUILD", TargetAdaptor(type_alias="", name="root"))}) address_specs = AddressSpecs([address_spec, address_spec]) addresses_with_origins = resolve_addresses_with_origins_from_address_specs( address_specs, address_family) assert len(addresses_with_origins) == 1 awo = addresses_with_origins[0] assert str(awo.address) == "root" assert awo.origin == address_spec
def test_address_specs_tag_filter() -> None: """Test that targets are filtered based on `tags`.""" address_specs = AddressSpecs([SiblingAddresses("root")], filter_by_global_options=True) address_family = AddressFamily( "root", { "a": ("root/BUILD", TargetAdaptor(type_alias="", name="a")), "b": ("root/BUILD", TargetAdaptor(type_alias="", name="b", tags={"integration"})), "c": ("root/BUILD", TargetAdaptor(type_alias="", name="c", tags={"not_integration"})), }, ) addresses_with_origins = resolve_addresses_with_origins_from_address_specs( address_specs, address_family, tags=["+integration"]) assert len(addresses_with_origins) == 1 awo = addresses_with_origins[0] assert str(awo.address) == "root:b" assert awo.origin == SiblingAddresses("root")
def test_address_specs_fail_on_nonexistent() -> None: """Test that address specs referring to nonexistent targets raise a ResolveError.""" address_family = AddressFamily( "root", {"a": ("root/BUILD", TargetAdaptor(type_alias="", name="a"))}) address_specs = AddressSpecs( [SingleAddress("root", "b"), SingleAddress("root", "a")]) expected_rx_str = re.escape( "'b' was not found in namespace 'root'. Did you mean one of:\n :a") with pytest.raises(ResolveError, match=expected_rx_str): resolve_addresses_with_origins_from_address_specs( address_specs, address_family) # Ensure that we still catch nonexistent targets later on in the list of command-line # address specs. address_specs = AddressSpecs( [SingleAddress("root", "a"), SingleAddress("root", "b")]) with pytest.raises(ResolveError, match=expected_rx_str): resolve_addresses_with_origins_from_address_specs( address_specs, address_family)
def make_target(target_name: str, **kwargs) -> TargetAdaptor: parsed_address = Address("", target_name=target_name) return TargetAdaptor(type_alias="", name=parsed_address.target_name, address=parsed_address, **kwargs)