From b46e4ceafd2648527bf8985bd9a78f274bd5a408 Mon Sep 17 00:00:00 2001 From: Vinayakumar B Date: Sat, 26 Sep 2015 21:36:17 +0530 Subject: [PATCH] HADOOP-12442. Display help if the command option to 'hdfs dfs' is not valid (Contributed by nijel) (cherry picked from commit 861b52db242f238d7e36ad75c158025be959a696) --- .../hadoop-common/CHANGES.txt | 3 ++ .../java/org/apache/hadoop/fs/FsShell.java | 1 + .../org/apache/hadoop/fs/TestFsShell.java | 33 +++++++++++++++++++ 3 files changed, 37 insertions(+) diff --git a/hadoop-common-project/hadoop-common/CHANGES.txt b/hadoop-common-project/hadoop-common/CHANGES.txt index 36418825a5a..f9d516a7d07 100644 --- a/hadoop-common-project/hadoop-common/CHANGES.txt +++ b/hadoop-common-project/hadoop-common/CHANGES.txt @@ -651,6 +651,9 @@ Release 2.8.0 - UNRELEASED HADOOP-12437. Allow SecurityUtil to lookup alternate hostnames. (Arpit Agarwal) + HADOOP-12442. Display help if the command option to 'hdfs dfs' is not valid + (nijel via vinayakumarb) + Release 2.7.2 - UNRELEASED INCOMPATIBLE CHANGES diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/FsShell.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/FsShell.java index a0510beaa72..35608e2c712 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/FsShell.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/FsShell.java @@ -313,6 +313,7 @@ public class FsShell extends Configured implements Tool { } } catch (IllegalArgumentException e) { displayError(cmd, e.getLocalizedMessage()); + printUsage(System.err); if (instance != null) { printInstanceUsage(System.err, instance); } diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestFsShell.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestFsShell.java index 59fcbe505d0..cc93d68401c 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestFsShell.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestFsShell.java @@ -17,8 +17,12 @@ */ package org.apache.hadoop.fs; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; + import junit.framework.AssertionFailedError; import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.io.IOUtils; import org.apache.hadoop.tracing.SetSpanReceiver; import org.apache.hadoop.tracing.SpanReceiverHost; import org.apache.hadoop.util.ToolRunner; @@ -67,4 +71,33 @@ public class TestFsShell { SetSpanReceiver.getMap() .get("help").get(0).getKVAnnotations().get("args")); } + + @Test + public void testDFSWithInvalidCommmand() throws Throwable { + Configuration conf = new Configuration(); + FsShell shell = new FsShell(conf); + String[] args = new String[1]; + args[0] = "dfs -mkdirs"; + final ByteArrayOutputStream bytes = new ByteArrayOutputStream(); + final PrintStream out = new PrintStream(bytes); + final PrintStream oldErr = System.err; + try { + System.setErr(out); + ToolRunner.run(shell, args); + String errorValue=new String(bytes.toString()); + Assert + .assertTrue( + "FSShell dfs command did not print the error " + + "message when invalid command is passed", + errorValue.contains("-mkdirs: Unknown command")); + Assert + .assertTrue( + "FSShell dfs command did not print help " + + "message when invalid command is passed", + errorValue.contains("Usage: hadoop fs [generic options]")); + } finally { + IOUtils.closeStream(out); + System.setErr(oldErr); + } + } }