Skip to content

Commit

Permalink
tests: improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Cristian Oliveira authored and cristianoliveira committed Oct 15, 2017
1 parent 7b3f0bf commit 6e31cc9
Show file tree
Hide file tree
Showing 2 changed files with 171 additions and 31 deletions.
76 changes: 45 additions & 31 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Usage:
ergo list-names
ergo url <name>
ergo setup [options] [linux-gnome|osx|windows] [-remove]
ergo add [options] <service-name> <host:port>
ergo add [options] <service-name> <host:port>
Options:
-h Shows this message.
Expand All @@ -40,22 +40,7 @@ setup:
-remove Set remove proxy configurations.
`

func main() {
help := flag.Bool("h", false, "Shows ergo's help.")
version := flag.Bool("v", false, "Shows ergo's version.")

flag.Parse()

if *help || len(os.Args) == 1 {
fmt.Println(USAGE)
os.Exit(0)
}

if *version {
fmt.Println(VERSION)
os.Exit(0)
}

func command() func() {
config := proxy.NewConfig()
command := flag.NewFlagSet(os.Args[1], flag.ExitOnError)
configFile := command.String("config", "./.ergo", "Set the services file")
Expand All @@ -70,52 +55,81 @@ func main() {

switch os.Args[1] {
case "list":
commands.List(config)
return func() {
commands.List(config)
}

case "list-names":
commands.ListNames(config)
return func() {
commands.ListNames(config)
}

case "setup":
if len(flag.Args()) < 1 {
fmt.Println(USAGE)
os.Exit(0)
if len(os.Args) <= 2 {
return nil
}

system := command.Args()[0]
setupRemove := command.Bool("remove", false, "Set remove proxy configurations.")
command.Parse(command.Args()[1:])

commands.Setup(system, *setupRemove, config)
return func() {
commands.Setup(system, *setupRemove, config)
}

case "url":
if len(os.Args) != 3 {
fmt.Println(USAGE)
os.Exit(0)
return nil
}

name := os.Args[2]
commands.URL(name, config)
return func() {
commands.URL(name, config)
}

case "run":
command.StringVar(&config.Port, "p", "2000", "Set port to the proxy")
command.BoolVar(&config.Verbose, "V", false, "Set verbosity on proxy output")

command.Parse(os.Args[2:])
commands.Run(config)

return func() {
commands.Run(config)
}
case "add":
if len(os.Args) <= 3 {
fmt.Println(USAGE)
os.Exit(0)
return nil
}

name := os.Args[2]
url := os.Args[3]
service := proxy.NewService(name, url)

commands.AddService(config, service, *configFile)
return func() {
commands.AddService(config, service, *configFile)
}
}

return nil
}

func main() {
help := flag.Bool("h", false, "Shows ergo's help.")
version := flag.Bool("v", false, "Shows ergo's version.")

flag.Parse()

if *version {
fmt.Println(VERSION)
return
}

cmd := command()
showUsage := *help || len(os.Args) == 1 || cmd == nil

default:
if showUsage {
fmt.Println(USAGE)
} else {
cmd()
}
}
126 changes: 126 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package main

import (
"os"
"testing"
)

func TestMain(t *testing.T) {
t.Run("it shows usage", func(tt *testing.T) {
args := []string{"ergo", "-h"}
os.Args = args

main()
// Output: USAGE
})
}

func TestListCommand(t *testing.T) {
t.Run("it is list command", func(tt *testing.T) {
args := []string{"ergo", "list"}
os.Args = args

result := command()
if result == nil {
t.Errorf("Expected result to not be nil")
}

result()
})
}

func TestListNamesCommand(t *testing.T) {
t.Run("it is list-names command", func(tt *testing.T) {
args := []string{"ergo", "list-names"}
os.Args = args

result := command()
if result == nil {
t.Errorf("Expected result to not be nil")
}

result()
})
}

func TestSetupCommand(t *testing.T) {
t.Run("it shows usage", func(tt *testing.T) {
args := []string{"ergo", "setup"}
os.Args = args

result := command()
if result != nil {
t.Errorf("Expected result to be nil")
}
})

t.Run("it is setup command", func(tt *testing.T) {
args := []string{"ergo", "setup", "osx"}
os.Args = args

result := command()
if result == nil {
t.Errorf("Expected result not to be nil")
}
})
}

func TestUrlCommand(t *testing.T) {
t.Run("it shows usage", func(tt *testing.T) {
args := []string{"ergo", "url"}
os.Args = args

result := command()
if result != nil {
t.Errorf("Expected result to be nil")
}
})

t.Run("it is url command", func(tt *testing.T) {
args := []string{"ergo", "url", "foo"}
os.Args = args

result := command()
if result == nil {
t.Errorf("Expected result not to be nil")
}

result()
})
}

func TestRunCommand(t *testing.T) {
t.Run("it is url command", func(tt *testing.T) {
args := []string{"ergo", "run"}
os.Args = args

result := command()
if result == nil {
t.Errorf("Expected result not to be nil")
}
})
}

func TestAddCommand(t *testing.T) {
t.Run("it shows usage", func(tt *testing.T) {
args := []string{"ergo", "add"}
os.Args = args

result := command()
if result != nil {
t.Errorf("Expected result to be nil")
}
})

t.Run("it is url command", func(tt *testing.T) {
args := []string{"ergo", "add", "foo", "bar"}
os.Args = args

result := command()
if result == nil {
t.Errorf("Expected result not to be nil")
}

result()
})
}

0 comments on commit 6e31cc9

Please sign in to comment.