@@ -2,11 +2,37 @@ package main
22
33import (
44 "bytes"
5- "os/exec"
5+ "fmt"
6+ "io"
7+ "os"
8+ "regexp"
69 "testing"
710)
811
9- func TestCLI (t * testing.T ) {
12+ // Strip ANSI color codes
13+ var ansiRegex = regexp .MustCompile (`\x1b\[[0-9;]*m` )
14+
15+ func stripANSI (input string ) string {
16+ return ansiRegex .ReplaceAllString (input , "" )
17+ }
18+
19+ func TestMainFunction (t * testing.T ) {
20+ version := "v0.6.4"
21+
22+ // Backup the original exit function and stdout/stderr
23+ originalExit := exit
24+ originalStdout := os .Stdout
25+ originalStderr := os .Stderr
26+ defer func () {
27+ exit = originalExit // Restore exit
28+ os .Stdout = originalStdout // Restore stdout
29+ os .Stderr = originalStderr // Restore stderr
30+ }()
31+
32+ // Replace exit with a function that captures the exit code
33+ exitCode := 0
34+ exit = func (code int ) { exitCode = code }
35+
1036 tests := []struct {
1137 args []string
1238 expectedOutput string
@@ -22,94 +48,20 @@ func TestCLI(t *testing.T) {
2248 "│ │\n " +
2349 "│Also run [poke-cli -h] for more info! │\n " +
2450 "╰──────────────────────────────────────────────────────╯\n " ,
25- expectedExit : 0 ,
26- },
27- {
28- args : []string {"pokemon" },
29- expectedOutput : "╭────────────────────────────────────────────────────────────╮\n " +
30- "│Error! │\n " +
31- "│Please declare a Pokémon's name after the [pokemon] command │\n " +
32- "│Run 'poke-cli pokemon -h' for more details │\n " +
33- "│error: insufficient arguments │\n " +
34- "╰────────────────────────────────────────────────────────────╯\n " ,
3551 expectedExit : 1 ,
3652 },
3753 {
38- args : []string {"pokemon" , "bulbasaur " },
39- expectedOutput : "Your selected Pokémon: Bulbasaur \n National Pokédex #: 1 \n " ,
54+ args : []string {"-l " },
55+ expectedOutput : fmt . Sprintf ( "Latest Docker image version: %s \n Latest release tag: %s \n ", version , version ) ,
4056 expectedExit : 0 ,
4157 },
4258 {
43- args : []string {"pokemon" , "mew" , "--types " },
44- expectedOutput : "Your selected Pokémon: Mew \n National Pokédex #: 151 \n ────── \n Typing \n Type 1: psychic \n " ,
59+ args : []string {"--latest " },
60+ expectedOutput : fmt . Sprintf ( "Latest Docker image version: %s \n Latest release tag: %s \n " , version , version ) ,
4561 expectedExit : 0 ,
4662 },
4763 {
48- args : []string {"pokemon" , "cacturne" , "--types" },
49- expectedOutput : "Your selected Pokémon: Cacturne\n National Pokédex #: 332\n ──────\n Typing\n Type 1: grass\n Type 2: dark\n " ,
50- expectedExit : 0 ,
51- },
52- {
53- args : []string {"pokemon" , "chimchar" , "types" },
54- expectedOutput : "╭─────────────────────────────────────────────────────────────────────────────────╮\n " +
55- "│Error! │\n " +
56- "│Invalid argument 'types'. Only flags are allowed after declaring a Pokémon's name│\n " +
57- "╰─────────────────────────────────────────────────────────────────────────────────╯\n " ,
58- expectedExit : 1 ,
59- },
60- {
61- args : []string {"pokemon" , "flutter-mane" , "types" },
62- expectedOutput : "╭─────────────────────────────────────────────────────────────────────────────────╮\n " +
63- "│Error! │\n " +
64- "│Invalid argument 'types'. Only flags are allowed after declaring a Pokémon's name│\n " +
65- "╰─────────────────────────────────────────────────────────────────────────────────╯\n " ,
66- expectedExit : 1 ,
67- },
68- {
69- args : []string {
70- "pokemon" , "AmPhaROs" , "--types" , "--abilities" ,
71- },
72- expectedOutput : "Your selected Pokémon: Ampharos\n " +
73- "National Pokédex #: 181\n " +
74- "──────\n " +
75- "Typing\n " +
76- "Type 1: electric\n " +
77- "─────────\n " +
78- "Abilities\n " +
79- "Ability 1: static\n " +
80- "Hidden Ability: plus\n " ,
81- expectedExit : 0 ,
82- },
83- {
84- args : []string {
85- "pokemon" , "CLOysTeR" , "-t" , "-a" ,
86- },
87- expectedOutput : "Your selected Pokémon: Cloyster\n " +
88- "National Pokédex #: 91\n " +
89- "──────\n " +
90- "Typing\n " +
91- "Type 1: water\n " +
92- "Type 2: ice\n " +
93- "─────────\n " +
94- "Abilities\n " +
95- "Ability 1: shell-armor\n " +
96- "Ability 2: skill-link\n " +
97- "Hidden Ability: overcoat\n " ,
98- expectedExit : 0 ,
99- },
100- {
101- args : []string {"pokemon" , "gyarados" , "--help" },
102- expectedOutput : "╭──────────────────────────────────────────────────────────────╮\n " +
103- "│poke-cli pokemon <pokemon-name> [flags] │\n " +
104- "│ │\n " +
105- "│FLAGS: │\n " +
106- "│ -a, --abilities Prints out the Pokémon's abilities. │\n " +
107- "│ -t, --types Prints out the Pokémon's typing. │\n " +
108- "╰──────────────────────────────────────────────────────────────╯\n " ,
109- expectedExit : 0 ,
110- },
111- {
112- args : []string {"--help" },
64+ args : []string {"-h" },
11365 expectedOutput : "╭──────────────────────────────────────────────────────╮\n " +
11466 "│Welcome! This tool displays data related to Pokémon! │\n " +
11567 "│ │\n " +
@@ -129,50 +81,42 @@ func TestCLI(t *testing.T) {
12981 "╰──────────────────────────────────────────────────────╯\n " ,
13082 expectedExit : 0 ,
13183 },
132- {
133- args : []string {"types" , "ground" , "all" },
134- expectedOutput : "╭──────────────────╮\n " +
135- "│Error! │\n " +
136- "│Too many arguments│\n " +
137- "╰──────────────────╯\n " ,
138- expectedExit : 1 ,
139- },
140- {
141- args : []string {"types" , "--help" },
142- expectedOutput : "╭───────────────────────────────────────────────────────────────╮\n " +
143- "│USAGE: │\n " +
144- "│ poke-cli types [flag] │\n " +
145- "│ Get details about a specific typing │\n " +
146- "│ ---------- │\n " +
147- "│ Examples: │\n " +
148- "│ poke-cli types │\n " +
149- "│ A table will then display with the option to select a type.│\n " +
150- "╰───────────────────────────────────────────────────────────────╯\n " ,
151- expectedExit : 0 ,
152- },
15384 }
15485
15586 for _ , test := range tests {
156- cmd := exec .Command ("poke-cli" , test .args ... )
157- var out bytes.Buffer
158- cmd .Stdout = & out
159- cmd .Stderr = & out
87+ // Create a pipe to capture output
88+ r , w , _ := os .Pipe ()
89+ os .Stdout = w
90+ os .Stderr = w
91+
92+ // Set os.Args for the test case
93+ os .Args = append ([]string {"poke-cli" }, test .args ... )
16094
161- err := cmd .Run ()
95+ // Run the main function
96+ main ()
16297
98+ // Close the writer and restore stdout and stderr
99+ err := w .Close ()
163100 if err != nil {
164- // If there's an error, but we expected a successful exit
165- if test .expectedExit == 0 {
166- t .Errorf ("Unexpected error: %v" , err )
167- }
101+ t .Fatalf ("Error closing pipe writer: %v" , err )
102+ }
103+ os .Stdout = originalStdout
104+ os .Stderr = originalStderr
105+
106+ // Read from the pipe
107+ var buf bytes.Buffer
108+ if _ , err := io .Copy (& buf , r ); err != nil {
109+ t .Errorf ("Error copying output: %v" , err )
168110 }
169111
170- if out .String () != test .expectedOutput {
171- t .Errorf ("Args: %v, Expected output: %q, Got: %q" , test .args , test .expectedOutput , out .String ())
112+ // Strip ANSI color codes from the actual output
113+ actualOutput := stripANSI (buf .String ())
114+ if actualOutput != test .expectedOutput {
115+ t .Errorf ("Args: %v\n Expected output: %q\n Got: %q\n " , test .args , test .expectedOutput , actualOutput )
172116 }
173117
174- if cmd . ProcessState . ExitCode () != test .expectedExit {
175- t .Errorf ("Args: %v, Expected exit code: %d, Got : %d" , test .args , test .expectedExit , cmd . ProcessState . ExitCode () )
118+ if exitCode != test .expectedExit {
119+ t .Errorf ("Args: %v\n Expected exit code: %d\n Got : %d\n " , test .args , test .expectedExit , exitCode )
176120 }
177121 }
178122}
0 commit comments