Skip to content

Commit 2e171fd

Browse files
updating tests to use golden files (#136)
1 parent 52def96 commit 2e171fd

File tree

1 file changed

+8
-68
lines changed

1 file changed

+8
-68
lines changed

cmd/types/types_test.go

Lines changed: 8 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,40 @@
11
package types
22

33
import (
4-
"bytes"
5-
"fmt"
4+
"github.com/digitalghost-dev/poke-cli/cmd/utils"
65
"github.com/digitalghost-dev/poke-cli/styling"
76
"github.com/stretchr/testify/assert"
87
"os"
98
"testing"
109
)
1110

12-
func captureTypesOutput(f func()) string {
13-
r, w, _ := os.Pipe()
14-
defer func(r *os.File) {
15-
err := r.Close()
16-
if err != nil {
17-
fmt.Println(err)
18-
}
19-
}(r)
20-
21-
oldStdout := os.Stdout
22-
os.Stdout = w
23-
defer func() { os.Stdout = oldStdout }()
24-
25-
f()
26-
err := w.Close()
27-
if err != nil {
28-
return ""
29-
}
30-
31-
var buf bytes.Buffer
32-
_, _ = buf.ReadFrom(r)
33-
return buf.String()
34-
}
35-
3611
func TestTypesCommand(t *testing.T) {
3712
tests := []struct {
3813
name string
3914
args []string
4015
expectedOutput string
41-
expectedError bool
16+
wantError bool
4217
}{
4318
{
44-
name: "Help flag",
45-
args: []string{"types", "-h"},
46-
expectedOutput: styling.StripANSI(
47-
"╭───────────────────────────────────────────────────────────╮\n" +
48-
"│Get details about a specific typing. │\n" +
49-
"│ │\n" +
50-
"│ USAGE: │\n" +
51-
"│ poke-cli types [flag] │\n" +
52-
"│ │\n" +
53-
"│ FLAGS: │\n" +
54-
"│ -h, --help Prints out the help menu│\n" +
55-
"╰───────────────────────────────────────────────────────────╯\n"),
56-
expectedError: false,
57-
},
58-
{
59-
name: "Help flag",
60-
args: []string{"types", "--help"},
61-
expectedOutput: styling.StripANSI(
62-
"╭───────────────────────────────────────────────────────────╮\n" +
63-
"│Get details about a specific typing. │\n" +
64-
"│ │\n" +
65-
"│ USAGE: │\n" +
66-
"│ poke-cli types [flag] │\n" +
67-
"│ │\n" +
68-
"│ FLAGS: │\n" +
69-
"│ -h, --help Prints out the help menu│\n" +
70-
"╰───────────────────────────────────────────────────────────╯\n"),
71-
expectedError: false,
19+
name: "Types help flag",
20+
args: []string{"types", "--help"},
21+
expectedOutput: utils.LoadGolden(t, "types_help.golden"),
7222
},
7323
}
7424

7525
for _, tt := range tests {
7626
t.Run(tt.name, func(t *testing.T) {
7727
originalArgs := os.Args
78-
defer func() { os.Args = originalArgs }()
79-
8028
os.Args = append([]string{"poke-cli"}, tt.args...)
29+
defer func() { os.Args = originalArgs }()
8130

82-
output := captureTypesOutput(func() {
83-
defer func() {
84-
if r := recover(); r != nil && !tt.expectedError {
85-
t.Fatalf("Unexpected error: %v", r)
86-
}
87-
}()
88-
TypesCommand()
89-
})
90-
31+
output := TypesCommand()
9132
cleanOutput := styling.StripANSI(output)
9233

93-
assert.Equal(t, tt.expectedOutput, cleanOutput, "Output should contain the expected string")
34+
assert.Equal(t, tt.expectedOutput, cleanOutput, "Output should match expected")
9435
})
9536
}
9637
}
97-
9838
func TestModelInit(t *testing.T) {
9939
m := model{}
10040
cmd := m.Init()

0 commit comments

Comments
 (0)