Skip to content

Commit

Permalink
Fix tests on Windows and use Sandbox as much as possible
Browse files Browse the repository at this point in the history
  • Loading branch information
philippe-granet committed Sep 24, 2016
1 parent d79fe6c commit 53b465f
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 138 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.jenkinsci.plugins.pipeline.utility.steps;

import org.apache.commons.io.FilenameUtils;

public class FilenameTestsUtils {

/**
* Converts all separators to the system separator
* and escape them for Windows.
*
* @param path the path to be changed, null ignored
* @return the updated path
*/
public static String separatorsToSystemEscaped(String path) {
if (path == null) {
return null;
}
String pathConverted=FilenameUtils.separatorsToSystem(path);
return pathConverted.replace("\\", "\\\\");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

import hudson.model.Label;
import hudson.model.Result;

import static org.jenkinsci.plugins.pipeline.utility.steps.FilenameTestsUtils.separatorsToSystemEscaped;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
Expand Down Expand Up @@ -68,12 +70,12 @@ public void readFile() throws Exception {
WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition(
"node('slaves') {\n" +
" def props = readProperties file: '" + file.getAbsolutePath() + "'\n" +
" def props = readProperties file: '" + separatorsToSystemEscaped(file.getAbsolutePath()) + "'\n" +
" assert props['test'] == 'One'\n" +
" assert props['another'] == 'Two'\n" +
" assert props.test == 'One'\n" +
" assert props.another == 'Two'\n" +
"}", false));
"}", true));
j.assertBuildStatusSuccess(p.scheduleBuild2(0));
}

Expand All @@ -91,14 +93,14 @@ public void readFileWithDefaults() throws Exception {
p.setDefinition(new CpsFlowDefinition(
"node('slaves') {\n" +
" def d = [test: 'Default', something: 'Default']\n" +
" def props = readProperties defaults: d, file: '" + file.getAbsolutePath() + "'\n" +
" def props = readProperties defaults: d, file: '" + separatorsToSystemEscaped(file.getAbsolutePath()) + "'\n" +
" assert props['test'] == 'One'\n" +
" assert props['another'] == 'Two'\n" +
" assert props.test == 'One'\n" +
" assert props.another == 'Two'\n" +
" assert props['something'] == 'Default'\n" +
" assert props.something == 'Default'\n" +
"}", false));
"}", true));
j.assertBuildStatusSuccess(p.scheduleBuild2(0));
}

Expand All @@ -115,13 +117,13 @@ public void readText() throws Exception {
WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition(
"node('slaves') {\n" +
" String propsText = readFile file: '" + file.getAbsolutePath() + "'\n" +
" String propsText = readFile file: '" + separatorsToSystemEscaped(file.getAbsolutePath()) + "'\n" +
" def props = readProperties text: propsText\n" +
" assert props['test'] == 'One'\n" +
" assert props['another'] == 'Two'\n" +
" assert props.test == 'One'\n" +
" assert props.another == 'Two'\n" +
"}", false));
"}", true));
j.assertBuildStatusSuccess(p.scheduleBuild2(0));
}

Expand All @@ -134,7 +136,7 @@ public void readDirectText() throws Exception {
" assert props['test'] == 'something'\n" +
" assert props.test == 'something'\n" +
" assert props.another == null\n" +
"}", false));
"}", true));
j.assertBuildStatusSuccess(p.scheduleBuild2(0));
}

Expand All @@ -144,7 +146,7 @@ public void readNone() throws Exception {
p.setDefinition(new CpsFlowDefinition(
"node('slaves') {\n" +
" def props = readProperties()\n" +
"}", false));
"}", true));
WorkflowRun run = j.assertBuildStatus(Result.FAILURE, p.scheduleBuild2(0).get());
j.assertLogContains("At least one of file or text needs to be provided to readProperties.", run);
}
Expand All @@ -170,15 +172,15 @@ public void readFileAndText() throws Exception {
WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition(
"node('slaves') {\n" +
" String propsText = readFile file: '" + textFile.getAbsolutePath() + "'\n" +
" def props = readProperties text: propsText, file: '" + file.getAbsolutePath() + "'\n" +
" String propsText = readFile file: '" + separatorsToSystemEscaped(textFile.getAbsolutePath()) + "'\n" +
" def props = readProperties text: propsText, file: '" + separatorsToSystemEscaped(file.getAbsolutePath()) + "'\n" +
" assert props['test'] == 'One'\n" +
" assert props.test == 'One'\n" +
" assert props['text'] == 'TextOne'\n" +
" assert props.text == 'TextOne'\n" +
" assert props['another'] == 'TextTwo'\n" +
" assert props.another == 'TextTwo'\n" +
"}", false));
"}", true));
j.assertBuildStatusSuccess(p.scheduleBuild2(0));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.File;

import org.apache.commons.io.FileUtils;
import static org.jenkinsci.plugins.pipeline.utility.steps.FilenameTestsUtils.separatorsToSystemEscaped;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
Expand Down Expand Up @@ -52,37 +53,50 @@ public void setup() throws Exception {
}

