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);
}
}
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");
}
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());
}
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));
}
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");
}
Aggregations