Search in sources :

Example 1 with ArArchiveEntry

use of org.apache.commons.compress.archivers.ar.ArArchiveEntry in project buck by facebook.

the class ArchiveStepIntegrationTest method inputDirs.

@Test
public void inputDirs() throws IOException, InterruptedException {
    assumeTrue(Platform.detect() == Platform.MACOS || Platform.detect() == Platform.LINUX);
    ProjectFilesystem filesystem = new ProjectFilesystem(tmp.getRoot());
    CxxPlatform platform = CxxPlatformUtils.build(new CxxBuckConfig(FakeBuckConfig.builder().build()));
    // Build up the paths to various files the archive step will use.
    SourcePathResolver sourcePathResolver = new SourcePathResolver(new SourcePathRuleFinder(new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer())));
    Archiver archiver = platform.getAr();
    Path output = filesystem.getPath("output.a");
    Path input = filesystem.getPath("foo/blah.dat");
    filesystem.mkdirs(input.getParent());
    filesystem.writeContentsToPath("blah", input);
    // Build an archive step.
    ArchiveStep archiveStep = new ArchiveStep(filesystem, archiver.getEnvironment(sourcePathResolver), archiver.getCommandPrefix(sourcePathResolver), ImmutableList.of(), getArchiveOptions(false), output, ImmutableList.of(input.getParent()), archiver);
    // Execute the archive step and verify it ran successfully.
    ExecutionContext executionContext = TestExecutionContext.newInstance();
    TestConsole console = (TestConsole) executionContext.getConsole();
    int exitCode = archiveStep.execute(executionContext).getExitCode();
    assertEquals("archive step failed: " + console.getTextWrittenToStdErr(), 0, exitCode);
    // zero'd out.
    try (ArArchiveInputStream stream = new ArArchiveInputStream(new FileInputStream(filesystem.resolve(output).toFile()))) {
        ArArchiveEntry entry = stream.getNextArEntry();
        assertThat(entry.getName(), Matchers.equalTo("blah.dat"));
    }
}
Also used : Path(java.nio.file.Path) ArArchiveEntry(org.apache.commons.compress.archivers.ar.ArArchiveEntry) ArArchiveInputStream(org.apache.commons.compress.archivers.ar.ArArchiveInputStream) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) FileInputStream(java.io.FileInputStream) ExecutionContext(com.facebook.buck.step.ExecutionContext) TestExecutionContext(com.facebook.buck.step.TestExecutionContext) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) TestConsole(com.facebook.buck.testutil.TestConsole) Test(org.junit.Test)

Example 2 with ArArchiveEntry

use of org.apache.commons.compress.archivers.ar.ArArchiveEntry in project buck by facebook.

the class ArchiveStepIntegrationTest method thinArchives.

