@Test
  public void sanitizeSymlinkedWorkingDirectory() throws IOException {
    TemporaryFolder folder = new TemporaryFolder();
    folder.create();

    // Setup up a symlink to our working directory.
    Path symlinkedRoot = folder.getRoot().toPath().resolve("symlinked-root");
    java.nio.file.Files.createSymbolicLink(symlinkedRoot, tmp.getRootPath());

    // Run the build, setting PWD to the above symlink.  Typically, this causes compilers to use
    // the symlinked directory, even though it's not the right project root.
    Map<String, String> envCopy = Maps.newHashMap(System.getenv());
    envCopy.put("PWD", symlinkedRoot.toString());
    workspace
        .runBuckCommandWithEnvironmentAndContext(
            tmp.getRootPath(),
            Optional.<NGContext>absent(),
            Optional.<BuckEventListener>absent(),
            Optional.of(ImmutableMap.copyOf(envCopy)),
            "build",
            "//:simple#default,static")
        .assertSuccess();

    // Verify that we still sanitized this path correctly.
    Path lib = workspace.getPath("buck-out/gen/simple#default,static/libsimple.a");
    String contents = Files.asByteSource(lib.toFile()).asCharSource(Charsets.ISO_8859_1).read();
    assertFalse(lib.toString(), contents.contains(tmp.getRootPath().toString()));
    assertFalse(lib.toString(), contents.contains(symlinkedRoot.toString()));

    folder.delete();
  }
  private void assertCompDir(Path compDir, Optional<String> failure) throws Exception {
    ProjectFilesystem filesystem = new ProjectFilesystem(tmp.getRoot().toPath());
    CxxPlatform platform = DefaultCxxPlatforms.build(new CxxBuckConfig(new FakeBuckConfig()));

    // Build up the paths to various files the archive step will use.
    ImmutableList<String> compiler =
        platform.getCc().getCommandPrefix(new SourcePathResolver(new BuildRuleResolver()));
    Path output = filesystem.resolve(Paths.get("output.o"));
    Path relativeInput = Paths.get("input.c");
    Path input = filesystem.resolve(relativeInput);
    filesystem.writeContentsToPath("int main() {}", relativeInput);

    ImmutableList.Builder<String> preprocessorCommand = ImmutableList.builder();
    preprocessorCommand.addAll(compiler);

    ImmutableList.Builder<String> compilerCommand = ImmutableList.builder();
    compilerCommand.addAll(compiler);
    compilerCommand.add("-g");

    DebugPathSanitizer sanitizer =
        new DebugPathSanitizer(200, File.separatorChar, compDir, ImmutableBiMap.<Path, Path>of());

    // Build an archive step.
    CxxPreprocessAndCompileStep step =
        new CxxPreprocessAndCompileStep(
            CxxPreprocessAndCompileStep.Operation.COMPILE_MUNGE_DEBUGINFO,
            output,
            relativeInput,
            CxxSource.Type.C,
            Optional.of(preprocessorCommand.build()),
            Optional.of(compilerCommand.build()),
            ImmutableMap.<Path, Path>of(),
            sanitizer);

    // Execute the archive step and verify it ran successfully.
    ExecutionContext executionContext =
        TestExecutionContext.newBuilder()
            .setProjectFilesystem(new ProjectFilesystem(tmp.getRoot().toPath()))
            .build();
    TestConsole console = (TestConsole) executionContext.getConsole();
    int exitCode = step.execute(executionContext);
    if (failure.isPresent()) {
      assertNotEquals("compile step succeeded", 0, exitCode);
      assertThat(
          console.getTextWrittenToStdErr(),
          console.getTextWrittenToStdErr(),
          Matchers.containsString(failure.get()));
    } else {
      assertEquals("compile step failed: " + console.getTextWrittenToStdErr(), 0, exitCode);
      // Verify that we find the expected compilation dir embedded in the file.
      String contents = new String(Files.readAllBytes(output));
      assertThat(contents, Matchers.containsString(sanitizer.getCompilationDirectory()));
    }

    // Cleanup.
    Files.delete(input);
    Files.deleteIfExists(output);
  }
  @Test
  public void binaryWithDependenciesCompilationDatabase() throws IOException {
    ProjectWorkspace workspace =
        TestDataHelper.createProjectWorkspaceForScenario(this, "compilation_database", tmp);
    workspace.setUp();
    Path compilationDatabase =
        workspace.buildAndReturnOutput("//:binary_with_dep#compilation-database");

    assertEquals(
        Paths.get("buck-out/gen/__binary_with_dep#compilation-database.json"),
        tmp.getRootPath().relativize(compilationDatabase));

    String binaryHeaderSymlinkTreeFolder =
        String.format(
            "buck-out/gen/binary_with_dep#default,%s",
            CxxDescriptionEnhancer.HEADER_SYMLINK_TREE_FLAVOR);
    String binaryExportedHeaderSymlinkTreeFoler =
        String.format(
            "buck-out/gen/library_with_header#default,%s",
            CxxDescriptionEnhancer.EXPORTED_HEADER_SYMLINK_TREE_FLAVOR);

    assertTrue(Files.exists(tmp.getRootPath().resolve(binaryHeaderSymlinkTreeFolder)));
    assertTrue(Files.exists(tmp.getRootPath().resolve(binaryExportedHeaderSymlinkTreeFoler)));

    String libraryExportedHeaderSymlinkTreeFoler =
        String.format(
            "buck-out/gen/library_with_header#default,%s",
            CxxDescriptionEnhancer.EXPORTED_HEADER_SYMLINK_TREE_FLAVOR);

    // Verify that symlink folders for headers are created and header file is linked.
    assertTrue(Files.exists(tmp.getRootPath().resolve(libraryExportedHeaderSymlinkTreeFoler)));
    assertTrue(
        Files.exists(tmp.getRootPath().resolve(libraryExportedHeaderSymlinkTreeFoler + "/bar.h")));

    Map<String, CxxCompilationDatabaseEntry> fileToEntry =
        CxxCompilationDatabaseEntry.parseCompilationDatabaseJsonFile(compilationDatabase);
    assertEquals(1, fileToEntry.size());
    assertHasEntry(
        fileToEntry,
        "foo.cpp",
        new ImmutableList.Builder<String>()
            .add(COMPILER_PATH)
            .add("-I")
            .add(headerSymlinkTreeIncludePath(binaryHeaderSymlinkTreeFolder))
            .add("-I")
            .add(headerSymlinkTreeIncludePath(binaryExportedHeaderSymlinkTreeFoler))
            .addAll(EXTRA_FLAGS_FOR_HEADER_MAPS)
            .addAll(COMPILER_SPECIFIC_FLAGS)
            .add("-x")
            .add("c++")
            .add("-c")
            .add("-o")
            .add("buck-out/gen/binary_with_dep#compile-foo.cpp.o,default/foo.cpp.o")
            .add("foo.cpp")
            .build());
  }
