aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/java/com/google/devtools/common/options
diff options
context:
space:
mode:
authorGravatar Googler <noreply@google.com>2017-01-24 23:04:52 +0000
committerGravatar Laszlo Csomor <laszlocsomor@google.com>2017-01-25 10:10:39 +0000
commit3f06748eb6c4fe1a8d30aae118f37885c23ec984 (patch)
treedadc0477e924653d384c31fec72e7892626f1450 /src/test/java/com/google/devtools/common/options
parentc24bca7893c126e345a5fd51def0fa46454fbe00 (diff)
Enable parameter file support for excessively long commandlines.
-- PiperOrigin-RevId: 145473123 MOS_MIGRATED_REVID=145473123
Diffstat (limited to 'src/test/java/com/google/devtools/common/options')
-rw-r--r--src/test/java/com/google/devtools/common/options/OptionsParserTest.java147
1 files changed, 147 insertions, 0 deletions
diff --git a/src/test/java/com/google/devtools/common/options/OptionsParserTest.java b/src/test/java/com/google/devtools/common/options/OptionsParserTest.java
index fcb55e9a61..c2e3d6018f 100644
--- a/src/test/java/com/google/devtools/common/options/OptionsParserTest.java
+++ b/src/test/java/com/google/devtools/common/options/OptionsParserTest.java
@@ -29,6 +29,12 @@ import com.google.common.collect.ImmutableMap;
import com.google.devtools.common.options.Converters.CommaSeparatedOptionListConverter;
import com.google.devtools.common.options.OptionsParser.OptionValueDescription;
import com.google.devtools.common.options.OptionsParser.UnparsedOptionValueDescription;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.FileSystems;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.StandardOpenOption;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
@@ -135,6 +141,146 @@ public class OptionsParserTest {
ExampleBaz baz = parser.getOptions(ExampleBaz.class);
assertEquals("oops", baz.baz);
}
+
+ @Test
+ public void parseWithParamsFile() throws OptionsParsingException, IOException {
+ // TODO(bazel-team): Switch to an in memory file system.
+ Path params = Files.createTempDirectory("foo").resolve("params");
+ Files.write(
+ params,
+ ImmutableList.of("--baz=oops --bar 17"),
+ StandardCharsets.UTF_8,
+ StandardOpenOption.CREATE);
+
+ OptionsParser parser = newOptionsParser(ExampleFoo.class, ExampleBaz.class);
+ parser.enableParamsFileSupport(FileSystems.getDefault());
+ parser.parse("@" + params);
+ ExampleFoo foo = parser.getOptions(ExampleFoo.class);
+ assertEquals("defaultFoo", foo.foo);
+ assertEquals(17, foo.bar);
+ ExampleBaz baz = parser.getOptions(ExampleBaz.class);
+ assertEquals("oops", baz.baz);
+ }
+
+ @Test
+ public void parseWithParamsFileWithQuotedSpaces() throws OptionsParsingException, IOException {
+ // TODO(bazel-team): Switch to an in memory file system.
+ Path params = Files.createTempDirectory("foo").resolve("params");
+ Files.write(
+ params,
+ ImmutableList.of("--foo=\"fuzzy\nfoo\" --bar 17"),
+ StandardCharsets.UTF_8,
+ StandardOpenOption.CREATE);
+
+ OptionsParser parser = newOptionsParser(ExampleFoo.class, ExampleBaz.class);
+ parser.enableParamsFileSupport(FileSystems.getDefault());
+ parser.parse("@" + params);
+ ExampleFoo foo = parser.getOptions(ExampleFoo.class);
+ assertEquals("\"fuzzy\nfoo\"", foo.foo);
+ assertEquals(17, foo.bar);
+ ExampleBaz baz = parser.getOptions(ExampleBaz.class);
+ assertEquals("defaultBaz", baz.baz);
+ }
+
+ @Test
+ public void parseWithParamsFileWithEscapedSpaces() throws OptionsParsingException, IOException {
+ // TODO(bazel-team): Switch to an in memory file system.
+ Path params = Files.createTempDirectory("foo").resolve("params");
+ Files.write(
+ params,
+ ImmutableList.of("--foo=fuzzy\\ foo --bar 17"),
+ StandardCharsets.UTF_8,
+ StandardOpenOption.CREATE);
+
+ OptionsParser parser = newOptionsParser(ExampleFoo.class, ExampleBaz.class);
+ parser.enableParamsFileSupport(FileSystems.getDefault());
+ parser.parse("@" + params);
+ ExampleFoo foo = parser.getOptions(ExampleFoo.class);
+ assertEquals("fuzzy\\ foo", foo.foo);
+ assertEquals(17, foo.bar);
+ ExampleBaz baz = parser.getOptions(ExampleBaz.class);
+ assertEquals("defaultBaz", baz.baz);
+ }
+
+ @Test
+ public void parseWithParamsFileWithEscapedQuotes() throws OptionsParsingException, IOException {
+ // TODO(bazel-team): Switch to an in memory file system.
+ Path params = Files.createTempDirectory("foo").resolve("params");
+ Files.write(
+ params,
+ ImmutableList.of("--foo=\"fuzzy\\\"foo\" --bar 17"),
+ StandardCharsets.UTF_8,
+ StandardOpenOption.CREATE);
+
+ OptionsParser parser = newOptionsParser(ExampleFoo.class, ExampleBaz.class);
+ parser.enableParamsFileSupport(FileSystems.getDefault());
+ parser.parse("@" + params);
+ ExampleFoo foo = parser.getOptions(ExampleFoo.class);
+ assertEquals("\"fuzzy\\\"foo\"", foo.foo);
+ assertEquals(17, foo.bar);
+ ExampleBaz baz = parser.getOptions(ExampleBaz.class);
+ assertEquals("defaultBaz", baz.baz);
+ }
+
+ @Test
+ public void parseWithParamsFileUnmatchedQuote() throws IOException {
+ // TODO(bazel-team): Switch to an in memory file system.
+ Path params = Files.createTempDirectory("foo").resolve("params");
+ Files.write(
+ params,
+ ImmutableList.of("--foo=\"fuzzy foo --bar 17"),
+ StandardCharsets.UTF_8,
+ StandardOpenOption.CREATE);
+
+ OptionsParser parser = newOptionsParser(ExampleFoo.class, ExampleBaz.class);
+ parser.enableParamsFileSupport(FileSystems.getDefault());
+ try {
+ parser.parse("@" + params);
+ fail();
+ } catch (OptionsParsingException e) {
+ assertEquals(
+ String.format(
+ ParamsFilePreProcessor.ERROR_MESSAGE_FORMAT,
+ params,
+ String.format(ParamsFilePreProcessor.UNFINISHED_QUOTE_MESSAGE_FORMAT, "\"", 6)),
+ e.getMessage());
+ }
+ }
+
+ @Test
+ public void parseWithParamsFileMultiline() throws OptionsParsingException, IOException {
+ // TODO(bazel-team): Switch to an in memory file system.
+ Path params = Files.createTempDirectory("foo").resolve("params");
+ Files.write(
+ params,
+ ImmutableList.of("--baz", "oops", "--bar", "17"),
+ StandardCharsets.UTF_8,
+ StandardOpenOption.CREATE);
+
+ OptionsParser parser = newOptionsParser(ExampleFoo.class, ExampleBaz.class);
+ parser.enableParamsFileSupport(FileSystems.getDefault());
+ parser.parse("@" + params);
+ ExampleFoo foo = parser.getOptions(ExampleFoo.class);
+ assertEquals("defaultFoo", foo.foo);
+ assertEquals(17, foo.bar);
+ ExampleBaz baz = parser.getOptions(ExampleBaz.class);
+ assertEquals("oops", baz.baz);
+ }
+
+ @Test
+ public void parsingFailsWithMissingParamsFile() {
+ OptionsParser parser = newOptionsParser(ExampleFoo.class, ExampleBaz.class);
+ parser.enableParamsFileSupport(FileSystems.getDefault());
+ List<String> unknownOpts = asList("@does/not/exist");
+ try {
+ parser.parse(unknownOpts);
+ fail();
+ } catch (OptionsParsingException e) {
+ assertEquals("@does/not/exist", e.getInvalidArgument());
+ assertNotNull(parser.getOptions(ExampleFoo.class));
+ assertNotNull(parser.getOptions(ExampleBaz.class));
+ }
+ }
@Test
public void parseWithOptionsInheritance() throws OptionsParsingException {
@@ -1176,3 +1322,4 @@ public class OptionsParserTest {
assertEquals(Arrays.asList("--flag1=true", "--flag2=87", "--flag3=bar"), canonicalized);
}
}
+