@Test
public void readDirectText() throws Exception {
public void checksPrimitivesAndDatesWithoutSandbox() throws Exception {

//We desactive Sandbox because Class.getName and Date.format are not permitted in Sandbox...

WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition(
"node('slaves') {\n" + " def yaml = readYaml text: '''" + yamlText + "'''\n"
+ " assert yaml.getClass().getName() == 'java.util.LinkedHashMap'\n" +
" assert yaml.boolean == true\n" +
" assert yaml.boolean.getClass().getName() == 'java.lang.Boolean'\n" +
" assert yaml.string == 'string'\n" +
" assert yaml.string.getClass().getName() == 'java.lang.String'\n" +
" assert yaml.integer == 3\n" +
" assert yaml.integer.getClass().getName() == 'java.lang.Integer'\n" +
" assert yaml.double == 3.14\n" +
" assert yaml.double.getClass().getName() == 'java.lang.Double'\n" +
" assert yaml.null == null\n" +
" def timeZone = TimeZone.getTimeZone('UTC')\n" +
" assert yaml.date.format('yyyy-MM-dd HH:mm:ss',timeZone) == '2001-12-14 21:59:43'\n" +
" assert yaml.date.getClass().getName() == 'java.util.Date'\n" +
" assert yaml.billTo.address.postal == 48046\n" +
" assert yaml.billTo.getClass().getName() == 'java.util.LinkedHashMap'\n" +
" assert yaml.billTo.address.getClass().getName() == 'java.util.LinkedHashMap'\n" +
" assert yaml.billTo.address.postal.getClass().getName() == 'java.lang.Integer'\n" +
" assert yaml.array.size() == 2\n" +
" assert yaml.array.getClass().getName() == 'java.util.ArrayList'\n" +
" assert yaml.array[0] == 'value1'\n" +
" assert yaml.array[0].getClass().getName() == 'java.lang.String'\n" +
" assert yaml.array[1] == 'value2'\n" +
" assert yaml.array[1].getClass().getName() == 'java.lang.String'\n" +
" assert yaml.another == null\n" + "}",
"}",
false));
j.assertBuildStatusSuccess(p.scheduleBuild2(0));
}

@Test
public void readDirectText() throws Exception {
WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition(
"node('slaves') {\n" + " def yaml = readYaml text: '''" + yamlText + "'''\n"
+ " assert yaml.boolean == true\n" +
" assert yaml.string == 'string'\n" +
" assert yaml.integer == 3\n" +
" assert yaml.double == 3.14\n" +
" assert yaml.null == null\n" +
" assert yaml.billTo.address.postal == 48046\n" +
" assert yaml.array.size() == 2\n" +
" assert yaml.array[0] == 'value1'\n" +
" assert yaml.array[1] == 'value2'\n" +
" assert yaml.another == null\n" + "}",
true));
j.assertBuildStatusSuccess(p.scheduleBuild2(0));
}

