From 8800a248545e3d8963d8a7d3fd88226d08dd49bf Mon Sep 17 00:00:00 2001 From: Mateusz Szostok Date: Sun, 31 Jan 2021 22:15:56 +0100 Subject: [PATCH] Migrate tests from TravisCI to GitHub Action (#66) --- .github/workflows/pull-requests.yml | 66 ++++++++++++++++++++++++--- .travis.yml | 58 ----------------------- Makefile | 11 ++++- hack/run-lint.sh | 11 ++++- tests/integration/helpers_test.go | 2 +- tests/integration/integration_test.go | 11 ++++- 6 files changed, 88 insertions(+), 71 deletions(-) delete mode 100644 .travis.yml diff --git a/.github/workflows/pull-requests.yml b/.github/workflows/pull-requests.yml index f432c2e..20028e6 100644 --- a/.github/workflows/pull-requests.yml +++ b/.github/workflows/pull-requests.yml @@ -1,17 +1,71 @@ -name: Pull request testing +name: Pull request on: pull_request: branches: [ master ] +env: + GO111MODULE: on + INSTALL_DEPS: true + +defaults: + run: + shell: bash + jobs: - build: - runs-on: ubuntu-latest + unit-test: + strategy: + fail-fast: false + matrix: + go-version: [1.14.x, 1.15.x] + os: [ubuntu-latest, macos-latest] + runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v2 - name: Set up Go uses: actions/setup-go@v2 with: - go-version: 1.15 - - name: "Initial Action Step" - run: echo "Initial Action" + go-version: ${{ matrix.go-version }} + - name: "Build and unit-test with Go ${{ matrix.go-version }}" + run: make test-unit + - name: "Hammer unit-test with ${{ matrix.go-version }}" + run: make test-hammer + code-quality-test: + strategy: + fail-fast: false + matrix: + go-version: [1.14.x, 1.15.x] + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Set up Go + uses: actions/setup-go@v2 + with: + go-version: ${{ matrix.go-version }} + - name: "Code Quality Analysis" + run: make test-lint + integration-test: + strategy: + fail-fast: false + matrix: + go-version: [1.14.x, 1.15.x] + os: [ubuntu-latest, macos-latest, windows-latest] + runs-on: ${{ matrix.os }} + steps: + - name: Set git to use LF + run: | + git config --global core.autocrlf false + git config --global core.eol lf + - uses: actions/checkout@v2 + - name: Set up Go + uses: actions/setup-go@v2 + with: + go-version: ${{ matrix.go-version }} + - if: matrix.os == 'windows-latest' + run: echo "BINARY_EXT=.exe" >> $GITHUB_ENV + - name: "Integration testing with ${{ matrix.go-version }}" + env: + GITHUB_TOKEN: ${{ secrets.TOKEN_INTEGRATION_TESTS }} + run: | + echo "${{ env.BINARY_PATH }}" + make test-integration diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 88718d1..0000000 --- a/.travis.yml +++ /dev/null @@ -1,58 +0,0 @@ -language: go - -# Skip the install step. Don't `go get` dependencies -install: skip - -branches: - only: - - "master" -env: - global: - - GO111MODULE=on - - INSTALL_DEPS=true - -script: skip - -jobs: - include: - - stage: test - name: "Build and unit-test with Go 1.14.x" - go: 1.14.x - script: make test-unit - - stage: test - name: "Build and unit-test with Go 1.15.x" - go: 1.15.x - script: make test-unit - - stage: test - name: "Hammer unit-test with Go 1.15.x" - go: 1.15.x - script: make test-hammer - - stage: test - name: "Code Quality Analysis" - script: make test-lint - go: 1.15.x - - stage: test-integration - name: "Integration testing on OSX" - script: make test-integration - os: osx - go: 1.15.x - - stage: test-integration - name: "Integration testing on Linux" - script: make test-integration - os: linux - go: 1.15.x -# Currently skipped as the new line encoding differs between linux and windows -# - stage: test-integration -# name: "Integration testing on Windows" -# before_install: -# - export "BINARY_PATH=$TRAVIS_BUILD_DIR/codeowners-validator.exe" -# script: -# - go build -o codeowners-validator.exe ./main.go -# - echo $(pwd) -# - go test ./tests/integration/... -v -tags=integration -# os: windows -# go: 1.15.x - -# push results to CodeCov -after_success: - - bash <(curl -s https://codecov.io/bash) || echo 'Codecov failed to upload' diff --git a/Makefile b/Makefile index dfd7c0e..a4a1817 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,7 @@ .DEFAULT_GOAL = all +ROOT_DIR:=$(dir $(abspath $(firstword $(MAKEFILE_LIST)))) + # enable module support across all go commands. export GO111MODULE = on # enable consistent Go 1.12/1.13 GOPROXY behavior. @@ -8,12 +10,17 @@ export GOPROXY = https://proxy.golang.org all: build-race test-unit test-integration test-lint .PHONY: all +# When running integration tests on windows machine +# it cannot execute binary without extension. +# It needs to be parametrized, so we can override it on CI. +export BINARY_PATH = $(ROOT_DIR)/codeowners-validator$(BINARY_EXT) + ############ # Building # ############ build: - go build -o codeowners-validator ./main.go + go build -o $(BINARY_PATH) ./main.go .PHONY: build build-race: @@ -29,7 +36,7 @@ test-unit: .PHONY: test-unit test-integration: build - env BINARY_PATH=$(PWD)/codeowners-validator ./hack/run-test-integration.sh + ./hack/run-test-integration.sh .PHONY: test-integration test-lint: diff --git a/hack/run-lint.sh b/hack/run-lint.sh index 86e4870..f200098 100755 --- a/hack/run-lint.sh +++ b/hack/run-lint.sh @@ -8,13 +8,20 @@ set -E # needs to be set if we want the ERR trap readonly CURRENT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) readonly ROOT_PATH=$( cd "${CURRENT_DIR}/.." && pwd ) readonly GOLANGCI_LINT_VERSION="v1.31.0" +readonly TMP_DIR=$(mktemp -d) # shellcheck source=./hack/lib/utilities.sh source "${CURRENT_DIR}/lib/utilities.sh" || { echo 'Cannot load CI utilities.'; exit 1; } host::install::golangci() { - shout "Install the golangci-lint in version ${GOLANGCI_LINT_VERSION}" - curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | bash -s -- -b "$GOPATH"/bin ${GOLANGCI_LINT_VERSION} + mkdir -p "${TMP_DIR}/bin" + export PATH="${TMP_DIR}/bin:${PATH}" + + shout "Install the golangci-lint ${GOLANGCI_LINT_VERSION} locally to a tempdir..." + curl -sfSL -o "${TMP_DIR}/golangci-lint.sh" https://install.goreleaser.com/github.com/golangci/golangci-lint.sh + chmod 700 "${TMP_DIR}/golangci-lint.sh" + + "${TMP_DIR}/golangci-lint.sh" -b "${TMP_DIR}/bin" ${GOLANGCI_LINT_VERSION} echo -e "${GREEN}√ install golangci-lint${NC}" } diff --git a/tests/integration/helpers_test.go b/tests/integration/helpers_test.go index 51aa11b..b8c29ad 100644 --- a/tests/integration/helpers_test.go +++ b/tests/integration/helpers_test.go @@ -21,7 +21,7 @@ import ( ) func normalizeTimeDurations(in string) string { - duration := regexp.MustCompile(`\([0-9]+.[0-9]+(ns|us|µs|ms|s|m|h)\)`) + duration := regexp.MustCompile(`\(\d+(\.\d+)?(ns|us|µs|ms|s|m|h)\)`) return duration.ReplaceAllString(in, "()") } diff --git a/tests/integration/integration_test.go b/tests/integration/integration_test.go index 2e06d76..8473d1e 100644 --- a/tests/integration/integration_test.go +++ b/tests/integration/integration_test.go @@ -4,6 +4,7 @@ package integration import ( "os" + "runtime" "testing" "time" @@ -28,8 +29,9 @@ const ( func TestCheckSuccess(t *testing.T) { type Envs map[string]string tests := []struct { - name string - envs Envs + name string + envs Envs + skipOS string }{ { name: "files", @@ -58,10 +60,15 @@ func TestCheckSuccess(t *testing.T) { "CHECKS": "disable-all", "EXPERIMENTAL_CHECKS": "notowned", }, + skipOS: "windows", }, } for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { + if runtime.GOOS == tc.skipOS { + t.Skip("this test is marked as skipped for this OS") + } + // given repoDir, cleanup := CloneRepo(t, codeownersSamplesRepo, "happy-path") defer cleanup()