Esempio n. 4
0
 @Test
 public void skipsFirstCacheBecauseIgnored() throws IOException {
   Path path = Paths.get("world.txt");
   Path fullPath = tmp.getRootPath().resolve(path);
   ProjectFilesystem filesystem = new ProjectFilesystem(tmp.getRootPath(), ImmutableSet.of(path));
   filesystem.touch(path);
   DefaultFileHashCache innerCache = new DefaultFileHashCache(filesystem);
   StackedFileHashCache cache = new StackedFileHashCache(ImmutableList.of(innerCache));
   expectedException.expect(NoSuchFileException.class);
   cache.get(fullPath);
 }
Esempio n. 5
0
  @Before
  public void setUp() throws IOException, InterruptedException {
    executorService = Executors.newScheduledThreadPool(5);
    // In case root_restrict_files is enabled in /etc/watchmanconfig, assume
    // this is one of the entries so it doesn't give up.
    tmp.newFolder(".git");
    Watchman watchman =
        Watchman.build(tmp.getRootPath(), getWatchmanEnv(), new TestConsole(), new FakeClock(0));

    // We assume watchman has been installed and configured properly on the system, and that setting
    // up the watch is successful.
    assumeFalse(watchman == Watchman.NULL_WATCHMAN);
  }
  @Test
  public void testAppleLibraryBuildsFramework() throws Exception {
    assumeTrue(Platform.detect() == Platform.MACOS);
    assumeTrue(
        AppleNativeIntegrationTestUtils.isApplePlatformAvailable(
            ApplePlatform.builder().setName(ApplePlatform.Name.MACOSX).build()));

    ProjectWorkspace workspace =
        TestDataHelper.createProjectWorkspaceForScenario(
            this, "apple_library_builds_something", tmp);
    workspace.setUp();

    ProjectWorkspace.ProcessResult result =
        workspace.runBuckCommand(
            "build", "//Libraries/TestLibrary:TestLibrary#framework,macosx-x86_64");
    result.assertSuccess();

    Path frameworkPath =
        tmp.getRootPath()
            .resolve(BuckConstant.GEN_DIR)
            .resolve(
                "Libraries/TestLibrary/TestLibrary#framework,macosx-x86_64/TestLibrary.framework");
    assertThat(Files.exists(frameworkPath), is(true));
    assertThat(Files.exists(frameworkPath.resolve("Contents/Info.plist")), is(true));
    Path libraryPath = frameworkPath.resolve("Contents/MacOS/TestLibrary");
    assertThat(Files.exists(libraryPath), is(true));
    assertThat(
        workspace.runCommand("file", libraryPath.toString()).getStdout().get(),
        containsString("dynamically linked shared library"));
  }
  @Test
  public void testGetDescription() throws IOException {
    Jsr199Javac javac = createJavac(/* withSyntaxError */ false);
    String pathToOutputDir = new File(tmp.getRoot(), "out").getAbsolutePath();

    assertEquals(
        String.format(
            "javac -source %s -target %s -g "
                + "-d %s "
                + "-classpath '' "
                + "@"
                + pathToSrcsList.toString(),
            JavaBuckConfig.TARGETED_JAVA_VERSION,
            JavaBuckConfig.TARGETED_JAVA_VERSION,
            pathToOutputDir),
        javac.getDescription(
            ImmutableList.of(
                "-source",
                JavaBuckConfig.TARGETED_JAVA_VERSION,
                "-target",
                JavaBuckConfig.TARGETED_JAVA_VERSION,
                "-g",
                "-d",
                pathToOutputDir,
                "-classpath",
                "''"),
            SOURCE_PATHS,
            Optional.of(pathToSrcsList)));
  }
 @Test
 public void sanitizeWorkingDirectory() throws IOException {
   workspace.runBuckBuild("//:simple#default,static").assertSuccess();
   Path lib = workspace.getPath("buck-out/gen/simple#default,static/libsimple.a");
   String contents = Files.asByteSource(lib.toFile()).asCharSource(Charsets.ISO_8859_1).read();
   assertFalse(lib.toString(), contents.contains(tmp.getRootPath().toString()));
 }
