def test_multiple_snapshots_from_outside_buildroot(self): with temporary_dir() as temp_dir: with open(os.path.join(temp_dir, "roland"), "w") as f: f.write("European Burmese") with open(os.path.join(temp_dir, "susannah"), "w") as f: f.write("I don't know") scheduler = self.mk_scheduler(rules=create_fs_rules()) snapshots = scheduler.capture_snapshots(( PathGlobsAndRoot(PathGlobs(("roland", ), ()), temp_dir), PathGlobsAndRoot(PathGlobs(("susannah", ), ()), temp_dir), PathGlobsAndRoot(PathGlobs(("doesnotexist", ), ()), temp_dir), )) self.assertEquals(3, len(snapshots)) self.assert_snapshot_equals( snapshots[0], ["roland"], DirectoryDigest( str("63949aa823baf765eff07b946050d76ec0033144c785a94d3ebd82baa931cd16" ), 80)) self.assert_snapshot_equals( snapshots[1], ["susannah"], DirectoryDigest( str("d3539cfc21eb4bab328ca9173144a8e932c515b1b9e26695454eeedbc5a95f6f" ), 82)) self.assert_snapshot_equals(snapshots[2], [], EMPTY_DIRECTORY_DIGEST)
def test_write_file(self): request = ExecuteProcessRequest( argv=("/bin/bash", "-c", "echo -n 'European Burmese' > roland"), description="echo roland", output_files=("roland", ), input_files=EMPTY_DIRECTORY_DIGEST, ) execute_process_result = self.scheduler.product_request( ExecuteProcessResult, [request], )[0] self.assertEqual( execute_process_result.output_directory_digest, DirectoryDigest( fingerprint=text_type( "63949aa823baf765eff07b946050d76ec0033144c785a94d3ebd82baa931cd16" ), serialized_bytes_length=80, )) files_content_result = self.scheduler.product_request( FilesContent, [execute_process_result.output_directory_digest], )[0] self.assertEqual(files_content_result.dependencies, (FileContent("roland", b"European Burmese"), ))
def test_write_file(self): scheduler = self.mk_scheduler_in_example_fs(()) request = ExecuteProcessRequest.create_with_empty_snapshot( ("/bin/bash", "-c", "echo -n 'European Burmese' > roland"), dict(), ("roland", )) execute_process_result = self.execute_expecting_one_result( scheduler, ExecuteProcessResult, request).value self.assertEquals( execute_process_result.output_directory_digest, DirectoryDigest( fingerprint=str( "63949aa823baf765eff07b946050d76ec0033144c785a94d3ebd82baa931cd16" ), serialized_bytes_length=80, )) files_content_result = self.execute_expecting_one_result( scheduler, FilesContent, execute_process_result.output_directory_digest).value self.assertEquals((files_content_result.dependencies), (FileContent("roland", "European Burmese"), ))
def test_empty(self): """Test that parsing an empty BUILD file results in an empty AddressFamily.""" address_mapper = AddressMapper(JsonParser(TestTable())) af = run_rule(parse_address_family, address_mapper, Dir('/dev/null'), { (Snapshot, PathGlobs): lambda _: Snapshot(DirectoryDigest('abc', 10), (File('/dev/null/BUILD'),)), (FilesContent, DirectoryDigest): lambda _: FilesContent([FileContent('/dev/null/BUILD', b'')]), }) self.assertEqual(len(af.objects_by_name), 0)
def test_snapshot_from_outside_buildroot(self): with temporary_dir() as temp_dir: with open(os.path.join(temp_dir, "roland"), "w") as f: f.write("European Burmese") scheduler = self.mk_scheduler(rules=create_fs_rules()) globs = PathGlobs(("*",), ()) snapshot = scheduler.capture_snapshots((PathGlobsAndRoot(globs, text_type(temp_dir)),))[0] self.assert_snapshot_equals(snapshot, ["roland"], DirectoryDigest( text_type("63949aa823baf765eff07b946050d76ec0033144c785a94d3ebd82baa931cd16"), 80 ))
def prime_store_with_roland_digest(self): """This method primes the store with a directory of a file named 'roland' and contents 'European Burmese'.""" with temporary_dir() as temp_dir: with open(os.path.join(temp_dir, "roland"), "w") as f: f.write("European Burmese") scheduler = self.mk_scheduler(rules=create_fs_rules()) globs = PathGlobs(("*",), ()) snapshot = scheduler.capture_snapshots((PathGlobsAndRoot(globs, text_type(temp_dir)),))[0] self.assert_snapshot_equals(snapshot, ["roland"], DirectoryDigest( text_type("63949aa823baf765eff07b946050d76ec0033144c785a94d3ebd82baa931cd16"), 80 ))
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_duplicated(self): """Test that matching the same Spec twice succeeds.""" address = SingleAddress('a', 'a') snapshot = Snapshot(DirectoryDigest('xx', 2), (Path('a/BUILD', File('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 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_materialize_directories(self): # I tried passing in the digest of a file, but it didn't make it to the # rust code due to all of the checks we have in place (which is probably a good thing). self.prime_store_with_roland_digest() with temporary_dir() as temp_dir: dir_path = os.path.join(temp_dir, "containing_roland") digest = DirectoryDigest( text_type("63949aa823baf765eff07b946050d76ec0033144c785a94d3ebd82baa931cd16"), 80 ) scheduler = self.mk_scheduler(rules=create_fs_rules()) scheduler.materialize_directories((DirectoryToMaterialize(text_type(dir_path), digest),)) created_file = os.path.join(dir_path, "roland") with open(created_file) as f: content = f.read() self.assertEquals(content, "European Burmese")
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 test_passes_eager_fileset_with_spec_through(self): self.create_file('foo/foo/a.txt', 'a_contents') fileset = self.sources_for(['foo/a.txt'], 'foo') sf = SourcesField(sources=fileset) self.assertIs(fileset, sf.sources) self.assertEqual(['foo/a.txt'], list(sf.source_paths)) self.assertEqual(['foo/foo/a.txt'], list(sf.relative_to_buildroot())) digest = str( '56001a7e48555f156420099a99da60a7a83acc90853046709341bf9f00a6f944') want_snapshot = Snapshot(DirectoryDigest( digest, 77), (Path('foo/foo/a.txt', stat=File('foo/foo/a.txt')), )) # We explicitly pass a None scheduler because we expect no scheduler lookups to be required # in order to get a Snapshot. self.assertEqual(sf.snapshot(scheduler=None), want_snapshot)
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 _snapshot(self): return Snapshot(DirectoryDigest('xx', 2), (Path('root/BUILD', File('root/BUILD')), ))