-
Notifications
You must be signed in to change notification settings - Fork 341
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
https://hadoop.apache.org/docs/stable/hadoop-project-dist/hadoop-common/FileSystemShell.html#test Fixes #157
- Loading branch information
Showing
4 changed files
with
200 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
) | ||
|
||
type testFn func(fi os.FileInfo) bool | ||
|
||
func test(args []string, exists, file, dir, empty, nonempty bool) { | ||
if len(args) == 0 { | ||
fatalWithUsage() | ||
} | ||
|
||
numFlags := 0 | ||
for _, b := range []bool{exists, dir, file, nonempty, empty} { | ||
if b { | ||
numFlags += 1 | ||
} | ||
} | ||
|
||
if numFlags != 1 { | ||
fatal("exactly one test flag must be specified") | ||
} | ||
|
||
var f func(fi os.FileInfo) bool | ||
switch { | ||
case exists: | ||
f = func(fi os.FileInfo) bool { return fi != nil } | ||
case dir: | ||
f = func(fi os.FileInfo) bool { return fi.IsDir() } | ||
case file: | ||
f = func(fi os.FileInfo) bool { return !fi.IsDir() } | ||
case nonempty: | ||
f = func(fi os.FileInfo) bool { return fi.Size() != 0 } | ||
case empty: | ||
f = func(fi os.FileInfo) bool { return fi.Size() == 0 } | ||
} | ||
|
||
expanded, client, err := getClientAndExpandedPaths(args) | ||
if err != nil { | ||
fatal(err) | ||
} | ||
|
||
for _, p := range expanded { | ||
fi, err := client.Stat(p) | ||
if err != nil && !errors.Is(err, os.ErrNotExist) { | ||
fmt.Fprintln(os.Stderr, err) | ||
continue | ||
} | ||
|
||
if !f(fi) { | ||
status = 1 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
#!/usr/bin/env bats | ||
|
||
load helper | ||
|
||
setup() { | ||
$HDFS mkdir -p /_test_cmd/test/dir1 | ||
$HDFS mkdir -p /_test_cmd/test/dir2 | ||
$HDFS mkdir -p /_test_cmd/test/dir3 | ||
$HDFS put $ROOT_TEST_DIR/testdata/empty.txt /_test_cmd/test/empty.txt | ||
$HDFS put $ROOT_TEST_DIR/testdata/foo.txt /_test_cmd/test/foo.txt | ||
$HDFS put $ROOT_TEST_DIR/testdata/foo.txt /_test_cmd/test/dir1/foo1.txt | ||
} | ||
|
||
@test "exists for existent file" { | ||
run $HDFS test -e /_test_cmd/test/foo.txt | ||
assert_success | ||
} | ||
|
||
@test "exists for existent dir" { | ||
run $HDFS test -e /_test_cmd/test/dir1 | ||
assert_success | ||
} | ||
|
||
@test "exists for non existent file" { | ||
run $HDFS test -e /_test_cmd/test/bar.txt | ||
assert_failure | ||
} | ||
|
||
@test "exists for non existent dir" { | ||
run $HDFS test -e /_test_cmd/test/dir9 | ||
assert_failure | ||
} | ||
|
||
@test "isfile for existent file" { | ||
run $HDFS test -f /_test_cmd/test/foo.txt | ||
assert_success | ||
} | ||
|
||
@test "isfile for existent dir" { | ||
run $HDFS test -f /_test_cmd/test/dir1 | ||
assert_failure | ||
} | ||
|
||
@test "isfile for non existent file" { | ||
run $HDFS test -f /_test_cmd/test/bar.txt | ||
assert_failure | ||
} | ||
|
||
@test "isfile for non existent dir" { | ||
run $HDFS test -f /_test_cmd/test/dir9 | ||
assert_failure | ||
} | ||
|
||
@test "isdir for existent file" { | ||
run $HDFS test -d /_test_cmd/test/foo.txt | ||
assert_failure | ||
} | ||
|
||
@test "isdir for existent dir" { | ||
run $HDFS test -d /_test_cmd/test/dir1 | ||
assert_success | ||
} | ||
|
||
@test "isdir for non existent file" { | ||
run $HDFS test -d /_test_cmd/test/bar.txt | ||
assert_failure | ||
} | ||
|
||
@test "isdir for non existent dir" { | ||
run $HDFS test -d /_test_cmd/test/dir9 | ||
assert_failure | ||
} | ||
|
||
@test "isnonempty for existent empty file" { | ||
run $HDFS test -s /_test_cmd/test/empty.txt | ||
assert_failure | ||
} | ||
|
||
@test "isnonempty for existent non empty file" { | ||
run $HDFS test -s /_test_cmd/test/foo.txt | ||
assert_success | ||
} | ||
|
||
@test "isnonempty for existent dir" { | ||
run $HDFS test -s /_test_cmd/test/dir1 | ||
assert_failure | ||
} | ||
|
||
@test "isnonempty for non existent file" { | ||
run $HDFS test -s /_test_cmd/test/bar.txt | ||
assert_failure | ||
} | ||
|
||
@test "isnonempty for non existent dir" { | ||
run $HDFS test -s /_test_cmd/test/dir9 | ||
assert_failure | ||
} | ||
|
||
@test "isempty for existent empty file" { | ||
run $HDFS test -z /_test_cmd/test/empty.txt | ||
assert_success | ||
} | ||
|
||
@test "isempty for existent non empty file" { | ||
run $HDFS test -z /_test_cmd/test/foo.txt | ||
assert_failure | ||
} | ||
|
||
# TOOD: check this outcome is correct | ||
@test "isempty for existent dir" { | ||
run $HDFS test -z /_test_cmd/test/dir1 | ||
assert_success | ||
} | ||
|
||
@test "isempty for non existent file" { | ||
run $HDFS test -z /_test_cmd/test/bar.txt | ||
assert_failure | ||
} | ||
|
||
@test "isempty for non existent dir" { | ||
run $HDFS test -z /_test_cmd/test/dir9 | ||
assert_failure | ||
} | ||
|
||
teardown() { | ||
$HDFS rm -r /_test_cmd/test | ||
} | ||
|
||
teardown() { | ||
$HDFS rm -r /_test_cmd/test | ||
} |
Empty file.