Search in sources :

Example 71 with ExecutionContext

use of com.facebook.buck.step.ExecutionContext in project buck by facebook.

the class JarDirectoryStepTest method entriesFromTheGivenManifestShouldOverrideThoseInTheJars.

@Test
public void entriesFromTheGivenManifestShouldOverrideThoseInTheJars() throws IOException {
    String expected = "1.4";
    // Write the manifest, setting the implementation version
    Path tmp = folder.newFolder();
    Manifest manifest = new Manifest();
    manifest.getMainAttributes().putValue(MANIFEST_VERSION.toString(), "1.0");
    manifest.getMainAttributes().putValue(IMPLEMENTATION_VERSION.toString(), expected);
    Path manifestFile = tmp.resolve("manifest");
    try (OutputStream fos = Files.newOutputStream(manifestFile)) {
        manifest.write(fos);
    }
    // Write another manifest, setting the implementation version to something else
    manifest = new Manifest();
    manifest.getMainAttributes().putValue(MANIFEST_VERSION.toString(), "1.0");
    manifest.getMainAttributes().putValue(IMPLEMENTATION_VERSION.toString(), "1.0");
    Path input = tmp.resolve("input.jar");
    try (CustomZipOutputStream out = ZipOutputStreams.newOutputStream(input)) {
        ZipEntry entry = new ZipEntry("META-INF/MANIFEST.MF");
        out.putNextEntry(entry);
        manifest.write(out);
    }
    Path output = tmp.resolve("output.jar");
    JarDirectoryStep step = new JarDirectoryStep(new ProjectFilesystem(tmp), output, ImmutableSortedSet.of(Paths.get("input.jar")), /* main class */
    null, tmp.resolve("manifest"), /* merge manifest */
    true, /* blacklist */
    ImmutableSet.of());
    ExecutionContext context = TestExecutionContext.newInstance();
    assertEquals(0, step.execute(context).getExitCode());
    try (Zip zip = new Zip(output, false)) {
        byte[] rawManifest = zip.readFully("META-INF/MANIFEST.MF");
        manifest = new Manifest(new ByteArrayInputStream(rawManifest));
        String version = manifest.getMainAttributes().getValue(IMPLEMENTATION_VERSION);
        assertEquals(expected, version);
    }
}
Also used : Path(java.nio.file.Path) Zip(com.facebook.buck.testutil.Zip) ExecutionContext(com.facebook.buck.step.ExecutionContext) TestExecutionContext(com.facebook.buck.step.TestExecutionContext) ByteArrayInputStream(java.io.ByteArrayInputStream) ZipOutputStream(java.util.zip.ZipOutputStream) JarOutputStream(java.util.jar.JarOutputStream) OutputStream(java.io.OutputStream) CustomZipOutputStream(com.facebook.buck.zip.CustomZipOutputStream) ZipEntry(java.util.zip.ZipEntry) CustomZipOutputStream(com.facebook.buck.zip.CustomZipOutputStream) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) Manifest(java.util.jar.Manifest) Test(org.junit.Test)

Example 72 with ExecutionContext

use of com.facebook.buck.step.ExecutionContext in project buck by facebook.

the class JarDirectoryStepTest method shouldNotComplainWhenDuplicateDirectoryNamesAreAdded.

@Test
public void shouldNotComplainWhenDuplicateDirectoryNamesAreAdded() throws IOException {
    Path zipup = folder.newFolder();
    Path first = createZip(zipup.resolve("first.zip"), "dir/example.txt", "dir/root1file.txt");
    Path second = createZip(zipup.resolve("second.zip"), "dir/example.txt", "dir/root2file.txt", "com/example/Main.class");
    JarDirectoryStep step = new JarDirectoryStep(new ProjectFilesystem(zipup), Paths.get("output.jar"), ImmutableSortedSet.of(first.getFileName(), second.getFileName()), "com.example.Main", /* manifest file */
    null);
    ExecutionContext context = TestExecutionContext.newInstance();
    int returnCode = step.execute(context).getExitCode();
    assertEquals(0, returnCode);
    Path zip = zipup.resolve("output.jar");
    // The three below plus the manifest and Main.class.
    assertZipFileCountIs(5, zip);
    assertZipContains(zip, "dir/example.txt", "dir/root1file.txt", "dir/root2file.txt");
}
Also used : Path(java.nio.file.Path) ExecutionContext(com.facebook.buck.step.ExecutionContext) TestExecutionContext(com.facebook.buck.step.TestExecutionContext) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) Test(org.junit.Test)

Example 73 with ExecutionContext

use of com.facebook.buck.step.ExecutionContext in project buck by facebook.

the class JarDirectoryStepTest method jarsShouldContainDirectoryEntries.

@Test
public void jarsShouldContainDirectoryEntries() throws IOException {
    Path zipup = folder.newFolder("dir-zip");
    Path subdir = zipup.resolve("dir/subdir");
    Files.createDirectories(subdir);
    Files.write(subdir.resolve("a.txt"), "cake".getBytes());
    JarDirectoryStep step = new JarDirectoryStep(new ProjectFilesystem(zipup), Paths.get("output.jar"), ImmutableSortedSet.of(zipup), /* main class */
    null, /* manifest file */
    null);
    ExecutionContext context = TestExecutionContext.newInstance();
    int returnCode = step.execute(context).getExitCode();
    assertEquals(0, returnCode);
    Path zip = zipup.resolve("output.jar");
    assertTrue(Files.exists(zip));
    // Iterate over each of the entries, expecting to see the directory names as entries.
    Set<String> expected = Sets.newHashSet("dir/", "dir/subdir/");
    try (ZipInputStream is = new ZipInputStream(Files.newInputStream(zip))) {
        for (ZipEntry entry = is.getNextEntry(); entry != null; entry = is.getNextEntry()) {
            expected.remove(entry.getName());
        }
    }
    assertTrue("Didn't see entries for: " + expected, expected.isEmpty());
}
Also used : Path(java.nio.file.Path) ZipInputStream(java.util.zip.ZipInputStream) ExecutionContext(com.facebook.buck.step.ExecutionContext) TestExecutionContext(com.facebook.buck.step.TestExecutionContext) ZipEntry(java.util.zip.ZipEntry) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) Test(org.junit.Test)

