Skip to content

Commit

Permalink
[FIXED JENKINS-48138] Log a warning when fileExists called with empty…
Browse files Browse the repository at this point in the history
… string
  • Loading branch information
abayer committed Jan 11, 2018
1 parent 2d44c83 commit 35c7166
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 3 deletions.
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<parent> <parent>
<groupId>org.jenkins-ci.plugins</groupId> <groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId> <artifactId>plugin</artifactId>
<version>2.33</version> <version>3.2</version>
<relativePath /> <relativePath />
</parent> </parent>
<groupId>org.jenkins-ci.plugins.workflow</groupId> <groupId>org.jenkins-ci.plugins.workflow</groupId>
Expand Down Expand Up @@ -63,6 +63,7 @@
</pluginRepositories> </pluginRepositories>
<properties> <properties>
<jenkins.version>2.7.3</jenkins.version> <jenkins.version>2.7.3</jenkins.version>
<java.level>7</java.level>
<workflow-step-api-plugin.version>2.13</workflow-step-api-plugin.version> <workflow-step-api-plugin.version>2.13</workflow-step-api-plugin.version>
<workflow-cps-plugin.version>2.32</workflow-cps-plugin.version> <workflow-cps-plugin.version>2.32</workflow-cps-plugin.version>
<workflow-support-plugin.version>2.14</workflow-support-plugin.version> <workflow-support-plugin.version>2.14</workflow-support-plugin.version>
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import java.util.Set; import java.util.Set;




import hudson.model.TaskListener;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.DataBoundConstructor; import org.kohsuke.stapler.DataBoundConstructor;


public final class FileExistsStep extends Step { public final class FileExistsStep extends Step {
Expand Down Expand Up @@ -76,6 +78,9 @@ public static final class Execution extends SynchronousNonBlockingStepExecution<
} }


@Override protected Boolean run() throws Exception { @Override protected Boolean run() throws Exception {
if (StringUtils.isEmpty(file)) {
getContext().get(TaskListener.class).getLogger().println(Messages.FileExistsStep_EmptyString());
}
return getContext().get(FilePath.class).child(file).exists(); return getContext().get(FilePath.class).child(file).exists();
} }


Expand Down
Original file line number Original file line Diff line number Diff line change
@@ -1,3 +1,4 @@
ArtifactArchiverStepExecution.Deprecated=The 'archive' step is deprecated, please use 'archiveArtifacts' instead." ArtifactArchiverStepExecution.Deprecated=The 'archive' step is deprecated, please use 'archiveArtifacts' instead."
ArtifactArchiverStepExecution.NoFiles=No files found to archive for pattern "{0}", continuing. ArtifactArchiverStepExecution.NoFiles=No files found to archive for pattern "{0}", continuing.
ArtifactArchiverStepExecution.NoFilesWithExcludes=No files found to archive for pattern "{0}", excluding "{1}", continuing. ArtifactArchiverStepExecution.NoFilesWithExcludes=No files found to archive for pattern "{0}", excluding "{1}", continuing.
FileExistsStep.EmptyString=The 'fileExists' step was called with '', the empty string, so the current directory will be checked instead.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import hudson.model.Slave; import hudson.model.Slave;
import hudson.model.TaskListener; import hudson.model.TaskListener;
import hudson.slaves.CommandLauncher; import hudson.slaves.CommandLauncher;
import hudson.slaves.ComputerLauncher;
import hudson.slaves.NodeProperty; import hudson.slaves.NodeProperty;
import hudson.slaves.RetentionStrategy; import hudson.slaves.RetentionStrategy;
import hudson.slaves.SlaveComputer; import hudson.slaves.SlaveComputer;
Expand Down Expand Up @@ -267,7 +268,7 @@ public static Slave createSpecialEnvSlave(JenkinsRule rule, String nodeName, @Ch
} }
private static class SpecialEnvSlave extends Slave { private static class SpecialEnvSlave extends Slave {
private final Map<String,String> env; private final Map<String,String> env;
SpecialEnvSlave(File remoteFS, CommandLauncher launcher, String nodeName, @Nonnull String labels, Map<String,String> env) throws Descriptor.FormException, IOException { SpecialEnvSlave(File remoteFS, ComputerLauncher launcher, String nodeName, @Nonnull String labels, Map<String,String> env) throws Descriptor.FormException, IOException {
super(nodeName, nodeName, remoteFS.getAbsolutePath(), 1, Node.Mode.NORMAL, labels, launcher, RetentionStrategy.NOOP, Collections.<NodeProperty<?>>emptyList()); super(nodeName, nodeName, remoteFS.getAbsolutePath(), 1, Node.Mode.NORMAL, labels, launcher, RetentionStrategy.NOOP, Collections.<NodeProperty<?>>emptyList());
this.env = env; this.env = env;
} }
Expand Down
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.jenkinsci.plugins.workflow.steps;

import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runners.model.Statement;
import org.jvnet.hudson.test.BuildWatcher;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.RestartableJenkinsRule;

public class FileExistsStepTest {

@ClassRule
public static BuildWatcher buildWatcher = new BuildWatcher();
@Rule
public RestartableJenkinsRule story = new RestartableJenkinsRule();

@Issue("JENKINS-48138")
@Test
public void emptyStringWarning() {
story.addStep(new Statement() {
@Override
public void evaluate() throws Throwable {
WorkflowJob p = story.j.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition("node { fileExists('') }", true));
story.j.assertLogContains(Messages.FileExistsStep_EmptyString(), story.j.buildAndAssertSuccess(p));
}
});
}
}

0 comments on commit 35c7166

Please sign in to comment.