@Test
public void thinArchives() throws IOException, InterruptedException {
    assumeTrue(Platform.detect() == Platform.MACOS || Platform.detect() == Platform.LINUX);
    ProjectFilesystem filesystem = new ProjectFilesystem(tmp.getRoot());
    CxxPlatform platform = CxxPlatformUtils.build(new CxxBuckConfig(FakeBuckConfig.builder().build()));
    assumeTrue(platform.getAr().supportsThinArchives());
    // Build up the paths to various files the archive step will use.
    SourcePathResolver sourcePathResolver = new SourcePathResolver(new SourcePathRuleFinder(new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer())));
    Archiver archiver = platform.getAr();
    Path output = filesystem.getPath("foo/libthin.a");
    filesystem.mkdirs(output.getParent());
    // Create a really large input file so it's obvious that the archive is thin.
    Path input = filesystem.getPath("bar/blah.dat");
    filesystem.mkdirs(input.getParent());
    byte[] largeInputFile = new byte[1024 * 1024];
    byte[] fillerToRepeat = "hello\n".getBytes(StandardCharsets.UTF_8);
    for (int i = 0; i < largeInputFile.length; i++) {
        largeInputFile[i] = fillerToRepeat[i % fillerToRepeat.length];
    }
    filesystem.writeBytesToPath(largeInputFile, input);
    // Build an archive step.
    ArchiveStep archiveStep = new ArchiveStep(filesystem, archiver.getEnvironment(sourcePathResolver), archiver.getCommandPrefix(sourcePathResolver), ImmutableList.of(), getArchiveOptions(true), output, ImmutableList.of(input), archiver);
    // Execute the archive step and verify it ran successfully.
    ExecutionContext executionContext = TestExecutionContext.newInstance();
    TestConsole console = (TestConsole) executionContext.getConsole();
    int exitCode = archiveStep.execute(executionContext).getExitCode();
    assertEquals("archive step failed: " + console.getTextWrittenToStdErr(), 0, exitCode);
    // Verify that the thin header is present.
    assertThat(filesystem.readFirstLine(output), Matchers.equalTo(Optional.of("!<thin>")));
    // Verify that even though the archived contents is really big, the archive is still small.
    assertThat(filesystem.getFileSize(output), Matchers.lessThan(1000L));
    // can parse the archive contents.
    try (OutputStream outputStream = Files.newOutputStream(filesystem.resolve(output), StandardOpenOption.WRITE)) {
        outputStream.write(ObjectFileScrubbers.GLOBAL_HEADER);
    }
    // zero'd out.
    try (ArArchiveInputStream stream = new ArArchiveInputStream(new FileInputStream(filesystem.resolve(output).toFile()))) {
        ArArchiveEntry entry = stream.getNextArEntry();
        // Verify that the input names are relative paths from the outputs parent dir.
        assertThat(entry.getName(), Matchers.equalTo(output.getParent().relativize(input).toString()));
    }
}
Also used : Path(java.nio.file.Path) ArArchiveEntry(org.apache.commons.compress.archivers.ar.ArArchiveEntry) OutputStream(java.io.OutputStream) ArArchiveInputStream(org.apache.commons.compress.archivers.ar.ArArchiveInputStream) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) FileInputStream(java.io.FileInputStream) ExecutionContext(com.facebook.buck.step.ExecutionContext) TestExecutionContext(com.facebook.buck.step.TestExecutionContext) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) TestConsole(com.facebook.buck.testutil.TestConsole) Test(org.junit.Test)

Example 3 with ArArchiveEntry

use of org.apache.commons.compress.archivers.ar.ArArchiveEntry in project buck by facebook.

the class ArchiveStepIntegrationTest method thatGeneratedArchivesAreDeterministic.

@Test
@SuppressWarnings("PMD.AvoidUsingOctalValues")
public void thatGeneratedArchivesAreDeterministic() throws IOException, InterruptedException {
    assumeTrue(Platform.detect() == Platform.MACOS || Platform.detect() == Platform.LINUX);
    ProjectFilesystem filesystem = new ProjectFilesystem(tmp.getRoot());
    CxxPlatform platform = CxxPlatformUtils.build(new CxxBuckConfig(FakeBuckConfig.builder().build()));
    // Build up the paths to various files the archive step will use.
    SourcePathResolver sourcePathResolver = new SourcePathResolver(new SourcePathRuleFinder(new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer())));
    Archiver archiver = platform.getAr();
    Path output = filesystem.getPath("output.a");
    Path input = filesystem.getPath("input.dat");
    filesystem.writeContentsToPath("blah", input);
    Preconditions.checkState(filesystem.resolve(input).toFile().setExecutable(true));
    // Build an archive step.
    ArchiveStep archiveStep = new ArchiveStep(filesystem, archiver.getEnvironment(sourcePathResolver), archiver.getCommandPrefix(sourcePathResolver), ImmutableList.of(), getArchiveOptions(false), output, ImmutableList.of(input), archiver);
    FileScrubberStep fileScrubberStep = new FileScrubberStep(filesystem, output, platform.getAr().getScrubbers());
    // Execute the archive step and verify it ran successfully.
    ExecutionContext executionContext = TestExecutionContext.newInstance();
    TestConsole console = (TestConsole) executionContext.getConsole();
    int exitCode = archiveStep.execute(executionContext).getExitCode();
    assertEquals("archive step failed: " + console.getTextWrittenToStdErr(), 0, exitCode);
    exitCode = fileScrubberStep.execute(executionContext).getExitCode();
    assertEquals("archive scrub step failed: " + console.getTextWrittenToStdErr(), 0, exitCode);
    // zero'd out.
    try (ArArchiveInputStream stream = new ArArchiveInputStream(new FileInputStream(filesystem.resolve(output).toFile()))) {
        ArArchiveEntry entry = stream.getNextArEntry();
        assertEquals(ObjectFileCommonModificationDate.COMMON_MODIFICATION_TIME_STAMP, entry.getLastModified());
        assertEquals(0, entry.getUserId());
        assertEquals(0, entry.getGroupId());
        assertEquals(String.format("0%o", entry.getMode()), 0100644, entry.getMode());
    }
}
Also used : Path(java.nio.file.Path) ArArchiveEntry(org.apache.commons.compress.archivers.ar.ArArchiveEntry) ArArchiveInputStream(org.apache.commons.compress.archivers.ar.ArArchiveInputStream) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) FileScrubberStep(com.facebook.buck.step.fs.FileScrubberStep) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) FileInputStream(java.io.FileInputStream) ExecutionContext(com.facebook.buck.step.ExecutionContext) TestExecutionContext(com.facebook.buck.step.TestExecutionContext) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) TestConsole(com.facebook.buck.testutil.TestConsole) Test(org.junit.Test)

