Skip to content

Commit

Permalink
test(cmd): capture stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
natesales committed Sep 4, 2024
1 parent 1eda163 commit 335c2d2
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 33 deletions.
4 changes: 3 additions & 1 deletion cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ var configCmd = &cobra.Command{
config = re.ReplaceAllString(config, fmt.Sprintf("${1}%s: REDACTED", key))
}

logknife.Knife(bytes.NewBuffer([]byte(config)), false, true, false, "")
var outBuf bytes.Buffer
logknife.Knife(bytes.NewBuffer([]byte(config)), &outBuf, false, true, false, "")
log.Println(outBuf.String())
} else {
log.Println(config)
}
Expand Down
17 changes: 8 additions & 9 deletions cmd/docs_test.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package cmd

import (
"os"
"testing"

"github.com/stretchr/testify/assert"

"github.com/natesales/pathvector/pkg/util/log"
)

func TestDocs(t *testing.T) {
old := os.Stdout
_, w, _ := os.Pipe()
os.Stdout = w
rootCmd.SetArgs([]string{
"docs",
})
if err := rootCmd.Execute(); err != nil {
t.Error(err)
}
w.Close()
os.Stdout = old
out := log.Capture()
defer log.ResetCapture()
assert.Nil(t, rootCmd.Execute())
assert.Contains(t, out.String(), "# Configuration\n")
}
39 changes: 18 additions & 21 deletions cmd/dump_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package cmd

import (
"os"
"testing"

"github.com/stretchr/testify/assert"

"github.com/natesales/pathvector/pkg/util/log"
)

func TestDumpTable(t *testing.T) {
old := os.Stdout
_, w, _ := os.Pipe()
os.Stdout = w

mkTmpCache(t)

args := []string{
Expand All @@ -23,21 +22,19 @@ func TestDumpTable(t *testing.T) {
"--config", testFile,
}...)
t.Logf("running dump integration with args %v", args)

out := log.Capture()
defer log.ResetCapture()

rootCmd.SetArgs(args)
if err := rootCmd.Execute(); err != nil {
t.Error(err)
}
assert.Nil(t, rootCmd.Execute())
assert.Contains(t, out.String(), "PREPENDS")
assert.Contains(t, out.String(), "NAME")
assert.Contains(t, out.String(), "ASN")
})

w.Close()
os.Stdout = old
}

func TestDumpYAML(t *testing.T) {
old := os.Stdout
_, w, _ := os.Pipe()
os.Stdout = w

mkTmpCache(t)

args := []string{
Expand All @@ -51,12 +48,12 @@ func TestDumpYAML(t *testing.T) {
"--config", testFile,
}...)
t.Logf("running dump integration with args %v", args)

out := log.Capture()
defer log.ResetCapture()

rootCmd.SetArgs(args)
if err := rootCmd.Execute(); err != nil {
t.Error(err)
}
assert.Nil(t, rootCmd.Execute())
assert.Contains(t, out.String(), "global-config: \"\"")
})

w.Close()
os.Stdout = old
}
4 changes: 4 additions & 0 deletions cmd/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/natesales/pathvector/pkg/util/log"
)

func TestGenerate(t *testing.T) {
Expand All @@ -21,6 +23,8 @@ func TestGenerate(t *testing.T) {
}...)
t.Logf("running generate integration with args %v", args)
rootCmd.SetArgs(args)
_ = log.Capture()
defer log.ResetCapture()
assert.Nil(t, rootCmd.Execute())
})
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/go-ping/ping v1.1.0
github.com/go-playground/validator/v10 v10.22.0
github.com/lithammer/fuzzysearch v1.1.8
github.com/natesales/logknife v0.0.4-0.20230403055117-5e928ad4153b
github.com/natesales/logknife v0.0.4-0.20240904212444-784fa10b2080
github.com/olekukonko/tablewriter v0.0.5
github.com/spf13/cobra v1.8.1
github.com/stretchr/testify v1.9.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo
github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8=
github.com/natesales/logknife v0.0.4-0.20230403055117-5e928ad4153b h1:oDEZbVwq3gZzqimMCrxDUWrn2e9RWws0q48mdsvUz7g=
github.com/natesales/logknife v0.0.4-0.20230403055117-5e928ad4153b/go.mod h1:tQLjEyj4CHMjYpG5icbEKjWtByZxU7b6CU1wEVIdp2k=
github.com/natesales/logknife v0.0.4-0.20240904212444-784fa10b2080 h1:x0IxtZknLd9Zx7IVzbV4TKsYMW3LDP0/AYclZfkXO3A=
github.com/natesales/logknife v0.0.4-0.20240904212444-784fa10b2080/go.mod h1:tQLjEyj4CHMjYpG5icbEKjWtByZxU7b6CU1wEVIdp2k=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
6 changes: 5 additions & 1 deletion pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ func PrintStructInfo(label string, instance interface{}) {

// PrintTable prints a table of data
func PrintTable(header []string, data [][]string) {
table := tablewriter.NewWriter(os.Stdout)
var buf bytes.Buffer

table := tablewriter.NewWriter(&buf)
table.SetHeader(header)
table.SetAutoFormatHeaders(true)
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
Expand All @@ -108,6 +110,8 @@ func PrintTable(header []string, data [][]string) {
table.SetAutoWrapText(false)
table.AppendBulk(data)
table.Render()

log.Println(buf.String())
}

// RemoveFileGlob removes files by glob
Expand Down

0 comments on commit 335c2d2

Please sign in to comment.