Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
}