Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[JENKINS-34392] Remove unused logic. Add test suite for basic use cases.
  • Loading branch information
armfergom committed Apr 26, 2016
1 parent 2638dc9 commit adf002f
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 8 deletions.
8 changes: 0 additions & 8 deletions src/main/java/org/jenkinsci/plugins/MsTestBuilder.java
Expand Up @@ -8,9 +8,7 @@
import hudson.Util;
import hudson.model.BuildListener;
import hudson.model.AbstractBuild;
import hudson.model.Computer;
import hudson.model.Descriptor;
import hudson.model.Node;
import hudson.model.AbstractProject;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.Builder;
Expand Down Expand Up @@ -114,12 +112,6 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
args.add(execName);
} else {
EnvVars env = build.getEnvironment(listener);
Node node = Computer.currentComputer().getNode();
if (node == null) {
listener.fatalError("Configuration has changed and node has been removed.");
return false;
}
installation = installation.forNode(node, listener);
installation = installation.forEnvironment(env);
String pathToMsTest = installation.getHome();
FilePath exec = new FilePath(launcher.getChannel(), pathToMsTest);
Expand Down
134 changes: 134 additions & 0 deletions src/test/java/org/jenkinsci/plugins/MsTestRunnerTest.java
@@ -0,0 +1,134 @@
package org.jenkinsci.plugins;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.concurrent.ExecutionException;

import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import hudson.Util;
import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.model.Result;
import hudson.util.jna.GNUCLibrary;

import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

public class MsTestRunnerTest {

private static final String MS_TEST_INSTALLATION = "MSTestInstallation";

private static final String SUCCESS_MESSAGE = "Running MsTest and succeeds";
private static final String FAILURE_MESSAGE = "Running MsTest and fails";

@Rule
public JenkinsRule r = new JenkinsRule();

@Test
public void runTestsBasic() throws URISyntaxException, IOException, InterruptedException, ExecutionException {
// Configure mocked MsTest tool that will not fail
mockMSTestInstallation(false);
FreeStyleProject p = createFreeStyleProjectWithMsTest("", "", false);
FreeStyleBuild build = p.scheduleBuild2(0).get();

assertLogContains(SUCCESS_MESSAGE, build);
assertBuildStatus(Result.SUCCESS, build);
}

@Test
public void runTestsFail() throws InterruptedException, ExecutionException, URISyntaxException, IOException {
// Configure mocked MsTest tool that will fail
mockMSTestInstallation(true);
FreeStyleProject p = createFreeStyleProjectWithMsTest("", "", false);
FreeStyleBuild build = p.scheduleBuild2(0).get();

assertLogContains(FAILURE_MESSAGE, build);
assertBuildStatus(Result.FAILURE, build);
}

@Test
public void ignoreFailingTests() throws InterruptedException, ExecutionException, URISyntaxException, IOException {
// Configure mocked MsTest tool that will fail
mockMSTestInstallation(true);
FreeStyleProject p = createFreeStyleProjectWithMsTest("", "", true);
FreeStyleBuild build = p.scheduleBuild2(0).get();

assertLogContains(FAILURE_MESSAGE, build);
assertBuildStatus(Result.SUCCESS, build);
}

@Test
public void runTestsWithCategories() throws URISyntaxException, IOException, InterruptedException, ExecutionException {
String category = "somecategory";
// Configure mocked MsTest tool that will not fail
mockMSTestInstallation(false);
FreeStyleProject p = createFreeStyleProjectWithMsTest(category, "", true);
FreeStyleBuild build = p.scheduleBuild2(0).get();

assertLogContains(SUCCESS_MESSAGE, build);
assertLogContains("/category:" + category, build);
assertBuildStatus(Result.SUCCESS, build);
}

@Test
public void runTestsWithCmdArguments() throws URISyntaxException, IOException, InterruptedException, ExecutionException {
String cmdArg = "/testsettings:Local.Testsettings";
// Configure mocked MsTest tool that will not fail
mockMSTestInstallation(false);
FreeStyleProject p = createFreeStyleProjectWithMsTest("", cmdArg, true);
FreeStyleBuild build = p.scheduleBuild2(0).get();

assertLogContains(SUCCESS_MESSAGE, build);
assertLogContains(cmdArg, build);
assertBuildStatus(Result.SUCCESS, build);
}

/**
* Configures the MSTest installation.
*
* @param failingInstallation. If true, the use of MsTest will make the build fail.
* @return
* @throws URISyntaxException
*/
private MsTestInstallation mockMSTestInstallation(boolean failingInstallation) throws URISyntaxException {
// Get appropriate installation file depending on parameter
String home = failingInstallation ? getClass().getResource("mstest/tool/mstest-fail").toURI().getPath()
: getClass().getResource("mstest/tool/mstest").toURI().getPath();
// Execution permissions for the tool
GNUCLibrary.LIBC.chmod(home, 0755);
// Configure installation
MsTestInstallation msTestInstallation = new MsTestInstallation(MS_TEST_INSTALLATION, home, null, false);
r.jenkins.getDescriptorByType(MsTestInstallation.DescriptorImpl.class).setInstallations(msTestInstallation);
return msTestInstallation;
}

/**
* Creates a FreeStyleProject with a MSTestRunner build step.
*
* @param categories the test categories to run
* @param cmdLineArgs the cmd line arguments to use when running MsTest
* @param ignoreFailingTests whether to ignore failing tests or not
* @return the FreeStyleProject
* @throws URISyntaxException
* @throws IOException
*/
private FreeStyleProject createFreeStyleProjectWithMsTest(String categories, String cmdLineArgs, boolean ignoreFailingTests) throws URISyntaxException, IOException {
String msTestFiles = getClass().getResource("mstest/testfile").toURI().getPath();
FreeStyleProject p = r.jenkins.createProject(FreeStyleProject.class, "MSTestProject");
p.getBuildersList().add(new MsTestBuilder(MS_TEST_INSTALLATION, msTestFiles, categories, "resultFile", cmdLineArgs, ignoreFailingTests));
return p;
}

private void assertLogContains(String text, FreeStyleBuild build) throws IOException {
String log = Util.loadFile(build.getLogFile(), build.getCharset());
assertTrue(log.contains(text));
}

private void assertBuildStatus(Result expectedStatus, FreeStyleBuild build) {
assertThat(build.getResult(), equalTo(expectedStatus));
}
}
2 changes: 2 additions & 0 deletions src/test/resources/org/jenkinsci/plugins/mstest/testfile
@@ -0,0 +1,2 @@
Content not relevant.
Just need the file to be present for the test.
@@ -0,0 +1 @@
echo "Running MsTest and succeeds"
@@ -0,0 +1,2 @@
echo "Running MsTest and fails"
exit 1

0 comments on commit adf002f

Please sign in to comment.