Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add junit test for stash file parameter #7

Merged
merged 34 commits into from
Jan 30, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
9476558
upgrade release drafter and add dependabot for ghactions
olamy Jan 20, 2021
9bda7b9
add junit for stash file parameter
olamy Jan 21, 2021
9dc68fa
add more details on manual tests done
olamy Jan 21, 2021
542a861
add more docs and inline help
olamy Jan 22, 2021
83d4e02
mar inline help as done
olamy Jan 22, 2021
a3e2f56
add more tests including auto creation of build parameters with pipeline
olamy Jan 22, 2021
779d206
add ignored test for a coming feature
olamy Jan 22, 2021
1ba3a0b
use org.jenkins-ci.plugins:jackson2-api
olamy Jan 22, 2021
7f25496
Update README.md
olamy Jan 23, 2021
237e9ee
Update README.md
olamy Jan 23, 2021
ed7877d
Update README.md
olamy Jan 23, 2021
ab26f20
Update src/main/java/io/jenkins/plugins/file_parameters/Base64FilePar…
olamy Jan 23, 2021
36d2f2f
Update README.md
olamy Jan 23, 2021
828f0ba
Update src/test/java/io/jenkins/plugins/file_parameters/FileParameter…
olamy Jan 23, 2021
1b62df5
fix unit test
olamy Jan 23, 2021
9558459
Update src/test/java/io/jenkins/plugins/file_parameters/FileParameter…
olamy Jan 24, 2021
ece717d
more fixes after review
olamy Jan 24, 2021
e5ae911
Update README.md
olamy Jan 24, 2021
ceabdb8
Update src/main/java/io/jenkins/plugins/file_parameters/StashedFilePa…
olamy Jan 24, 2021
2c2d4f5
Apply suggestions from code review
olamy Jan 24, 2021
e677d30
fix test as simplified parameter syntax do not work
olamy Jan 24, 2021
292f870
add allowNoFile option to not fail if no parameter
olamy Jan 25, 2021
b960b79
option to tolerate undefined parameter has been done
olamy Jan 25, 2021
b2e08aa
all done for withFileParameter
olamy Jan 25, 2021
5c29809
add a comment on why this dependency with exclusions
olamy Jan 27, 2021
4920a15
Update src/test/java/io/jenkins/plugins/file_parameters/FileParameter…
olamy Jan 27, 2021
06333dc
Update src/test/java/io/jenkins/plugins/file_parameters/FileParameter…
olamy Jan 27, 2021
0d3acb6
Update src/main/resources/io/jenkins/plugins/file_parameters/Base64Fi…
olamy Jan 27, 2021
89ee670
Update src/main/resources/io/jenkins/plugins/file_parameters/StashedF…
olamy Jan 27, 2021
4535fd3
Update src/test/java/io/jenkins/plugins/file_parameters/FileParameter…
olamy Jan 27, 2021
be09527
remove extra empty lines
olamy Jan 27, 2021
71928ff
add allowNoFile help and move help
olamy Jan 27, 2021
12b668f
Update README.md
olamy Jan 27, 2021
405415f
stashed is for big files
olamy Jan 27, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ pipeline {
- [X] implementation
- [ ] manual test
- [ ] automated test
- [ ] `withFileParameter` wrapper step
- [X] `withFileParameter` wrapper step
- [X] implementation
- [X] inline help text
- [X] manual test
- [X] automated test
- [ ] option to tolerate undefined parameter
- [X] option to tolerate undefined parameter
- [ ] `input` step submission
- [ ] design
- [ ] manual test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import jenkins.tasks.SimpleBuildWrapper;
import org.jenkinsci.Symbol;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;

/**
* Saves a file parameter to a temporary local file.
Expand All @@ -49,22 +50,37 @@ public final class FileParameterWrapper extends SimpleBuildWrapper {

public final String name;

private boolean allowNoFile;

@DataBoundConstructor public FileParameterWrapper(String name) {
this.name = name;
}

public boolean isAllowNoFile() {
return allowNoFile;
}

@DataBoundSetter
public void setAllowNoFile(boolean allowNoFile) {
this.allowNoFile = allowNoFile;
}

@Override public void setUp(Context context, Run<?, ?> build, FilePath workspace, Launcher launcher, TaskListener listener, EnvVars initialEnvironment) throws IOException, InterruptedException {
ParametersAction pa = build.getAction(ParametersAction.class);
if (pa == null) {
throw new AbortException("No parameters");
}
ParameterValue pv = pa.getParameter(name);
if (pv == null) {
if (pv == null && !allowNoFile) {
throw new AbortException("No parameter named " + name);
}
if (!(pv instanceof AbstractFileParameterValue)) {
if (!(pv instanceof AbstractFileParameterValue) && !allowNoFile) {
throw new AbortException("Unsupported parameter type");
}
if (pv == null && allowNoFile) {
listener.getLogger().println("Skip file parameter as there is no parameter with name: '" + name + "'");
return;
}
FilePath tempDir = WorkspaceList.tempDir(workspace);
if (tempDir == null) {
throw new AbortException("Missing workspace or could not make temp dir");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public final class StashedFileParameterDefinition extends AbstractFileParameterD
@Extension public static final class DescriptorImpl extends ParameterDefinition.ParameterDescriptor {

@Override public String getDisplayName() {
return "Stashed Base64 File Parameter";
return "Stashed File Parameter";
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,24 @@
}
</pre>
</div>
<div>
Per default, there will be an error if there is no parameter for the build but you can ignore this error using the
parameter attribute <code>allowNoFile</code>. In this case your pipeline must take into account the file doesn't exists
<pre>
pipeline {
agent any
parameters {
base64File(name: 'THEFILE')
}
stages {
stage('Example') {
steps {
withFileParameter(name:'THEFILE', allowNoFile: true) {
echo(/loaded '${readFile(FILE)}' from $FILE/)
}
}
}
}
}
</pre>
</div>
olamy marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
pipeline {
agent any
parameters {
stashedBase64File(name: 'FILE-STASH')
stashedFile(name: 'FILE-STASH')
}
stages {
stage('Example') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,14 @@ public class FileParameterWrapperTest {
r.assertLogContains("loaded 'UPLOADED CONTENT HERE' from ", b);
}

@Ignore("need to implement option to tolerate undefined parameter")
@Test public void base64Undefined() throws Exception {
@Test public void base64UndefinedFail() throws Exception {
r.createSlave("remote", null, null);
WorkflowJob p = r.createProject(WorkflowJob.class, "myjob");
p.addProperty(new ParametersDefinitionProperty(new Base64FileParameterDefinition("FILE", null)));
String pipeline = "pipeline {\n" +
" agent any\n" +
" parameters {\n" +
" base64File 'FILE'\n" +
" base64File (name:'FILE')\n" +
olamy marked this conversation as resolved.
Show resolved Hide resolved
" }\n" +
" stages {\n" +
" stage('Example') {\n" +
Expand All @@ -85,10 +84,35 @@ public class FileParameterWrapperTest {
p.setDefinition(new CpsFlowDefinition(pipeline, true));
WorkflowRun run = p.scheduleBuild2(0).get();
r.waitForCompletion(run);
// definitely will fail but we just ensure parameter has been created
r.assertBuildStatus(Result.FAILURE, run);
r.assertLogContains("No parameter named FILE", run);
}

@Test public void base64WithAllowNoFile() throws Exception {
r.createSlave("remote", null, null);
WorkflowJob p = r.createProject(WorkflowJob.class, "myjob");
p.addProperty(new ParametersDefinitionProperty(new Base64FileParameterDefinition("FILE", null)));
String pipeline = "pipeline {\n" +
" agent any\n" +
" parameters {\n" +
" base64File (name:'FILE')\n" +
olamy marked this conversation as resolved.
Show resolved Hide resolved
" }\n" +
" stages {\n" +
" stage('Example') {\n" +
" steps {\n" +
" withFileParameter(name:'FILE', allowNoFile: true) {\n" +
" echo('foo') \n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
"}";
p.setDefinition(new CpsFlowDefinition(pipeline, true));
WorkflowRun run = p.scheduleBuild2(0).get();
r.waitForCompletion(run);
r.assertBuildStatus(Result.SUCCESS, run);
WorkflowRun b = p.getBuildByNumber(1);
r.assertLogContains("foo", b);
r.assertLogContains("foo", run);
r.assertLogContains("Skip file parameter as there is no parameter with name: 'FILE'", run);
}

@Test public void base64DeclarativeParameterCreated() throws Exception {
Expand All @@ -98,7 +122,7 @@ public class FileParameterWrapperTest {
String pipeline = "pipeline {\n" +
" agent any\n" +
" parameters {\n" +
" base64File 'FILE'\n" +
" base64File (name: 'FILE')\n" +
" }\n" +
" stages {\n" +
" stage('Example') {\n" +
Expand Down Expand Up @@ -155,7 +179,7 @@ public class FileParameterWrapperTest {
String pipeline = "pipeline {\n" +
" agent any\n" +
" parameters {\n" +
" stashedBase64File 'FILE-STASH'\n" +
" stashedFile (name:'FILE-STASH')\n" +
olamy marked this conversation as resolved.
Show resolved Hide resolved
" }\n" +
" stages {\n" +
" stage('Example') {\n" +
Expand Down