Skip to content

Commit

Permalink
Issue #5: Add mock client (#6)
Browse files Browse the repository at this point in the history
- Add interface for a "status page client" to implement.
- Add an unexported type and change the existing functions to be receiver methods for this type in turn satisfying the new interface.
- Add a `NewStatusPageClient()` function to create a client for use.
- Update documentation accordingly.
- Add mock for implementors of the library to use in their unit tests.

Closes #5
  • Loading branch information
sprak3000 authored Sep 29, 2023
1 parent bb6177a commit ef03c34
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 4 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ GOFILES = $(shell go list -mod vendor ./... | grep -v vendor)
help:
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

.PHONY: generate
generate: ## Generate the mock client
@go generate -mod=mod ./...

.PHONY: analyze
analyze: lint vet test ## Run lint, vet, and test

Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ import (
)

func main() {
v, err := whatsup.StatuspageIoService("github", "https://www.githubstatus.com/api/v2/status.json")
c := whatsup.NewStatusPageClient()
v, err := c.StatuspageIoService("github", "https://www.githubstatus.com/api/v2/status.json")
if err != nil {
fmt.Println(err)
os.Exit(1)
Expand All @@ -56,7 +57,8 @@ import (
)

func main() {
v, err := whatsup.Slack()
c := whatsup.NewStatusPageClient()
v, err := c.Slack()
if err != nil {
fmt.Println(err)
os.Exit(1)
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.7.0 h1:LapD9S96VoQRhi/GrNTqeBJFrUjs5UHCAtTlgwA5oZA=
golang.org/x/mod v0.7.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
Expand All @@ -24,12 +26,16 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ=
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.4.0 h1:7mTAgkunk3fr4GAloyyCasadO6h9zSsQZbwvcaIciV4=
golang.org/x/tools v0.4.0/go.mod h1:UE5sM2OK9E/d67R0ANs2xJizIymRP5gJU295PvKXxjQ=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
20 changes: 18 additions & 2 deletions whatsup/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,24 @@ import (
"github.com/sprak3000/go-whatsup-client/statuspageio"
)

//go:generate go run -mod=mod github.com/golang/mock/mockgen -package clientmock -destination=./clientmock/client-mock.go -source=../whatsup/client.go -build_flags=-mod=mod

// StatusPageClient handles the requests for status pages
type StatusPageClient interface {
StatuspageIoService(serviceName, pageURL string) (status.Details, glitch.DataError)
Slack() (status.Details, glitch.DataError)
}

type statusPageClient struct {
}

// NewStatusPageClient creates a new StatusPageClient
func NewStatusPageClient() StatusPageClient {
return &statusPageClient{}
}

// StatuspageIoService handles fetching statuspage.io style status pages
func StatuspageIoService(serviceName, pageURL string) (status.Details, glitch.DataError) {
func (spc *statusPageClient) StatuspageIoService(serviceName, pageURL string) (status.Details, glitch.DataError) {
u, err := url.Parse(pageURL)
sf := func(serviceName string, useTLS bool) (url.URL, error) {
return *u, err
Expand All @@ -24,7 +40,7 @@ func StatuspageIoService(serviceName, pageURL string) (status.Details, glitch.Da
}

// Slack handles fetching the Slack status page
func Slack() (status.Details, glitch.DataError) {
func (spc *statusPageClient) Slack() (status.Details, glitch.DataError) {
sn := slack.ServiceType
u, err := url.Parse("https://status.slack.com/api/v2.0.0/current")
sf := func(serviceName string, useTLS bool) (url.URL, error) {
Expand Down
66 changes: 66 additions & 0 deletions whatsup/clientmock/client-mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ef03c34

Please sign in to comment.