Skip to content
Permalink
Browse files

[JENKINS-46035] Don't count empty lines (#27)

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

Change-Id: I0baa892f0c9e21852b5a423d2189f79fbc119985

* Refactored a bit and removed duplicated hardcoded strings
  • Loading branch information
v1v committed Aug 10, 2017
1 parent fb24d88 commit 6458bd62d06a61b230d9456ac804b6236660a9f5
@@ -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;
}
@@ -17,6 +17,15 @@
*/
public class HardcodedScriptCheckerTestCase extends AbstractTestCase {
private HardcodedScriptChecker checker = new HardcodedScriptChecker(true, HardcodedScriptChecker.THRESHOLD);
private final String SINGLE_LINE_BASH = "#!/bin/bash #single line";
private final String MULTI_LINE_BASH = "#!/bin/bash\nline1\nline2\nline3\nline4\nline5\nline6";
private final String SINGLE_LINE_BATCH = "echo first";
private final String MULTI_LINE_BATCH = "echo first\nline1\nline2\nline3\nline4\nline5\nline6";
private final String MULTI_EMPTY_LINE_BASH = "#!/bin/bash\n\n\n\n\n\n";
private final String MULTI_SPACE_LINE1_BASH = "echo first\n\t\n\t\n\t\n\t\n\t\n\t";
private final String MULTI_SPACE_LINE2_BASH = "echo first\n \n \n \n \n \n ";
private final String MULTI_LINE_WITH_BLANK1_BASH = "echo first\n\n\n\n\n\nline1";
private final String MULTI_LINE_WITH_BLANK2_BASH = "echo first\n\n\n\n\n\nline1\nline2";

@Test public void testDefaultJob() throws Exception {
FreeStyleProject project = j.createFreeStyleProject();
@@ -26,7 +35,8 @@
FreeStyleProject project = j.createFreeStyleProject("");
assertFalse(checker.executeCheck(project));
}
@Test public void testMavenJobName() throws Exception {
@Issue("JENKINS-38616")
@Test public void testMavenModuleJob() throws Exception {
MavenModuleSet project = j.createMavenProject();
assertFalse(checker.executeCheck(project));
}
@@ -36,50 +46,28 @@
assertFalse(checker.executeCheck(project));
}
@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));
project.delete();
project = j.createMatrixProject("Bash_Multiple_Line");
project.getBuildersList().add(new hudson.tasks.Shell("#!/bin/bash\nline1\nline2\nline3\nline4\nline5\nline6"));
assertTrue(checker.executeCheck(project));
project.delete();
project = j.createMatrixProject("Batch_Single_Line");
project.getBuildersList().add(new hudson.tasks.BatchFile("echo first"));
assertFalse(checker.executeCheck(project));
project.delete();
project = j.createMatrixProject("Batch_Multiple_Line");
project.getBuildersList().add(new hudson.tasks.BatchFile("echo first\nline1\nline2\nline3\nline4\nline5\nline6"));
assertTrue(checker.executeCheck(project));
project.delete();
assertFalse(getMatrixProject(SINGLE_LINE_BASH, true));
assertTrue(getMatrixProject(MULTI_LINE_BASH, true));
assertFalse(getMatrixProject(SINGLE_LINE_BATCH, false));
assertTrue(getMatrixProject(MULTI_LINE_BATCH, false));
MatrixProject project = j.createMatrixProject();
int threshold = checker.getThreshold();
checker.setThreshold(14);
project = j.createMatrixProject("Batch_Multiple_Line_2");
project.getBuildersList().add(new hudson.tasks.BatchFile("echo first\nline1\nline2\nline3\nline4\nline5\nline6"));
project = j.createMatrixProject();
project.getBuildersList().add(new hudson.tasks.BatchFile(MULTI_LINE_BATCH));
assertFalse(checker.executeCheck(project));
checker.setThreshold(threshold);
}
@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));
project.delete();
project = j.createFreeStyleProject("Bash_Multiple_Line");
project.getBuildersList().add(new hudson.tasks.Shell("#!/bin/bash\nline1\nline2\nline3\nline4\nline5\nline6"));
assertTrue(checker.executeCheck(project));
project.delete();
project = j.createFreeStyleProject("Batch_Single_Line");
project.getBuildersList().add(new hudson.tasks.BatchFile("echo first"));
assertFalse(checker.executeCheck(project));
project.delete();
project = j.createFreeStyleProject("Batch_Multiple_Line");
project.getBuildersList().add(new hudson.tasks.BatchFile("echo first\nline1\nline2\nline3\nline4\nline5\nline6"));
assertTrue(checker.executeCheck(project));
project.delete();
assertFalse(getJob(SINGLE_LINE_BASH, true));
assertTrue(getJob(MULTI_LINE_BASH, true));
assertFalse(getJob(SINGLE_LINE_BATCH, false));
assertTrue(getJob(MULTI_LINE_BATCH, false));
FreeStyleProject project = j.createFreeStyleProject();
int threshold = checker.getThreshold();
checker.setThreshold(14);
project = j.createFreeStyleProject("Batch_Multiple_Line_2");
project.getBuildersList().add(new hudson.tasks.BatchFile("echo first\nline1\nline2\nline3\nline4\nline5\nline6"));
project = j.createFreeStyleProject();
project.getBuildersList().add(new hudson.tasks.BatchFile(MULTI_LINE_BATCH));
assertFalse(checker.executeCheck(project));
checker.setThreshold(threshold);
}
@@ -100,38 +88,84 @@
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 {
MavenModuleSet project = j.createMavenProject();
assertFalse(checker.executeCheck(project));
}
@Issue("JENKINS-38616")
@Test public void testMavenModuleJobbWithHardcodedScript() throws Exception {
assertFalse(getMavenModule(SINGLE_LINE_BASH, true));
assertTrue(getMavenModule(MULTI_LINE_BASH, true));
assertFalse(getMavenModule(SINGLE_LINE_BATCH, true));
assertFalse(getMavenModule(SINGLE_LINE_BASH, false));
assertTrue(getMavenModule(MULTI_LINE_BASH, false));
assertFalse(getMavenModule(SINGLE_LINE_BATCH, false));
MavenModuleSet project = j.createMavenProject();
project.getPrebuilders().add(new hudson.tasks.Shell("#!/bin/bash #single line"));
assertFalse(checker.executeCheck(project));
project.delete();
project = j.createMavenProject("Bash_Multiple_Line");
project.getPrebuilders().add(new hudson.tasks.Shell("#!/bin/bash\nline1\nline2\nline3\nline4\nline5\nline6"));
assertTrue(checker.executeCheck(project));
project.delete();
project = j.createMavenProject("Batch_Single_Line");
project.getPrebuilders().add(new hudson.tasks.BatchFile("echo first"));
assertFalse(checker.executeCheck(project));
project.delete();
project = j.createMavenProject("Batch_Multiple_Line");
project.getPrebuilders().add(new hudson.tasks.BatchFile("echo first\nline1\nline2\nline3\nline4\nline5\nline6"));
assertTrue(checker.executeCheck(project));
project.delete();
int threshold = checker.getThreshold();
checker.setThreshold(14);
project = j.createMavenProject("Batch_Multiple_Line_2");
project.getPrebuilders().add(new hudson.tasks.BatchFile("echo first\nline1\nline2\nline3\nline4\nline5\nline6"));
project = j.createMavenProject();
project.getPrebuilders().add(new hudson.tasks.BatchFile(MULTI_LINE_BATCH));
assertFalse(checker.executeCheck(project));
checker.setThreshold(threshold);
}
@Issue("JENKINS-46035")
@Test public void testJobWithHardcodedAndBlankLinesScript() throws Exception {
assertFalse(getJob(MULTI_EMPTY_LINE_BASH, true));
assertFalse(getJob(MULTI_SPACE_LINE1_BASH, true));
assertFalse(getJob(MULTI_SPACE_LINE2_BASH, true));
assertFalse(getJob(MULTI_LINE_WITH_BLANK1_BASH, true));
assertTrue(getJob(MULTI_LINE_WITH_BLANK2_BASH, true));
}
@Issue("JENKINS-46035")
@Test public void testMavenModuleJobWithHardcodedAndBlankLinesScript() throws Exception {
assertFalse(getMavenModule(MULTI_EMPTY_LINE_BASH, true));
assertFalse(getMavenModule(MULTI_SPACE_LINE1_BASH, true));
assertFalse(getMavenModule(MULTI_SPACE_LINE2_BASH, true));
assertFalse(getMavenModule(MULTI_LINE_WITH_BLANK1_BASH, true));
assertTrue(getMavenModule(MULTI_LINE_WITH_BLANK2_BASH, true));
}
@Issue("JENKINS-46035")
@Test public void testMatrixProjectWithHardcodedAndBlankLinesScript() throws Exception {
assertFalse(getMatrixProject(MULTI_EMPTY_LINE_BASH, true));
assertFalse(getMatrixProject(MULTI_SPACE_LINE1_BASH, true));
assertFalse(getMatrixProject(MULTI_SPACE_LINE2_BASH, true));
assertFalse(getMatrixProject(MULTI_LINE_WITH_BLANK1_BASH, true));
assertTrue(getMatrixProject(MULTI_LINE_WITH_BLANK2_BASH, true));
}

private boolean getMatrixProject(String shell, boolean isUnix) throws Exception {
MatrixProject project = j.createMatrixProject();
if (isUnix) {
project.getBuildersList().add(new hudson.tasks.Shell(shell));
} else {
project.getBuildersList().add(new hudson.tasks.BatchFile(shell));
}
boolean status = checker.executeCheck(project);
project.delete();
return status;
}

private boolean getMavenModule(String shell, boolean isUnix) throws Exception {
MavenModuleSet project = j.createMavenProject();
if (isUnix) {
project.getPrebuilders().add(new hudson.tasks.Shell(shell));
} else {
project.getPrebuilders().add(new hudson.tasks.BatchFile(shell));
}
boolean status = checker.executeCheck(project);
project.delete();
return status;
}

private boolean getJob(String shell, boolean isUnix) throws Exception {
FreeStyleProject project = j.createFreeStyleProject();
if (isUnix) {
project.getBuildersList().add(new hudson.tasks.Shell(shell));
} else {
project.getBuildersList().add(new hudson.tasks.BatchFile(shell));
}
boolean status = checker.executeCheck(project);
project.delete();
return status;
}
}

0 comments on commit 6458bd6

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