From 4b7951756961d1756aa33c6407f08f905be6f8a6 Mon Sep 17 00:00:00 2001 From: Alexandr Yepishev Date: Mon, 14 Oct 2024 13:11:49 +0100 Subject: [PATCH] Exclude tests and mocks from capabilities SonarQube coverage --- .github/workflows/golangci_lint.yml | 31 ++++++++----------- .github/workflows/sonar-scan.yml | 46 ++++------------------------- .gitignore | 6 ++-- fail_test.go | 40 +++++++++++++++++++++++++ sonar-project.properties | 11 +++++-- 5 files changed, 71 insertions(+), 63 deletions(-) create mode 100644 fail_test.go diff --git a/.github/workflows/golangci_lint.yml b/.github/workflows/golangci_lint.yml index 2b787841e..62162fb5b 100644 --- a/.github/workflows/golangci_lint.yml +++ b/.github/workflows/golangci_lint.yml @@ -5,23 +5,18 @@ on: [pull_request] jobs: golangci-lint: runs-on: ubuntu-latest + permissions: + id-token: write + contents: read + actions: read steps: - - uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # v4.1.2 - - name: Set up Go - uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 # v5.0.0 + - name: ci-lint + uses: smartcontractkit/.github/actions/ci-lint-go@7a4d99cb349ea8f25195d2390d157942031f8a57 # ci-lint-go@0.2.4 with: - go-version-file: "go.mod" - - name: Build binary - shell: bash - run: go build ./... - - name: golangci-lint - uses: golangci/golangci-lint-action@aaa42aa0628b4ae2578232a66b541047968fac86 # v6.1.0 - with: - version: v1.60.1 - # only-new-issues is only applicable to PRs, otherwise it is always set to false - only-new-issues: true - args: --out-format colored-line-number,checkstyle:golangci-lint-report.xml - - name: Print lint report artifact - if: failure() - shell: bash - run: cat ./golangci-lint-report.xml + # grafana inputs + metrics-job-name: golangci-lint + gc-basic-auth: ${{ secrets.GRAFANA_INTERNAL_BASIC_AUTH }} + gc-host: ${{ secrets.GRAFANA_INTERNAL_HOST }} + gc-org-id: ${{ secrets.GRAFANA_INTERNAL_TENANT_ID }} + go-version-file: go.mod + \ No newline at end of file diff --git a/.github/workflows/sonar-scan.yml b/.github/workflows/sonar-scan.yml index a1c2b77fb..f9fe02392 100644 --- a/.github/workflows/sonar-scan.yml +++ b/.github/workflows/sonar-scan.yml @@ -30,46 +30,10 @@ jobs: runs-on: ubuntu-latest if: always() steps: - - name: Checkout the repo - uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4.1.4 - with: - fetch-depth: 0 # fetches all history for all tags and branches to provide more metadata for sonar reports - - - name: Download Golangci-lint report - if: always() - uses: dawidd6/action-download-artifact@bf251b5aa9c2f7eeb574a96ee720e24f801b7c11 # v6 - with: - workflow: golangci_lint.yml - workflow_conclusion: "" - name_is_regexp: true - name: golangci-lint-report - if_no_artifact_found: warn - - - name: Download Go PKG test reports - if: always() - uses: dawidd6/action-download-artifact@bf251b5aa9c2f7eeb574a96ee720e24f801b7c11 # v6 - with: - workflow: pkg.yml - workflow_conclusion: "" - name_is_regexp: true - name: go-test-results - if_no_artifact_found: warn - - - name: Set SonarQube Report Paths - if: always() - id: sonarqube_report_paths - shell: bash - run: | - echo "sonarqube_coverage_report_paths=$(find -type f -name '*coverage.out' -printf "%p,")" >> $GITHUB_OUTPUT - echo "sonarqube_golangci_report_paths=$(find -type f -name 'golangci-lint-report.xml' -printf "%p,")" >> $GITHUB_OUTPUT - - name: SonarQube Scan - if: always() - uses: sonarsource/sonarqube-scan-action@53c3e3207fe4b8d52e2f1ac9d6eb1d2506f626c0 # v2.0.2 + uses: smartcontractkit/.github/actions/ci-sonarqube-go@dca9ab89d734e82738b8aa52bd25d09b205ec6ee # v0.1.1 with: - args: > - -Dsonar.go.coverage.reportPaths=${{ steps.sonarqube_report_paths.outputs.sonarqube_coverage_report_paths }} - -Dsonar.go.golangci-lint.reportPaths=${{ steps.sonarqube_report_paths.outputs.sonarqube_golangci_report_paths }} - env: - SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} - SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }} + # sonarqube inputs + include-lint: "true" + sonar-token: ${{ secrets.SONAR_TOKEN }} + sonar-host-url: ${{ secrets.SONAR_HOST_URL }} \ No newline at end of file diff --git a/.gitignore b/.gitignore index 6d1bc4012..493ae60ed 100644 --- a/.gitignore +++ b/.gitignore @@ -19,11 +19,13 @@ **/testdata/fuzz/* # Dependency directories (remove the comment below to include it) -# vendor/ +vendor/ # IntelliJ IDE .idea -vendor/ +# Visual Studio Code +.vscode +# Generated files *.wasm diff --git a/fail_test.go b/fail_test.go new file mode 100644 index 000000000..a98e840b7 --- /dev/null +++ b/fail_test.go @@ -0,0 +1,40 @@ +package main + +import ( + "errors" + "os" + "sync" + "testing" +) + +func TestFail(t *testing.T) { + if testing.Short() { + t.Skip() + } + t.Fatal("fake failure") +} + +func TestRace(t *testing.T) { + var v int + var wg sync.WaitGroup + wg.Add(100) + for i := 0; i < 100; i++ { + go func() { + defer wg.Done() + v++ + v-- + }() + } + wg.Wait() + t.Log(v) +} + +func TestLint(t *testing.T) { + const UnusedVar = 1 // lint should complain for unused variable + const ALL_CAPS = 10 // should be AllCaps + err := os.ErrNotExist + if err == os.ErrNotExist { // should use errors.Is + err := errors.New("fake error") // shadowed variable + t.Log(err) + } +} diff --git a/sonar-project.properties b/sonar-project.properties index a29d74421..93b5ff237 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -16,14 +16,21 @@ sonar.exclusions=\ **/*report.xml,\ **/*.txt,\ **/*.abi,\ -**/*.bin +**/*.bin,\ +**/generated_*,\ +**/*_generated.go,\ +**/mock_*.go # Coverage exclusions sonar.coverage.exclusions=\ **/test/**/*,\ **/*_test.go,\ observability-lib/**,\ -**/fuzz/**/* +**/fuzz/**/*,\ +**/capabilities/consensus/ocr3/ocr3cap/ocr3captest/**/*,\ +**/capabilities/targets/chainwriter/chainwritertest/**/*,\ +**/capabilities/triggers/streams/streamstest/**/* + # Tests' root folder, inclusions (tests to check and count) and exclusions sonar.tests=.