From c8333315aa026f00f45bd7d1425e1e928b48d3c4 Mon Sep 17 00:00:00 2001 From: Luther Monson Date: Fri, 10 Apr 2020 05:26:10 -0700 Subject: [PATCH] testing procedure cleanup and docs (#98) --- .travis.yml | 3 ++- Dockerfile | 8 ++---- Makefile | 14 +++++++++++ README.md | 63 ++++++++++++++++++++++++++++++++++++++++------ docker-compose.yml | 5 ++-- make.bat | 25 ++++++++++++++++++ 6 files changed, 102 insertions(+), 16 deletions(-) create mode 100644 Makefile create mode 100644 make.bat diff --git a/.travis.yml b/.travis.yml index 584e2b2b..947491d3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,8 +9,9 @@ go: - "1.12" - "1.13" - "1.14" +install: + - make travis script: - go test -coverprofile=coverage.txt after_success: - bash <(curl -s https://codecov.io/bash) - diff --git a/Dockerfile b/Dockerfile index e2072a6a..279eec01 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,9 +1,5 @@ -FROM golang:1.9 +FROM golang:1.14-alpine -# This is similar to the golang-onbuild image but with different paths and -# test-dependencies loaded as well. +ENV CGO_ENABLED=0 RUN mkdir -p /go/src/github.com/bold-commerce/go-shopify WORKDIR /go/src/github.com/bold-commerce/go-shopify - -COPY . /go/src/github.com/bold-commerce/go-shopify -RUN go get -v -d -t diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..d639909e --- /dev/null +++ b/Makefile @@ -0,0 +1,14 @@ +container: + @docker-compose build test +travis: + @go get -v -d -t +test: + @docker-compose run --rm test +coverage: + @docker-compose run --rm test sh -c 'go test -coverprofile=coverage.out ./... && go tool cover -html coverage.out -o coverage.html' + @open coverage.html +clean: + @docker image rm go-shopify + @rm -f coverage.html coverage.out + +.DEFAULT_GOAL := container diff --git a/README.md b/README.md index ea295c32..7569c2f5 100644 --- a/README.md +++ b/README.md @@ -97,6 +97,29 @@ client := goshopify.NewClient(app, "shopname", "") // Fetch the number of products. numProducts, err := client.Product.Count(nil) ``` +### Client Options +When creating a client there are configuration options you can pass to NewClient. Simply use the last variadic param and +pass in the built in options or create your own and manipulate the client. See [options.go](https://github.com/bold-commerce/go-shopify/blob/master/options.go) +for more details. + +#### WithVersion +Read more details on the [Shopify API Versioning](https://shopify.dev/concepts/about-apis/versioning) +to understand the format and release schedules. You can use `WithVersion` to specify a specific version +of the API. If you do not use this option you will be defaulted to the oldest stable API. + +```go +client := goshopify.NewClient(app, "shopname", "", goshopify.WithVersion("2019-04")) +``` + +#### WithRetry +Shopify [Rate Limits](https://shopify.dev/concepts/about-apis/rate-limits) their API and if this happens to you they +will send a back off (usually 2s) to tell you to retry your request. To support this functionality seamlessly within +the client a `WithRetry` option exists where you can pass an `int` of how many times you wish to retry per-request +before returning an error. `WithRetry` additionally supports retrying HTTP503 errors. + +```go +client := goshopify.NewClient(app, "shopname", "", goshopify.WithRetry(3)) +``` #### Query options @@ -176,14 +199,40 @@ func ValidateWebhook(httpRequest *http.Request) (bool) { ``` ## Develop and test +`docker` and `docker-compose` must be installed + +### Mac/Linux/Windows with make +Using the make file is the easiest way to get started with the tests and wraps the manual steps below with easy to use +make commands. -There's nothing special to note about the tests except that if you have Docker -and Compose installed, you can test like this: +```shell +make && make test +``` +#### Makefile goals +* `make` or `make container`: default goal is to make the `go-shopify:latest` build container +* `make test`: run go test in the container +* `make clean`: deletes the `go-shopify:latest` image and coverage output +* `make coverage`: generates the coverage.html and opens it + +### Manually +To run the tests you will need the `go-shopify:latest` image built to run your tests, to do this run +``` +docker-compose build test +``` - $ docker-compose build dev - $ docker-compose run --rm dev +To run tests you can use run +```shell +docker-compose run --rm tests +``` -Testing the package is the default command for the dev container. To create a -coverage profile: +To create a coverage profile run the following to generate a coverage.html +``` +docker-compose run --rm dev sh -c 'go test -coverprofile=coverage.out ./... && go tool cover -html coverage.out -o coverage.html' +``` + +When done testing and you want to cleanup simply run +``` +docker image rm go-shopify:latest +``` - $ docker-compose run --rm dev bash -c 'go test -coverprofile=coverage.out ./... && go tool cover -html coverage.out -o coverage.html' +Read the docker-compose.yml and Dockerfile for further details. diff --git a/docker-compose.yml b/docker-compose.yml index 04683e48..ab54abd5 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,7 +1,8 @@ -version: '2' +version: '3.7' services: - dev: + test: + image: go-shopify:latest build: . command: go test -v -cover ./... volumes: diff --git a/make.bat b/make.bat new file mode 100644 index 00000000..f12e259c --- /dev/null +++ b/make.bat @@ -0,0 +1,25 @@ +@echo off +if "%1%"=="" ( + set cmd=container +) else ( + set cmd=%1% +) +if "%cmd%"=="container" ( + docker-compose build test + goto :eof +) +if "%cmd%"=="test" ( + docker-compose run --rm test + goto :eof +) +if "%cmd%"=="coverage" ( + docker-compose run --rm test sh -c "go test -coverprofile=coverage.out ./... && go tool cover -html coverage.out -o coverage.html" + coverage.html + goto :eof +) +if "%cmd%"=="clean" ( + docker image rm go-shopify + del coverage.html + del coverage.out + goto :eof +)