Created
February 19, 2023 07:13
-
-
Save nik-hil/1b32df6a19a6bed66d1310e55f370989 to your computer and use it in GitHub Desktop.
Golang using interface for better testing.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "testing" | |
type FakeReleaseInfoer struct { | |
Tag string | |
Err error | |
} | |
func (f FakeReleaseInfoer) GetLatestReleaseTag(repo string) (string, error) { | |
if f.Err != nil { | |
return "", f.Err | |
} | |
return f.Tag, nil | |
} | |
func TestGetReleaseTagMessage(t *testing.T) { | |
f := FakeReleaseInfoer{ | |
Tag: "v0.1.0", | |
Err: nil, | |
} | |
expectedMsg := "The latest release is v0.1.0" | |
msg, err := getReleaseTagMessage(f, "dev/null") | |
if err != nil { | |
t.Fatalf("Expected err to be nil but it was %s", err) | |
} | |
if expectedMsg != msg { | |
t.Fatalf("Expected %s but got %s", expectedMsg, msg) | |
} | |
} | |
func TestGetReleaseTagMessage(t *testing.T) { | |
cases := []struct { | |
f FakeReleaseInfoer | |
repo string | |
expectedMsg string | |
expectedErr error | |
}{ | |
{ | |
f: FakeReleaseInfoer{ | |
Tag: "v0.1.0", | |
Err: nil, | |
}, | |
repo: "doesnt/matter", | |
expectedMsg: "The latest release is v0.1.0", | |
expectedErr: nil, | |
}, | |
{ | |
f: FakeReleaseInfoer{ | |
Tag: "v0.1.0", | |
Err: errors.New("TCP timeout"), | |
}, | |
repo: "doesnt/foo", | |
expectedMsg: "", | |
expectedErr: errors.New("Error querying GitHub API: TCP timeout"), | |
}, | |
} | |
for _, c := range cases { | |
msg, err := getReleaseTagMessage(c.f, c.repo) | |
if !reflect.DeepEqual(err, c.expectedErr) { | |
t.Errorf("Expected err to be %q but it was %q", c.expectedErr, err) | |
} | |
if c.expectedMsg != msg { | |
t.Errorf("Expected %q but got %q", c.expectedMsg, msg) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"io/ioutil" | |
"net/http" | |
"os" | |
) | |
type ReleasesInfo struct { | |
Id uint `json:"id"` | |
TagName string `json:"tag_name"` | |
} | |
// Function to actually query the GitHub API for the release information. | |
func getLatestReleaseTag(repo string) (string, error) { | |
apiUrl := fmt.Sprintf("https://api.github.com/repos/%s/releases", repo) | |
response, err := http.Get(apiUrl) | |
if err != nil { | |
return "", err | |
} | |
defer response.Body.Close() | |
body, err := ioutil.ReadAll(response.Body) | |
if err != nil { | |
return "", err | |
} | |
releases := []ReleasesInfo{} | |
if err := json.Unmarshal(body, &releases); err != nil { | |
return "", err | |
} | |
tag := releases[0].TagName | |
return tag, nil | |
} | |
// Function to get the message to display to the end user. | |
func getReleaseTagMessage(repo string) (string, error) { | |
tag, err := getLatestReleaseTag(repo) | |
if err != nil { | |
return "", fmt.Errorf("Error querying GitHub API: %s", err) | |
} | |
return fmt.Sprintf("The latest release is %s", tag), nil | |
} | |
func main() { | |
msg, err := getReleaseTagMessage("docker/machine") | |
if err != nil { | |
fmt.Fprintln(os.Stderr, msg) | |
} | |
fmt.Println(msg) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"io/ioutil" | |
"net/http" | |
"os" | |
) | |
type ReleasesInfo struct { | |
Id uint `json:"id"` | |
TagName string `json:"tag_name"` | |
} | |
type ReleaseInfoer interface { | |
GetLatestReleaseTag(string) (string, error) | |
} | |
type GithubReleaseInfoer struct{} | |
// Function to actually query the GitHub API for the release information. | |
func (gh GithubReleaseInfoer) getLatestReleaseTag(repo string) (string, error) { | |
apiUrl := fmt.Sprintf("https://api.github.com/repos/%s/releases", repo) | |
response, err := http.Get(apiUrl) | |
if err != nil { | |
return "", err | |
} | |
defer response.Body.Close() | |
body, err := ioutil.ReadAll(response.Body) | |
if err != nil { | |
return "", err | |
} | |
releases := []ReleasesInfo{} | |
if err := json.Unmarshal(body, &releases); err != nil { | |
return "", err | |
} | |
tag := releases[0].TagName | |
return tag, nil | |
} | |
// Function to get the message to display to the end user. | |
func getReleaseTagMessage(ri ReleaseInfoer, repo string) (string, error) { | |
tag, err := ri.GetLatestReleaseTag(repo) | |
if err != nil { | |
return "", fmt.Errorf("Error query GitHub API: %s", err) | |
} | |
return fmt.Sprintf("The latest release is %s", tag), nil | |
} | |
func main() { | |
gh := GithubReleaseInfoer{} | |
msg, err := getReleaseTagMessage(gh, "docker/machine") | |
if err != nil { | |
fmt.Fprintln(os.Stderr, err) | |
os.Exit(1) | |
} | |
fmt.Println(msg) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment