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));
}
try {
optionsHelper.handleOptions(this, args);
optionsHelper.handleOptionsNoUnhandled(this, args);
if (options.help) {
optionsHelper.showHelp(PROGNAME);
return EXIT_OK;

View File

@ -466,7 +466,21 @@ public final class TaskHelper {
return pp;
}
// used by jimage. Return unhandled arguments like "create", "describe".
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.
command = Arrays.copyOf(args, args.length);
@ -499,10 +513,10 @@ public final class TaskHelper {
String[] arr = new String[filteredArgs.size()];
args = filteredArgs.toArray(arr);
List<String> rest = new ArrayList<>();
List<String> rest = collectUnhandled? new ArrayList<>() : null;
// process options
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];
PlugOption pluginOption = null;
Option<T> option = getOption(name);
@ -539,7 +553,12 @@ public final class TaskHelper {
i = args.length;
}
} else {
rest.add(args[i]);
if (collectUnhandled) {
rest.add(args[i]);
} else {
throw new BadArgs("err.orphan.argument", args[i]).
showUsage(true);
}
}
}
return rest;

View File

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

View File

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

View File

@ -433,13 +433,23 @@ public class IncludeLocalesPluginTest {
for (Object[] data : testData) {
// create image for each test data
System.out.println("Invoking jlink with \"" + data[INCLUDE_LOCALES_OPTION] + "\"");
Result result = JImageGenerator.getJLinkTask()
Result result;
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())
.output(helper.createNewImageDir(moduleName))
.addMods((String) data[ADDMODS_OPTION])
.option((String) data[INCLUDE_LOCALES_OPTION])
.call();
}
String errorMsg = (String) data[ERROR_MESSAGE];
if (errorMsg.isEmpty()) {