def test_duplicate_names(self) -> None: with self.assertRaises(DuplicateNameError): AddressFamily.create( "name/space", [ AddressMap("name/space/0", {"one": Thing(name="one", age=42)}), AddressMap("name/space/1", {"one": Thing(name="one", age=37)}), ], )
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)} ), ], )
async def parse_address_family(address_mapper: AddressMapper, prelude_symbols: BuildFilePreludeSymbols, directory: Dir) -> AddressFamily: """Given an AddressMapper and a directory, return an AddressFamily. The AddressFamily may be empty, but it will not be None. """ path_globs = PathGlobs(globs=( *(os.path.join(directory.path, p) for p in address_mapper.build_patterns), *(f"!{p}" for p in address_mapper.build_ignore_patterns), )) snapshot = await Get[Snapshot](PathGlobs, path_globs) files_content = await Get[FilesContent](Digest, snapshot.digest) if not files_content: raise ResolveError( 'Directory "{}" does not contain any BUILD files.'.format( directory.path)) address_maps = [] for filecontent_product in files_content: address_maps.append( AddressMap.parse( filecontent_product.path, filecontent_product.content, address_mapper.parser, prelude_symbols, )) return AddressFamily.create(directory.path, address_maps)
async def parse_address_family( parser: Parser, build_file_options: BuildFileOptions, prelude_symbols: BuildFilePreludeSymbols, directory: AddressFamilyDir, ) -> AddressFamily: """Given an AddressMapper and a directory, return an AddressFamily. The AddressFamily may be empty, but it will not be None. """ digest_contents = await Get( DigestContents, PathGlobs(globs=( *(os.path.join(directory.path, p) for p in build_file_options.patterns), *(f"!{p}" for p in build_file_options.ignores), )), ) if not digest_contents: raise ResolveError( f"Directory '{directory.path}' does not contain any BUILD files.") address_maps = [ AddressMap.parse(fc.path, fc.content.decode(), parser, prelude_symbols) for fc in digest_contents ] return AddressFamily.create(directory.path, address_maps)
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_exclude_pattern_with_single_address(self) -> None: """Test that single address targets are filtered based on exclude patterns.""" address_specs = AddressSpecs( [SingleAddress("root", "not_me")], exclude_patterns=tuple(["root.*"]) ) address_family = AddressFamily("root", {"not_me": ("root/BUILD", TargetAdaptor())}) targets = self._resolve_addresses( address_specs, address_family, self._snapshot(), self._address_mapper() ) assert len(targets.dependencies) == 0
def test_duplicated(self) -> None: """Test that matching the same AddressSpec twice succeeds.""" address = SingleAddress("a", "a") snapshot = Snapshot(Digest("xx", 2), ("a/BUILD",), ()) address_family = AddressFamily("a", {"a": ("a/BUILD", "this is an object!")}) address_specs = AddressSpecs([address, address]) addresses = self._resolve_addresses( address_specs, address_family, snapshot, self._address_mapper() ) assert len(addresses) == 1 assert addresses[0].spec == "a:a"
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_create_multiple(self) -> None: address_family = AddressFamily.create( "name/space", [ AddressMap("name/space/0", {"one": Thing(name="one", age=42)}), AddressMap("name/space/1", {"two": Thing(name="two", age=37)}), ], ) self.assertEqual("name/space", address_family.namespace) self.assertEqual( { Address.parse("name/space:one"): Thing(name="one", age=42), Address.parse("name/space:two"): Thing(name="two", age=37), }, address_family.addressables, )
def test_tag_filter(self) -> None: """Test that targets are filtered based on `tags`.""" address_specs = AddressSpecs([SiblingAddresses("root")], tags=["+integration"]) address_family = AddressFamily( "root", { "a": ("root/BUILD", TargetAdaptor()), "b": ("root/BUILD", TargetAdaptor(tags={"integration"})), "c": ("root/BUILD", TargetAdaptor(tags={"not_integration"})), }, ) targets = self._resolve_addresses( address_specs, address_family, self._snapshot(), self._address_mapper() ) assert len(targets) == 1 assert targets[0].spec == "root:b"
def test_exclude_pattern(self) -> None: """Test that targets are filtered based on exclude patterns.""" address_specs = AddressSpecs([SiblingAddresses("root")], exclude_patterns=tuple([".exclude*"])) address_family = AddressFamily( "root", { "exclude_me": ("root/BUILD", TargetAdaptor()), "not_me": ("root/BUILD", TargetAdaptor()), }, ) targets = self._resolve_addresses(address_specs, address_family, self._snapshot(), self._address_mapper()) assert len(targets) == 1 assert targets[0].spec == "root:not_me"
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_create_single(self) -> None: address_family = AddressFamily.create( "", [ AddressMap( "0", { "one": Thing(name="one", age=42), "two": Thing(name="two", age=37) }) ], ) self.assertEqual("", address_family.namespace) self.assertEqual( { Address.parse("//:one"): Thing(name="one", age=42), Address.parse("//:two"): Thing(name="two", age=37), }, address_family.addressables, )
def test_fails_on_nonexistent_specs(self) -> None: """Test that address specs referring to nonexistent targets raise a ResolveError.""" address_family = AddressFamily("root", {"a": ("root/BUILD", TargetAdaptor())}) 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 self.assertRaisesRegex(ResolveError, expected_rx_str): self._resolve_addresses( address_specs, address_family, self._snapshot(), self._address_mapper() ) # 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 self.assertRaisesRegex(ResolveError, expected_rx_str): self._resolve_addresses( address_specs, address_family, self._snapshot(), self._address_mapper() )
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 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 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_create_empty(self) -> None: # Case where directory exists but is empty. address_family = AddressFamily.create("name/space", []) self.assertEqual(dict(), address_family.addressables)
def test_address_family_mismatching_paths() -> None: with pytest.raises(DifferingFamiliesError): AddressFamily.create("one", [ AddressMap("/dev/null/one/0", {}), AddressMap("/dev/null/two/0", {}) ])
def test_address_family_create_empty() -> None: # Case where directory exists but is empty. address_family = AddressFamily.create("name/space", []) assert {} == address_family.addresses_to_target_adaptors assert () == address_family.build_file_addresses
def test_mismatching_paths(self) -> None: with self.assertRaises(DifferingFamiliesError): AddressFamily.create("one", [ AddressMap("/dev/null/one/0", {}), AddressMap("/dev/null/two/0", {}) ])