aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorGravatar apell <apell@google.com>2017-11-02 14:57:44 -0400
committerGravatar John Cater <jcater@google.com>2017-11-03 09:53:09 -0400
commit1ce985a704357c35d6691a80d94ca55372593b16 (patch)
tree23cfdb48ae86a27bd5b1dda310b349d17d6c6d20 /src
parent92d672d2be050547a0027f5bdefb8c69fe36c0a4 (diff)
Remove unused LegacyParamsFilePreProcessor. Removed test cases from OptionsParserTest are implemented in ParamsFilePreProcessorTest, ShellQuotedParamsFilePreProcessorTest and UnquotedParamsFilePreProcessorTest.
RELNOTES: None. PiperOrigin-RevId: 174359569
Diffstat (limited to 'src')
-rw-r--r--src/main/java/com/google/devtools/common/options/LegacyParamsFilePreProcessor.java151
-rw-r--r--src/test/java/com/google/devtools/common/options/OptionsParserTest.java313
2 files changed, 0 insertions, 464 deletions
diff --git a/src/main/java/com/google/devtools/common/options/LegacyParamsFilePreProcessor.java b/src/main/java/com/google/devtools/common/options/LegacyParamsFilePreProcessor.java
deleted file mode 100644
index 56a7d2c474..0000000000
--- a/src/main/java/com/google/devtools/common/options/LegacyParamsFilePreProcessor.java
+++ /dev/null
@@ -1,151 +0,0 @@
-// Copyright 2017 The Bazel Authors. All rights reserved.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-package com.google.devtools.common.options;
-
-import java.io.IOException;
-import java.io.Reader;
-import java.nio.charset.StandardCharsets;
-import java.nio.file.FileSystem;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.NoSuchElementException;
-
-/**
- * A {@link ParamsFilePreProcessor} that processes a parameter file using a custom format. This
- * format assumes each parameter is separated by whitespace and allows arguments to use single and
- * double quotes and quote and whitespace escaping.
- *
- * <p><em>NOTE:</em> This class is deprecated; use either {@link ShellQuotedParamsFilePreProcessor}
- * or {@link UnquotedParamsFilePreProcessor} depending on the format of the provided params file.
- */
-@Deprecated
-public class LegacyParamsFilePreProcessor extends ParamsFilePreProcessor {
-
- public LegacyParamsFilePreProcessor(FileSystem fs) {
- super(fs);
- }
-
- @Override
- protected List<String> parse(Path paramsFile) throws IOException, OptionsParsingException {
- try (Reader params = Files.newBufferedReader(paramsFile, StandardCharsets.UTF_8)) {
- List<String> newArgs = new ArrayList<>();
- StringBuilder arg = new StringBuilder();
- CharIterator iterator = CharIterator.wrap(params);
- while (iterator.hasNext()) {
- char next = iterator.next();
- if (Character.isWhitespace(next) && !iterator.isInQuote() && !iterator.isEscaped()) {
- newArgs.add(unescape(arg.toString()));
- arg = new StringBuilder();
- } else {
- arg.append(next);
- }
- }
- // If there is an arg in the buffer, add it.
- if (arg.length() > 0) {
- newArgs.add(arg.toString());
- }
- // If we're still in a quote by the end of the file, throw an error.
- if (iterator.isInQuote()) {
- throw new OptionsParsingException(
- String.format(ERROR_MESSAGE_FORMAT, paramsFile, iterator.getUnmatchedQuoteMessage()));
- }
- return newArgs;
- }
- }
-
- private String unescape(String arg) {
- if (arg.startsWith("'") && arg.endsWith("'")) {
- String unescaped = arg.replace("'\\''", "'");
- return unescaped.substring(1, unescaped.length() - 1);
- }
- return arg;
- }
-
- // Doesn't implement iterator to avoid autoboxing and to throw exceptions.
- private static class CharIterator {
-
- private final Reader reader;
- private int readerPosition = 0;
- private int singleQuoteStart = -1;
- private int doubleQuoteStart = -1;
- private boolean escaped = false;
- private char lastChar = (char) -1;
-
- public static CharIterator wrap(Reader reader) {
- return new CharIterator(reader);
- }
-
- public CharIterator(Reader reader) {
- this.reader = reader;
- }
-
- public boolean hasNext() throws IOException {
- return peek() != -1;
- }
-
- private int peek() throws IOException {
- reader.mark(1);
- int next = reader.read();
- reader.reset();
- return next;
- }
-
- public boolean isInQuote() {
- return singleQuoteStart != -1 || doubleQuoteStart != -1;
- }
-
- public boolean isEscaped() {
- return escaped;
- }
-
- public String getUnmatchedQuoteMessage() {
- StringBuilder message = new StringBuilder();
- if (singleQuoteStart != -1) {
- message.append(String.format(UNFINISHED_QUOTE_MESSAGE_FORMAT, "'", singleQuoteStart));
- }
- if (doubleQuoteStart != -1) {
- message.append(String.format(UNFINISHED_QUOTE_MESSAGE_FORMAT, "\"", doubleQuoteStart));
- }
- return message.toString();
- }
-
- public char next() throws IOException {
- if (!hasNext()) {
- throw new NoSuchElementException();
- }
- char current = (char) reader.read();
-
- // check for \r\n line endings. If found, drop the \r for normalized parsing.
- if (current == '\r' && peek() == '\n') {
- current = (char) reader.read();
- }
-
- // check to see if the current position is escaped
- escaped = (lastChar == '\\');
-
- if (!escaped && current == '\'') {
- singleQuoteStart = singleQuoteStart == -1 ? readerPosition : -1;
- }
- if (!escaped && current == '"') {
- doubleQuoteStart = doubleQuoteStart == -1 ? readerPosition : -1;
- }
-
- readerPosition++;
- lastChar = current;
- return current;
- }
- }
-}
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 49e9d9d9a9..25ce6dd0cd 100644
--- a/src/test/java/com/google/devtools/common/options/OptionsParserTest.java
+++ b/src/test/java/com/google/devtools/common/options/OptionsParserTest.java
@@ -25,15 +25,9 @@ import com.google.devtools.common.options.Converters.CommaSeparatedOptionListCon
import com.google.devtools.common.options.OptionPriority.PriorityCategory;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
-import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
-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.ArrayList;
import java.util.Arrays;
import java.util.Collections;
@@ -228,313 +222,6 @@ public class OptionsParserTest {
}
@Test
- public void parseWithParamsFile() throws OptionsParsingException, IOException {
- // TODO(bazel-team): Switch to an in memory file system, here and below.
- 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(new LegacyParamsFilePreProcessor(FileSystems.getDefault()));
- parser.parse("@" + params);
- ExampleFoo foo = parser.getOptions(ExampleFoo.class);
- assertThat(foo.foo).isEqualTo("defaultFoo");
- assertThat(foo.bar).isEqualTo(17);
- ExampleBaz baz = parser.getOptions(ExampleBaz.class);
- assertThat(baz.baz).isEqualTo("oops");
- }
-
- @Test
- public void parseWithEmptyParamsFile() throws OptionsParsingException, IOException {
- // TODO(bazel-team): Switch to an in memory file system, here and below.
- Path params = Files.createTempDirectory("foo").resolve("params");
- Files.write(
- params,
- ImmutableList.of(""),
- StandardCharsets.UTF_8,
- StandardOpenOption.CREATE);
-
- OptionsParser parser = newOptionsParser(ExampleFoo.class, ExampleBaz.class);
- parser.enableParamsFileSupport(new LegacyParamsFilePreProcessor(FileSystems.getDefault()));
- parser.parse("@" + params);
- ExampleFoo foo = parser.getOptions(ExampleFoo.class);
- assertThat(foo.foo).isEqualTo("defaultFoo");
- assertThat(foo.bar).isEqualTo(42);
- ExampleBaz baz = parser.getOptions(ExampleBaz.class);
- assertThat(baz.baz).isEqualTo("defaultBaz");
- }
-
- @Test
- public void parseWithParamsFileWithEmptyStringValues() throws Exception {
- Path params = Files.createTempDirectory("foo").resolve("params");
- Files.write(
- params,
- ImmutableList.of("--baz", "", "--foo", ""),
- StandardCharsets.UTF_8,
- StandardOpenOption.CREATE);
-
- OptionsParser parser = newOptionsParser(ExampleFoo.class, ExampleBaz.class);
- parser.enableParamsFileSupport(new LegacyParamsFilePreProcessor(FileSystems.getDefault()));
- parser.parse("@" + params);
- ExampleFoo foo = parser.getOptions(ExampleFoo.class);
- assertThat(foo.foo).isEmpty();
- ExampleBaz baz = parser.getOptions(ExampleBaz.class);
- assertThat(baz.baz).isEmpty();
- }
-
- @Test
- public void parseWithParamsFileWithEmptyString() throws OptionsParsingException, IOException {
- // TODO(bazel-team): Switch to an in memory file system, here and below.
- Path params = Files.createTempDirectory("foo").resolve("params");
- Files.write(
- params,
- ImmutableList.of("--baz --bar 17"),
- StandardCharsets.UTF_8,
- StandardOpenOption.CREATE);
-
- OptionsParser parser = newOptionsParser(ExampleFoo.class, ExampleBaz.class);
- parser.enableParamsFileSupport(new LegacyParamsFilePreProcessor(FileSystems.getDefault()));
- parser.parse("@" + params);
- ExampleFoo foo = parser.getOptions(ExampleFoo.class);
- assertThat(foo.foo).isEqualTo("defaultFoo");
- assertThat(foo.bar).isEqualTo(17);
- ExampleBaz baz = parser.getOptions(ExampleBaz.class);
- assertThat(baz.baz).isEmpty();
- }
-
- @Test
- public void parseWithParamsFileWithEmptyStringAtEnd()
- throws OptionsParsingException, IOException {
- // TODO(bazel-team): Switch to an in memory file system, here and below.
- Path params = Files.createTempDirectory("foo").resolve("params");
- Files.write(
- params,
- ImmutableList.of("--bar",
- "17",
- " --baz",
- ""),
- StandardCharsets.UTF_8,
- StandardOpenOption.CREATE);
-
- OptionsParser parser = newOptionsParser(ExampleFoo.class, ExampleBaz.class);
- parser.enableParamsFileSupport(new LegacyParamsFilePreProcessor(FileSystems.getDefault()));
- parser.parse("@" + params);
- ExampleFoo foo = parser.getOptions(ExampleFoo.class);
- assertThat(foo.foo).isEqualTo("defaultFoo");
- assertThat(foo.bar).isEqualTo(17);
- ExampleBaz baz = parser.getOptions(ExampleBaz.class);
- assertThat(baz.baz).isEmpty();
- }
-
- @Test
- public void parseWithParamsFileWithQuotedSpaces() throws OptionsParsingException, IOException {
- 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(new LegacyParamsFilePreProcessor(FileSystems.getDefault()));
- parser.parse("@" + params);
- ExampleFoo foo = parser.getOptions(ExampleFoo.class);
- assertThat(foo.foo).isEqualTo("\"fuzzy\nfoo\"");
- assertThat(foo.bar).isEqualTo(17);
- ExampleBaz baz = parser.getOptions(ExampleBaz.class);
- assertThat(baz.baz).isEqualTo("defaultBaz");
- }
-
- @Test
- public void parseWithParamsFileWithEscapedSpaces() throws OptionsParsingException, IOException {
- 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(new LegacyParamsFilePreProcessor(FileSystems.getDefault()));
- parser.parse("@" + params);
- ExampleFoo foo = parser.getOptions(ExampleFoo.class);
- assertThat(foo.foo).isEqualTo("fuzzy\\ foo");
- assertThat(foo.bar).isEqualTo(17);
- ExampleBaz baz = parser.getOptions(ExampleBaz.class);
- assertThat(baz.baz).isEqualTo("defaultBaz");
- }
-
- @Test
- public void parseWithParamsFileWithEscapedQuotes() throws OptionsParsingException, IOException {
- 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(new LegacyParamsFilePreProcessor(FileSystems.getDefault()));
- parser.parse("@" + params);
- ExampleFoo foo = parser.getOptions(ExampleFoo.class);
- assertThat(foo.foo).isEqualTo("\"fuzzy\\\"foo\"");
- assertThat(foo.bar).isEqualTo(17);
- ExampleBaz baz = parser.getOptions(ExampleBaz.class);
- assertThat(baz.baz).isEqualTo("defaultBaz");
- }
-
- @Test
- public void parseWithParamsFileSingleQuotesUnescaping()
- throws OptionsParsingException, IOException {
- 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(new LegacyParamsFilePreProcessor(FileSystems.getDefault()));
- parser.parse("@" + params);
- ExampleFoo foo = parser.getOptions(ExampleFoo.class);
- assertThat(foo.foo).isEqualTo("fuzzy 'foo");
- assertThat(foo.bar).isEqualTo(17);
- ExampleBaz baz = parser.getOptions(ExampleBaz.class);
- assertThat(baz.baz).isEqualTo("defaultBaz");
- }
-
- @Test
- public void parseWithParamsFilePartiallyQuotedNoUnescaping()
- throws OptionsParsingException, IOException {
- 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(new LegacyParamsFilePreProcessor(FileSystems.getDefault()));
- parser.parse("@" + params);
- ExampleFoo foo = parser.getOptions(ExampleFoo.class);
- assertThat(foo.foo).isEqualTo("'fuzzy 'foo");
- assertThat(foo.bar).isEqualTo(17);
- ExampleBaz baz = parser.getOptions(ExampleBaz.class);
- assertThat(baz.baz).isEqualTo("defaultBaz");
- }
-
- @Test
- public void parseWithParamsFileUnmatchedQuote() throws IOException {
- 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(new LegacyParamsFilePreProcessor(FileSystems.getDefault()));
- try {
- parser.parse("@" + params);
- fail();
- } catch (OptionsParsingException e) {
- assertThat(e)
- .hasMessageThat()
- .isEqualTo(
- String.format(
- ParamsFilePreProcessor.ERROR_MESSAGE_FORMAT,
- params,
- String.format(ParamsFilePreProcessor.UNFINISHED_QUOTE_MESSAGE_FORMAT, "\"", 6)));
- }
- }
-
- @Test
- public void parseWithParamsFileWithMultilineStringValues() throws Exception {
- Path params = Files.createTempDirectory("foo").resolve("params");
- Files.write(
- params,
- ImmutableList.of(
- "--baz",
- "'hello\nworld'",
- "--foo",
- "hello\\",
- "world",
- "--nodoc",
- "\"hello",
- "world\""),
- StandardCharsets.UTF_8,
- StandardOpenOption.CREATE);
-
- OptionsParser parser = newOptionsParser(ExampleFoo.class, ExampleBaz.class);
- parser.enableParamsFileSupport(new LegacyParamsFilePreProcessor(FileSystems.getDefault()));
- parser.parse("@" + params);
- ExampleFoo foo = parser.getOptions(ExampleFoo.class);
- assertThat(foo.foo).isEqualTo("hello\\\nworld");
- assertThat(foo.nodoc).isEqualTo("\"hello\nworld\"");
- ExampleBaz baz = parser.getOptions(ExampleBaz.class);
- assertThat(baz.baz).isEqualTo("hello\nworld");
- }
-
- @Test
- public void parseWithParamsFileWithMultilineStringValuesCRLF() throws Exception {
- Path params = Files.createTempDirectory("foo").resolve("params");
- Files.write(
- params,
- ImmutableList.of(
- "--baz\r\n'hello\nworld'\r\n--foo\r\nhello\\\r\nworld\r\n\r\n"
- + "--nodoc\r\n\"hello\r\nworld\""),
- StandardCharsets.UTF_8,
- StandardOpenOption.CREATE);
-
- OptionsParser parser = newOptionsParser(ExampleFoo.class, ExampleBaz.class);
- parser.enableParamsFileSupport(new LegacyParamsFilePreProcessor(FileSystems.getDefault()));
- parser.parse("@" + params);
- ExampleBaz baz = parser.getOptions(ExampleBaz.class);
- assertThat(baz.baz).isEqualTo("hello\nworld");
- ExampleFoo foo = parser.getOptions(ExampleFoo.class);
- assertThat(foo.foo).isEqualTo("hello\\\nworld");
- assertThat(foo.nodoc).isEqualTo("\"hello\nworld\"");
- }
-
- @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(new LegacyParamsFilePreProcessor(FileSystems.getDefault()));
- parser.parse("@" + params);
- ExampleFoo foo = parser.getOptions(ExampleFoo.class);
- assertThat(foo.foo).isEqualTo("defaultFoo");
- assertThat(foo.bar).isEqualTo(17);
- ExampleBaz baz = parser.getOptions(ExampleBaz.class);
- assertThat(baz.baz).isEqualTo("oops");
- }
-
- @Test
- public void parsingFailsWithMissingParamsFile() {
- OptionsParser parser = newOptionsParser(ExampleFoo.class, ExampleBaz.class);
- parser.enableParamsFileSupport(new LegacyParamsFilePreProcessor(FileSystems.getDefault()));
- List<String> unknownOpts = asList("@does/not/exist");
- try {
- parser.parse(unknownOpts);
- fail();
- } catch (OptionsParsingException e) {
- assertThat(e.getInvalidArgument()).isEqualTo("@does/not/exist");
- assertThat(parser.getOptions(ExampleFoo.class)).isNotNull();
- assertThat(parser.getOptions(ExampleBaz.class)).isNotNull();
- }
- }
-
- @Test
public void parseWithOptionsInheritance() throws OptionsParsingException {
OptionsParser parser = newOptionsParser(ExampleBazSubclass.class);
parser.parse("--baz_subclass=cat", "--baz=dog");