Skip to content

Commit

Permalink
Add "test" command
Browse files Browse the repository at this point in the history
  • Loading branch information
Colm Dougan authored and colinmarc committed Sep 2, 2022
1 parent 1dee011 commit 6e006c3
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 0 deletions.
12 changes: 12 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()
teste = testOpts.Bool('e')
testf = testOpts.Bool('f')
testd = testOpts.Bool('d')
testz = testOpts.Bool('z')
tests = testOpts.Bool('s')

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,9 @@ func main() {
case "df":
dfOpts.Parse(argv)
df(*dfh)
case "test":
testOpts.Parse(argv)
test(testOpts.Args(), *teste, *testf, *testd, *testz, *tests)
case "truncate":
truncate(argv[1:])
// it's a seeeeecret command
Expand Down
57 changes: 57 additions & 0 deletions cmd/hdfs/test.go
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
}
}
}
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 6e006c3

Please sign in to comment.