Skip to content
Permalink
Browse files

[JENKINS-46035] Empty and Blank lines should be ignored

Change-Id: I0baa892f0c9e21852b5a423d2189f79fbc119985
  • Loading branch information
v1v committed Aug 9, 2017
1 parent 44cc101 commit 5d985b2bcb22b2a3b04978d46fa12f10c91297e6
@@ -5,6 +5,7 @@
import hudson.tasks.Builder;
import hudson.tasks.CommandInterpreter;
import org.jenkins.ci.plugins.jenkinslint.model.AbstractCheck;
import org.jenkins.ci.plugins.jenkinslint.utils.StringUtils;

import java.util.List;
import java.util.logging.Level;
@@ -71,7 +72,13 @@ private boolean isBuilderHarcoded (List<Builder> builders) {

private boolean isHarcoded (String content, int threshold) {
if (content != null) {
return content.split("\r\n|\r|\n").length > threshold;
int length = 0;
for (String line : content.split("\r\n|\r|\n")) {
if (!StringUtils.isEmptyOrBlank(line)) {
length++;
}
}
return length > threshold;
} else {
return false;
}
@@ -18,24 +18,29 @@
public class HardcodedScriptCheckerTestCase extends AbstractTestCase {
private HardcodedScriptChecker checker = new HardcodedScriptChecker(true, HardcodedScriptChecker.THRESHOLD);

@Test public void testDefaultJob() throws Exception {
@Test
public void testDefaultJob() throws Exception {
FreeStyleProject project = j.createFreeStyleProject();
assertFalse(checker.executeCheck(project));
}
@Test public void testEmptyJobName() throws Exception {
@Test
public void testEmptyJobName() throws Exception {
FreeStyleProject project = j.createFreeStyleProject("");
assertFalse(checker.executeCheck(project));
}
@Test public void testMavenJobName() throws Exception {
@Test
public void testMavenJobName() throws Exception {
MavenModuleSet project = j.createMavenProject();
assertFalse(checker.executeCheck(project));
}
@Issue("JENKINS-38616")
@Test public void testMatrixProject() throws Exception {
@Test
public void testMatrixProject() throws Exception {
MatrixProject project = j.createMatrixProject();
assertFalse(checker.executeCheck(project));
}
@Test public void testMatrixProjectWithHardcodedScript() throws Exception {
@Test
public void testMatrixProjectWithHardcodedScript() throws Exception {
MatrixProject project = j.createMatrixProject("Bash_Single_Line");
project.getBuildersList().add(new hudson.tasks.Shell("#!/bin/bash #single line"));
assertFalse(checker.executeCheck(project));
@@ -59,7 +64,8 @@
assertFalse(checker.executeCheck(project));
checker.setThreshold(threshold);
}
@Test public void testJobWithHardcodedScript() throws Exception {
@Test
public void testJobWithHardcodedScript() throws Exception {
FreeStyleProject project = j.createFreeStyleProject("Bash_Single_Line");
project.getBuildersList().add(new hudson.tasks.Shell("#!/bin/bash #single line"));
assertFalse(checker.executeCheck(project));
@@ -83,7 +89,8 @@
assertFalse(checker.executeCheck(project));
checker.setThreshold(threshold);
}
@Test public void testControlComment() throws Exception {
@Test
public void testControlComment() throws Exception {
FreeStyleProject project = j.createFreeStyleProject();
assertFalse(checker.isIgnored(project.getDescription()));
project.setDescription("#lint:ignore:" + checker.getClass().getSimpleName());
@@ -94,23 +101,26 @@
assertTrue(checker.isIgnored(mavenProject.getDescription()));
}
@Issue("JENKINS-38616")
@Test public void testAnotherBuilders() throws Exception {
@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("","","","",""));
project.getBuildersList().add(new hudson.tasks.Ant("", "", "", "", ""));
assertFalse(checker.executeCheck(project));
project.delete();
}
@Issue("JENKINS-38616")
@Test public void testMavenModuleJob() throws Exception {
@Test
public void testMavenModuleJob() throws Exception {
MavenModuleSet project = j.createMavenProject();
assertFalse(checker.executeCheck(project));
}
@Issue("JENKINS-38616")
@Test public void testMavenModuleJobbWithHardcodedScript() throws Exception {
@Test
public void testMavenModuleJobbWithHardcodedScript() throws Exception {
MavenModuleSet project = j.createMavenProject();
project.getPrebuilders().add(new hudson.tasks.Shell("#!/bin/bash #single line"));
assertFalse(checker.executeCheck(project));
@@ -134,4 +144,85 @@
assertFalse(checker.executeCheck(project));
checker.setThreshold(threshold);
}
@Issue("JENKINS-46035")
@Test
public void testJobWithHardcodedAndBlankLinesScript() throws Exception {
FreeStyleProject project = j.createFreeStyleProject("Bash_Single_Line");
project.getBuildersList().add(new hudson.tasks.Shell("#!/bin/bash #single line"));
assertFalse(checker.executeCheck(project));
project.delete();
project = j.createFreeStyleProject("Bash_Multiple_Empty_Lines");
project.getBuildersList().add(new hudson.tasks.Shell("#!/bin/bash\n\n\n\n\n\n"));
assertFalse(checker.executeCheck(project));
project.delete();
project = j.createFreeStyleProject("Batch_Multiple_And_Tab_Lines");
project.getBuildersList().add(new hudson.tasks.Shell("echo first\n\t\n\t\n\t\n\t\n\t\n\t"));
assertFalse(checker.executeCheck(project));
project.delete();
project = j.createFreeStyleProject("Batch_Multiple_And_Space_Lines");
project.getBuildersList().add(new hudson.tasks.Shell("echo first\n \n \n \n \n \n "));
assertFalse(checker.executeCheck(project));
project.delete();
project = j.createFreeStyleProject("Batch_Multiple_Line_2");
project.getBuildersList().add(new hudson.tasks.Shell("echo first\n\n\n\n\n\nline1"));
assertFalse(checker.executeCheck(project));
project.delete();
project = j.createFreeStyleProject("Batch_Multiple_Line_3");
project.getBuildersList().add(new hudson.tasks.Shell("echo first\n\n\n\n\n\nline1\nline2"));
assertTrue(checker.executeCheck(project));
}
@Issue("JENKINS-46035")
@Test
public void testMavenModuleJobWithHardcodedAndBlankLinesScript() throws Exception {
MavenModuleSet project = j.createMavenProject("Bash_Single_Line");
project.getPrebuilders().add(new hudson.tasks.Shell("#!/bin/bash #single line"));
assertFalse(checker.executeCheck(project));
project.delete();
project = j.createMavenProject("Bash_Multiple_Empty_Lines");
project.getPrebuilders().add(new hudson.tasks.Shell("#!/bin/bash\n\n\n\n\n\n"));
assertFalse(checker.executeCheck(project));
project.delete();
project = j.createMavenProject("Batch_Multiple_And_Tab_Lines");
project.getPrebuilders().add(new hudson.tasks.Shell("echo first\n\t\n\t\n\t\n\t\n\t\n\t"));
assertFalse(checker.executeCheck(project));
project.delete();
project = j.createMavenProject("Batch_Multiple_And_Space_Lines");
project.getPrebuilders().add(new hudson.tasks.Shell("echo first\n \n \n \n \n \n "));
assertFalse(checker.executeCheck(project));
project.delete();
project = j.createMavenProject("Batch_Multiple_Line_2");
project.getPrebuilders().add(new hudson.tasks.Shell("echo first\n\n\n\n\n\nline1"));
assertFalse(checker.executeCheck(project));
project.delete();
project = j.createMavenProject("Batch_Multiple_Line_3");
project.getPrebuilders().add(new hudson.tasks.Shell("echo first\n\n\n\n\n\nline1\nline2"));
assertTrue(checker.executeCheck(project));
}
@Issue("JENKINS-46035")
@Test
public void testMatrixProjectWithHardcodedAndBlankLinesScript() throws Exception {
MatrixProject project = j.createMatrixProject("Bash_Single_Line");
project.getBuildersList().add(new hudson.tasks.Shell("#!/bin/bash #single line"));
assertFalse(checker.executeCheck(project));
project.delete();
project = j.createMatrixProject("Bash_Multiple_Empty_Lines");
project.getBuildersList().add(new hudson.tasks.Shell("#!/bin/bash\n\n\n\n\n\n"));
assertFalse(checker.executeCheck(project));
project.delete();
project = j.createMatrixProject("Batch_Multiple_And_Tab_Lines");
project.getBuildersList().add(new hudson.tasks.Shell("echo first\n\t\n\t\n\t\n\t\n\t\n\t"));
assertFalse(checker.executeCheck(project));
project.delete();
project = j.createMatrixProject("Batch_Multiple_And_Space_Lines");
project.getBuildersList().add(new hudson.tasks.Shell("echo first\n \n \n \n \n \n "));
assertFalse(checker.executeCheck(project));
project.delete();
project = j.createMatrixProject("Batch_Multiple_Line_2");
project.getBuildersList().add(new hudson.tasks.Shell("echo first\n\n\n\n\n\nline1"));
assertFalse(checker.executeCheck(project));
project.delete();
project = j.createMatrixProject("Batch_Multiple_Line_3");
project.getBuildersList().add(new hudson.tasks.Shell("echo first\n\n\n\n\n\nline1\nline2"));
assertTrue(checker.executeCheck(project));
}
}

0 comments on commit 5d985b2

Please sign in to comment.
You can’t perform that action at this time.