diff --git a/README.md b/README.md index 3ee267c..215fa77 100644 --- a/README.md +++ b/README.md @@ -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) +
diff --git a/cmd/version.go b/cmd/version.go index 9cc2483..e19dec0 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -17,6 +17,10 @@ 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", @@ -24,7 +28,7 @@ var versionCmd = &cobra.Command{ 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) @@ -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 @@ -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{ diff --git a/cmd/version_test.go b/cmd/version_test.go index 499bff6..d31e056 100644 --- a/cmd/version_test.go +++ b/cmd/version_test.go @@ -5,6 +5,8 @@ package cmd import ( "fmt" + "net/http" + "net/http/httptest" "testing" ) @@ -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) } @@ -30,7 +32,7 @@ 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) } @@ -38,3 +40,47 @@ func TestCheckForNewRelease(t *testing.T) { 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")) + })) + 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) + } +}