提交 410fd3f5 编写于 作者: O Oliver Gondža

Improve ArgumentListBuilder#toWindowsCommand test coverage

上级 32da5d79
......@@ -23,16 +23,25 @@
*/
package hudson.util;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import hudson.Functions;
import hudson.Launcher.LocalLauncher;
import hudson.Launcher.RemoteLauncher;
import hudson.Proc;
import hudson.model.Slave;
import org.apache.tools.ant.util.JavaEnvUtils;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.Email;
import org.jvnet.hudson.test.JenkinsRule;
import com.google.common.base.Joiner;
import java.io.ByteArrayOutputStream;
import java.io.StringWriter;
/**
......@@ -61,4 +70,33 @@ public class ArgumentListBuilder2Test {
System.out.println(out);
assertTrue(out.toString().contains("$ java ********"));
}
@Test
public void ensureArgumentsArePassedToCommandUnmodified() throws Exception {
String[] specials = new String[] {
"~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")",
"_", "+", "{", "}", "[", "]", ":", ";", "\"", "'", "\\", "|",
"<", ">", ",", ".", "/", "?", " "
};
ArgumentListBuilder args = new ArgumentListBuilder(JavaEnvUtils.getJreExecutable("java"), "-cp", "target/test-classes/", "hudson.util.EchoCommand");
args.add(specials);
if (Functions.isWindows()) {
args = args.toWindowsCommand();
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
final StreamTaskListener listener = new StreamTaskListener(out);
Proc p = new LocalLauncher(listener)
.launch()
.stderr(System.err)
.stdout(out)
.cmds(args)
.start()
;
int code = p.join();
String expected = String.format("%n%s", Joiner.on(" ").join(specials));
assertThat("Error code " + code, out.toString(), containsString(expected));
}
}
/*
* The MIT License
*
* Copyright (c) 2015 Red Hat, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.util;
/**
* Command line utility to print all arguments as provided.
*
* @author ogondza
*/
public final class EchoCommand {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
for (String a: args) {
if (sb.length() != 0) sb.append(' ');
sb.append(a);
}
System.out.println(sb.toString());
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册