Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some tests using bats. #46

Merged
merged 10 commits into from
Nov 22, 2016
Prev Previous commit
Next Next commit
Add some default commands to handle flags when no arguments are set.
  • Loading branch information
Frank Carey committed Nov 21, 2016
commit 3444b28f01594cf43aa06841869d781dbadf79f7
32 changes: 29 additions & 3 deletions ahoy.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"flag"
"fmt"
"github.com/codegangsta/cli"
Expand Down Expand Up @@ -257,11 +258,36 @@ func BashComplete(c *cli.Context) {
}
}

func setupApp(args []string) *cli.App {
initFlags(args)
//log.Println(sourcefile)
// This is the application wide default action, for when no arguments are passed.
func NoArgsAction(c *cli.Context) {
if c.Bool("help") {
cli.ShowAppHelp(c)
}
}

// This runs before every command so arguments must be passed.
func BeforeCommand(c *cli.Context) error {
args := c.Args()
if c.Bool("version") {
fmt.Println(version)
return errors.New("don't continue with commands")
}
if c.Bool("help") {
if len(args) > 0 {
cli.ShowCommandHelp(c, args.First())
return errors.New("don't continue with commands")
}
}
//fmt.Printf("%+v\n", args)
return nil
}

func setupApp(localArgs []string) *cli.App {
initFlags(localArgs)
// cli stuff
app = cli.NewApp()
app.Action = NoArgsAction
app.Before = BeforeCommand
app.Name = "ahoy"
app.Version = version
app.Usage = "Creates a configurable cli app for running commands."
Expand Down
2 changes: 1 addition & 1 deletion tests/flags.bats
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
@test "get the version of ahoy with --version" {
run ./ahoy -f testdata/simple.ahoy.yml --version
[ $status -eq 0 ]
[ "$result" == "2.0.0-alpha-23-g0479480" ]
[ $(expr "$output" : "[0-9.]\.[0-9.]\.[0-9.]") -ne 0 ]
}

@test "get help instead of running a command with --help" {
Expand Down
11 changes: 11 additions & 0 deletions tests/no-ahoy-file.bats
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ teardown() {
mv tmp.ahoy.yml .ahoy.yml
}

@test "run ahoy withough a command and without a .ahoy.yml file" {
result="$(./ahoy)"
echo "$result"
[ "$result" -eq 4 ]
}

@test "run an ahoy command without a .ahoy.yml file" {
result="$(./ahoy something)"
[ "$result" -eq 4 ]
}

@test "run ahoy init without a .ahoy.yml file" {
result="$(./ahoy init)"
[ "$result" -eq 4 ]
Expand Down