Skip to content
This repository has been archived by the owner on Oct 4, 2019. It is now read-only.

Show help and exit on invalid command. #191

Merged
merged 12 commits into from
Apr 25, 2017
23 changes: 19 additions & 4 deletions accounts/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,26 +106,41 @@ func TestWatchNoDir(t *testing.T) {
// Create the directory and copy a key file into it.
os.MkdirAll(dir, 0700)
defer os.RemoveAll(dir)

file := filepath.Join(dir, "aaa")
data, err := ioutil.ReadFile(cachetestAccounts[0].File)
if err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(file, data, 0666); err != nil {
ff, err := os.Create(file)
if err != nil {
t.Fatal(err)
}
_, err = ff.Write(data)
if err != nil {
t.Fatal(err)
}
ff.Close()

// am should see the account.
wantAccounts := []Account{cachetestAccounts[0]}
wantAccounts[0].File = file
for d := 200 * time.Millisecond; d < 8*time.Second; d *= 2 {
var seen, gotAccounts = make(map[time.Duration]bool), make(map[time.Duration][]Account)
for d := 200 * time.Second; d < 8*time.Second; d *= 2 {
list = am.Accounts()
seen[d] = false
if reflect.DeepEqual(list, wantAccounts) {
return
seen[d] = true
} else {
}
gotAccounts[d] = list
time.Sleep(d)
}
t.Errorf("\ngot %v\nwant %v", list, wantAccounts)
for i, saw := range seen {
if !saw {
t.Errorf("\nat %v got %v\nwant %v", i, gotAccounts[i], wantAccounts)
}
}
}

func TestCacheInitialReload(t *testing.T) {
Expand Down
80 changes: 80 additions & 0 deletions cmd/geth/cli.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/usr/bin/env bats

: ${GETH_CMD:=$GOPATH/bin/geth}

setup() {
DATA_DIR=`mktemp -d`
}

teardown() {
rm -fr $DATA_DIR
}

# Invalid flags exit with exit code 1.
# Invalid commands and subcommands exit with exit code 3.

@test "runs with valid command" {
run $GETH_CMD version
[ "$status" -eq 0 ]
[[ "$output" == *"Geth"* ]]
[[ "$output" == *"Version: "* ]]
[[ "$output" == *"Go Version: "* ]]
[[ "$output" == *"OS: "* ]]
[[ "$output" == *"GOPATH="* ]]
[[ "$output" == *"GOROOT="* ]]
}

@test "displays help with invalid command" {
run $GETH_CMD verison
[ "$status" -eq 3 ]
[[ "$output" == *"Invalid command"* ]]
[[ "$output" == *"USAGE"* ]]
}

@test "displays help with invalid flag" {
run $GETH_CMD --fat
[ "$status" -eq 1 ]
[[ "$output" == *"flag provided but not defined"* ]]
}

@test "runs with valid flag and valid command" {
run $GETH_CMD --datadir $DATA_DIR --maxpeers 0 --nodiscover --nat none --ipcdisable --exec 'exit' console
echo "$output"

[ "$status" -eq 0 ]
[[ "$output" == *"Starting"* ]]
[[ "$output" == *"Loading blockchain: "* ]]
[[ "$output" == *"Blockchain DB Version: "* ]]
[[ "$output" == *"Starting Server"* ]]
}

@test "displays help with invalid flag and valid command" {
# --nodisco
run $GETH_CMD --datadir $DATA_DIR --maxpeers 0 --nodisco --nat none --ipcdisable --exec 'exit' console
echo "$output"

[ "$status" -eq 1 ]
[[ "$output" == *"flag provided but not defined"* ]]
[[ "$output" == *"USAGE"* ]]
}

@test "displays help with valid flag and invalid command" {
# conso
run $GETH_CMD --datadir $DATA_DIR --maxpeers 0 --nodiscover --nat none --ipcdisable --exec 'exit' conso
echo "$output"

[ "$status" -eq 3 ]
[[ "$output" == *"Invalid command"* ]]
[[ "$output" == *"USAGE"* ]]
}

# TODO
# This doesn't pass, and that's an issue.
# @test "displays help with valid command and invalid subcommand" {
# # lisr
# run $GETH_CMD account lisr
# echo "$output"

# [ "$status" -eq 3 ]
# [[ "$output" == *"SUBCOMMANDS"* ]]
# }
23 changes: 23 additions & 0 deletions cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,23 @@ participating.
}

app.Before = func(ctx *cli.Context) error {

// It's a patch.
// Don't know why urfave/cli isn't catching the unknown command on its own.
if ctx.Args().Present() {
commandExists := false
for _, cmd := range app.Commands {
if cmd.HasName(ctx.Args().First()) {
commandExists = true
}
}
if !commandExists {
if e := cli.ShowCommandHelp(ctx, ctx.Args().First()); e != nil {
return e
}
}
}

runtime.GOMAXPROCS(runtime.NumCPU())

glog.CopyStandardLogTo("INFO")
Expand Down Expand Up @@ -204,6 +221,12 @@ participating.
return nil
}

app.CommandNotFound = func(c *cli.Context, command string) {
fmt.Fprintf(c.App.Writer, "Invalid command: %q. Please find `geth` usage below. \n", command)
cli.ShowAppHelp(c)
os.Exit(3)
}

if err := app.Run(os.Args); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
Expand Down
2 changes: 1 addition & 1 deletion eth/downloader/downloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1605,7 +1605,7 @@ func testFailedSyncProgress(t *testing.T, protocol int, mode SyncMode) {
pending.Wait()

// Wait to make sure all data is set after sync
time.Sleep(200 * time.Millisecond)
time.Sleep(500 * time.Millisecond)

// Check final progress after successful sync
if origin, current, latest, _, _ := tester.downloader.Progress(); origin > uint64(targetBlocks/2) || current != uint64(targetBlocks) || latest != uint64(targetBlocks) {
Expand Down