HDFS-1331. dfs -test should work like /bin/test (Andy Isaacson via daryn)

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1406198 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Daryn Sharp 2012-11-06 15:57:58 +00:00
parent 5605b54010
commit 3a698e6aea
4 changed files with 119 additions and 7 deletions

View File

@ -37,16 +37,21 @@ class Test extends FsCommand {
}
public static final String NAME = "test";
public static final String USAGE = "-[ezd] <path>";
public static final String USAGE = "-[defsz] <path>";
public static final String DESCRIPTION =
"If file exists, has zero length, is a directory\n" +
"then return 0, else return 1.";
"Answer various questions about <path>, with result via exit status.\n" +
" -d return 0 if <path> is a directory.\n" +
" -e return 0 if <path> exists.\n" +
" -f return 0 if <path> is a file.\n" +
" -s return 0 if file <path> is greater than zero bytes in size.\n" +
" -z return 0 if file <path> is zero bytes in size.\n" +
"else, return 1.";
private char flag;
@Override
protected void processOptions(LinkedList<String> args) {
CommandFormat cf = new CommandFormat(1, 1, "e", "d", "z");
CommandFormat cf = new CommandFormat(1, 1, "e", "d", "f", "s", "z");
cf.parse(args);
String[] opts = cf.getOpts().toArray(new String[0]);
@ -71,6 +76,12 @@ class Test extends FsCommand {
case 'd':
test = item.stat.isDirectory();
break;
case 'f':
test = item.stat.isFile();
break;
case 's':
test = (item.stat.getLen() > 0);
break;
case 'z':
test = (item.stat.getLen() == 0);
break;

View File

@ -591,11 +591,11 @@
<comparators>
<comparator>
<type>RegexpComparator</type>
<expected-output>^-test -\[ezd\] &lt;path&gt;:\s+If file exists, has zero length, is a directory( )*</expected-output>
<expected-output>^-test -\[defsz\] &lt;path&gt;:\sAnswer various questions about &lt;path&gt;, with result via exit status.</expected-output>
</comparator>
<comparator>
<type>RegexpComparator</type>
<expected-output>^( |\t)*then return 0, else return 1.( )*</expected-output>
<expected-output>^( |\t)*else, return 1.( )*</expected-output>
</comparator>
</comparators>
</test>

View File

@ -551,6 +551,8 @@ Release 2.0.3-alpha - Unreleased
HDFS-4132. When libwebhdfs is not enabled, nativeMiniDfsClient frees
uninitialized memory (Colin Patrick McCabe via todd)
HDFS-1331. dfs -test should work like /bin/test (Andy Isaacson via daryn)
Release 2.0.2-alpha - 2012-09-07
INCOMPATIBLE CHANGES

View File

@ -1243,7 +1243,106 @@ public class TestDFSShell {
}
assertEquals(0, val);
}
// Verify -test -f negative case (missing file)
{
String[] args = new String[3];
args[0] = "-test";
args[1] = "-f";
args[2] = "/test/mkdirs/noFileHere";
int val = -1;
try {
val = shell.run(args);
} catch (Exception e) {
System.err.println("Exception raised from DFSShell.run " +
e.getLocalizedMessage());
}
assertEquals(1, val);
}
// Verify -test -f negative case (directory rather than file)
{
String[] args = new String[3];
args[0] = "-test";
args[1] = "-f";
args[2] = "/test/mkdirs";
int val = -1;
try {
val = shell.run(args);
} catch (Exception e) {
System.err.println("Exception raised from DFSShell.run " +
e.getLocalizedMessage());
}
assertEquals(1, val);
}
// Verify -test -f positive case
{
writeFile(fileSys, myFile);
assertTrue(fileSys.exists(myFile));
String[] args = new String[3];
args[0] = "-test";
args[1] = "-f";
args[2] = myFile.toString();
int val = -1;
try {
val = shell.run(args);
} catch (Exception e) {
System.err.println("Exception raised from DFSShell.run " +
e.getLocalizedMessage());
}
assertEquals(0, val);
}
// Verify -test -s negative case (missing file)
{
String[] args = new String[3];
args[0] = "-test";
args[1] = "-s";
args[2] = "/test/mkdirs/noFileHere";
int val = -1;
try {
val = shell.run(args);
} catch (Exception e) {
System.err.println("Exception raised from DFSShell.run " +
e.getLocalizedMessage());
}
assertEquals(1, val);
}
// Verify -test -s negative case (zero length file)
{
String[] args = new String[3];
args[0] = "-test";
args[1] = "-s";
args[2] = "/test/mkdirs/isFileHere";
int val = -1;
try {
val = shell.run(args);
} catch (Exception e) {
System.err.println("Exception raised from DFSShell.run " +
e.getLocalizedMessage());
}
assertEquals(1, val);
}
// Verify -test -s positive case (nonzero length file)
{
String[] args = new String[3];
args[0] = "-test";
args[1] = "-s";
args[2] = myFile.toString();
int val = -1;
try {
val = shell.run(args);
} catch (Exception e) {
System.err.println("Exception raised from DFSShell.run " +
e.getLocalizedMessage());
}
assertEquals(0, val);
}
} finally {
try {
fileSys.close();