diff --git a/cmd/app.go b/cmd/app.go index dbb0bfd..9cd25fe 100644 --- a/cmd/app.go +++ b/cmd/app.go @@ -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 { diff --git a/cmd/app_test.go b/cmd/app_test.go index 95198bb..991b865 100644 --- a/cmd/app_test.go +++ b/cmd/app_test.go @@ -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{} diff --git a/main.go b/main.go index a990080..ec3c832 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "os" "github.com/sirupsen/logrus" @@ -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) } diff --git a/main_test.go b/main_test.go index d0774d2..d0fffc8 100644 --- a/main_test.go +++ b/main_test.go @@ -3,7 +3,6 @@ package main import ( "bytes" "os" - "strings" "testing" "github.com/goodhosts/cli/cmd" @@ -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") }