diff --git a/.github/workflows/terraform_provider.yml b/.github/workflows/terraform_provider.yml index b232b7b87..ea1e8e1f9 100644 --- a/.github/workflows/terraform_provider.yml +++ b/.github/workflows/terraform_provider.yml @@ -11,6 +11,25 @@ on: - 'README.md' jobs: + # ensure go.mod and go.sum are updated + depscheck: + name: Check Dependencies + runs-on: ubuntu-latest + steps: + + - name: Set up Go + uses: actions/setup-go@v2.1.3 + with: + go-version: '1.15' + id: go + + - name: Check out code into the Go module directory + uses: actions/checkout@v2.3.3 + + - name: Run 'go mod tidy' and check for differences + run: | + make depscheck + # ensure the code builds build: name: Build @@ -55,7 +74,47 @@ jobs: run: | go mod download - - name: Unit tests - timeout-minutes: 5 + - name: Run unit tests run: | - go test ./internal/... + make test + + # ensure code is formatted + fmtcheck: + name: Check Code Formatting + needs: build + runs-on: ubuntu-latest + steps: + + - name: Set up Go + uses: actions/setup-go@v2.1.3 + with: + go-version: '1.15' + id: go + + - name: Check out code into the Go module directory + uses: actions/checkout@v2.3.3 + + - name: Generate docs and check for differences + run: | + make fmtcheck + + # ensure docs are generated + gencheck: + name: Check Generated Docs + needs: build + runs-on: ubuntu-latest + steps: + + - name: Set up Go + uses: actions/setup-go@v2.1.3 + with: + go-version: '1.15' + id: go + + - name: Check out code into the Go module directory + uses: actions/checkout@v2.3.3 + + - name: Generate docs and check for differences + run: | + make gencheck + diff --git a/GNUmakefile b/GNUmakefile index 4982b7c94..cabdfc24f 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -1,8 +1,8 @@ -default: testacc GOOS=$(shell go env GOOS) GOARCH=$(shell go env GOARCH) INSTALL_PATH=~/.local/share/terraform/plugins/localhost/providers/hcp/0.0.1/linux_$(GOARCH) BUILD_ALL_PATH=${PWD}/bin +TEST?=./internal/... ifeq ($(GOOS), darwin) INSTALL_PATH=~/Library/Application\ Support/io.terraform/plugins/localhost/providers/hcp/0.0.1/darwin_$(GOARCH) @@ -11,10 +11,7 @@ ifeq ($(GOOS), "windows") INSTALL_PATH=%APPDATA%/HashiCorp/Terraform/plugins/localhost/providers/hcp/0.0.1/windows_$(GOARCH) endif -# Run acceptance tests -.PHONY: testacc -testacc: - TF_ACC=1 go test ./... -v $(TESTARGS) -timeout 120m +default: dev dev: mkdir -p $(INSTALL_PATH) @@ -26,9 +23,39 @@ all: GOOS=windows go build -o $(BUILD_ALL_PATH)/terraform-provider-hcp_windows-amd64 main.go GOOS=linux go build -o $(BUILD_ALL_PATH)/terraform-provider-hcp_linux-amd64 main.go -# these are just assumed to exist by the build system +fmt: + @echo "==> Fixing source code with gofmt..." + gofmt -s -w ./internal + fmtcheck: - echo "Placeholder" + @./scripts/gofmtcheck.sh + +test: fmtcheck + go test $(TEST) $(TESTARGS) -timeout=5m -parallel=4 + +testacc: fmtcheck + @if [ "$(TESTARGS)" = "-run=TestAccXXX" ]; then \ + echo ""; \ + echo "Error: Skipping example acceptance testing pattern. Update TESTARGS to match the test naming in the relevant *_test.go file."; \ + echo ""; \ + echo "For example if updating resource_hvn.go, use the test names in resource_hvn_test.go starting with TestAcc:"; \ + echo "make testacc TESTARGS='-run=TestAccHvn'"; \ + echo ""; \ + echo "See the contributing guide for more information: https://github.com/hashicorp/terraform-provider-hcp/blob/main/contributing/writing-tests.md"; \ + exit 1; \ + fi + TF_ACC=1 go test $(TEST) -v $(TESTARGS) -timeout 120m + +depscheck: + @echo "==> Checking source code with go mod tidy..." + @go mod tidy + @git diff --exit-code -- go.mod go.sum || \ + (echo; echo "Unexpected difference in go.mod/go.sum files. Run 'go mod tidy' command or revert any go.mod/go.sum changes and commit."; exit 1) + +gencheck: + @echo "==> Checking generated source code..." + go generate + @git diff --compact-summary --exit-code || \ + (echo; echo "Unexpected difference in directories after code generation. Run 'go generate' command and commit."; exit 1) -test: - echo "Placeholder" \ No newline at end of file +.PHONY: dev all fmt fmtcheck test testacc depscheck gencheck diff --git a/scripts/gofmtcheck.sh b/scripts/gofmtcheck.sh new file mode 100755 index 000000000..75140f7d6 --- /dev/null +++ b/scripts/gofmtcheck.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash + +# Check gofmt +echo "==> Checking that code complies with gofmt requirements..." +gofmt_files=$(gofmt -l `find ./internal -name '*.go'`) +if [[ -n ${gofmt_files} ]]; then + echo 'gofmt needs running on the following files:' + echo "${gofmt_files}" + echo "You can use the command: 'make fmt' to reformat code." + exit 1 +fi + +exit 0