Esempio n. 9
0
  @Test
  public void whenAndroidDirectoryResolverChangesParserInvalidated()
      throws IOException, InterruptedException {
    ProjectFilesystem filesystem = new ProjectFilesystem(tmp.getRoot().toPath());

    Object daemon =
        Main.getDaemon(
            new TestCellBuilder()
                .setAndroidDirectoryResolver(
                    new FakeAndroidDirectoryResolver(
                        Optional.<Path>absent(),
                        Optional.<Path>absent(),
                        Optional.<Path>absent(),
                        Optional.of("something")))
                .setFilesystem(filesystem)
                .build(),
            new ObjectMapper());

    assertNotEquals(
        "Daemon should be replaced when not equal.",
        daemon,
        Main.getDaemon(
            new TestCellBuilder()
                .setAndroidDirectoryResolver(
                    new FakeAndroidDirectoryResolver(
                        Optional.<Path>absent(),
                        Optional.<Path>absent(),
                        Optional.<Path>absent(),
                        Optional.of("different")))
                .setFilesystem(filesystem)
                .build(),
            new ObjectMapper()));
  }
  @Test
  public void testAppleLibraryExportedHeaderSymlinkTree() throws IOException {
    assumeTrue(Platform.detect() == Platform.MACOS);

    ProjectWorkspace workspace =
        TestDataHelper.createProjectWorkspaceForScenario(
            this, "apple_library_header_symlink_tree", tmp);
    workspace.setUp();

    BuildTarget buildTarget =
        BuildTargetFactory.newInstance(
            "//Libraries/TestLibrary:TestLibrary#"
                + "default,"
                + CxxDescriptionEnhancer.EXPORTED_HEADER_SYMLINK_TREE_FLAVOR);
    ProjectWorkspace.ProcessResult result =
        workspace.runBuckCommand("build", buildTarget.getFullyQualifiedName());
    result.assertSuccess();

    Path projectRoot = tmp.getRootPath().toRealPath();

    Path inputPath = projectRoot.resolve(buildTarget.getBasePath());
    Path outputPath = projectRoot.resolve(BuildTargets.getGenPath(buildTarget, "%s"));

    assertIsSymbolicLink(
        outputPath.resolve("TestLibrary/PublicHeader.h"), inputPath.resolve("PublicHeader.h"));
  }
 @Test
 public void testAndroidInstrumentationTest() throws IOException {
   tmpFolder.doNotDeleteOnExit();
   ProjectWorkspace workspace =
       TestDataHelper.createProjectWorkspaceForScenario(
           this, "android_instrumentation_test_integration_test", tmpFolder);
   workspace.setUp();
   workspace.runBuckCommand("test").assertSuccess();
 }
