Skip to content

Commit 79180a2

Browse files
updating testability
1 parent 6032c15 commit 79180a2

File tree

2 files changed

+41
-22
lines changed

2 files changed

+41
-22
lines changed

flags/version.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,22 @@ import (
1111
"runtime"
1212
)
1313

14-
func latestDockerImage() {
14+
type commandRunner func(name string, args ...string) *exec.Cmd
15+
16+
func latestDockerImage(run commandRunner) {
1517
var cmd *exec.Cmd
1618

1719
if runtime.GOOS == "windows" {
18-
// Windows PowerShell equivalent
19-
cmd = exec.Command("powershell", "-Command", `
20-
$tags = Invoke-RestMethod -Uri "https://hub.docker.com/v2/repositories/digitalghostdev/poke-cli/tags/?page_size=1";
21-
$tags.results[0].name
22-
`)
20+
cmd = run("powershell", "-Command", `
21+
$tags = Invoke-RestMethod -Uri "https://hub.docker.com/v2/repositories/digitalghostdev/poke-cli/tags/?page_size=1";
22+
$tags.results[0].name
23+
`)
2324
} else {
24-
// Check if curl is available
2525
_, err := exec.LookPath("curl")
2626
if err == nil {
27-
// Use curl if available
28-
cmd = exec.Command("sh", "-c", `curl -s https://hub.docker.com/v2/repositories/digitalghostdev/poke-cli/tags/?page_size=1 | grep -o '"name":"[^"]*"' | cut -d '"' -f 4`)
27+
cmd = run("sh", "-c", `curl -s https://hub.docker.com/v2/repositories/digitalghostdev/poke-cli/tags/?page_size=1 | grep -o '"name":"[^"]*"' | cut -d '"' -f 4`)
2928
} else {
30-
// Use wget as a fallback
31-
cmd = exec.Command("sh", "-c", `wget -qO- https://hub.docker.com/v2/repositories/digitalghostdev/poke-cli/tags/?page_size=1 | grep -o '"name":"[^"]*"' | cut -d '"' -f 4`)
29+
cmd = run("sh", "-c", `wget -qO- https://hub.docker.com/v2/repositories/digitalghostdev/poke-cli/tags/?page_size=1 | grep -o '"name":"[^"]*"' | cut -d '"' -f 4`)
3230
}
3331
}
3432

@@ -90,6 +88,6 @@ func latestRelease(githubAPIURL string) {
9088

9189
func LatestFlag() {
9290
// cmd := exec.Command("git", "describe", "--tags", "--abbrev=0")
93-
latestDockerImage()
91+
latestDockerImage(exec.Command)
9492
latestRelease("https://api.github.com/repos/digitalghost-dev/poke-cli/releases/latest")
9593
}

flags/version_test.go

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"net/http"
88
"net/http/httptest"
99
"os"
10-
"runtime"
10+
"os/exec"
1111
"testing"
1212
)
1313

@@ -36,17 +36,38 @@ func captureOutput(f func()) string {
3636
}
3737

3838
func TestLatestDockerImage(t *testing.T) {
39-
output := captureOutput(func() { latestDockerImage() })
40-
41-
assert.Contains(t, output, "Latest Docker image version:")
39+
tests := []struct {
40+
name string
41+
mockRunner func(name string, args ...string) *exec.Cmd
42+
expectError bool
43+
expectText string
44+
}{
45+
{
46+
name: "success",
47+
mockRunner: func(name string, args ...string) *exec.Cmd {
48+
return exec.Command("echo", "v1.0.0\n")
49+
},
50+
expectError: false,
51+
expectText: "Latest Docker image version: v1.0.0",
52+
},
53+
{
54+
name: "error from command",
55+
mockRunner: func(name string, args ...string) *exec.Cmd {
56+
return exec.Command("false") // returns error
57+
},
58+
expectError: true,
59+
expectText: "Error running command",
60+
},
61+
}
4262

43-
// Since the actual API response might change, avoid testing the exact version number.
44-
// Instead, check if a non-empty version string is printed.
63+
for _, tt := range tests {
64+
t.Run(tt.name, func(t *testing.T) {
65+
output := captureOutput(func() {
66+
latestDockerImage(tt.mockRunner)
67+
})
4568

46-
if runtime.GOOS == "windows" {
47-
assert.Contains(t, output, "\n")
48-
} else {
49-
assert.Contains(t, output, "\n")
69+
assert.Contains(t, output, tt.expectText)
70+
})
5071
}
5172
}
5273

0 commit comments

Comments
 (0)