Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[JENKINS-42268] Added CyclomaticComplexity test cases (WiP)
  • Loading branch information
v1v committed Aug 13, 2017
1 parent 7eaac06 commit 96ed145
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pom.xml
Expand Up @@ -195,6 +195,20 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>conditional-buildstep</artifactId>
<version>1.3.3</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>run-condition</artifactId>
<version>1.0</version>
<scope>test</scope>
</dependency>

<!-- JENKINS-29427 -->
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
Expand Down
@@ -0,0 +1,87 @@
package org.jenkins.ci.plugins.jenkinslint.check;

import hudson.matrix.MatrixProject;
import hudson.maven.MavenModuleSet;
import hudson.model.FreeStyleProject;
import hudson.tasks.BuildStep;
import hudson.tasks.Shell;
import org.jenkins_ci.plugins.run_condition.core.AlwaysRun;
import org.jenkins_ci.plugins.run_condition.core.NeverRun;
import org.jenkins_ci.plugins.run_condition.core.StringsMatchCondition;
import org.jenkins_ci.plugins.run_condition.logic.ConditionContainer;
import org.jenkins_ci.plugins.run_condition.logic.Or;
import org.jenkinsci.plugins.conditionalbuildstep.ConditionalBuilder;
import org.jenkins_ci.plugins.run_condition.BuildStepRunner;
import org.jenkins_ci.plugins.run_condition.core.BooleanCondition;
import org.jenkins_ci.plugins.run_condition.logic.And;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import java.util.ArrayList;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

/**
* CyclomaticComplexityChecker Test Case.
*
* @author Victor Martinez
*/
public class CyclomaticComplexityCheckerTestCase {
private CyclomaticComplexityChecker checker = new CyclomaticComplexityChecker();

@Rule public JenkinsRule j = new JenkinsRule();

private ConditionalBuilder booleanCondition() {
ArrayList<BuildStep> list = new ArrayList<BuildStep>();
list.add(new Shell("ls"));
list.add(new Shell("ls"));
return new ConditionalBuilder(new BooleanCondition("true"), new BuildStepRunner.Run(), list);
}
private ConditionalBuilder andCondition() {
ArrayList<BuildStep> list = new ArrayList<BuildStep>();
list.add(new Shell("ls"));
list.add(new Shell("ls"));
ArrayList<ConditionContainer> conditions = new ArrayList<ConditionContainer>();
conditions.add(new ConditionContainer(new AlwaysRun()));
conditions.add(new ConditionContainer(new StringsMatchCondition("test", "test", false)));
return new ConditionalBuilder(new And(conditions), new BuildStepRunner.Run(), list);
}
private ConditionalBuilder orCondition() {
ArrayList<BuildStep> list = new ArrayList<BuildStep>();
list.add(new Shell("ls"));
list.add(new Shell("ls"));
ArrayList<ConditionContainer> conditions = new ArrayList<ConditionContainer>();
conditions.add(new ConditionContainer(new AlwaysRun()));
conditions.add(new ConditionContainer(new StringsMatchCondition("test", "test", false)));
return new ConditionalBuilder(new Or(conditions), new BuildStepRunner.Run(), list);
}
@Test public void testEmptyJob() throws Exception {
FreeStyleProject project = j.createFreeStyleProject();
assertFalse(checker.executeCheck(project));
}
@Test public void testConditionJob() throws Exception {
FreeStyleProject project = j.createFreeStyleProject();
project.getBuildersList().add(booleanCondition());
project.getBuildersList().add(booleanCondition());
project.getBuildersList().add(andCondition());
project.getBuildersList().add(andCondition());
project.getBuildersList().add(orCondition());
project.getBuildersList().add(orCondition());
assertTrue(checker.executeCheck(project));
}
@Test public void testMavenModuleJob() throws Exception {
MavenModuleSet project = j.createMavenProject();
assertFalse(checker.executeCheck(project));
}
@Test public void testMatrixProject() throws Exception {
MatrixProject project = j.createMatrixProject();
assertFalse(checker.executeCheck(project));
}
@Test public void testControlComment() throws Exception {
FreeStyleProject project = j.createFreeStyleProject();
assertFalse(checker.isIgnored(project.getDescription()));
project.setDescription("#lint:ignore:" + checker.getClass().getSimpleName());
}
}

0 comments on commit 96ed145

Please sign in to comment.