Skip to content

Commit

Permalink
added "test" command (issue colinmarc#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
Colm Dougan committed Apr 30, 2022
1 parent 1dee011 commit 865fc98
Show file tree
Hide file tree
Showing 4 changed files with 220 additions and 0 deletions.
17 changes: 17 additions & 0 deletions cmd/hdfs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Valid commands:
cat SOURCE...
head [-n LINES | -c BYTES] SOURCE...
tail [-n LINES | -c BYTES] SOURCE...
test [-defsz] FILE...
du [-sh] FILE...
checksum FILE...
get SOURCE [DEST]
Expand All @@ -57,6 +58,13 @@ Valid commands:
mkdirOpts = getopt.New()
mkdirp = mkdirOpts.Bool('p')

testOpts = getopt.New()
testIsDir = testOpts.Bool('d')
testPathExists = testOpts.Bool('e')
testIsFile = testOpts.Bool('f')
testIsNonEmpty = testOpts.Bool('s')
testIsEmpty = testOpts.Bool('z')

touchOpts = getopt.New()
touchc = touchOpts.Bool('c')

Expand Down Expand Up @@ -95,6 +103,7 @@ func init() {
duOpts.SetUsage(printHelp)
getmergeOpts.SetUsage(printHelp)
dfOpts.SetUsage(printHelp)
testOpts.SetUsage(printHelp)
}

func main() {
Expand Down Expand Up @@ -148,6 +157,14 @@ func main() {
case "df":
dfOpts.Parse(argv)
df(*dfh)
case "test":
testOpts.Parse(argv)
test(testOpts.Args(), testCommandFlags{
exists: *testPathExists,
isFile: *testIsFile,
isDir: *testIsDir,
isEmpty: *testIsEmpty,
isNonEmpty: *testIsNonEmpty})
case "truncate":
truncate(argv[1:])
// it's a seeeeecret command
Expand Down
72 changes: 72 additions & 0 deletions cmd/hdfs/test.go
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
}
131 changes: 131 additions & 0 deletions cmd/hdfs/test/test.bats
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 added testdata/empty.txt
Empty file.

0 comments on commit 865fc98

Please sign in to comment.