Skip to content

Commit

Permalink
Add some tests to the main file
Browse files Browse the repository at this point in the history
  • Loading branch information
Dobefu committed Nov 24, 2024
1 parent b46cd26 commit a7dde36
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
10 changes: 6 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,19 @@ func parseSubCommands() (string, subCommand) {
}

if len(os.Args) < 2 {
listCmds()
listSubCommands()
}

subCmd, subCmdExists := cmds[os.Args[1]]

if !subCmdExists {
listCmds()
listSubCommands()
}

return os.Args[1], subCmd
}

func listCmds() {
func listSubCommands() {
cmds := getSubCommands()

fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\n", os.Args[0])
Expand All @@ -114,5 +114,7 @@ func listCmds() {
fmt.Printf(" %s\n", cmd.desc)
}

os.Exit(1)
if flag.Lookup("test.v") == nil {
os.Exit(1)
}
}
26 changes: 26 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func TestMain(t *testing.T) {
main()
}

func TestParseSubCommands(t *testing.T) {
os.Args[1] = "migrate:db"
_, v := parseSubCommands()
assert.NotEmpty(t, v.desc)

os.Args[1] = "bogus"
_, v = parseSubCommands()
assert.Empty(t, v.desc)
}

func TestListSubCommands(t *testing.T) {
listSubCommands()
}

0 comments on commit a7dde36

Please sign in to comment.