Basic check for printed environment vars in a java build.
package io.jenkins.plugins.analysis.warnings.plugins;
+import java.io.BufferedInputStream;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.util.Arrays;
+import java.util.List;
+
import org.junit.Test;
+import com.google.common.collect.Lists;
+
import org.jenkinsci.plugins.envinject.EnvInjectBuildWrapper;
import org.jenkinsci.plugins.envinject.EnvInjectJobPropertyInfo;
import org.jenkinsci.plugins.envinject.EnvInjectPlugin;
+import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.model.Result;
+import io.jenkins.plugins.analysis.core.model.AnalysisResult;
import io.jenkins.plugins.analysis.core.testutil.IntegrationTestWithJenkinsPerTest;
import io.jenkins.plugins.analysis.warnings.Java;
+import static io.jenkins.plugins.analysis.core.assertions.Assertions.*;
+
public class EnvInjectPluginITest extends IntegrationTestWithJenkinsPerTest {
@Test
- public void shouldRunWithEnvPlugin() {
+ public void shouldRunWithEnvPlugin() throws IOException {
// Set up the project and configure the java warnings
FreeStyleProject project = createFreeStyleProject();
@@ -24,13 +38,44 @@ public class EnvInjectPluginITest extends IntegrationTestWithJenkinsPerTest {
createFileWithJavaWarnings(project, 1, 2);
- EnvInjectPlugin envInjectPlugin = new EnvInjectPlugin();
- EnvInjectBuildWrapper envInjectBuildWrapper = new EnvInjectBuildWrapper(new EnvInjectJobPropertyInfo());
+ EnvInjectBuildWrapper envInjectBuildWrapper = new EnvInjectBuildWrapper(new EnvInjectJobPropertyInfo(
+ null, "HELLO_WORLD=hello_test\nMY_ENV_VAR=42",
+ null, null, false, null
+ ));
+
+ // Set the env inject plugin to run and add a script step which will print out all set env vars
project.getBuildWrappersList().add(envInjectBuildWrapper);
+ addScriptStep(project, "printenv");
+
+ // Do the actual build
+ AnalysisResult analysisResult = scheduleBuildAndAssertStatus(project, Result.SUCCESS);
+ // Assert that the printenv command displayed both of our set variables
+ // Each env var will be printed out by the plugin itself once, so we need to search for two occurrences
+ assertThatLogContainsNTimes(project, "HELLO_WORLD=hello_test", 2);
+ assertThatLogContainsNTimes(project, "MY_ENV_VAR=42", 2);
- scheduleBuildAndAssertStatus(project, Result.SUCCESS);
+ assertThat(analysisResult).hasTotalSize(2);
+ }
+
+ private void assertThatLogContainsNTimes(final FreeStyleProject project, final String element, final int times) throws IOException {
+ FreeStyleBuild lastBuild = project.getLastBuild();
+ assertThat(lastBuild).isNotNull();
+
+ InputStream inputStream = lastBuild.getLogInputStream();
+ BufferedReader logReader = new BufferedReader(new InputStreamReader(inputStream));
+
+ int foundOccurences = 0;
+ String logLine = logReader.readLine();
+ while (logLine != null) {
+ if (logLine.equals(element)) {
+ foundOccurences++;
+ }
+ logLine = logReader.readLine();
+ }
+ assertThat(foundOccurences).withFailMessage("Element was not found as often as expected in log")
+ .isEqualTo(times);
}
