|
1 | 1 | package types |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "bytes" |
5 | | - "fmt" |
| 4 | + "github.com/digitalghost-dev/poke-cli/cmd/utils" |
6 | 5 | "github.com/digitalghost-dev/poke-cli/styling" |
7 | 6 | "github.com/stretchr/testify/assert" |
8 | 7 | "os" |
9 | 8 | "testing" |
10 | 9 | ) |
11 | 10 |
|
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 | | - |
36 | 11 | func TestTypesCommand(t *testing.T) { |
37 | 12 | tests := []struct { |
38 | 13 | name string |
39 | 14 | args []string |
40 | 15 | expectedOutput string |
41 | | - expectedError bool |
| 16 | + wantError bool |
42 | 17 | }{ |
43 | 18 | { |
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"), |
72 | 22 | }, |
73 | 23 | } |
74 | 24 |
|
75 | 25 | for _, tt := range tests { |
76 | 26 | t.Run(tt.name, func(t *testing.T) { |
77 | 27 | originalArgs := os.Args |
78 | | - defer func() { os.Args = originalArgs }() |
79 | | - |
80 | 28 | os.Args = append([]string{"poke-cli"}, tt.args...) |
| 29 | + defer func() { os.Args = originalArgs }() |
81 | 30 |
|
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() |
91 | 32 | cleanOutput := styling.StripANSI(output) |
92 | 33 |
|
93 | | - assert.Equal(t, tt.expectedOutput, cleanOutput, "Output should contain the expected string") |
| 34 | + assert.Equal(t, tt.expectedOutput, cleanOutput, "Output should match expected") |
94 | 35 | }) |
95 | 36 | } |
96 | 37 | } |
97 | | - |
98 | 38 | func TestModelInit(t *testing.T) { |
99 | 39 | m := model{} |
100 | 40 | cmd := m.Init() |
|
0 commit comments