From f58362c81c5b59a74978f6c9da37e65695b10ac5 Mon Sep 17 00:00:00 2001 From: Hosh Date: Mon, 2 Jan 2023 00:46:44 +0000 Subject: [PATCH] Fix CI issues (#1) --- .github/labeler.yml | 1 - .github/workflows/labeler.yml | 1 - .github/workflows/site.yml | 18 ++-- .github/workflows/test.yml | 184 +++++++++++++++++++------------- .pre-commit-config.yaml | 40 +++++++ Makefile | 6 +- bash_completions_test.go | 4 +- command_test.go | 13 ++- fish_completions_test.go | 4 +- helpers_notwin_test.go | 10 ++ helpers_win_test.go | 14 +++ site/content/getting_started.md | 4 +- 12 files changed, 198 insertions(+), 101 deletions(-) create mode 100644 .pre-commit-config.yaml create mode 100644 helpers_notwin_test.go create mode 100644 helpers_win_test.go diff --git a/.github/labeler.yml b/.github/labeler.yml index 23d9e1e..c93dca1 100644 --- a/.github/labeler.yml +++ b/.github/labeler.yml @@ -13,4 +13,3 @@ # changes to shell completions "area/*sh completion": - ./*completions* - diff --git a/.github/workflows/labeler.yml b/.github/workflows/labeler.yml index ce4fc1f..e0cebbe 100644 --- a/.github/workflows/labeler.yml +++ b/.github/workflows/labeler.yml @@ -9,4 +9,3 @@ jobs: - uses: actions/labeler@v3 with: repo-token: "${{ github.token }}" - diff --git a/.github/workflows/site.yml b/.github/workflows/site.yml index f517d78..dc1cc65 100644 --- a/.github/workflows/site.yml +++ b/.github/workflows/site.yml @@ -2,14 +2,20 @@ name: 'site' on: push: + paths: + - 'site/**' + - '.github/workflows/site.yml' pull_request: + paths: + - 'site/**' + - '.github/workflows/site.yml' jobs: doc: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Get Hugo and Theme run: | @@ -26,21 +32,21 @@ jobs: run: | cd site hugo version - hugo - - uses: actions/upload-artifact@v2 + hugo --gc --minify + - uses: actions/upload-artifact@v3 with: name: site path: site/public - name: Publish site to gh-pages - if: github.event_name != 'pull_request' + if: success() && github.event_name != 'pull_request' && github.ref == 'refs/heads/main' run: | cd site/public/ touch .nojekyll git init cp ../../.git/config ./.git/config git add . - git config --local user.email "push@gha" - git config --local user.name "GHA" + git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com" + git config --local user.name "github-actions[bot]" git commit -a -m "update ${{ github.sha }}" git push -u origin +HEAD:gh-pages diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 4aaeff3..b55030c 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -8,89 +8,119 @@ env: GO111MODULE: on jobs: - - golangci-lint: + DetermineVersion: runs-on: ubuntu-latest steps: - - - uses: actions/setup-go@v2 + - name: Checkout code + uses: actions/checkout@v3 + - id: go-version-matrix + run: | + go_version="$(awk '/^go/{print $2}' go.mod)" + echo "go_version=${go_version}.x" >> $GITHUB_OUTPUT + + matrix_versions="$(git ls-remote --tags https://github.com/golang/go | + awk -F/ '$3 ~ /^go([0-9]+(\.[0-9]+)$)/{gsub(/^go/, "", $3); print $3}' | + sort -V | + sed -ne "/^$go_version$/,$ p" | + jq -cMnR '[inputs | select(length>0)] | map(. + ".x") + ["tip"]')" + echo "matrix=$matrix_versions" >> $GITHUB_OUTPUT + + golangci_version="$(yq '.repos[] | select(.repo | contains("golangci-lint")) | .rev' .pre-commit-config.yaml)" + echo "golangci_version=$golangci_version" >> $GITHUB_OUTPUT + outputs: + matrix: ${{ steps.go-version-matrix.outputs.matrix }} + go_version: ${{ steps.go-version-matrix.outputs.go_version }} + golangci_version: ${{ steps.go-version-matrix.outputs.golangci_version }} + + Pre-Commit: + needs: DetermineVersion + runs-on: ubuntu-latest + steps: + - uses: actions/setup-python@v3 + - name: Setup go + run: curl -sL https://raw.githubusercontent.com/maxatome/install-go/v3.3/install-go.pl | perl - ${{ needs.DetermineVersion.outputs.go_version }} $HOME + - uses: actions/checkout@v3 + - uses: actions/cache@v3 with: - go-version: '1.17' - - - uses: actions/checkout@v2 - - - uses: golangci/golangci-lint-action@v2 + path: | + ~/.cache/go-build + ~/Library/Caches/go-build + ~/AppData/Local/go-build + ~/go/pkg/mod + key: ${{ runner.os }}-go-${{ needs.DetermineVersion.outputs.go_version }}-${{ hashFiles('**/go.sum') }} + restore-keys: go-mod + - name: Setup mdtoc + run: go install sigs.k8s.io/mdtoc@latest + - uses: pre-commit/action@v3.0.0 + env: + SKIP: golangci-lint,go-build,go-unit-tests + + GolangCI-Lint: + needs: DetermineVersion + runs-on: ubuntu-latest + steps: + - name: Setup go + run: curl -sL https://raw.githubusercontent.com/maxatome/install-go/v3.3/install-go.pl | perl - ${{ needs.DetermineVersion.outputs.go_version }} $HOME + - name: Checkout code + uses: actions/checkout@v3 + - uses: actions/cache@v3 + with: + path: | + ~/.cache/go-build + ~/Library/Caches/go-build + ~/AppData/Local/go-build + ~/go/pkg/mod + key: ${{ runner.os }}-go-${{ needs.DetermineVersion.outputs.go_version }}-${{ hashFiles('**/go.sum') }} + restore-keys: go-mod + - name: Run linters + uses: golangci/golangci-lint-action@v3 with: - version: latest - args: --verbose + version: ${{ needs.DetermineVersion.outputs.golangci_version }} - test-unix: + Vet: + needs: DetermineVersion + runs-on: ubuntu-latest + steps: + - name: Setup go + run: curl -sL https://raw.githubusercontent.com/maxatome/install-go/v3.3/install-go.pl | perl - ${{ needs.DetermineVersion.outputs.go_version }} $HOME + - name: Checkout code + uses: actions/checkout@v3 + - uses: actions/cache@v3 + with: + path: | + ~/.cache/go-build + ~/Library/Caches/go-build + ~/AppData/Local/go-build + ~/go/pkg/mod + key: ${{ runner.os }}-go-${{ needs.DetermineVersion.outputs.go_version }}-${{ hashFiles('**/go.sum') }} + restore-keys: go-mod + - name: Run vet + run: go vet + + Test: + needs: DetermineVersion strategy: fail-fast: false matrix: - platform: - - ubuntu - - macOS - go: - - 16 - - 17 - - 18 - - 19 - name: '${{ matrix.platform }} | 1.${{ matrix.go }}.x' - runs-on: ${{ matrix.platform }}-latest + go: ${{ fromJson(needs.DetermineVersion.outputs.matrix) }} + os: [ ubuntu-latest, macOS-latest, windows-latest ] + runs-on: ${{ matrix.os }} + name: ${{ matrix.os }} Go ${{ matrix.go }} Tests steps: - - - uses: actions/setup-go@v2 - with: - go-version: 1.${{ matrix.go }}.x - - - uses: actions/checkout@v2 - - - uses: actions/cache@v2 - with: - path: ~/go/pkg/mod - key: ${{ runner.os }}-1.${{ matrix.go }}.x-${{ hashFiles('**/go.sum') }} - restore-keys: ${{ runner.os }}-1.${{ matrix.go }}.x- - - - run: | - export GOBIN=$HOME/go/bin - case "${{ matrix.go }}" in - 16|17) _version='@latest';; - *) _version='';; - esac - go install github.com/kyoh86/richgo"${_version}" - go install github.com/mitchellh/gox"${_version}" - - test-win: - name: MINGW64 - defaults: - run: - shell: msys2 {0} - runs-on: windows-latest - steps: - - - shell: bash - run: git config --global core.autocrlf input - - - uses: msys2/setup-msys2@v2 - with: - msystem: MINGW64 - update: true - install: > - git - make - unzip - mingw-w64-x86_64-go - - - uses: actions/checkout@v2 - - - uses: actions/cache@v2 - with: - path: ~/go/pkg/mod - key: ${{ runner.os }}-${{ matrix.go }}-${{ hashFiles('**/go.sum') }} - restore-keys: ${{ runner.os }}-${{ matrix.go }}- - - - run: | - export GOBIN=$HOME/go/bin - go install github.com/kyoh86/richgo@latest - go install github.com/mitchellh/gox@latest + - name: Setup go + shell: bash + run: curl -sL https://raw.githubusercontent.com/maxatome/install-go/v3.3/install-go.pl | perl - ${{ matrix.go }} $HOME + - name: Checkout code + uses: actions/checkout@v3 + - uses: actions/cache@v3 + with: + path: | + ~/.cache/go-build + ~/Library/Caches/go-build + ~/AppData/Local/go-build + ~/go/pkg/mod + key: ${{ runner.os }}-go-${{ matrix.go }}-${{ hashFiles('**/go.sum') }} + restore-keys: go-mod + - name: Run tests + shell: bash + run: go test -v -cover -race diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..4cd70f4 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,40 @@ +minimum_pre_commit_version: '2.9.3' + +repos: + - repo: https://github.com/pre-commit/pre-commit + rev: v2.17.0 + hooks: + - id: validate_manifest + + - repo: https://github.com/golangci/golangci-lint + rev: v1.47.0 + hooks: + - id: golangci-lint + + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.1.0 + hooks: + - id: check-merge-conflict + stages: [commit] + - id: check-symlinks + stages: [commit] + - id: check-yaml + stages: [commit] + - id: end-of-file-fixer + stages: [commit] + - id: trailing-whitespace + stages: [commit] + + - repo: local + hooks: + - name: Go Tests + id: go-tests + language: system + files: '\.go$' + pass_filenames: false + entry: go test -timeout 30s -short -v ./... + - name: Go Mod Tidy + id: go-mod-tidy + language: system + pass_filenames: false + entry: go mod tidy diff --git a/Makefile b/Makefile index 443ef1a..430fe04 100644 --- a/Makefile +++ b/Makefile @@ -5,10 +5,6 @@ ifeq (, $(shell which golangci-lint)) $(warning "could not find golangci-lint in $(PATH), run: curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh") endif -ifeq (, $(shell which richgo)) -$(warning "could not find richgo in $(PATH), run: go get github.com/kyoh86/richgo") -endif - .PHONY: fmt lint test install_deps clean default: all @@ -25,7 +21,7 @@ lint: test: install_deps $(info ******************** running tests ********************) - richgo test -v ./... + go test -v ./... install_deps: $(info ******************** downloading dependencies ********************) diff --git a/bash_completions_test.go b/bash_completions_test.go index 3edfbe0..3af5f90 100644 --- a/bash_completions_test.go +++ b/bash_completions_test.go @@ -115,7 +115,7 @@ func TestFailGenBashCompletionFile(t *testing.T) { t.Error("should raise permission denied error") } - if got.Error() != "open ./tmp/test: permission denied" { - t.Errorf("got: %s, want: %s", got.Error(), "open ./tmp/test: permission denied") + if got.Error() != expectedPermissionError { + t.Errorf("got: %s, want: %s", got.Error(), expectedPermissionError) } } diff --git a/command_test.go b/command_test.go index 1abeadf..4426a2e 100644 --- a/command_test.go +++ b/command_test.go @@ -928,8 +928,8 @@ Global Flags: --bar parent bar usage ` - if got != expected { - t.Errorf("Help text mismatch.\nExpected:\n%s\n\nGot:\n%s\n", expected, got) + if rmCarriageRet(got) != expected { + t.Errorf("Help text mismatch.\nExpected:\n%q\n\nGot:\n%q\n", expected, got) } } @@ -1717,6 +1717,7 @@ func TestUsageWithGroup(t *testing.T) { t.Errorf("Unexpected error: %v", err) } + output = rmCarriageRet(output) // help should be ungrouped here checkStringContains(t, output, "\nAvailable Commands:\n help") checkStringContains(t, output, "\ngroup1\n cmd1") @@ -1734,6 +1735,7 @@ func TestUsageHelpGroup(t *testing.T) { t.Errorf("Unexpected error: %v", err) } + output = rmCarriageRet(output) // now help should be grouped under "group" checkStringOmits(t, output, "\nAvailable Commands:\n help") checkStringContains(t, output, "\nAvailable Commands:\n\ngroup\n help") @@ -1750,6 +1752,7 @@ func TestAddGroup(t *testing.T) { t.Errorf("Unexpected error: %v", err) } + output = rmCarriageRet(output) checkStringContains(t, output, "\nTest group\n cmd") } @@ -1865,11 +1868,11 @@ func TestFlagErrorFuncHelp(t *testing.T) { out, err := executeCommand(c, "--help") assertNoErr(t, err) - assertEqual(t, expected, out) + assertEqual(t, expected, rmCarriageRet(out)) out, err = executeCommand(c, "-h") assertNoErr(t, err) - assertEqual(t, expected, out) + assertEqual(t, expected, rmCarriageRet(out)) } // TestSortedFlags checks, @@ -2541,7 +2544,7 @@ Use "root child [command] --help" for more information about a command. t.FailNow() } - output := buf.String() + output := rmCarriageRet(buf.String()) if output != test.expectedUsage { t.Errorf("Expecting: \n %q\nGot:\n %q\n", test.expectedUsage, output) } diff --git a/fish_completions_test.go b/fish_completions_test.go index 1c2cc09..4158a06 100644 --- a/fish_completions_test.go +++ b/fish_completions_test.go @@ -115,7 +115,7 @@ func TestFailGenFishCompletionFile(t *testing.T) { t.Error("should raise permission denied error") } - if got.Error() != "open ./tmp/test: permission denied" { - t.Errorf("got: %s, want: %s", got.Error(), "open ./tmp/test: permission denied") + if got.Error() != expectedPermissionError { + t.Errorf("got: %s, want: %s", got.Error(), expectedPermissionError) } } diff --git a/helpers_notwin_test.go b/helpers_notwin_test.go new file mode 100644 index 0000000..a537cd8 --- /dev/null +++ b/helpers_notwin_test.go @@ -0,0 +1,10 @@ +//go:build !windows +// +build !windows + +package zulu_test + +const expectedPermissionError = "open ./tmp/test: permission denied" + +func rmCarriageRet(subject string) string { + return subject +} diff --git a/helpers_win_test.go b/helpers_win_test.go new file mode 100644 index 0000000..95b5b0e --- /dev/null +++ b/helpers_win_test.go @@ -0,0 +1,14 @@ +//go:build windows +// +build windows + +package zulu_test + +import ( + "strings" +) + +const expectedPermissionError = "open ./tmp/test: Access is denied." + +func rmCarriageRet(subject string) string { + return strings.ReplaceAll(subject, "\r\n", "\n") +} diff --git a/site/content/getting_started.md b/site/content/getting_started.md index 5bb70d7..1d106ee 100644 --- a/site/content/getting_started.md +++ b/site/content/getting_started.md @@ -96,7 +96,7 @@ to quickly create a Zulu application.`, cmd.AddCommand(addCmd) cmd.AddCommand(initCmd) - + return nil }, } @@ -308,7 +308,7 @@ a count and a string.`, for i := 0; i < echoTimes; i++ { fmt.Println("Echo: " + strings.Join(args, " ")) } - + return nil }, }