Esempio n. 12
0
 @Test
 public void skipsFirstCache() throws IOException {
   Path fullPath = Paths.get("some/path");
   ProjectFilesystem filesystem = new FakeProjectFilesystem(tmp.getRoot());
   DefaultFileHashCache innerCache = new DefaultFileHashCache(filesystem);
   StackedFileHashCache cache = new StackedFileHashCache(ImmutableList.of(innerCache));
   expectedException.expect(NoSuchFileException.class);
   cache.get(fullPath);
 }
Esempio n. 13
0
  @Test
  public void usesSecondCache() throws IOException {
    Path path = Paths.get("world.txt");
    Path fullPath = tmp2.getRootPath().resolve(path);

    DefaultFileHashCache innerCache =
        new DefaultFileHashCache(new ProjectFilesystem(tmp.getRootPath()));

    // The second project filesystem has the file.
    ProjectFilesystem filesystem2 = new ProjectFilesystem(tmp2.getRootPath());
    DefaultFileHashCache innerCache2 = new DefaultFileHashCache(filesystem2);
    filesystem2.touch(path);

    StackedFileHashCache cache =
        new StackedFileHashCache(ImmutableList.of(innerCache, innerCache2));
    cache.get(fullPath);
    assertTrue(innerCache2.willGet(path));
  }
 private void assertHasEntry(
     Map<String, CxxCompilationDatabaseEntry> fileToEntry, String fileName, List<String> command)
     throws IOException {
   String key = tmp.getRootPath().toRealPath().resolve(fileName).toString();
   CxxCompilationDatabaseEntry entry = fileToEntry.get(key);
   assertNotNull("There should be an entry for " + key + ".", entry);
   MoreAsserts.assertIterablesEquals(command, entry.args);
   assertEquals(
       Joiner.on(' ').join(Iterables.transform(command, Escaper.SHELL_ESCAPER)), entry.command);
 }
 @Before
 public void enableVerboseRuleKeys() throws Exception {
   lastPositionInLog = 0;
   ruleKeyBuilderLogger = Logger.getLogger(RuleKeyBuilder.class.getName());
   previousRuleKeyBuilderLevel = ruleKeyBuilderLogger.getLevel();
   ruleKeyBuilderLogger.setLevel(Level.FINER);
   Path fullLogFilePath = tmp.getRootPath().resolve(LOG_FILE_PATH);
   Files.createDirectories(fullLogFilePath.getParent());
   FileHandler handler = new FileHandler(fullLogFilePath.toString());
   handler.setFormatter(new LogFormatter());
   ruleKeyBuilderLogger.addHandler(handler);
 }
 private ProcessExecutor.Result runRuleKeyDiffer(ProjectWorkspace workspace)
     throws IOException, InterruptedException {
   ProcessExecutor.Result result =
       workspace.runCommand(
           "python2.7",
           Paths.get("scripts", "diff_rulekeys.py").toString(),
           tmp.getRootPath().resolve("buck-0.log").toString(),
           tmp.getRootPath().resolve("buck-1.log").toString(),
           "//:java_lib_2");
   assertThat(result.getStderr(), Matchers.equalTo(Optional.of("")));
   assertThat(result.getExitCode(), Matchers.is(0));
   return result;
 }
  @Test
  public void testAppleLibraryBuildsSomething() throws IOException {
    assumeTrue(Platform.detect() == Platform.MACOS);
    ProjectWorkspace workspace =
        TestDataHelper.createProjectWorkspaceForScenario(
            this, "apple_library_builds_something", tmp);
    workspace.setUp();

    ProjectWorkspace.ProcessResult result =
        workspace.runBuckCommand("build", "//Libraries/TestLibrary:TestLibrary#static,default");
    result.assertSuccess();

    assertTrue(Files.exists(tmp.getRootPath().resolve(BuckConstant.GEN_DIR)));
  }
  private Jsr199Javac createJavac(boolean withSyntaxError, Optional<Path> javacJar)
      throws IOException {

    File exampleJava = tmp.newFile("Example.java");
    Files.write(
        Joiner.on('\n')
            .join(
                "package com.example;",
                "",
                "public class Example {" + (withSyntaxError ? "" : "}")),
        exampleJava,
        Charsets.UTF_8);

    Path pathToOutputDirectory = Paths.get("out");
    tmp.newFolder(pathToOutputDirectory.toString());

    Optional<SourcePath> jar =
        javacJar.transform(SourcePaths.toSourcePath(new FakeProjectFilesystem()));
    if (jar.isPresent()) {
      return new JarBackedJavac("com.sun.tools.javac.api.JavacTool", ImmutableSet.of(jar.get()));
    }

    return new JdkProvidedInMemoryJavac();
  }
  @Test
  public void shouldFindNeededDependenciesFromSymbols() throws IOException, InterruptedException {
    ProjectWorkspace workspace =
        TestDataHelper.createProjectWorkspaceForScenario(this, "symbol_finder", temporaryFolder);
    workspace.setUp();

    ProjectFilesystem projectFilesystem = new ProjectFilesystem(temporaryFolder.getRootPath());
    ImmutableMap<String, String> environment = ImmutableMap.copyOf(System.getenv());

    Config rawConfig =
        Config.createDefaultConfig(
            projectFilesystem.getRootPath(),
            ImmutableMap.<String, ImmutableMap<String, String>>of());
    BuckConfig config =
        new BuckConfig(
            rawConfig, projectFilesystem, Architecture.detect(), Platform.detect(), environment);
    ImmutableSet<Description<?>> allDescriptions =
        DefaultKnownBuildRuleTypes.getDefaultKnownBuildRuleTypes(projectFilesystem)
            .getAllDescriptions();
    BuckEventBus buckEventBus = BuckEventBusFactory.newInstance();

    MissingSymbolsHandler missingSymbolsHandler =
        MissingSymbolsHandler.create(
            projectFilesystem,
            allDescriptions,
            config,
            buckEventBus,
            new TestConsole(),
            DEFAULT_JAVAC_OPTIONS,
            environment);

    MissingSymbolEvent missingSymbolEvent =
        MissingSymbolEvent.create(
            BuildTargetFactory.newInstance(workspace.getDestPath(), "//java/com/example/b:b"),
            "com.example.a.A",
            MissingSymbolEvent.SymbolType.Java);

    ImmutableSetMultimap<BuildTarget, BuildTarget> neededDeps =
        missingSymbolsHandler.getNeededDependencies(ImmutableList.of(missingSymbolEvent));

    assertEquals(
        "MissingSymbolsHandler failed to find the needed dependency.",
        neededDeps,
        ImmutableSetMultimap.of(
            BuildTargetFactory.newInstance(workspace.getDestPath(), "//java/com/example/b:b"),
            BuildTargetFactory.newInstance(workspace.getDestPath(), "//java/com/example/a:a")));
  }
