8166810: jlink should fail on extra arguments

Reviewed-by: jlaskey
This commit is contained in:
Athijegannathan Sundararajan 2016-10-26 10:42:55 +05:30
parent cb0a755549
commit e23ef30b8e
5 changed files with 47 additions and 19 deletions

View File

@ -186,7 +186,7 @@ public class JlinkTask {
new PrintWriter(System.err, true)); new PrintWriter(System.err, true));
} }
try { try {
optionsHelper.handleOptions(this, args); optionsHelper.handleOptionsNoUnhandled(this, args);
if (options.help) { if (options.help) {
optionsHelper.showHelp(PROGNAME); optionsHelper.showHelp(PROGNAME);
return EXIT_OK; return EXIT_OK;

View File

@ -466,7 +466,21 @@ public final class TaskHelper {
return pp; return pp;
} }
// used by jimage. Return unhandled arguments like "create", "describe".
public List<String> handleOptions(T task, String[] args) throws BadArgs { public List<String> handleOptions(T task, String[] args) throws BadArgs {
return handleOptions(task, args, true);
}
// used by jlink. No unhandled arguments like "create", "describe".
void handleOptionsNoUnhandled(T task, String[] args) throws BadArgs {
handleOptions(task, args, false);
}
// shared code that handles options for both jlink and jimage. jimage uses arguments like
// "create", "describe" etc. as "task names". Those arguments are unhandled here and returned
// as "unhandled arguments list". jlink does not want such arguments. "collectUnhandled" flag
// tells whether to allow for unhandled arguments or not.
private List<String> handleOptions(T task, String[] args, boolean collectUnhandled) throws BadArgs {
// findbugs warning, copy instead of keeping a reference. // findbugs warning, copy instead of keeping a reference.
command = Arrays.copyOf(args, args.length); command = Arrays.copyOf(args, args.length);
@ -499,10 +513,10 @@ public final class TaskHelper {
String[] arr = new String[filteredArgs.size()]; String[] arr = new String[filteredArgs.size()];
args = filteredArgs.toArray(arr); args = filteredArgs.toArray(arr);
List<String> rest = new ArrayList<>(); List<String> rest = collectUnhandled? new ArrayList<>() : null;
// process options // process options
for (int i = 0; i < args.length; i++) { for (int i = 0; i < args.length; i++) {
if (!args[i].isEmpty() && args[i].charAt(0) == '-') { if (args[i].charAt(0) == '-') {
String name = args[i]; String name = args[i];
PlugOption pluginOption = null; PlugOption pluginOption = null;
Option<T> option = getOption(name); Option<T> option = getOption(name);
@ -539,7 +553,12 @@ public final class TaskHelper {
i = args.length; i = args.length;
} }
} else { } else {
rest.add(args[i]); if (collectUnhandled) {
rest.add(args[i]);
} else {
throw new BadArgs("err.orphan.argument", args[i]).
showUsage(true);
}
} }
} }
return rest; return rest;

View File

@ -95,6 +95,7 @@ err.file.error=cannot access file: {0}
err.dir.exists={0} already exists err.dir.exists={0} already exists
err.badpattern=bad pattern {0} err.badpattern=bad pattern {0}
err.unknown.option=unknown option: {0} err.unknown.option=unknown option: {0}
err.orphan.argument=orphan argument: {0}
err.missing.arg=no value given for {0} err.missing.arg=no value given for {0}
err.internal.error=internal error: {0} {1} {2} err.internal.error=internal error: {0} {1} {2}
err.invalid.arg.for.option=invalid argument for option: {0} err.invalid.arg.for.option=invalid argument for option: {0}

View File

@ -109,19 +109,16 @@ public class JLinkTest {
.modulePath(helper.defaultModulePath()) .modulePath(helper.defaultModulePath())
.output(helper.createNewImageDir(moduleName)) .output(helper.createNewImageDir(moduleName))
.addMods("leaf1") .addMods("leaf1")
.option("")
.call().assertSuccess(); .call().assertSuccess();
JImageGenerator.getJLinkTask() JImageGenerator.getJLinkTask()
.modulePath(helper.defaultModulePath()) .modulePath(helper.defaultModulePath())
.addMods("leaf1") .addMods("leaf1")
.option("--output") .option("--output")
.option("")
.call().assertFailure("Error: no value given for --output"); .call().assertFailure("Error: no value given for --output");
JImageGenerator.getJLinkTask() JImageGenerator.getJLinkTask()
.modulePath("") .modulePath("")
.output(helper.createNewImageDir(moduleName)) .output(helper.createNewImageDir(moduleName))
.addMods("leaf1") .addMods("leaf1")
.option("")
.call().assertFailure("Error: no value given for --module-path"); .call().assertFailure("Error: no value given for --module-path");
} }
@ -132,7 +129,6 @@ public class JLinkTest {
.modulePath(helper.defaultModulePath()) .modulePath(helper.defaultModulePath())
.output(helper.createNewImageDir(moduleName)) .output(helper.createNewImageDir(moduleName))
.addMods("m") .addMods("m")
.option("")
.call().assertSuccess(); .call().assertSuccess();
moduleName = "mod"; moduleName = "mod";
jmod = helper.generateDefaultJModule(moduleName).assertSuccess(); jmod = helper.generateDefaultJModule(moduleName).assertSuccess();
@ -140,7 +136,6 @@ public class JLinkTest {
.modulePath(helper.defaultModulePath()) .modulePath(helper.defaultModulePath())
.output(helper.createNewImageDir(moduleName)) .output(helper.createNewImageDir(moduleName))
.addMods("m") .addMods("m")
.option("")
.call().assertSuccess(); .call().assertSuccess();
} }
@ -282,18 +277,21 @@ public class JLinkTest {
helper.generateDefaultImage(userOptions, moduleName).assertFailure("Error: Invalid compression level invalid"); helper.generateDefaultImage(userOptions, moduleName).assertFailure("Error: Invalid compression level invalid");
} }
// @file // orphan argument - JDK-8166810
{ {
Path path = Paths.get("embedded.properties"); String[] userOptions = {"--compress", "2", "foo" };
Files.write(path, Collections.singletonList("--strip-debug --add-modules " + String moduleName = "orphanarg1";
"toto.unknown --compress UNKNOWN\n"));
String[] userOptions = {"@", path.toAbsolutePath().toString()};
String moduleName = "configembeddednocompresscomposite2";
helper.generateDefaultJModule(moduleName, "composite2"); helper.generateDefaultJModule(moduleName, "composite2");
Path imageDir = helper.generateDefaultImage(userOptions, moduleName).assertSuccess(); helper.generateDefaultImage(userOptions, moduleName).assertFailure("Error: orphan argument: foo");
helper.checkImage(imageDir, moduleName, null, null);
} }
// orphan argument - JDK-8166810
{
String[] userOptions = {"--output", "foo", "bar" };
String moduleName = "orphanarg2";
helper.generateDefaultJModule(moduleName, "composite2");
helper.generateDefaultImage(userOptions, moduleName).assertFailure("Error: orphan argument: bar");
}
} }
private static void testCompress(Helper helper, String moduleName, String... userOptions) throws IOException { private static void testCompress(Helper helper, String moduleName, String... userOptions) throws IOException {

View File

@ -433,13 +433,23 @@ public class IncludeLocalesPluginTest {
for (Object[] data : testData) { for (Object[] data : testData) {
// create image for each test data // create image for each test data
System.out.println("Invoking jlink with \"" + data[INCLUDE_LOCALES_OPTION] + "\""); Result result;
Result result = JImageGenerator.getJLinkTask() if (data[INCLUDE_LOCALES_OPTION].toString().isEmpty()) {
System.out.println("Invoking jlink with no --include-locales option");
result = JImageGenerator.getJLinkTask()
.modulePath(helper.defaultModulePath())
.output(helper.createNewImageDir(moduleName))
.addMods((String) data[ADDMODS_OPTION])
.call();
} else {
System.out.println("Invoking jlink with \"" + data[INCLUDE_LOCALES_OPTION] + "\"");
result = JImageGenerator.getJLinkTask()
.modulePath(helper.defaultModulePath()) .modulePath(helper.defaultModulePath())
.output(helper.createNewImageDir(moduleName)) .output(helper.createNewImageDir(moduleName))
.addMods((String) data[ADDMODS_OPTION]) .addMods((String) data[ADDMODS_OPTION])
.option((String) data[INCLUDE_LOCALES_OPTION]) .option((String) data[INCLUDE_LOCALES_OPTION])
.call(); .call();
}
String errorMsg = (String) data[ERROR_MESSAGE]; String errorMsg = (String) data[ERROR_MESSAGE];
if (errorMsg.isEmpty()) { if (errorMsg.isEmpty()) {