Skip to content

Commit

Permalink
reworked tests for version, moved test debug/quiet to cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
luthermonson committed Dec 25, 2023
1 parent 15dcc9a commit 963f712
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 25 deletions.
4 changes: 2 additions & 2 deletions cmd/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ func Commands() []*cli.Command {
}
}

func Version(v string) {
version = v
func Version(v, c, d string) {
version = fmt.Sprintf("goodhosts %s@%s built on %s", v, c, d)
}

func DefaultAction(c *cli.Context) error {
Expand Down
24 changes: 22 additions & 2 deletions cmd/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,33 @@ package cmd
import (
"bytes"
"os"
"strings"
"testing"

"github.com/stretchr/testify/assert"

"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
easy "github.com/t-tomalak/logrus-easy-formatter"
)

func TestDebug(t *testing.T) {
args, out := setup("--debug", "version")
Version("test-version", "test-commit", "test-date")
assert.Nil(t, App.Run(args))
assert.True(t, strings.Contains(out.String(), "level=info msg=\"goodhosts test-version@test-commit built on test-date\""))

// reset logrus for other tests
logrus.SetLevel(logrus.InfoLevel)
logrus.SetFormatter(&easy.Formatter{
LogFormat: "%msg%",
})
}

func TestQuiet(t *testing.T) {
args, out := setup("--quiet", "version")
assert.Nil(t, App.Run(args))
assert.Empty(t, out.String())
}

// Setup takes args slice and resets logrus and returns args to pass to App.Run
func setup(args ...string) ([]string, *bytes.Buffer) {
out := &bytes.Buffer{}
Expand Down
7 changes: 1 addition & 6 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"fmt"
"os"

"github.com/sirupsen/logrus"
Expand All @@ -21,11 +20,7 @@ func main() {
}
}

func formatVersion(version, commit, date string) string {
return fmt.Sprintf("goodhosts %s@%s built on %s", version, commit, date)
}

func run(args []string) error {
cmd.Version(formatVersion(version, commit, date))
cmd.Version(version, commit, date)
return cmd.App.Run(args)
}
17 changes: 2 additions & 15 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"bytes"
"os"
"strings"
"testing"

"github.com/goodhosts/cli/cmd"
Expand All @@ -30,22 +29,10 @@ func TestVersion(t *testing.T) {

// test that version@commit + date work
args, out = setup("version")
cmd.Version(formatVersion("test-version", "test-commit", "test-date"))
cmd.Version("test-version", "test-commit", "test-date")
assert.Nil(t, cmd.App.Run(args))
assert.Equal(t, "goodhosts test-version@test-commit built on test-date", out.String())

// reset for future tests
cmd.Version(formatVersion("dev", "none", "unknown"))
}

func TestDebug(t *testing.T) {
args, out := setup("--debug", "version")
assert.Nil(t, cmd.App.Run(args))
assert.True(t, strings.Contains(out.String(), "level=info msg=\"goodhosts dev@none built on unknown\""))
}

func TestQuiet(t *testing.T) {
args, out := setup("--quiet", "version")
assert.Nil(t, cmd.App.Run(args))
assert.Empty(t, out.String())
cmd.Version("dev", "none", "unknown")
}

0 comments on commit 963f712

Please sign in to comment.