Esempio n. 20
0
  @Test
  public void whenBuckConfigChangesParserInvalidated() throws IOException, InterruptedException {
    ProjectFilesystem filesystem = new ProjectFilesystem(tmp.getRoot().toPath());

    Object daemon =
        Main.getDaemon(
            new TestCellBuilder()
                .setBuckConfig(
                    FakeBuckConfig.builder()
                        .setSections(
                            ImmutableMap.of(
                                "somesection", ImmutableMap.of("somename", "somevalue")))
                        .build())
                .setFilesystem(filesystem)
                .build(),
            new ObjectMapper());
    assertEquals(
        "Daemon should not be replaced when config equal.",
        daemon,
        Main.getDaemon(
            new TestCellBuilder()
                .setBuckConfig(
                    FakeBuckConfig.builder()
                        .setSections(
                            ImmutableMap.of(
                                "somesection", ImmutableMap.of("somename", "somevalue")))
                        .build())
                .setFilesystem(filesystem)
                .build(),
            new ObjectMapper()));

    assertNotEquals(
        "Daemon should be replaced when config not equal.",
        daemon,
        Main.getDaemon(
            new TestCellBuilder()
                .setBuckConfig(
                    FakeBuckConfig.builder()
                        .setSections(
                            ImmutableMap.of(
                                "somesection", ImmutableMap.of("somename", "someothervalue")))
                        .build())
                .setFilesystem(filesystem)
                .build(),
            new ObjectMapper()));
  }
  @Test
  public void testAppleLibraryBuildsForWatchSimulator() throws IOException {
    assumeTrue(
        AppleNativeIntegrationTestUtils.isApplePlatformAvailable(
            ApplePlatform.builder().setName(ApplePlatform.Name.WATCHSIMULATOR).build()));

    ProjectWorkspace workspace =
        TestDataHelper.createProjectWorkspaceForScenario(
            this, "apple_library_builds_something", tmp);
    workspace.setUp();

    ProjectWorkspace.ProcessResult result =
        workspace.runBuckCommand(
            "build", "//Libraries/TestLibrary:TestLibrary#watchsimulator-i386,static");
    result.assertSuccess();

    assertTrue(Files.exists(tmp.getRootPath().resolve(BuckConstant.GEN_DIR)));
  }
  @Test
  public void testAppleLibraryBuildsSomethingUsingAppleCxxPlatform() throws IOException {
    assumeTrue(Platform.detect() == Platform.MACOS);
    assumeTrue(
        AppleNativeIntegrationTestUtils.isApplePlatformAvailable(
            ApplePlatform.builder().setName(ApplePlatform.Name.MACOSX).build()));

    ProjectWorkspace workspace =
        TestDataHelper.createProjectWorkspaceForScenario(
            this, "apple_library_builds_something", tmp);
    workspace.setUp();

    ProjectWorkspace.ProcessResult result =
        workspace.runBuckCommand(
            "build", "//Libraries/TestLibrary:TestLibrary#static,macosx-x86_64");
    result.assertSuccess();

    assertTrue(Files.exists(tmp.getRootPath().resolve(BuckConstant.GEN_DIR)));
  }
  @Test
  public void testKeepGoingWithBuildReport() throws IOException {
    ProjectWorkspace workspace =
        TestDataHelper.createProjectWorkspaceForScenario(this, "keep_going", tmp).setUp();

    File buildReport = new File(tmpFolderForBuildReport.getRoot(), "build-report.txt");
    workspace
        .runBuckBuild(
            "--build-report",
            buildReport.getAbsolutePath(),
            "--keep-going",
            "//:rule_with_output",
            "//:failing_rule")
        .assertFailure();

    assertTrue(buildReport.exists());
    String buildReportContents = com.google.common.io.Files.toString(buildReport, Charsets.UTF_8);
    String expectedReport =
        Joiner.on('\n')
            .join(
                "{",
                "  \"success\" : false,",
                "  \"results\" : {",
                "    \"//:rule_with_output\" : {",
                "      \"success\" : true,",
                "      \"type\" : \"BUILT_LOCALLY\",",
                "      \"output\" : \"" + GENRULE_OUTPUT + "\"",
                "    },",
                "    \"//:failing_rule\" : {",
                "      \"success\" : false",
                "    }",
                "  },",
                "  \"failures\" : {",
                "    \"//:failing_rule\" : \"//:failing_rule failed with exit code 2:\\ngenrule\"",
                "  }",
                "}");
    assertEquals(expectedReport, buildReportContents);
  }
 private Path createPath(String first, String... more) {
   return tmpFolder.getRootPath().getFileSystem().getPath(first, more);
 }
Esempio n. 25
0
 public VacationIntegrationTest() throws IOException {
   super();
   temporaryFolder.create();
 }
 private ProjectFilesystem createProjectFilesystem() {
   return new ProjectFilesystem(tmp.getRootPath());
 }
 @Before
 public void setUp() {
   pathToSrcsList = Paths.get(tmp.getRoot().getPath(), "srcs_list");
 }