@@ -7,6 +7,63 @@ import (
77 "testing"
88)
99
10+ func TestCurrentVersion (t * testing.T ) {
11+ tests := []struct {
12+ name string
13+ version string
14+ expectedOutput string
15+ }{
16+ {
17+ name : "Version set by ldflags" ,
18+ version : "v1.0.0" ,
19+ expectedOutput : "Version: v1.0.0\n " ,
20+ },
21+ {
22+ name : "Version set to (devel)" ,
23+ version : "(devel)" ,
24+ expectedOutput : "Version: (devel)\n " , // Simplified assumption
25+ },
26+ }
27+
28+ // Save the original version and restore it after tests
29+ originalVersion := version
30+ defer func () { version = originalVersion }()
31+
32+ for _ , tt := range tests {
33+ t .Run (tt .name , func (t * testing.T ) {
34+ // Set the version for this test case
35+ version = tt .version
36+
37+ // Redirect stdout to capture the output
38+ r , w , _ := os .Pipe ()
39+ oldStdout := os .Stdout
40+ os .Stdout = w
41+
42+ // Call the function
43+ currentVersion ()
44+
45+ // Close the writer and restore stdout
46+ err := w .Close ()
47+ if err != nil {
48+ t .Fatalf ("Failed to close pipe: %v" , err )
49+ }
50+ os .Stdout = oldStdout
51+
52+ // Read the output from the pipe
53+ var buf bytes.Buffer
54+ if _ , err := buf .ReadFrom (r ); err != nil {
55+ t .Fatalf ("Failed to read from pipe: %v" , err )
56+ }
57+
58+ // Compare the output with the expected result
59+ got := buf .String ()
60+ if got != tt .expectedOutput {
61+ t .Errorf ("Expected %q, got %q" , tt .expectedOutput , got )
62+ }
63+ })
64+ }
65+ }
66+
1067var ansiRegex = regexp .MustCompile (`\x1b\[[0-9;]*m` )
1168
1269func captureOutput (f func ()) string {
@@ -110,7 +167,7 @@ func TestRunCLI(t *testing.T) {
110167 {
111168 name : "Latest Flag" ,
112169 args : []string {"-l" },
113- expectedOutput : "Latest Docker image version: v0.9.0 \n Latest release tag: v0.9.0 \n " ,
170+ expectedOutput : "Latest Docker image version: v0.9.1 \n Latest release tag: v0.9.1 \n " ,
114171 expectedCode : 0 ,
115172 },
116173 }
@@ -133,3 +190,44 @@ func TestRunCLI(t *testing.T) {
133190 })
134191 }
135192}
193+
194+ func TestMainFunction (t * testing.T ) {
195+ originalExit := exit
196+ defer func () { exit = originalExit }() // Restore original exit after test
197+
198+ tests := []struct {
199+ name string
200+ args []string
201+ expectedOutput string
202+ expectedCode int
203+ }{
204+ {
205+ name : "Run main command" ,
206+ args : []string {"poke-cli" },
207+ expectedOutput : "Welcome! This tool displays data related to Pokémon!" ,
208+ expectedCode : 0 ,
209+ },
210+ }
211+
212+ for _ , tt := range tests {
213+ t .Run (tt .name , func (t * testing.T ) {
214+ exitCode := 0
215+ exit = func (code int ) { exitCode = code }
216+
217+ output := captureOutput (func () {
218+ os .Args = tt .args
219+ main ()
220+ })
221+
222+ output = stripANSI (output )
223+
224+ if exitCode != tt .expectedCode {
225+ t .Errorf ("Expected exit code %d, got %d" , tt .expectedCode , exitCode )
226+ }
227+
228+ if ! bytes .Contains ([]byte (output ), []byte (tt .expectedOutput )) {
229+ t .Errorf ("Expected output to contain %q, got %q" , tt .expectedOutput , output )
230+ }
231+ })
232+ }
233+ }
0 commit comments