@Test
  public void shouldPopulatePropertiesContentWithCustomWorkspace() throws Exception {
    final String customWorkspaceValue = tmp.newFolder().getAbsolutePath();
    String customEnvVarName = "materialize_workspace_path";
    String customEnvVarValue = "${WORKSPACE}/materialize_workspace";

    FreeStyleProject project = j.createFreeStyleProject();
    project.setCustomWorkspace(customWorkspaceValue);

    EnvVars.masterEnvVars.remove("WORKSPACE"); // ensure build node don't have such var already

    EnvInjectBuildWrapper envInjectBuildWrapper = new EnvInjectBuildWrapper();
    envInjectBuildWrapper.setInfo(withPropContent(customEnvVarName + "=" + customEnvVarValue));
    project.getBuildWrappersList().add(envInjectBuildWrapper);

    FreeStyleBuild build = j.buildAndAssertSuccess(project);

    // Retrieve build workspace
    String buildWorkspaceValue = build.getWorkspace().getRemote();
    assertThat(
        "1. Should see actual ws equal to custom",
        buildWorkspaceValue,
        equalTo(customWorkspaceValue));

    // Compute value with workspace
    String expectedCustomEnvVarValue =
        replaceMacro(customEnvVarValue, of("WORKSPACE", buildWorkspaceValue));

    assertThat(
        "2. Property should be resolved with custom ws in build",
        build,
        withEnvInjectAction(map(hasEntry(customEnvVarName, expectedCustomEnvVarValue))));
  }
  @Test
  public void doNothingIfItWillNotShortenThePath() throws Exception {
    DumbSlave s = j.createOnlineSlave();

    // Would be turned into 'short_project...XXXXXXXX' which is in fact longer than the original
    FreeStyleProject p = j.createFreeStyleProject("short_project_name");
    p.setAssignedNode(s);

    // Not enough for anything
    setMaxPathLength(s, 1);

    FreeStyleBuild b = p.scheduleBuild2(0).get();
    assertThat(
        b.getWorkspace().getRemote(),
        equalTo(s.getRootPath() + DS + "workspace" + DS + p.getFullName().replace("/", DS)));
  }
  @Test
  public void doNothingIfThereIsEnoughRoom() throws Exception {
    DumbSlave s = j.createOnlineSlave();

    MockFolder f = j.createFolder("this_is_my_folder_alright");
    FreeStyleProject p = f.createProject(FreeStyleProject.class, "and_a_project_in_it");
    p.setAssignedNode(s);

    // Enough for the test - even on windows
    setMaxPathLength(s, 4096);

    FreeStyleBuild b = p.scheduleBuild2(0).get();
    assertThat(
        b.getWorkspace().getRemote(),
        equalTo(s.getRootPath() + DS + "workspace" + DS + p.getFullName().replace("/", DS)));
  }
  @Test
  public void unwrapFolders() throws Exception {
    DumbSlave s = j.createOnlineSlave();

    MockFolder f = j.createFolder("this_is_my_folder_alright");
    FreeStyleProject p = f.createProject(FreeStyleProject.class, "and_a_project_in_it");
    p.setAssignedNode(s);

    // Not enough for anything
    setMaxPathLength(s, 1);

    FreeStyleBuild b = p.scheduleBuild2(0).get();
    String buildWs = b.getWorkspace().getRemote();
    String wsDir = s.getRootPath() + DS + "workspace" + DS;
    assertThat(buildWs, startsWith(wsDir + "and_a_pro"));
    assertThat(buildWs, buildWs.length(), equalTo(wsDir.length() + 24));
  }
  public void testManualAbortProcess() throws Exception {
    ProcessTree.enabled = true;
    FreeStyleProject project = createFreeStyleProject();

    // this contains a maven project with a single test that sleeps 5s.
    project.setScm(
        new ExtractResourceSCM(getClass().getResource("ProcessTreeKiller-test-project.jar")));
    project.getBuildersList().add(new Maven("install", "maven"));

    // build the project, wait until tests are running, then cancel.
    project.scheduleBuild2(0).waitForStart();

    FreeStyleBuild b = project.getLastBuild();
    b.doStop();

    Thread.sleep(1000);

    // will fail (at least on windows) if test process is still running
    b.getWorkspace().deleteRecursive();
  }
  /**
   * Also accepts variable expression.
   *
   * @throws Exception
   */
  @Test
  public void testVariableExpression() throws Exception {
    // Prepare an artifact to be copied.
    FreeStyleProject copiee = j.createFreeStyleProject();
    copiee.getBuildersList().add(new FileWriteBuilder("artifact.txt", "foobar"));
    copiee.getPublishersList().add(new ArtifactArchiver("artifact.txt"));
    j.assertBuildStatusSuccess(copiee.scheduleBuild2(0));

    FreeStyleProject copier = j.createFreeStyleProject();
    ParameterizedBuildSelector pbs = new ParameterizedBuildSelector("${SELECTOR}");
    copier
        .getBuildersList()
        .add(
            CopyArtifactUtil.createCopyArtifact(
                copiee.getFullName(),
                null, // parameters
                pbs,
                "**/*", // filter
                "", // excludes
                false, // flatten
                false, // optional
                false // finterprintArtifacts
                ));
    FreeStyleBuild b =
        j.assertBuildStatusSuccess(
            (FreeStyleBuild)
                copier
                    .scheduleBuild2(
                        0,
                        new ParametersAction(
                            new StringParameterValue(
                                "SELECTOR",
                                "<StatusBuildSelector><stable>true</stable></StatusBuildSelector>")))
                    .get());

    assertEquals("foobar", b.getWorkspace().child("artifact.txt").readToString());
  }