use of com.facebook.buck.rules.FakeBuildableContext in project buck by facebook.
the class GenruleTest method testCreateAndRunGenrule.
@Test
public void testCreateAndRunGenrule() throws IOException, NoSuchBuildTargetException {
/*
* Programmatically build up a Genrule that corresponds to:
*
* genrule(
* name = 'katana_manifest',
* srcs = [
* 'convert_to_katana.py',
* 'AndroidManifest.xml',
* ],
* cmd = 'python $SRCDIR/* > $OUT',
* out = 'AndroidManifest.xml',
* )
*/
BuildRuleResolver ruleResolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(ruleResolver));
createSampleJavaBinaryRule(ruleResolver);
// From the Python object, create a GenruleBuildRuleFactory to create a Genrule.Builder
// that builds a Genrule from the Python object.
// BuildTargetParser parser = BuildTargetParser.INSTANCE;
// EasyMock.expect(parser.parse(EasyMock.eq("//java/com/facebook/util:util"),
// EasyMock.anyObject(BuildTargetPatternParser.class)))
// .andStubReturn(BuildTargetFactory.newInstance("//java/com/facebook/util:util"));
// EasyMock.replay(parser);
BuildTarget buildTarget = BuildTargetFactory.newInstance(filesystem.getRootPath(), "//src/com/facebook/katana:katana_manifest");
BuildRule genrule = GenruleBuilder.newGenruleBuilder(buildTarget).setBash("python convert_to_katana.py AndroidManifest.xml > $OUT").setCmdExe("python convert_to_katana.py AndroidManifest.xml > %OUT%").setOut("AndroidManifest.xml").setSrcs(ImmutableList.of(new PathSourcePath(filesystem, filesystem.getPath("src/com/facebook/katana/convert_to_katana.py")), new PathSourcePath(filesystem, filesystem.getPath("src/com/facebook/katana/AndroidManifest.xml")))).build(ruleResolver, filesystem);
// Verify all of the observers of the Genrule.
assertEquals(filesystem.getBuckPaths().getGenDir().resolve("src/com/facebook/katana/katana_manifest/AndroidManifest.xml"), pathResolver.getRelativePath(genrule.getSourcePathToOutput()));
assertEquals(filesystem.resolve(filesystem.getBuckPaths().getGenDir().resolve("src/com/facebook/katana/katana_manifest/AndroidManifest.xml")).toString(), ((Genrule) genrule).getAbsoluteOutputFilePath(pathResolver));
BuildContext buildContext = FakeBuildContext.withSourcePathResolver(pathResolver);
ImmutableList<Path> inputsToCompareToOutputs = ImmutableList.of(filesystem.getPath("src/com/facebook/katana/convert_to_katana.py"), filesystem.getPath("src/com/facebook/katana/AndroidManifest.xml"));
assertEquals(inputsToCompareToOutputs, pathResolver.filterInputsToCompareToOutput(((Genrule) genrule).getSrcs()));
// Verify that the shell commands that the genrule produces are correct.
List<Step> steps = genrule.getBuildSteps(buildContext, new FakeBuildableContext());
assertEquals(6, steps.size());
ExecutionContext executionContext = newEmptyExecutionContext();
Step firstStep = steps.get(0);
assertTrue(firstStep instanceof MakeCleanDirectoryStep);
MakeCleanDirectoryStep mkdirCommand = (MakeCleanDirectoryStep) firstStep;
Path pathToOutDir = filesystem.getBuckPaths().getGenDir().resolve("src/com/facebook/katana/katana_manifest");
assertEquals("First command should make sure the output directory exists and is empty.", pathToOutDir, mkdirCommand.getPath());
Step mkTmpDir = steps.get(1);
assertTrue(mkTmpDir instanceof MakeCleanDirectoryStep);
MakeCleanDirectoryStep secondMkdirCommand = (MakeCleanDirectoryStep) mkTmpDir;
Path pathToTmpDir = filesystem.getBuckPaths().getGenDir().resolve("src/com/facebook/katana/katana_manifest__tmp");
assertEquals("Second command should create the temp directory to be written by the genrule.", pathToTmpDir, secondMkdirCommand.getPath());
Step mkSrcDir = steps.get(2);
assertTrue(mkSrcDir instanceof MakeCleanDirectoryStep);
MakeCleanDirectoryStep thirdMkdirCommand = (MakeCleanDirectoryStep) mkTmpDir;
Path pathToSrcDir = filesystem.getBuckPaths().getGenDir().resolve("src/com/facebook/katana/katana_manifest__srcs");
assertEquals("Third command should create the temp source directory to be written by the genrule.", pathToTmpDir, thirdMkdirCommand.getPath());
MkdirAndSymlinkFileStep linkSource1 = (MkdirAndSymlinkFileStep) steps.get(3);
assertEquals(filesystem.getPath("src/com/facebook/katana/convert_to_katana.py"), linkSource1.getSource());
assertEquals(filesystem.getPath(pathToSrcDir + "/convert_to_katana.py"), linkSource1.getTarget());
MkdirAndSymlinkFileStep linkSource2 = (MkdirAndSymlinkFileStep) steps.get(4);
assertEquals(filesystem.getPath("src/com/facebook/katana/AndroidManifest.xml"), linkSource2.getSource());
assertEquals(filesystem.getPath(pathToSrcDir + "/AndroidManifest.xml"), linkSource2.getTarget());
Step sixthStep = steps.get(5);
assertTrue(sixthStep instanceof AbstractGenruleStep);
AbstractGenruleStep genruleCommand = (AbstractGenruleStep) sixthStep;
assertEquals("genrule", genruleCommand.getShortName());
assertEquals(ImmutableMap.<String, String>builder().put("OUT", filesystem.resolve(filesystem.getBuckPaths().getGenDir().resolve("src/com/facebook/katana/katana_manifest/AndroidManifest.xml")).toString()).build(), genruleCommand.getEnvironmentVariables(executionContext));
Path scriptFilePath = genruleCommand.getScriptFilePath(executionContext);
String scriptFileContents = genruleCommand.getScriptFileContents(executionContext);
if (Platform.detect() == Platform.WINDOWS) {
assertEquals(ImmutableList.of(scriptFilePath.toString()), genruleCommand.getShellCommand(executionContext));
assertEquals("python convert_to_katana.py AndroidManifest.xml > %OUT%", scriptFileContents);
} else {
assertEquals(ImmutableList.of("/bin/bash", "-e", scriptFilePath.toString()), genruleCommand.getShellCommand(executionContext));
assertEquals("python convert_to_katana.py AndroidManifest.xml > $OUT", scriptFileContents);
}
}
use of com.facebook.buck.rules.FakeBuildableContext in project buck by facebook.
the class ExportFileTest method shouldSetOutToNameParamValueIfSrcIsSet.
@Test
public void shouldSetOutToNameParamValueIfSrcIsSet() throws Exception {
BuildRuleResolver resolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(resolver));
ExportFile exportFile = ExportFileBuilder.newExportFileBuilder(target).setOut("fish").build(resolver, projectFilesystem);
List<Step> steps = exportFile.getBuildSteps(FakeBuildContext.withSourcePathResolver(pathResolver), new FakeBuildableContext());
MoreAsserts.assertSteps("The output directory should be created and then the file should be copied there.", ImmutableList.of("mkdir -p " + projectFilesystem.resolve("buck-out/gen"), "rm -f -r " + projectFilesystem.resolve("buck-out/gen/fish"), "cp " + projectFilesystem.resolve("example.html") + " " + Paths.get("buck-out/gen/fish")), steps, TestExecutionContext.newInstance());
assertEquals(projectFilesystem.getBuckPaths().getGenDir().resolve("fish"), pathResolver.getRelativePath(exportFile.getSourcePathToOutput()));
}
use of com.facebook.buck.rules.FakeBuildableContext in project buck by facebook.
the class ExportFileTest method shouldSetOutAndSrcAndNameParametersSeparately.
@Test
public void shouldSetOutAndSrcAndNameParametersSeparately() throws Exception {
BuildRuleResolver resolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(resolver));
ExportFile exportFile = ExportFileBuilder.newExportFileBuilder(target).setSrc(new PathSourcePath(projectFilesystem, Paths.get("chips"))).setOut("fish").build(resolver, projectFilesystem);
List<Step> steps = exportFile.getBuildSteps(FakeBuildContext.withSourcePathResolver(pathResolver), new FakeBuildableContext());
MoreAsserts.assertSteps("The output directory should be created and then the file should be copied there.", ImmutableList.of("mkdir -p " + projectFilesystem.resolve("buck-out/gen"), "rm -f -r " + projectFilesystem.resolve("buck-out/gen/fish"), "cp " + projectFilesystem.resolve("chips") + " " + Paths.get("buck-out/gen/fish")), steps, TestExecutionContext.newInstance());
assertEquals(projectFilesystem.getBuckPaths().getGenDir().resolve("fish"), pathResolver.getRelativePath(exportFile.getSourcePathToOutput()));
}
Aggregations