Example 74 with ExecutionContext

use of com.facebook.buck.step.ExecutionContext in project buck by facebook.

the class JarDirectoryStepTest method shouldNotifyEventBusWhenDuplicateClassesAreFound.

@Test
public void shouldNotifyEventBusWhenDuplicateClassesAreFound() throws IOException {
    Path jarDirectory = folder.newFolder("jarDir");
    Path first = createZip(jarDirectory.resolve("a.jar"), "com/example/Main.class", "com/example/common/Helper.class");
    Path second = createZip(jarDirectory.resolve("b.jar"), "com/example/common/Helper.class");
    final Path outputPath = Paths.get("output.jar");
    JarDirectoryStep step = new JarDirectoryStep(new ProjectFilesystem(jarDirectory), outputPath, ImmutableSortedSet.of(first.getFileName(), second.getFileName()), "com.example.Main", /* manifest file */
    null);
    ExecutionContext context = TestExecutionContext.newInstance();
    final BuckEventBusFactory.CapturingConsoleEventListener listener = new BuckEventBusFactory.CapturingConsoleEventListener();
    context.getBuckEventBus().register(listener);
    step.execute(context);
    final String expectedMessage = String.format("Duplicate found when adding 'com/example/common/Helper.class' to '%s' from '%s'", outputPath.toAbsolutePath(), second.toAbsolutePath());
    assertThat(listener.getLogMessages(), hasItem(expectedMessage));
}
Also used : Path(java.nio.file.Path) BuckEventBusFactory(com.facebook.buck.event.BuckEventBusFactory) ExecutionContext(com.facebook.buck.step.ExecutionContext) TestExecutionContext(com.facebook.buck.step.TestExecutionContext) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) Test(org.junit.Test)

Example 75 with ExecutionContext

use of com.facebook.buck.step.ExecutionContext in project buck by facebook.

the class CalculateAbiStepTest method shouldCalculateAbiFromAStubJar.

@Test
public void shouldCalculateAbiFromAStubJar() throws IOException {
    Path outDir = temp.newFolder().toAbsolutePath();
    ProjectFilesystem filesystem = new ProjectFilesystem(outDir);
    Path directory = TestDataHelper.getTestDataDirectory(this);
    Path source = directory.resolve("prebuilt/junit.jar");
    Path binJar = Paths.get("source.jar");
    Files.copy(source, outDir.resolve(binJar));
    Path abiJar = outDir.resolve("abi.jar");
    ExecutionContext executionContext = TestExecutionContext.newInstance();
    FakeBuildableContext context = new FakeBuildableContext();
    new CalculateAbiStep(context, filesystem, binJar, abiJar).execute(executionContext);
    String seenHash = filesystem.computeSha1(Paths.get("abi.jar")).getHash();
    // Hi there! This is hardcoded here because we want to make sure buck always produces the same
    // jar files across timezones and versions. If the test is failing because of an intentional
    // modification to how we produce abi .jar files, then just update the hash, otherwise please
    // investigate why the value is different.
    // NOTE: If this starts failing on CI for no obvious reason it's possible that the offset
    // calculation in ZipConstants.getFakeTime() does not account for DST correctly.
    assertEquals("2f8dd47439697c6d633f7baef3d0f71cbac9d4a4", seenHash);
    // Assert that the abiJar contains non-class resources (like txt files).
    ZipInspector inspector = new ZipInspector(abiJar);
    inspector.assertFileExists("LICENSE.txt");
}
Also used : Path(java.nio.file.Path) FakeBuildableContext(com.facebook.buck.rules.FakeBuildableContext) ExecutionContext(com.facebook.buck.step.ExecutionContext) TestExecutionContext(com.facebook.buck.step.TestExecutionContext) ZipInspector(com.facebook.buck.testutil.integration.ZipInspector) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) Test(org.junit.Test)

Aggregations

ExecutionContext (com.facebook.buck.step.ExecutionContext)176 TestExecutionContext (com.facebook.buck.step.TestExecutionContext)140 Test (org.junit.Test)128 Path (java.nio.file.Path)79 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)67 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)66 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)55 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)50 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)45 DefaultTargetNodeToBuildRuleTransformer (com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)44 Step (com.facebook.buck.step.Step)34 ImmutableList (com.google.common.collect.ImmutableList)32 BuildTarget (com.facebook.buck.model.BuildTarget)31 SourcePath (com.facebook.buck.rules.SourcePath)26 TestConsole (com.facebook.buck.testutil.TestConsole)25 FakeProcess (com.facebook.buck.util.FakeProcess)21 ProcessExecutorParams (com.facebook.buck.util.ProcessExecutorParams)20 ImmutableMap (com.google.common.collect.ImmutableMap)20 MakeCleanDirectoryStep (com.facebook.buck.step.fs.MakeCleanDirectoryStep)19 FakeProcessExecutor (com.facebook.buck.util.FakeProcessExecutor)19