def test_duplicate_names(self): 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_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 parse_address_family(address_mapper, directory): """Given an AddressMapper and a directory, return an AddressFamily. The AddressFamily may be empty, but it will not be None. """ patterns = tuple( join(directory.path, p) for p in address_mapper.build_patterns) path_globs = PathGlobs.create('', include=patterns, exclude=address_mapper.build_ignore_patterns) snapshot = yield Get(Snapshot, PathGlobs, path_globs) files_content = yield Get(FilesContent, DirectoryDigest, snapshot.directory_digest) if not files_content: raise ResolveError( 'Directory "{}" does not contain build files.'.format( directory.path)) address_maps = [] for filecontent_product in files_content.dependencies: address_maps.append( AddressMap.parse(filecontent_product.path, filecontent_product.content, address_mapper.parser)) yield AddressFamily.create(directory.path, address_maps)
async def parse_address_family(address_mapper: AddressMapper, 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.directory_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 ) ) return AddressFamily.create(directory.path, address_maps)
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: :a""") with self.assertRaisesRegex(ResolveError, expected_rx_str): self._resolve_build_file_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_build_file_addresses(address_specs, address_family, self._snapshot(), self._address_mapper())
def test_create_single(self): 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_create_multiple(self): 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_duplicated(self): """Test that matching the same Spec twice succeeds.""" address = SingleAddress('a', 'a') snapshot = Snapshot(Digest('xx', 2), ('a/BUILD',), ()) address_family = AddressFamily('a', {'a': ('a/BUILD', 'this is an object!')}) specs = Specs([address, address]) bfas = self._resolve_build_file_addresses( specs, address_family, snapshot, self._address_mapper()) self.assertEqual(len(bfas.dependencies), 1) self.assertEqual(bfas.dependencies[0].spec, 'a:a')
def parse_address_family(address_mapper, path, build_files_content): """Given the contents of the build files in one directory, return an AddressFamily. The AddressFamily may be empty, but it will not be None. """ address_maps = [] for filepath, filecontent in build_files_content.dependencies: address_maps.append(AddressMap.parse(filepath, filecontent, address_mapper.symbol_table_cls, address_mapper.parser_cls)) return AddressFamily.create(path.path, address_maps)
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()) self.assertEqual(len(targets.dependencies), 0)
def parse_address_family(address_mapper, path, build_files_content): """Given the contents of the build files in one directory, return an AddressFamily. The AddressFamily may be empty, but it will not be None. """ address_maps = [] for filepath, filecontent in build_files_content.dependencies: address_maps.append( AddressMap.parse(filepath, filecontent, address_mapper.symbol_table_cls, address_mapper.parser_cls)) return AddressFamily.create(path.path, address_maps)
def test_exclude_pattern_with_single_address(self): """Test that single address targets are filtered based on exclude patterns.""" specs = Specs([SingleAddress('root', 'not_me')], exclude_patterns=tuple(['root.*'])) address_family = AddressFamily('root', { 'not_me': ('root/BUILD', TargetAdaptor()), } ) targets = self._resolve_build_file_addresses( specs, address_family, self._snapshot(), self._address_mapper()) self.assertEqual(len(targets.dependencies), 0)
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_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()) self.assertEqual(len(addresses.dependencies), 1) self.assertEqual(addresses.dependencies[0].spec, "a:a")
def test_duplicated(self): """Test that matching the same Spec twice succeeds.""" address = SingleAddress('a', 'a') address_mapper = AddressMapper(JsonParser(TestTable())) snapshot = Snapshot(DirectoryDigest(str('xx'), 2), (Path('a/BUILD', File('a/BUILD')),)) address_family = AddressFamily('a', {'a': ('a/BUILD', 'this is an object!')}) bfas = run_rule(addresses_from_address_families, address_mapper, Specs([address, address]), { (Snapshot, PathGlobs): lambda _: snapshot, (AddressFamily, Dir): lambda _: address_family, }) self.assertEquals(len(bfas.dependencies), 1) self.assertEquals(bfas.dependencies[0].spec, 'a:a')
def test_exclude_pattern(self): """Test that targets are filtered based on exclude patterns.""" specs = Specs([SiblingAddresses('root')], exclude_patterns=tuple(['.exclude*'])) address_family = AddressFamily('root', {'exclude_me': ('root/BUILD', TargetAdaptor()), 'not_me': ('root/BUILD', TargetAdaptor()), } ) targets = self._resolve_build_file_addresses( specs, address_family, self._snapshot(), self._address_mapper()) self.assertEqual(len(targets.dependencies), 1) self.assertEqual(targets.dependencies[0].spec, 'root:not_me')
def parse_address_family(address_mapper, path, build_files): """Given the contents of the build files in one directory, return an AddressFamily. The AddressFamily may be empty, but it will not be None. """ files_content = build_files.files_content.dependencies if not files_content: raise ResolveError('Directory "{}" does not contain build files.'.format(path)) address_maps = [] for filecontent_product in files_content: address_maps.append(AddressMap.parse(filecontent_product.path, filecontent_product.content, address_mapper.parser)) return AddressFamily.create(path.path, address_maps)
def test_tag_filter(self): """Test that targets are filtered based on `tags`.""" specs = Specs([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_build_file_addresses( specs, address_family, self._snapshot(), self._address_mapper()) self.assertEqual(len(targets.dependencies), 1) self.assertEqual(targets.dependencies[0].spec, 'root:b')
def test_exclude_pattern_with_single_address(self): """Test that single address targets are filtered based on exclude patterns.""" spec = SingleAddress('root', 'not_me') address_mapper = AddressMapper(JsonParser(TestTable())) snapshot = Snapshot(DirectoryDigest('xx', 2), (Path('root/BUILD', File('root/BUILD')),)) address_family = AddressFamily('root', { 'not_me': ('root/BUILD', TargetAdaptor()), } ) targets = run_rule( addresses_from_address_families, address_mapper, Specs([spec], exclude_patterns=tuple(['root.*'])),{ (Snapshot, PathGlobs): lambda _: snapshot, (AddressFamily, Dir): lambda _: address_family, }) self.assertEqual(len(targets.dependencies), 0)
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 parse_address_family(address_mapper, path, build_files): """Given the contents of the build files in one directory, return an AddressFamily. The AddressFamily may be empty, but it will not be None. """ files_content = build_files.files_content.dependencies if not files_content: raise ResolveError('Directory "{}" does not contain build files.'.format(path)) address_maps = [] paths = (f.path for f in files_content) ignored_paths = set(address_mapper.build_ignore_patterns.match_files(paths)) for filecontent_product in files_content: if filecontent_product.path in ignored_paths: continue address_maps.append(AddressMap.parse(filecontent_product.path, filecontent_product.content, address_mapper.parser)) return AddressFamily.create(path.path, address_maps)
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()) self.assertEqual(len(targets.dependencies), 1) self.assertEqual(targets.dependencies[0].spec, "root:not_me")
def test_exclude_pattern(self): """Test that targets are filtered based on exclude patterns.""" spec = SiblingAddresses('root') address_mapper = AddressMapper(JsonParser(TestTable())) snapshot = Snapshot(DirectoryDigest(text_type('xx'), 2), (Path('root/BUILD', File('root/BUILD')), )) address_family = AddressFamily( 'root', { 'exclude_me': ('root/BUILD', TargetAdaptor()), 'not_me': ('root/BUILD', TargetAdaptor()), }) targets = run_rule( addresses_from_address_families, address_mapper, Specs([spec], exclude_patterns=tuple(['.exclude*'])), { (Snapshot, PathGlobs): lambda _: snapshot, (AddressFamily, Dir): lambda _: address_family, }) self.assertEquals(len(targets.dependencies), 1) self.assertEquals(targets.dependencies[0].spec, 'root:not_me')
def parse_address_family(address_mapper, path, build_files_content): """Given the contents of the build files in one directory, return an AddressFamily. The AddressFamily may be empty, but it will not be None. """ if not build_files_content.dependencies: raise ResolveError('Directory "{}" does not contain build files.'.format(path)) address_maps = [] for filecontent_product in build_files_content.dependencies: address_maps.append( AddressMap.parse( filecontent_product.path, filecontent_product.content, address_mapper.symbol_table_cls, address_mapper.parser_cls, address_mapper.exclude_patterns, ) ) return AddressFamily.create(path.path, address_maps)
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()) self.assertEqual(len(targets.dependencies), 1) self.assertEqual(targets.dependencies[0].spec, "root:b")
def parse_address_family(address_mapper, directory): """Given an AddressMapper and a directory, return an AddressFamily. The AddressFamily may be empty, but it will not be None. """ patterns = tuple(join(directory.path, p) for p in address_mapper.build_patterns) path_globs = PathGlobs(include=patterns, exclude=address_mapper.build_ignore_patterns) snapshot = yield Get(Snapshot, PathGlobs, path_globs) files_content = yield Get(FilesContent, DirectoryDigest, snapshot.directory_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.dependencies: address_maps.append(AddressMap.parse(filecontent_product.path, filecontent_product.content, address_mapper.parser)) yield AddressFamily.create(directory.path, address_maps)
def parse_address_family(address_mapper, path, build_files): """Given the contents of the build files in one directory, return an AddressFamily. The AddressFamily may be empty, but it will not be None. """ files_content = build_files.files_content.dependencies if not files_content: raise ResolveError('Directory "{}" does not contain build files.'.format(path)) address_maps = [] paths = (f.path for f in files_content) ignored_paths = set(address_mapper.build_ignore_patterns.match_files(paths)) for filecontent_product in files_content: if filecontent_product.path in ignored_paths: continue address_maps.append(AddressMap.parse(filecontent_product.path, filecontent_product.content, address_mapper.symbol_table_cls, address_mapper.parser_cls, address_mapper.exclude_patterns)) return AddressFamily.create(path.path, address_maps)
def test_tag_filter(self): """Test that targets are filtered based on `tags`.""" spec = SiblingAddresses('root') address_mapper = AddressMapper(JsonParser(TestTable())) snapshot = Snapshot(DirectoryDigest(str('xx'), 2), (Path('root/BUILD', File('root/BUILD')),)) address_family = AddressFamily('root', {'a': ('root/BUILD', TargetAdaptor()), 'b': ('root/BUILD', TargetAdaptor(tags={'integration'})), 'c': ('root/BUILD', TargetAdaptor(tags={'not_integration'})) } ) targets = run_rule( addresses_from_address_families, address_mapper, Specs([spec], tags=['+integration']), { (Snapshot, PathGlobs): lambda _: snapshot, (AddressFamily, Dir): lambda _: address_family, }) self.assertEquals(len(targets.dependencies), 1) self.assertEquals(targets.dependencies[0].spec, 'root:b')
def test_mismatching_paths(self): with self.assertRaises(DifferingFamiliesError): AddressFamily.create('one', [AddressMap('/dev/null/one/0', {}), AddressMap('/dev/null/two/0', {})])
def test_create_empty(self): # Case where directory exists but is empty. address_family = AddressFamily.create('name/space', []) self.assertEquals(dict(), address_family.addressables)
def test_mismatching_paths(self) -> None: with self.assertRaises(DifferingFamiliesError): AddressFamily.create( "one", [AddressMap("/dev/null/one/0", {}), AddressMap("/dev/null/two/0", {})] )