@Test public void testGetBuildStepsWhenThereAreNoClassesToDex() throws IOException, InterruptedException { JavaLibrary javaLibrary = createMock(JavaLibrary.class); expect(javaLibrary.getClassNamesToHashes()) .andReturn(ImmutableSortedMap.<String, HashCode>of()); BuildContext context = createMock(BuildContext.class); FakeBuildableContext buildableContext = new FakeBuildableContext(); ProjectFilesystem projectFilesystem = createMock(ProjectFilesystem.class); replayAll(); BuildTarget buildTarget = BuildTargetFactory.newInstance("//foo:bar"); BuildRuleParams params = new FakeBuildRuleParamsBuilder(buildTarget).setProjectFilesystem(projectFilesystem).build(); DexProducedFromJavaLibrary preDex = new DexProducedFromJavaLibrary( params, new SourcePathResolver(new BuildRuleResolver()), javaLibrary); List<Step> steps = preDex.getBuildSteps(context, buildableContext); verifyAll(); resetAll(); expect(projectFilesystem.resolve(Paths.get("buck-out/gen/foo"))) .andReturn(Paths.get("/home/user/buck-out/gen/foo")); expect(projectFilesystem.resolve(Paths.get("buck-out/gen/foo/bar.dex.jar"))) .andReturn(Paths.get("/home/user/buck-out/gen/foo/bar.dex.jar")); replayAll(); ExecutionContext executionContext = TestExecutionContext.newBuilder().build(); MoreAsserts.assertSteps( "Do not generate a .dex.jar file.", ImmutableList.of( String.format("rm -f %s", Paths.get("/home/user/buck-out/gen/foo/bar.dex.jar")), String.format("mkdir -p %s", Paths.get("/home/user/buck-out/gen/foo")), "record_empty_dx"), steps, executionContext); verifyAll(); resetAll(); replayAll(); Step recordArtifactAndMetadataStep = steps.get(2); assertThat(recordArtifactAndMetadataStep.getShortName(), startsWith("record_")); int exitCode = recordArtifactAndMetadataStep.execute(executionContext); assertEquals(0, exitCode); verifyAll(); }
@Test public void testGetBuildStepsWhenThereAreClassesToDex() throws IOException, InterruptedException { SourcePathResolver pathResolver = new SourcePathResolver(new BuildRuleResolver()); FakeJavaLibrary javaLibraryRule = new FakeJavaLibrary(BuildTargetFactory.newInstance("//foo:bar"), pathResolver) { @Override public ImmutableSortedMap<String, HashCode> getClassNamesToHashes() { return ImmutableSortedMap.of("com/example/Foo", HashCode.fromString("cafebabe")); } @Override public Sha1HashCode getAbiKey() { return Sha1HashCode.of("f7f34ed13b881c6c6f663533cde4a436ea84435e"); } }; javaLibraryRule.setOutputFile("buck-out/gen/foo/bar.jar"); BuildContext context = createMock(BuildContext.class); FakeBuildableContext buildableContext = new FakeBuildableContext(); replayAll(); ProjectFilesystem filesystem = FakeProjectFilesystem.createJavaOnlyFilesystem("/home/user"); createFiles(filesystem, "buck-out/gen/foo/bar#dex.dex.jar", "buck-out/gen/foo/bar.jar"); BuildTarget buildTarget = BuildTargetFactory.newInstance("//foo:bar#dex"); BuildRuleParams params = new FakeBuildRuleParamsBuilder(buildTarget).setProjectFilesystem(filesystem).build(); DexProducedFromJavaLibrary preDex = new DexProducedFromJavaLibrary(params, pathResolver, javaLibraryRule); List<Step> steps = preDex.getBuildSteps(context, buildableContext); verifyAll(); resetAll(); AndroidPlatformTarget androidPlatformTarget = createMock(AndroidPlatformTarget.class); expect(androidPlatformTarget.getDxExecutable()).andReturn(Paths.get("/usr/bin/dx")); replayAll(); ExecutionContext executionContext = TestExecutionContext.newBuilder() .setAndroidPlatformTargetSupplier(Suppliers.ofInstance(androidPlatformTarget)) .build(); String expectedDxCommand = String.format( "%s --dex --no-optimize --force-jumbo --output %s %s", Paths.get("/usr/bin/dx"), Paths.get("/home/user/buck-out/gen/foo/bar#dex.dex.jar"), Paths.get("/home/user/buck-out/gen/foo/bar.jar")); MoreAsserts.assertSteps( "Generate bar.dex.jar.", ImmutableList.of( String.format("rm -f %s", Paths.get("/home/user/buck-out/gen/foo/bar#dex.dex.jar")), String.format("mkdir -p %s", Paths.get("/home/user/buck-out/gen/foo")), "estimate_linear_alloc", "(cd /home/user && " + expectedDxCommand + ")", "zip-scrub buck-out/gen/foo/bar#dex.dex.jar", "record_dx_success"), steps, executionContext); verifyAll(); resetAll(); replayAll(); ((EstimateLinearAllocStep) steps.get(2)).setLinearAllocEstimateForTesting(250); Step recordArtifactAndMetadataStep = steps.get(5); int exitCode = recordArtifactAndMetadataStep.execute(executionContext); assertEquals(0, exitCode); assertEquals( "The generated .dex.jar file should be in the set of recorded artifacts.", ImmutableSet.of(Paths.get("buck-out/gen/foo/bar#dex.dex.jar")), buildableContext.getRecordedArtifacts()); buildableContext.assertContainsMetadataMapping( DexProducedFromJavaLibrary.LINEAR_ALLOC_KEY_ON_DISK_METADATA, "250"); verifyAll(); }