Example 4 with ArArchiveEntry

use of org.apache.commons.compress.archivers.ar.ArArchiveEntry in project buck by facebook.

the class CxxLibraryIntegrationTest method thinArchivesDoNotContainAbsolutePaths.

@Test
public void thinArchivesDoNotContainAbsolutePaths() throws IOException {
    CxxPlatform cxxPlatform = CxxPlatformUtils.build(new CxxBuckConfig(FakeBuckConfig.builder().build()));
    assumeTrue(cxxPlatform.getAr().supportsThinArchives());
    ProjectWorkspace workspace = TestDataHelper.createProjectWorkspaceForScenario(this, "cxx_library", tmp);
    workspace.setUp();
    Path archive = workspace.buildAndReturnOutput("-c", "cxx.archive_contents=thin", "//:foo#default,static");
    // can parse the archive contents.
    try (OutputStream outputStream = Files.newOutputStream(workspace.getPath(archive), StandardOpenOption.WRITE)) {
        outputStream.write(ObjectFileScrubbers.GLOBAL_HEADER);
    }
    // Now iterate the archive and verify it contains no absolute paths.
    try (ArArchiveInputStream stream = new ArArchiveInputStream(new FileInputStream(workspace.getPath(archive).toFile()))) {
        ArArchiveEntry entry;
        while ((entry = stream.getNextArEntry()) != null) {
            if (!entry.getName().isEmpty()) {
                assertFalse("found absolute path: " + entry.getName(), workspace.getDestPath().getFileSystem().getPath(entry.getName()).isAbsolute());
            }
        }
    }
}
Also used : Path(java.nio.file.Path) ProjectWorkspace(com.facebook.buck.testutil.integration.ProjectWorkspace) ArArchiveEntry(org.apache.commons.compress.archivers.ar.ArArchiveEntry) OutputStream(java.io.OutputStream) ArArchiveInputStream(org.apache.commons.compress.archivers.ar.ArArchiveInputStream) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Aggregations

FileInputStream (java.io.FileInputStream)4 Path (java.nio.file.Path)4 ArArchiveEntry (org.apache.commons.compress.archivers.ar.ArArchiveEntry)4 ArArchiveInputStream (org.apache.commons.compress.archivers.ar.ArArchiveInputStream)4 Test (org.junit.Test)4 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)3 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)3 DefaultTargetNodeToBuildRuleTransformer (com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)3 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)3 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)3 ExecutionContext (com.facebook.buck.step.ExecutionContext)3 TestExecutionContext (com.facebook.buck.step.TestExecutionContext)3 TestConsole (com.facebook.buck.testutil.TestConsole)3 OutputStream (java.io.OutputStream)2 FileScrubberStep (com.facebook.buck.step.fs.FileScrubberStep)1 ProjectWorkspace (com.facebook.buck.testutil.integration.ProjectWorkspace)1