Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[JENKINS-38617] System exit when running system groovy. WIP
  • Loading branch information
v1v committed Sep 29, 2016
1 parent 2d83215 commit c63106a
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Expand Up @@ -96,6 +96,13 @@
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>groovy</artifactId>
<version>1.10</version>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>ws-cleanup</artifactId>
Expand Down
@@ -0,0 +1,62 @@
package org.jenkins.ci.plugins.jenkinslint.check;

import hudson.matrix.MatrixProject;
import hudson.maven.MavenModuleSet;
import hudson.model.Item;
import hudson.model.Project;
import hudson.plugins.groovy.StringScriptSource;
import hudson.tasks.Builder;
import jenkins.model.Jenkins;
import org.jenkins.ci.plugins.jenkinslint.model.AbstractCheck;

import java.util.List;
import java.util.logging.Level;

/**
* @author Victor Martinez
*/
public class GroovySystemExitChecker extends AbstractCheck {

public GroovySystemExitChecker() {
super();
this.setDescription("By distributing the wrapper with your project, anyone can work with it without needing to " +
"install Gradle beforehand. Even better, users of the build <br/> are guaranteed to use the " +
"version of Gradle that the build was designed to work with. Further details: " +
"<a href=https://docs.gradle.org/current/userguide/gradle_wrapper.html> Gradle Wrapper docs</a>.");
this.setSeverity("High");
}

public boolean executeCheck(Item item) {
boolean found = false;
if (Jenkins.getInstance().pluginManager.getPlugin("groovy") != null) {

if (Jenkins.getInstance().pluginManager.getPlugin("maven-plugin")!=null) {
if (item instanceof MavenModuleSet) {
found = isSystemExit(((MavenModuleSet) item).getPrebuilders());
}
}
if (item instanceof Project) {
found = isSystemExit(((Project) item).getBuilders());
}
if (item instanceof MatrixProject) {
found = isSystemExit(((MatrixProject) item).getBuilders());
}

}
return found;
}

private boolean isSystemExit (List<Builder> builders) {
boolean status = false;
if (builders != null && builders.size() > 0 ) {
for (Builder builder : builders) {
if (builder instanceof hudson.plugins.groovy.SystemGroovy) {
if ((( hudson.plugins.groovy.SystemGroovy) builder).getCommand().toLowerCase().contains("system.exit") ) {
status = true;
}
}
}
}
return status;
}
}
@@ -0,0 +1,99 @@
package org.jenkins.ci.plugins.jenkinslint.check;

import hudson.matrix.MatrixProject;
import hudson.maven.MavenModuleSet;
import hudson.model.FreeStyleProject;
import hudson.plugins.groovy.ScriptSource;
import hudson.plugins.groovy.StringScriptSource;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

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

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

@Rule public JenkinsRule j = new JenkinsRule();
@Test public void testDefaultJob() throws Exception {
FreeStyleProject project = j.createFreeStyleProject();
assertFalse(checker.executeCheck(project));
}
@Test public void testEmptyJobName() throws Exception {
FreeStyleProject project = j.createFreeStyleProject("");
assertFalse(checker.executeCheck(project));
}
@Test public void testMavenJobName() throws Exception {
MavenModuleSet project = j.createMavenProject();
assertFalse(checker.executeCheck(project));
}
//@Issue("JENKINS-29444")
@Test public void testMatrixProject() throws Exception {
MatrixProject project = j.createMatrixProject();
assertFalse(checker.executeCheck(project));
}
@Test public void testMatrixProjectWithSystemGroovy() throws Exception {
MatrixProject project = j.createMatrixProject("WithoutSystem");
project.getBuildersList().add(new hudson.plugins.groovy.SystemGroovy(new StringScriptSource("println 'hi'"),null,null));
assertFalse(checker.executeCheck(project));
project.delete();
project = j.createMatrixProject("WithSystem");
project.getBuildersList().add(new hudson.plugins.groovy.SystemGroovy(new StringScriptSource("System.exit(0)"),null,null));
project.save();
assertTrue(checker.executeCheck(project));
}
@Test public void testJobWithSystemGroovy() throws Exception {
FreeStyleProject project = j.createFreeStyleProject("WithoutSystem");
project.getBuildersList().add(new hudson.plugins.groovy.SystemGroovy(new StringScriptSource("println 'hi'"),null,null));
assertFalse(checker.executeCheck(project));
project.delete();
project = j.createFreeStyleProject("WithSystem");
project.getBuildersList().add(new hudson.plugins.groovy.SystemGroovy(new StringScriptSource("System.exit(0)"),null,null));
project.save();
assertTrue(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());
assertTrue(checker.isIgnored(project.getDescription()));
project.delete();
MavenModuleSet mavenProject = j.createMavenProject();
assertFalse(checker.isIgnored(mavenProject.getDescription()));
mavenProject.setDescription("#lint:ignore:" + checker.getClass().getSimpleName());
assertTrue(checker.isIgnored(mavenProject.getDescription()));
}
//@Issue("JENKINS-29427")
@Test public void testAnotherBuilders() throws Exception {
FreeStyleProject project = j.createFreeStyleProject("MsBuildBuilder");
project.getBuildersList().add(new hudson.plugins.msbuild.MsBuildBuilder("", "", "", true, true, true));
assertFalse(checker.executeCheck(project));
project.delete();
project = j.createFreeStyleProject("Ant");
project.getBuildersList().add(new hudson.tasks.Ant("","","","",""));
assertFalse(checker.executeCheck(project));
project.delete();
}
//@Issue("JENKINS-38616")
@Test public void testMavenModuleJob() throws Exception {
MavenModuleSet project = j.createMavenProject();
assertFalse(checker.executeCheck(project));
}
//@Issue("JENKINS-38616")
@Test public void testMavenModuleJobbWithGroovy() throws Exception {
MavenModuleSet project = j.createMavenProject("WithoutSystem");
project.getPrebuilders().add(new hudson.plugins.groovy.SystemGroovy(new StringScriptSource("println 'hi'"),null,null));
project.save();
assertFalse(checker.executeCheck(project));
project.delete();
project = j.createMavenProject("WithSystem");
project.getPrebuilders().add(new hudson.plugins.groovy.SystemGroovy(new StringScriptSource("System.exit(0)"),null,null));
assertTrue(checker.executeCheck(project));
}
}

0 comments on commit c63106a

Please sign in to comment.