diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..ea8f3c8 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,55 @@ +name: Build & Test Pairing +on: + push: + branches: + - master + tags-ignore: + - "*.*" + paths: + - "pairing/**" + pull_request: + branches: + - master + paths: + - "pairing/**" + +jobs: + test: + runs-on: ${{ matrix.os }} + defaults: + run: + working-directory: ./pairing + strategy: + matrix: + os: [ ubuntu-latest ] + name: Test + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Install Go + uses: actions/setup-go@v5 + with: + go-version-file: ./pairing/go.mod + + - name: Cache dependencies + uses: actions/cache@v4 + with: + path: ~/go/pkg/mod + key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} + restore-keys: | + ${{ runner.os }}-go- + + - name: Lint + uses: golangci/golangci-lint-action@v8 + with: + version: v2.1.6 + skip-pkg-cache: true + skip-build-cache: true + working-directory: ./pairing + args: --config=./.golangci.yml --verbose + + - name: Test + run: | + make test diff --git a/.gitignore b/.gitignore index 5302c46..a7ff25a 100644 --- a/.gitignore +++ b/.gitignore @@ -329,4 +329,7 @@ $RECYCLE.BIN/ # Windows shortcuts *.lnk +# Ignore build output artifacts for the pairing module +pairing/build/_output/ + # End of https://www.gitignore.io/api/vim,git,macos,linux,emacs,windows,eclipse,intellij+all,visualstudiocode \ No newline at end of file diff --git a/pairing/.golangci.yml b/pairing/.golangci.yml new file mode 100644 index 0000000..095f99d --- /dev/null +++ b/pairing/.golangci.yml @@ -0,0 +1,62 @@ +version: "2" +linters: + enable: + - asasalint + - asciicheck + - bidichk + - bodyclose + - durationcheck + - errchkjson + - errorlint + - exhaustive + - gocheckcompilerdirectives + - gochecksumtype + - gocyclo + - gosec + - gosmopolitan + - loggercheck + - makezero + - misspell + - musttag + - nilerr + - nilnesserr + - protogetter + - reassign + - recvcheck + - revive + - rowserrcheck + - spancheck + - sqlclosecheck + - testifylint + - unparam + - zerologlint + disable: + - contextcheck + - noctx + settings: + revive: + rules: + - name: dot-imports + disabled: true + unparam: + check-exported: true + exclusions: + generated: lax + presets: + - comments + - common-false-positives + - legacy + - std-error-handling + paths: + - third_party$ + - builtin$ + - examples$ +formatters: + enable: + - gofmt + exclusions: + generated: lax + paths: + - third_party$ + - builtin$ + - examples$ diff --git a/pairing/Makefile b/pairing/Makefile new file mode 100644 index 0000000..4d53244 --- /dev/null +++ b/pairing/Makefile @@ -0,0 +1,5 @@ +# It's necessary to set this because some environments don't link sh -> bash. +SHELL := /bin/bash + +include ./make/*.mk +.DEFAULT_GOAL := help \ No newline at end of file diff --git a/pairing/dummy/func.go b/pairing/dummy/func.go new file mode 100644 index 0000000..9e0c3cc --- /dev/null +++ b/pairing/dummy/func.go @@ -0,0 +1,5 @@ +package dummy + +func Add(a, b int) int { + return a + b +} diff --git a/pairing/dummy/func_test.go b/pairing/dummy/func_test.go new file mode 100644 index 0000000..6ab9857 --- /dev/null +++ b/pairing/dummy/func_test.go @@ -0,0 +1,13 @@ +package dummy + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestAdd(t *testing.T) { + result := Add(2, 3) + + assert.Equal(t, 5, result) +} diff --git a/pairing/go.mod b/pairing/go.mod new file mode 100644 index 0000000..eeea9f4 --- /dev/null +++ b/pairing/go.mod @@ -0,0 +1,11 @@ +module github.com/codeready-toolchain/pairing + +go 1.22.12 + +require github.com/stretchr/testify v1.10.0 + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/pairing/go.sum b/pairing/go.sum new file mode 100644 index 0000000..713a0b4 --- /dev/null +++ b/pairing/go.sum @@ -0,0 +1,10 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= +github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/pairing/main.go b/pairing/main.go new file mode 100644 index 0000000..293dda3 --- /dev/null +++ b/pairing/main.go @@ -0,0 +1,12 @@ +package main + +import ( + "fmt" + + "github.com/codeready-toolchain/pairing/dummy" +) + +func main() { + result := dummy.Add(2, 3) + fmt.Printf("2 + 3 = %d\n", result) +} diff --git a/pairing/make/build.mk b/pairing/make/build.mk new file mode 100644 index 0000000..0d49163 --- /dev/null +++ b/pairing/make/build.mk @@ -0,0 +1,29 @@ +GO_PACKAGE_ORG_NAME ?= codeready-toolchain +GO_PACKAGE_REPO_NAME ?= pairing +GO_PACKAGE_PATH ?= github.com/${GO_PACKAGE_ORG_NAME}/${GO_PACKAGE_REPO_NAME} +goarch ?= amd64 + +BIN_DIR = $(OUT_DIR)/bin +.PHONY: build +## Build the operator +build: GO_COMMAND=build +build: GO_EXTRA_FLAGS=-o $(BIN_DIR)/ +build: clean-bin run-go + +.PHONY: install +## installs the binary executable +install: GO_COMMAND=install +install: run-go + +run-go: + $(Q)CGO_ENABLED=0 \ + env GOOS=linux GOARCH=${goarch} go ${GO_COMMAND} ${V_FLAG} \ + -ldflags "-X ${GO_PACKAGE_PATH}/pkg/version.Commit=${GIT_COMMIT_ID} -X ${GO_PACKAGE_PATH}/pkg/version.BuildTime=${BUILD_TIME}" \ + ${GO_EXTRA_FLAGS} ${GO_PACKAGE_PATH}/... + +.PHONY: lint +lint: +ifeq (, $(shell which golangci-lint 2>/dev/null)) + $(error "golangci-lint not found in PATH. Please install it using instructions on https://golangci-lint.run/usage/install/#local-installation") +endif + golangci-lint ${V_FLAG} run --config=./.golangci.yml --verbose ./... \ No newline at end of file diff --git a/pairing/make/clean.mk b/pairing/make/clean.mk new file mode 100644 index 0000000..924379c --- /dev/null +++ b/pairing/make/clean.mk @@ -0,0 +1,9 @@ +.PHONY: clean +clean: clean-bin + $(Q)-rm -rf ${V_FLAG} ./vendor + $(Q)-rm -rf ($COV_DIR) + $(Q)go clean ${X_FLAG} ./... + +.PHONY: clean-bin +clean-bin: + @rm -rf $(BIN_DIR) 2>/dev/null || true diff --git a/pairing/make/out.mk b/pairing/make/out.mk new file mode 100644 index 0000000..41de240 --- /dev/null +++ b/pairing/make/out.mk @@ -0,0 +1,5 @@ +# Create output directory for artifacts and test results. `./build/_output` is supposed to +# be a safe place for all targets to write to while knowing that all content +# inside of `./build/_output` is wiped once "make clean" is run. +OUT_DIR := ./build/_output +$(shell mkdir -p $(OUT_DIR)) \ No newline at end of file diff --git a/pairing/make/test.mk b/pairing/make/test.mk new file mode 100644 index 0000000..33a19c2 --- /dev/null +++ b/pairing/make/test.mk @@ -0,0 +1,17 @@ +.PHONY: test +test: build + @echo "running the pairing unit tests..." + go test -v -failfast ./... + + +# Output directory for coverage information +COV_DIR = $(OUT_DIR)/coverage + +.PHONY: test-with-coverage +## runs the tests with coverage +test-with-coverage: + @echo "running the unit tests with coverage..." + @-mkdir -p $(COV_DIR) + @-rm $(COV_DIR)/coverage.txt + $(Q)go test -vet off ${V_FLAG} $(shell go list ./... | grep -v /cmd/manager) -coverprofile=$(COV_DIR)/coverage.txt -covermode=atomic ./... + diff --git a/pairing/make/verbose.mk b/pairing/make/verbose.mk new file mode 100644 index 0000000..a7f57d1 --- /dev/null +++ b/pairing/make/verbose.mk @@ -0,0 +1,24 @@ +# When you run make VERBOSE=1 (the default), executed commands will be printed +# before executed. If you run make VERBOSE=2 verbose flags are turned on and +# quiet flags are turned off for various commands. Use V_FLAG in places where +# you can toggle on/off verbosity using -v. Use Q_FLAG in places where you can +# toggle on/off quiet mode using -q. Use S_FLAG where you want to toggle on/off +# silence mode using -s... +VERBOSE ?= 1 +Q = @ +Q_FLAG = -q +QUIET_FLAG = --quiet +V_FLAG = +S_FLAG = -s +X_FLAG = +ifeq ($(VERBOSE),1) + Q = +endif +ifeq ($(VERBOSE),2) + Q = + Q_FLAG = + QUIET_FLAG = + S_FLAG = + V_FLAG = -v + X_FLAG = -x +endif