Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HCPE-926 - Add github checks for depscheck, fmtcheck, and gencheck #90

Merged
merged 1 commit into from
Mar 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 62 additions & 3 deletions .github/workflows/terraform_provider.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]
with:
go-version: '1.15'
id: go

- name: Check out code into the Go module directory
uses: actions/[email protected]

- name: Run 'go mod tidy' and check for differences
run: |
make depscheck

# ensure the code builds
build:
name: Build
Expand Down Expand Up @@ -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/[email protected]
with:
go-version: '1.15'
id: go

- name: Check out code into the Go module directory
uses: actions/[email protected]

- 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/[email protected]
with:
go-version: '1.15'
id: go

- name: Check out code into the Go module directory
uses: actions/[email protected]

- name: Generate docs and check for differences
run: |
make gencheck

45 changes: 36 additions & 9 deletions GNUmakefile
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)
Expand All @@ -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"
.PHONY: dev all fmt fmtcheck test testacc depscheck gencheck
13 changes: 13 additions & 0 deletions scripts/gofmtcheck.sh
Original file line number Diff line number Diff line change
@@ -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