diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index c7145d4..82bb2d6 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,20 +1,65 @@ -name: Test and Check +name: CI -run-name: Running tests and checks for policies - -on: [push] +on: + push: + branches: + - main + - release-* + pull_request: {} jobs: - Test-And-Check: + setup-go: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version-file: "go.mod" + + - name: Download dependencies + run: go mod download + + - name: Check build + run: go build ./... + + unit-test: + needs: setup-go runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v3 - permissions: - contents: write + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version-file: "go.mod" + - name: Run unit tests + run: make test + + go-fmt-check: + needs: setup-go + runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 - - uses: actions/setup-go@v5 + - name: Checkout code + uses: actions/checkout@v3 - - name: Test - run: go test ./... + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version-file: "go.mod" + - name: Check go fmt changes + run: | + go fmt ./... + git status --porcelain + if [ "$(git status --porcelain)" != "" ]; then + echo "The following files would be changed by go fmt:" + git status --porcelain + exit 1 + else + echo "All files are properly formatted." + fi diff --git a/.gitignore b/.gitignore index 3269be1..5906c43 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,7 @@ data/ dist/ .compliance-framework/ agent -main \ No newline at end of file +main + +cover.out +config.yml \ No newline at end of file diff --git a/Makefile b/Makefile index 36abf51..f5f1e7f 100644 --- a/Makefile +++ b/Makefile @@ -9,6 +9,23 @@ # More info on the awk command: # http://linuxcommand.org/lc3_adv_awk.php +BLUE := $(shell printf "\033[34m") +YELLOW := $(shell printf "\033[33m") +RED := $(shell printf "\033[31m") +GREEN := $(shell printf "\033[32m") +CNone := $(shell printf "\033[0m") + +INFO = echo ${TIME} ${BLUE}[ .. ]${CNone} +WARN = echo ${TIME} ${YELLOW}[WARN]${CNone} +ERR = echo ${TIME} ${RED}[FAIL]${CNone} +OK = echo ${TIME} ${GREEN}[ OK ]${CNone} +FAIL = (echo ${TIME} ${RED}[FAIL]${CNone} && false) + +# Setting SHELL to bash allows bash commands to be executed by recipes. +# Options are set to exit when a recipe line exits non-zero or a piped command fails. +SHELL = /usr/bin/env bash -o pipefail +.SHELLFLAGS = -ec + help: ## Display this concise help, ie only the porcelain target. @awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-25s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST) @@ -17,3 +34,12 @@ help-all: ## Display all help items, ie including plumbing targets. proto-gen: ## Generate objects from proto definitions @buf generate + +##@ Test +.PHONY: test +test: ## Run tests + @if ! go test ./... -coverprofile cover.out -v; then \ + $(WARN) "Tests failed"; \ + exit 1; \ + fi ; \ + $(OK) Tests passed \ No newline at end of file