@Test
public void readSeveralDocuments() throws Exception {
Expand All @@ -106,33 +120,19 @@ public void readText() throws Exception {
WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition(
"node('slaves') {\n" +
" String yamlText = readFile file: '" + file.getAbsolutePath().replace("\\","\\\\") + "'\n" +
" def yaml = readYaml text: yamlText\n"
+ " assert yaml.getClass().getName() == 'java.util.LinkedHashMap'\n" +
" String yamlText = readFile file: '" + separatorsToSystemEscaped(file.getAbsolutePath()) + "'\n" +
" def yaml = readYaml text: yamlText\n" +
" assert yaml.boolean == true\n" +
" assert yaml.boolean.getClass().getName() == 'java.lang.Boolean'\n" +
" assert yaml.string == 'string'\n" +
" assert yaml.string.getClass().getName() == 'java.lang.String'\n" +
" assert yaml.integer == 3\n" +
" assert yaml.integer.getClass().getName() == 'java.lang.Integer'\n" +
" assert yaml.double == 3.14\n" +
" assert yaml.double.getClass().getName() == 'java.lang.Double'\n" +
" assert yaml.null == null\n" +
" def timeZone = TimeZone.getTimeZone('UTC')\n" +
" assert yaml.date.format('yyyy-MM-dd HH:mm:ss',timeZone) == '2001-12-14 21:59:43'\n" +
" assert yaml.date.getClass().getName() == 'java.util.Date'\n" +
" assert yaml.billTo.address.postal == 48046\n" +
" assert yaml.billTo.getClass().getName() == 'java.util.LinkedHashMap'\n" +
" assert yaml.billTo.address.getClass().getName() == 'java.util.LinkedHashMap'\n" +
" assert yaml.billTo.address.postal.getClass().getName() == 'java.lang.Integer'\n" +
" assert yaml.array.size() == 2\n" +
" assert yaml.array.getClass().getName() == 'java.util.ArrayList'\n" +
" assert yaml.array[0] == 'value1'\n" +
" assert yaml.array[0].getClass().getName() == 'java.lang.String'\n" +
" assert yaml.array[1] == 'value2'\n" +
" assert yaml.array[1].getClass().getName() == 'java.lang.String'\n" +
" assert yaml.another == null\n" + "}",
false));
true));
WorkflowRun run = p.scheduleBuild2(0).get();
System.out.println(JenkinsRule.getLog(run));
j.assertBuildStatusSuccess(run);
Expand All @@ -147,32 +147,18 @@ public void readFile() throws Exception {
WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition(
"node('slaves') {\n" +
" def yaml = readYaml file: '" + file.getAbsolutePath().replace("\\","\\\\") + "'\n" +
" assert yaml.getClass().getName() == 'java.util.LinkedHashMap'\n" +
" def yaml = readYaml file: '" + separatorsToSystemEscaped(file.getAbsolutePath()) + "'\n" +
" assert yaml.boolean == true\n" +
" assert yaml.boolean.getClass().getName() == 'java.lang.Boolean'\n" +
" assert yaml.string == 'string'\n" +
" assert yaml.string.getClass().getName() == 'java.lang.String'\n" +
" assert yaml.integer == 3\n" +
" assert yaml.integer.getClass().getName() == 'java.lang.Integer'\n" +
" assert yaml.double == 3.14\n" +
" assert yaml.double.getClass().getName() == 'java.lang.Double'\n" +
" assert yaml.null == null\n" +
" def timeZone = TimeZone.getTimeZone('UTC')\n" +
" assert yaml.date.format('yyyy-MM-dd HH:mm:ss',timeZone) == '2001-12-14 21:59:43'\n" +
" assert yaml.date.getClass().getName() == 'java.util.Date'\n" +
" assert yaml.billTo.address.postal == 48046\n" +
" assert yaml.billTo.getClass().getName() == 'java.util.LinkedHashMap'\n" +
" assert yaml.billTo.address.getClass().getName() == 'java.util.LinkedHashMap'\n" +
" assert yaml.billTo.address.postal.getClass().getName() == 'java.lang.Integer'\n" +
" assert yaml.array.size() == 2\n" +
" assert yaml.array.getClass().getName() == 'java.util.ArrayList'\n" +
" assert yaml.array[0] == 'value1'\n" +
" assert yaml.array[0].getClass().getName() == 'java.lang.String'\n" +
" assert yaml.array[1] == 'value2'\n" +
" assert yaml.array[1].getClass().getName() == 'java.lang.String'\n" +
" assert yaml.another == null\n" + "}",
false));
true));
j.assertBuildStatusSuccess(p.scheduleBuild2(0));
}

Expand All @@ -188,33 +174,19 @@ public void readFileAndText() throws Exception {
WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition(
"node('slaves') {\n" +
" String yamlText = readFile file: '" + fileOverride.getAbsolutePath().replace("\\","\\\\") + "'\n" +
" def yaml = readYaml text: yamlText, file: '" + file.getAbsolutePath().replace("\\","\\\\") + "'\n" +
" assert yaml.getClass().getName() == 'java.util.LinkedHashMap'\n" +
" String yamlText = readFile file: '" + separatorsToSystemEscaped(fileOverride.getAbsolutePath()) + "'\n" +
" def yaml = readYaml text: yamlText, file: '" + separatorsToSystemEscaped(file.getAbsolutePath()) + "'\n" +
" assert yaml.boolean == false\n" +
" assert yaml.boolean.getClass().getName() == 'java.lang.Boolean'\n" +
" assert yaml.string == 'string'\n" +
" assert yaml.string.getClass().getName() == 'java.lang.String'\n" +
" assert yaml.integer == 0\n" +
" assert yaml.integer.getClass().getName() == 'java.lang.Integer'\n" +
" assert yaml.double == 3.14\n" +
" assert yaml.double.getClass().getName() == 'java.lang.Double'\n" +
" assert yaml.null == null\n" +
" def timeZone = TimeZone.getTimeZone('UTC')\n" +
" assert yaml.date.format('yyyy-MM-dd HH:mm:ss',timeZone) == '2001-12-14 21:59:43'\n" +
" assert yaml.date.getClass().getName() == 'java.util.Date'\n" +
" assert yaml.billTo.address.postal == 48046\n" +
" assert yaml.billTo.getClass().getName() == 'java.util.LinkedHashMap'\n" +
" assert yaml.billTo.address.getClass().getName() == 'java.util.LinkedHashMap'\n" +
" assert yaml.billTo.address.postal.getClass().getName() == 'java.lang.Integer'\n" +
" assert yaml.array.size() == 2\n" +
" assert yaml.array.getClass().getName() == 'java.util.ArrayList'\n" +
" assert yaml.array[0] == 'value1'\n" +
" assert yaml.array[0].getClass().getName() == 'java.lang.String'\n" +
" assert yaml.array[1] == 'value2'\n" +
" assert yaml.array[1].getClass().getName() == 'java.lang.String'\n" +
" assert yaml.another == null\n" + "}",
false));
true));
j.assertBuildStatusSuccess(p.scheduleBuild2(0));
}

Expand Down
Loading

0 comments on commit 53b465f

Please sign in to comment.