def rule_runner() -> RuleRunner: return RuleRunner( rules=[ *archive_rules(), QueryRule(Digest, [CreateArchive]), QueryRule(ExtractedArchive, [Digest]), ], )
def rule_runner() -> RuleRunner: rule_runner = RuleRunner( rules=[ *archive_rules(), *shell_command_rules(), *source_files_rules(), *target_type_rules(), QueryRule(GeneratedSources, [GenerateFilesFromShellCommandRequest]), QueryRule(TransitiveTargets, [TransitiveTargetsRequest]), QueryRule(SourceFiles, [SourceFilesRequest]), ], target_types=[ShellCommand, ShellSourcesGeneratorTarget, Files], ) rule_runner.set_options([], env_inherit={"PATH"}) return rule_runner
def test_relocated_files() -> None: rule_runner = RuleRunner( rules=[ *target_type_rules(), *archive_rules(), *source_files_rules(), QueryRule(GeneratedSources, [RelocateFilesViaCodegenRequest]), QueryRule(TransitiveTargets, [TransitiveTargetsRequest]), QueryRule(SourceFiles, [SourceFilesRequest]), ], target_types=[FilesGeneratorTarget, RelocatedFiles], ) def assert_prefix_mapping( *, original: str, src: str, dest: str, expected: str, ) -> None: rule_runner.write_files({ original: "", "BUILD": dedent(f"""\ files(name="original", sources=[{repr(original)}]) relocated_files( name="relocated", files_targets=[":original"], src={repr(src)}, dest={repr(dest)}, ) """), }) tgt = rule_runner.get_target(Address("", target_name="relocated")) result = rule_runner.request( GeneratedSources, [RelocateFilesViaCodegenRequest(EMPTY_SNAPSHOT, tgt)]) assert result.snapshot.files == (expected, ) # We also ensure that when looking at the transitive dependencies of the `relocated_files` # target and then getting all the code of that closure, we only end up with the relocated # files. If we naively marked the original files targets as a typical `Dependencies` field, # we would hit this issue. transitive_targets = rule_runner.request( TransitiveTargets, [TransitiveTargetsRequest([tgt.address])]) all_sources = rule_runner.request( SourceFiles, [ SourceFilesRequest( (tgt.get(SourcesField) for tgt in transitive_targets.closure), enable_codegen=True, for_sources_types=(FileSourceField, ), ) ], ) assert all_sources.snapshot.files == (expected, ) # No-op. assert_prefix_mapping(original="old_prefix/f.ext", src="", dest="", expected="old_prefix/f.ext") assert_prefix_mapping( original="old_prefix/f.ext", src="old_prefix", dest="old_prefix", expected="old_prefix/f.ext", ) # Remove prefix. assert_prefix_mapping(original="old_prefix/f.ext", src="old_prefix", dest="", expected="f.ext") assert_prefix_mapping(original="old_prefix/subdir/f.ext", src="old_prefix", dest="", expected="subdir/f.ext") # Add prefix. assert_prefix_mapping(original="f.ext", src="", dest="new_prefix", expected="new_prefix/f.ext") assert_prefix_mapping( original="old_prefix/f.ext", src="", dest="new_prefix", expected="new_prefix/old_prefix/f.ext", ) # Replace prefix. assert_prefix_mapping( original="old_prefix/f.ext", src="old_prefix", dest="new_prefix", expected="new_prefix/f.ext", ) assert_prefix_mapping( original="old_prefix/f.ext", src="old_prefix", dest="new_prefix/subdir", expected="new_prefix/subdir/f.ext", ) # Replace prefix, but preserve a common start. assert_prefix_mapping( original="common_prefix/foo/f.ext", src="common_prefix/foo", dest="common_prefix/bar", expected="common_prefix/bar/f.ext", ) assert_prefix_mapping( original="common_prefix/subdir/f.ext", src="common_prefix/subdir", dest="common_prefix", expected="common_prefix/f.ext", )
def rule_runner() -> RuleRunner: return RuleRunner(rules=[ *archive_rules(), QueryRule(ExtractedDigest, (MaybeExtractable, )) ])
def rules(cls): return (*super().rules(), *archive_rules(), RootRule(Snapshot))