Skip to content

Commit

Permalink
test: version tests
Browse files Browse the repository at this point in the history
  • Loading branch information
karl-cardenas-coding committed Nov 18, 2023
1 parent 439fb17 commit 0c5a4e5
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
[![Main](https://github.com/karl-cardenas-coding/go-lambda-cleanup/actions/workflows/build.yml/badge.svg)](https://github.com/karl-cardenas-coding/go-lambda-cleanup/actions/workflows/build.yml)
[![Go version](https://img.shields.io/github/go-mod/go-version/karl-cardenas-coding/go-lambda-cleanup)](https://golang.org/dl/)
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
[![codecov](https://codecov.io/gh/karl-cardenas-coding/go-lambda-cleanup/graph/badge.svg?token=S8SY4ZA2ZA)](https://codecov.io/gh/karl-cardenas-coding/go-lambda-cleanup)


<p align="center">
<img src="/static/logo.png" alt="drawing" width="400"/>
Expand Down
21 changes: 18 additions & 3 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@ func init() {
rootCmd.AddCommand(versionCmd)
}

const (
url = "https://api.github.com/repos/karl-cardenas-coding/go-lambda-cleanup/releases/latest"
)

var versionCmd = &cobra.Command{
Use: "version",
Short: "Print the current version number of go-lambda-cleanup",
Long: `Prints the current version number of go-lambda-cleanup`,
Run: func(cmd *cobra.Command, args []string) {
version := fmt.Sprintf("go-lambda-cleanup v%s", VersionString)
log.Info(version)
_, message, err := checkForNewRelease(GlobalHTTPClient, VersionString, UserAgent)
_, message, err := checkForNewRelease(GlobalHTTPClient, VersionString, UserAgent, url)
if err != nil {
log.Error(err)
log.Fatal("unable to check for new releases. " + IssueMSG)
Expand All @@ -33,8 +37,7 @@ var versionCmd = &cobra.Command{
},
}

func checkForNewRelease(client *http.Client, currentVersion, useragent string) (bool, string, error) {
const url string = "https://api.github.com/repos/karl-cardenas-coding/go-lambda-cleanup/releases/latest"
func checkForNewRelease(client *http.Client, currentVersion, useragent, url string) (bool, string, error) {
var (
output bool
message string
Expand Down Expand Up @@ -70,6 +73,18 @@ func checkForNewRelease(client *http.Client, currentVersion, useragent string) (
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
log.WithFields(log.Fields{
"package": "cmd",
"file": "version.go",
"parent_function": "checkForNewRelease",
"function": "client.Do",
"error": err,
"data": nil,
}).Debug("Error initaiting connection to, ", url, IssueMSG)
return output, message, fmt.Errorf("error connecting to %s", url)
}

// Unmarshal the JSON to the Github Release strcut
if err := json.NewDecoder(resp.Body).Decode(&release); err != nil {
log.WithFields(log.Fields{
Expand Down
50 changes: 48 additions & 2 deletions cmd/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package cmd

import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
)

Expand All @@ -20,7 +22,7 @@ func TestCheckForNewRelease(t *testing.T) {
useragent := fmt.Sprintf("go-lambda-cleanup/%s", version)

want := true
got, msg, err := checkForNewRelease(client, version, useragent)
got, msg, err := checkForNewRelease(client, version, useragent, url)
if err != nil {
t.Fatalf("Error checking for new release: %s", err)
}
Expand All @@ -30,11 +32,55 @@ func TestCheckForNewRelease(t *testing.T) {

version = "100.0.0"
want2 := true
got2, msg2, err := checkForNewRelease(client, version, useragent)
got2, msg2, err := checkForNewRelease(client, version, useragent, url)
if err != nil {
t.Fatalf("Error checking for new release: %s", err)
}
if got2 != want2 && msg2 != youAreRunningAPreReleaseVersion {
t.Fatalf("Error checking for new release: %s", err)
}
}

func TestErrorPath(t *testing.T) {

mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Mock error response", http.StatusInternalServerError)
}))
defer mockServer.Close()

// Override the base URL to use the mock server URL
baseURL := mockServer.URL

client := createHTTPClient()
version := "------"
useragent := fmt.Sprintf("go-lambda-cleanup/%s", version)

_, _, err := checkForNewRelease(client, version, useragent, baseURL)
if err == nil {
t.Fatalf("Error Expected: %s", err)
}
}

func TestErrorPathJSON(t *testing.T) {

mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Set the Content-Type header to indicate JSON response
w.Header().Set("Content-Type", "application/json")

// Write an invalid JSON payload to the response writer
w.Write([]byte("invalid-json-payload"))

Check failure on line 71 in cmd/version_test.go

View workflow job for this annotation

GitHub Actions / Test

Error return value of `w.Write` is not checked (errcheck)

Check failure on line 71 in cmd/version_test.go

View workflow job for this annotation

GitHub Actions / Test

Error return value of `w.Write` is not checked (errcheck)
}))
defer mockServer.Close()

// Override the base URL to use the mock server URL
baseURL := mockServer.URL

client := createHTTPClient()
version := "------"
useragent := fmt.Sprintf("go-lambda-cleanup/%s", version)

_, _, err := checkForNewRelease(client, version, useragent, baseURL)
if err == nil {
t.Fatalf("Error Expected: %s", err)
}
}

0 comments on commit 0c5a4e5

Please sign in to comment.