diff options
author | 2018-04-06 17:08:31 -0700 | |
---|---|---|
committer | 2018-04-06 17:10:25 -0700 | |
commit | 4a1e76bef02460826772b02fe2d50992810efc2d (patch) | |
tree | 30c1653b660c9d84372e1cd52efe009fc204d94d /src | |
parent | 3ab52e63079f1e43cb2c973425f615836a334082 (diff) |
Emit a usage error if --output is not provided
PiperOrigin-RevId: 191961686
Diffstat (limited to 'src')
3 files changed, 62 insertions, 4 deletions
diff --git a/src/java_tools/import_deps_checker/java/com/google/devtools/build/importdeps/Main.java b/src/java_tools/import_deps_checker/java/com/google/devtools/build/importdeps/Main.java index 6eed69e665..4d632d34e3 100644 --- a/src/java_tools/import_deps_checker/java/com/google/devtools/build/importdeps/Main.java +++ b/src/java_tools/import_deps_checker/java/com/google/devtools/build/importdeps/Main.java @@ -15,8 +15,9 @@ package com.google.devtools.build.importdeps; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkState; -import static com.google.common.io.Files.asCharSink; +import static com.google.common.io.MoreFiles.asCharSink; +import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.ImmutableList; import com.google.devtools.common.options.Converter; import com.google.devtools.common.options.Option; @@ -84,7 +85,7 @@ public class Main { @Option( name = "output", - defaultValue = "", + defaultValue = "null", category = "output", documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, effectTags = {OptionEffectTag.UNKNOWN}, @@ -133,13 +134,14 @@ public class Main { + ", classpath = " + options.classpath); System.err.println(result); - asCharSink(options.output.toFile(), StandardCharsets.UTF_8).write(result); + asCharSink(options.output, StandardCharsets.UTF_8).write(result); } } System.exit(exitCode); } - private static Options parseCommandLineOptions(String[] args) throws IOException { + @VisibleForTesting + static Options parseCommandLineOptions(String[] args) throws IOException { OptionsParser optionsParser = OptionsParser.newOptionsParser(Options.class); optionsParser.setAllowResidue(false); optionsParser.enableParamsFileSupport( @@ -148,6 +150,7 @@ public class Main { Options options = optionsParser.getOptions(Options.class); checkArgument(!options.inputJars.isEmpty(), "--input is required"); + checkArgument(options.output != null, "--output is required"); checkArgument(!options.bootclasspath.isEmpty(), "--bootclasspath_entry is required"); return options; } diff --git a/src/java_tools/import_deps_checker/javatests/com/google/devtools/build/importdeps/BUILD b/src/java_tools/import_deps_checker/javatests/com/google/devtools/build/importdeps/BUILD index da4b1a9c55..19a70eb328 100644 --- a/src/java_tools/import_deps_checker/javatests/com/google/devtools/build/importdeps/BUILD +++ b/src/java_tools/import_deps_checker/javatests/com/google/devtools/build/importdeps/BUILD @@ -205,3 +205,14 @@ create_golden_test( rt_jar = "@local_jdk//:jre/lib/rt.jar", testdata_pkg = "//src/java_tools/import_deps_checker/javatests/com/google/devtools/build/importdeps/testdata", ) + +java_test( + name = "MainTest", + srcs = ["MainTest.java"], + deps = [ + "//third_party:junit4", + "//third_party:truth", + "//third_party/bazel/src/java_tools/import_deps_checker/java/com/google/devtools/build/importdeps:main", + "//third_party/bazel/src/test/java/com/google/devtools/build/lib/testutil", + ], +) diff --git a/src/java_tools/import_deps_checker/javatests/com/google/devtools/build/importdeps/MainTest.java b/src/java_tools/import_deps_checker/javatests/com/google/devtools/build/importdeps/MainTest.java new file mode 100644 index 0000000000..e5bcb446e1 --- /dev/null +++ b/src/java_tools/import_deps_checker/javatests/com/google/devtools/build/importdeps/MainTest.java @@ -0,0 +1,44 @@ +// Copyright 2018 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.build.importdeps; + +import static com.google.common.truth.Truth.assertThat; +import static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows; + +import java.nio.file.Path; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public final class MainTest { + + @Rule public final TemporaryFolder tempFolder = new TemporaryFolder(); + + @Test + public void usage() throws Exception { + Path lib = tempFolder.newFile("lib.jar").toPath(); + Path in = tempFolder.newFile("in.jar").toPath(); + IllegalArgumentException thrown = + assertThrows( + IllegalArgumentException.class, + () -> + Main.parseCommandLineOptions( + new String[] {"--classpath_entry", lib.toString(), "--input", in.toString()})); + assertThat(thrown).hasMessageThat().contains("--output"); + } +} |