├── go.mod ├── .github ├── dependabot.yml └── workflows │ ├── enforce-license-compliance.yml │ ├── ci.yml │ └── codeql-analysis.yml ├── .circleci ├── config.yml └── .circleci │ └── config.yml ├── calculator.go ├── LICENSE.md ├── README.md └── calculator_test.go /go.mod: -------------------------------------------------------------------------------- 1 | module example-go 2 | 3 | go 1.18 4 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: npm 4 | directory: "/" 5 | schedule: 6 | interval: daily 7 | open-pull-requests-limit: 10 8 | -------------------------------------------------------------------------------- /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | version: 2.1 2 | orbs: 3 | codecov: codecov/codecov@3 4 | 5 | jobs: 6 | build: 7 | docker: 8 | - image: cimg/go:1.18 9 | steps: 10 | - checkout 11 | - run: 12 | name: Run tests and collect coverage 13 | command: go test -race -coverprofile=coverage.txt -covermode=atomic 14 | - codecov/upload 15 | 16 | workflow: 17 | version: 2.1 18 | build-test: 19 | jobs: 20 | - build 21 | -------------------------------------------------------------------------------- /calculator.go: -------------------------------------------------------------------------------- 1 | package calculator 2 | 3 | import "errors" 4 | 5 | func Add(x, y int) (int, error) { 6 | return x + y, nil 7 | } 8 | 9 | func Subtract(x, y int) (int, error) { 10 | return x - y, nil 11 | } 12 | 13 | func Multiply(x, y int) (int, error) { 14 | return x * y, nil 15 | } 16 | 17 | func Divide(x, y int) (float64, error) { 18 | if y == 0 { 19 | return 0, errors.New("Cannot divide by 0") 20 | } 21 | return float64(x) / float64(y), nil 22 | } 23 | -------------------------------------------------------------------------------- /.github/workflows/enforce-license-compliance.yml: -------------------------------------------------------------------------------- 1 | name: Enforce License Compliance 2 | 3 | on: 4 | pull_request: 5 | branches: [main, master] 6 | 7 | jobs: 8 | enforce-license-compliance: 9 | runs-on: ubuntu-latest 10 | steps: 11 | - name: 'Enforce License Compliance' 12 | uses: getsentry/action-enforce-license-compliance@57ba820387a1a9315a46115ee276b2968da51f3d # main 13 | with: 14 | fossa_api_key: ${{ secrets.FOSSA_API_KEY }} 15 | -------------------------------------------------------------------------------- /.circleci/.circleci/config.yml: -------------------------------------------------------------------------------- 1 | version: 2.1 2 | orbs: 3 | codecov: codecov/codecov@3 4 | 5 | jobs: 6 | build: 7 | docker: 8 | - image: cimg/node:current 9 | steps: 10 | - checkout 11 | - run: 12 | name: Install dependencies 13 | command: npm install 14 | - run: 15 | name: Run tests and collect coverage 16 | command: npm run test 17 | - codecov/upload 18 | 19 | workflow: 20 | version: 2.1 21 | build-test: 22 | jobs: 23 | - build 24 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: Test and coverage 2 | 3 | on: [push, pull_request] 4 | 5 | jobs: 6 | build: 7 | runs-on: ubuntu-latest 8 | steps: 9 | - uses: actions/checkout@v4 10 | - uses: actions/setup-go@v5 11 | with: 12 | go-version: 'stable' 13 | - name: Gather dependencies 14 | run: go mod download 15 | - name: Run coverage 16 | run: go test -race -coverprofile=coverage.txt -covermode=atomic ./... 17 | - name: Upload coverage to Codecov 18 | uses: codecov/codecov-action@v5 19 | with: 20 | token: ${{ secrets.CODECOV_ORG_TOKEN }} 21 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Codecov 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # [Codecov](https://codecov.io) Go Example 2 | [![codecov](https://codecov.io/gh/codecov/example-go/branch/main/graph/badge.svg?token=tNKcOjlxLo)](https://codecov.io/gh/codecov/example-go) 3 | [![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fcodecov%2Fexample-go.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2Fcodecov%2Fexample-go?ref=badge_shield) 4 | 5 | This example repository shows how Codecov can be integrated with a simple go project. It uses **GitHub Actions** and **CircleCI** as CI/CD providers and **go** as the coverage provider. 6 | 7 | For more information, please see the links below. 8 | 9 | ## Links 10 | - [Quick Start](https://docs.codecov.com/docs/quick-start) 11 | - [GitHub Tutorial](https://docs.codecov.com/docs/github-tutorial) 12 | - [Community Boards](https://community.codecov.io) 13 | - [Support](https://codecov.io/support) 14 | - [Documentation](https://docs.codecov.io) 15 | 16 | 17 | ## License 18 | [![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fcodecov%2Fexample-go.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2Fcodecov%2Fexample-go?ref=badge_large) 19 | -------------------------------------------------------------------------------- /.github/workflows/codeql-analysis.yml: -------------------------------------------------------------------------------- 1 | # For most projects, this workflow file will not need changing; you simply need 2 | # to commit it to your repository. 3 | # 4 | # You may wish to alter this file to override the set of languages analyzed, 5 | # or to provide custom queries or build logic. 6 | # 7 | # ******** NOTE ******** 8 | # We have attempted to detect the languages in your repository. Please check 9 | # the `language` matrix defined below to confirm you have the correct set of 10 | # supported CodeQL languages. 11 | # 12 | name: "CodeQL" 13 | 14 | on: 15 | push: 16 | branches: [ master ] 17 | pull_request: 18 | # The branches below must be a subset of the branches above 19 | branches: [ master ] 20 | schedule: 21 | - cron: '17 11 * * 2' 22 | 23 | jobs: 24 | analyze: 25 | name: Analyze 26 | runs-on: ubuntu-latest 27 | permissions: 28 | actions: read 29 | contents: read 30 | security-events: write 31 | 32 | strategy: 33 | fail-fast: false 34 | matrix: 35 | language: [ 'go' ] 36 | # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ] 37 | # Learn more about CodeQL language support at https://git.io/codeql-language-support 38 | 39 | steps: 40 | - name: Checkout repository 41 | uses: actions/checkout@v2 42 | 43 | # Initializes the CodeQL tools for scanning. 44 | - name: Initialize CodeQL 45 | uses: github/codeql-action/init@v1 46 | with: 47 | languages: ${{ matrix.language }} 48 | # If you wish to specify custom queries, you can do so here or in a config file. 49 | # By default, queries listed here will override any specified in a config file. 50 | # Prefix the list here with "+" to use these queries and those in the config file. 51 | # queries: ./path/to/local/query, your-org/your-repo/queries@main 52 | 53 | # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). 54 | # If this step fails, then you should remove it and run the build manually (see below) 55 | - name: Autobuild 56 | uses: github/codeql-action/autobuild@v1 57 | 58 | # ℹ️ Command-line programs to run using the OS shell. 59 | # 📚 https://git.io/JvXDl 60 | 61 | # ✏️ If the Autobuild fails above, remove it and uncomment the following three lines 62 | # and modify them (or add more) to build your code if your project 63 | # uses a compiled language 64 | 65 | #- run: | 66 | # make bootstrap 67 | # make release 68 | 69 | - name: Perform CodeQL Analysis 70 | uses: github/codeql-action/analyze@v1 71 | -------------------------------------------------------------------------------- /calculator_test.go: -------------------------------------------------------------------------------- 1 | package calculator 2 | 3 | import "testing" 4 | 5 | func TestAdd(t *testing.T) { 6 | tables := []struct { 7 | x int 8 | y int 9 | n int 10 | e string 11 | }{ 12 | {1, 2, 3, ""}, 13 | {1.0, 2.0, 3.0, ""}, 14 | {0, 2.0, 2.0, ""}, 15 | {2.0, 0, 2.0, ""}, 16 | {-4, 2.0, -2.0, ""}, 17 | } 18 | 19 | for _, table := range tables { 20 | if total, err := Add(table.x, table.y); err != nil && err.Error() != table.e { 21 | t.Errorf("Add of (%d+%d) produced wrong error, got %v, want %v.", table.x, table.y, err.Error(), table.e) 22 | } else if total != table.n { 23 | t.Errorf("Add of (%d+%d) produced wrong result, got %d, want %d.", table.x, table.y, total, table.n) 24 | } 25 | } 26 | } 27 | 28 | func TestSubtract(t *testing.T) { 29 | tables := []struct { 30 | x int 31 | y int 32 | n int 33 | e string 34 | }{ 35 | {1, 2, -1.0, ""}, 36 | {2, 1, 1.0, ""}, 37 | {1.0, 2.0, -1.0, ""}, 38 | {0, 2.0, -2.0, ""}, 39 | {2.0, 0, 2.0, ""}, 40 | {-4, 2.0, -6.0, ""}, 41 | } 42 | 43 | for _, table := range tables { 44 | if total, err := Subtract(table.x, table.y); err != nil && err.Error() != table.e { 45 | t.Errorf("Subtract of (%d-%d) produced wrong error, got %v, want %v.", table.x, table.y, err.Error(), table.e) 46 | } else if total != table.n { 47 | t.Errorf("Subtract of (%d-%d) produced wrong result, got %d, want %d.", table.x, table.y, total, table.n) 48 | } 49 | } 50 | } 51 | 52 | func TestMultiply(t *testing.T) { 53 | tables := []struct { 54 | x int 55 | y int 56 | n int 57 | e string 58 | }{ 59 | {1, 2, 2.0, ""}, 60 | {1.0, 2.0, 2.0, ""}, 61 | {0, 2.0, 0, ""}, 62 | {2.0, 0, 0, ""}, 63 | {-4, 2.0, -8.0, ""}, 64 | } 65 | 66 | for _, table := range tables { 67 | if total, err := Multiply(table.x, table.y); err != nil && err.Error() != table.e { 68 | t.Errorf("Multiply of (%d*%d) produced wrong error, got %v, want %v.", table.x, table.y, err.Error(), table.e) 69 | } else if total != table.n { 70 | t.Errorf("Multiply of (%d*%d) produced wrong result, got %d, want %d.", table.x, table.y, total, table.n) 71 | } 72 | } 73 | } 74 | 75 | func TestDivide(t *testing.T) { 76 | tables := []struct { 77 | x int 78 | y int 79 | n float64 80 | e string 81 | }{ 82 | {1, 2, 0.5, ""}, 83 | {1.0, 2.0, 0.5, ""}, 84 | {0, 2.0, 0, ""}, 85 | {-4, 2.0, -2.0, ""}, 86 | // {2.0, 0, 0, "Cannot divide by 0"}, 87 | } 88 | 89 | for _, table := range tables { 90 | if total, err := Divide(table.x, table.y); err != nil && err.Error() != table.e { 91 | t.Errorf("Divide of (%d/%d) produced wrong error, got %v, want %v.", table.x, table.y, err.Error(), table.e) 92 | } else if total != table.n { 93 | t.Errorf("Divide of (%d/%d) produced wrong result, got %f, want %f.", table.x, table.y, total, table.n) 94 | } 95 | } 96 | } 97 | --------------------------------------------------------------------------------