@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))));
  }
  /**
   * When a build is marked as NOT_BUILD And more builds are started And there are no changes Then
   * all following builds should also be marked NOT_BUILD
   */
  public void testShouldMarkBuildsAsNotBuilt() throws Exception {
    setup();
    File dir = createTempDirectory();
    MercurialBridge plugin = new MercurialBridge(false, "test", "default");
    plugin.setWorkingDirectory(new FilePath(dir));

    hg(dir, "init");
    shell(dir, "touch", "foo");
    hg(dir, "add", "foo");
    hg(dir, "commit", "-m", "\"added foo\"");
    // String rev = hg(dir,"tip","--template","{node}").toString();
    hg(dir, "branch", "test");
    shell(dir, "touch", "bar");
    hg(dir, "add", "bar");
    hg(dir, "commit", "-m", "\"added bar\"");

    MercurialSCM scm =
        new MercurialSCM(null, dir.getAbsolutePath(), "test", null, null, null, true);
    FreeStyleProject project =
        Hudson.getInstance().createProject(FreeStyleProject.class, "testproject");
    project.setScm(scm);
    project.getBuildWrappersList().add(new PretestedIntegrationBuildWrapper(plugin));

    Future<FreeStyleBuild> f = project.scheduleBuild2(0);
    FreeStyleBuild build = f.get();
    assertEquals(Result.SUCCESS, build.getResult());
    f = project.scheduleBuild2(0);
    build = f.get();
    assertEquals(Result.NOT_BUILT, build.getResult());
    f = project.scheduleBuild2(0);
    build = f.get();
    assertEquals(Result.NOT_BUILT, build.getResult());
    cleanup(dir);
  }
  @Test
  public void injectFromScript() throws Exception {
    FreeStyleProject p = j.createFreeStyleProject();

    p.setScm(new SingleFileSCM("vars.groovy", "return ['FILE_VAR': 'fvalue']"));
    EnvInjectBuildWrapper wrapper = new EnvInjectBuildWrapper();
    p.getBuildWrappersList().add(wrapper);
    wrapper.setInfo(
        new EnvInjectJobPropertyInfo(
            null, null, null, null, "return ['GROOVY_VAR': 'gvalue']", false));

    CaptureEnvironmentBuilder capture = new CaptureEnvironmentBuilder();
    p.getBuildersList().add(capture);
    p.scheduleBuild2(0).get();

    assertEquals("gvalue", capture.getEnvVars().get("GROOVY_VAR"));
  }
  @Test
  public void injectText() throws Exception {
    FreeStyleProject p = j.createFreeStyleProject();

    p.setScm(new SingleFileSCM("vars.properties", "FILE_VAR=fvalue"));
    EnvInjectBuildWrapper wrapper = new EnvInjectBuildWrapper();
    p.getBuildWrappersList().add(wrapper);
    wrapper.setInfo(
        new EnvInjectJobPropertyInfo(
            "vars.properties", "TEXT_VAR=tvalue", null, null, null, false));

    CaptureEnvironmentBuilder capture = new CaptureEnvironmentBuilder();
    p.getBuildersList().add(capture);
    p.scheduleBuild2(0).get();

    assertEquals("tvalue", capture.getEnvVars().get("TEXT_VAR"));
    assertEquals("fvalue", capture.getEnvVars().get("FILE_VAR"));
  }