Skip to content

Commit

Permalink
part of increasing test coverage above 50% (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
digitalghost-dev committed Nov 19, 2024
1 parent b7ac64b commit 773941b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
10 changes: 4 additions & 6 deletions flags/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ import (
"os/exec"
)

func latestDockerImage() {
fullCommand := `curl -s https://hub.docker.com/v2/repositories/digitalghostdev/poke-cli/tags/?page_size=1 | grep -o '"name":"[^"]*"' | cut -d '"' -f 4`

func latestDockerImage(fullCommand string) {
cmd := exec.Command("bash", "-c", fullCommand)

output, err := cmd.Output()
if err != nil {
fmt.Printf("error running 'command': %v\n", err)
fmt.Printf("error running command: %v\n", err)
return
}

fmt.Print("Latest Docker image version: ", string(output))
Expand Down Expand Up @@ -55,6 +53,6 @@ func latestRelease(githubAPIURL string) {

func LatestFlag() {
// cmd := exec.Command("git", "describe", "--tags", "--abbrev=0")
latestDockerImage()
latestDockerImage(`curl -s https://hub.docker.com/v2/repositories/digitalghostdev/poke-cli/tags/?page_size=1 | grep -o '"name":"[^"]*"' | cut -d '"' -f 4`)
latestRelease("https://api.github.com/repos/digitalghost-dev/poke-cli/releases/latest")
}
28 changes: 22 additions & 6 deletions flags/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ func captureOutput(f func()) string {
f()

// Restore the original stdout and close the writer
w.Close()
err := w.Close()
if err != nil {
return fmt.Sprintf("error closing writer: %v", err)
}
os.Stdout = oldStdout

// Read the captured output
Expand All @@ -32,10 +35,15 @@ func captureOutput(f func()) string {
}

func TestLatestDockerImage(t *testing.T) {
output := captureOutput(latestDockerImage)

// Modify this assertion as needed based on expected output
// Test normal execution with a valid command
validCommand := `curl -s https://hub.docker.com/v2/repositories/digitalghostdev/poke-cli/tags/?page_size=1 | grep -o '"name":"[^"]*"' | cut -d '"' -f 4`
output := captureOutput(func() { latestDockerImage(validCommand) })
assert.Contains(t, output, "Latest Docker image version:")

// Test command failure with an invalid command
invalidCommand := "invalidcommand"
output = captureOutput(func() { latestDockerImage(invalidCommand) })
assert.Contains(t, output, "error running command:")
}

func TestLatestRelease(t *testing.T) {
Expand All @@ -48,7 +56,11 @@ func TestLatestRelease(t *testing.T) {
func TestLatestRelease_Success(t *testing.T) {
// Create a mock server that simulates a successful response
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, `{"tag_name": "v1.0.0"}`)
_, err := fmt.Fprintln(w, `{"tag_name": "v1.0.0"}`)
if err != nil {
t.Errorf("failed to write response: %v", err)
return
}
})
server := httptest.NewServer(handler)
defer server.Close()
Expand All @@ -61,7 +73,11 @@ func TestLatestRelease_Success(t *testing.T) {
func TestLatestRelease_InvalidJSON(t *testing.T) {
// Create a mock server that returns invalid JSON
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, `invalid-json`)
_, err := fmt.Fprintln(w, `invalid-json`)
if err != nil {
t.Errorf("failed to write response: %v", err)
return
}
})
server := httptest.NewServer(handler)
defer server.Close()
Expand Down

0 comments on commit 773941b

Please sign in to comment.