@@ -2,6 +2,9 @@ package main
22
33import (
44 "bytes"
5+ "github.com/digitalghost-dev/poke-cli/cmd/utils"
6+ "github.com/digitalghost-dev/poke-cli/styling"
7+ "github.com/stretchr/testify/assert"
58 "os"
69 "regexp"
710 "testing"
@@ -93,119 +96,28 @@ func TestRunCLI(t *testing.T) {
9396 expectedCode int
9497 }{
9598 {
96- name : "No Arguments" ,
97- args : []string {},
98- expectedOutput : "╭──────────────────────────────────────────────────────────╮\n " +
99- "│Welcome! This tool displays data related to Pokémon! │\n " +
100- "│ │\n " +
101- "│ USAGE: │\n " +
102- "│ poke-cli [flag] │\n " +
103- "│ poke-cli <command> [flag] │\n " +
104- "│ poke-cli <command> <subcommand> [flag] │\n " +
105- "│ │\n " +
106- "│ FLAGS: │\n " +
107- "│ -h, --help Shows the help menu │\n " +
108- "│ -l, --latest Prints the latest version available │\n " +
109- "│ -v, --version Prints the current version │\n " +
110- "│ │\n " +
111- "│ COMMANDS: │\n " +
112- "│ ability Get details about an ability │\n " +
113- "│ move Get details about a move │\n " +
114- "│ natures Get details about all natures │\n " +
115- "│ pokemon Get details about a Pokémon │\n " +
116- "│ search Search for a resource │\n " +
117- "│ types Get details about a typing │\n " +
118- "│ │\n " +
119- "│ hint: when calling a resource with a space, use a hyphen │\n " +
120- "│ example: poke-cli ability strong-jaw │\n " +
121- "│ example: poke-cli pokemon flutter-mane │\n " +
122- "╰──────────────────────────────────────────────────────────╯" ,
123- expectedCode : 0 ,
124- },
125- {
126- name : "Help Flag Short" ,
127- args : []string {"-h" },
128- expectedOutput : "╭──────────────────────────────────────────────────────────╮\n " +
129- "│Welcome! This tool displays data related to Pokémon! │\n " +
130- "│ │\n " +
131- "│ USAGE: │\n " +
132- "│ poke-cli [flag] │\n " +
133- "│ poke-cli <command> [flag] │\n " +
134- "│ poke-cli <command> <subcommand> [flag] │\n " +
135- "│ │\n " +
136- "│ FLAGS: │\n " +
137- "│ -h, --help Shows the help menu │\n " +
138- "│ -l, --latest Prints the latest version available │\n " +
139- "│ -v, --version Prints the current version │\n " +
140- "│ │\n " +
141- "│ COMMANDS: │\n " +
142- "│ ability Get details about an ability │\n " +
143- "│ move Get details about a move │\n " +
144- "│ natures Get details about all natures │\n " +
145- "│ pokemon Get details about a Pokémon │\n " +
146- "│ search Search for a resource │\n " +
147- "│ types Get details about a typing │\n " +
148- "│ │\n " +
149- "│ hint: when calling a resource with a space, use a hyphen │\n " +
150- "│ example: poke-cli ability strong-jaw │\n " +
151- "│ example: poke-cli pokemon flutter-mane │\n " +
152- "╰──────────────────────────────────────────────────────────╯" ,
153- expectedCode : 0 ,
154- },
155- {
156- name : "Help Flag Long" ,
157- args : []string {"--help" },
158- expectedOutput : "╭──────────────────────────────────────────────────────────╮\n " +
159- "│Welcome! This tool displays data related to Pokémon! │\n " +
160- "│ │\n " +
161- "│ USAGE: │\n " +
162- "│ poke-cli [flag] │\n " +
163- "│ poke-cli <command> [flag] │\n " +
164- "│ poke-cli <command> <subcommand> [flag] │\n " +
165- "│ │\n " +
166- "│ FLAGS: │\n " +
167- "│ -h, --help Shows the help menu │\n " +
168- "│ -l, --latest Prints the latest version available │\n " +
169- "│ -v, --version Prints the current version │\n " +
170- "│ │\n " +
171- "│ COMMANDS: │\n " +
172- "│ ability Get details about an ability │\n " +
173- "│ move Get details about a move │\n " +
174- "│ natures Get details about all natures │\n " +
175- "│ pokemon Get details about a Pokémon │\n " +
176- "│ search Search for a resource │\n " +
177- "│ types Get details about a typing │\n " +
178- "│ │\n " +
179- "│ hint: when calling a resource with a space, use a hyphen │\n " +
180- "│ example: poke-cli ability strong-jaw │\n " +
181- "│ example: poke-cli pokemon flutter-mane │\n " +
182- "╰──────────────────────────────────────────────────────────╯" ,
183-
184- expectedCode : 0 ,
185- },
186- {
187- name : "Invalid Command" ,
188- args : []string {"invalid" },
189- expectedOutput : "Error!" ,
190- expectedCode : 1 ,
99+ name : "Test CLI help flag" ,
100+ args : []string {"--help" },
101+ expectedOutput : utils .LoadGolden (t , "cli_help.golden" ),
102+ expectedCode : 0 ,
191103 },
192104 }
193105
194106 for _ , tt := range tests {
195107 t .Run (tt .name , func (t * testing.T ) {
196- exit = func (code int ) {}
108+ originalArgs := os .Args
109+ os .Args = append ([]string {"poke-cli" }, tt .args ... )
110+ defer func () { os .Args = originalArgs }()
111+
112+ var exitCode int
197113 output := captureOutput (func () {
198- code := runCLI (tt .args )
199- if code != tt .expectedCode {
200- t .Errorf ("Expected exit code %d, got %d" , tt .expectedCode , code )
201- }
114+ exitCode = runCLI (tt .args )
202115 })
203116
204- output = stripANSI (output )
117+ cleanOutput := styling . StripANSI (output )
205118
206- if ! bytes .Contains ([]byte (output ), []byte (tt .expectedOutput )) {
207- t .Errorf ("Expected output to contain %q, got %q" , tt .expectedOutput , output )
208- }
119+ assert .Equal (t , tt .expectedOutput , cleanOutput , "Output should match expected" )
120+ assert .Equal (t , tt .expectedCode , exitCode , "Exit code should match expected" )
209121 })
210122 }
211123}
0 commit comments