forked from colinmarc/hdfs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added "test" command (issue colinmarc#157)
- Loading branch information
Colm Dougan
committed
Apr 30, 2022
1 parent
1dee011
commit 865fc98
Showing
4 changed files
with
220 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,72 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
) | ||
|
||
type testCommandFlags struct { | ||
exists bool | ||
isDir bool | ||
isFile bool | ||
isNonEmpty bool | ||
isEmpty bool | ||
} | ||
|
||
func test(args []string, flags testCommandFlags) { | ||
if len(args) == 0 { | ||
fatalWithUsage() | ||
} | ||
|
||
expanded, client, err := getClientAndExpandedPaths(args) | ||
if err != nil { | ||
fatal(err) | ||
} | ||
|
||
for _, p := range expanded { | ||
fi, err := client.Stat(p) | ||
if err != nil { | ||
//fmt.Fprintln(os.Stderr, err) | ||
status = 1 | ||
continue | ||
} | ||
|
||
if !doTest(fi, flags) { | ||
status = 1 | ||
continue | ||
} | ||
} | ||
} | ||
|
||
func doTest(fi os.FileInfo, flags testCommandFlags) bool { | ||
var result bool | ||
var flags_count int | ||
|
||
if flags.exists { | ||
result = true | ||
flags_count++ | ||
} | ||
if flags.isDir { | ||
result = fi.IsDir() | ||
flags_count++ | ||
} | ||
if flags.isFile { | ||
result = !fi.IsDir() | ||
flags_count++ | ||
} | ||
if flags.isNonEmpty { | ||
result = fi.Size() > 0 | ||
flags_count++ | ||
} | ||
if flags.isEmpty { | ||
result = fi.Size() == 0 | ||
flags_count++ | ||
} | ||
|
||
if flags_count != 1 { | ||
fmt.Fprintln(os.Stderr, "Only one test flag is allowed") | ||
return false | ||
} | ||
|
||
return result | ||
} |
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.