vladopajic / go-test-coverage

go-test-coverage is tool and github action which reports issues when test coverage is below set threshold
GNU General Public License v3.0
78 stars 9 forks source link

feat: Github Actions - set working directory in step #66

Open pixo-wnobrien opened 6 months ago

pixo-wnobrien commented 6 months ago

I have a repo with a couple different projects in it, and I would like to set up a workflow that tests all the projects on a PR. It would be great if I could set the working directory in the step like below:

      - name: Check Test Coverage
        uses: vladopajic/go-test-coverage@v2
        with:
          working-directory: app1
          config: .coverage.yaml

      - name: Check Test Coverage
        uses: vladopajic/go-test-coverage@v2
        with:
          working-directory: app2
          config: .coverage.yaml

I usually use golangci-lint in the same pipeline before I run my tests, and the Github Action for that works in this way

      - name: Lint
        uses: golangci/golangci-lint-action@v3
        with:
          version: v1.54
          working-directory: ./app1

I've tried many different workarounds, but I'm having a hard time getting it to work in my repo with multiple projects without this feature. If there are any suggestions that would be greatly appreciated! I put my workflow file below for reference

jobs:
  test-app1:
    runs-on: ubuntu-latest

    defaults:
      run:
        working-directory: ./app1

    steps:
      - uses: actions/checkout@v3

      - name: Setup Go
        uses: actions/setup-go@v4
        with:
          go-version: '1.21'

      - name: Lint
        uses: golangci/golangci-lint-action@v3
        with:
          version: v1.54
          install-mode: "goinstall"
          args: --timeout=30m
          working-directory: ./app1

      - name: Run Tests
        run: make test

      - name: Check Test Coverage
        uses: vladopajic/go-test-coverage@v2
        with:
          config: app1/.coverage.yaml

  test-app2:
    runs-on: ubuntu-latest

    defaults:
      run:
        working-directory: ./app2

    steps:
      - uses: actions/checkout@v3

      - name: Setup Go
        uses: actions/setup-go@v4
        with:
          go-version: '1.21'

      - name: Lint
        uses: golangci/golangci-lint-action@v3
        with:
          version: v1.54
          install-mode: "goinstall"
          args: --timeout=30m
          working-directory: ./app2

      - name: Run Tests
        run: make test

      - name: Check Test Coverage
        uses: vladopajic/go-test-coverage@v2
        with:
          config: app2/.coverage.yaml

permissions:
  contents: read

I can configure the step to use the corresponding cover.out file, but since the step is not running in the directory that the tests were run in I get the following error:

failed to generate coverage statistics: could not find file [github.com/org/repo/app1/docs/docs.go]: can't find "docs.go": no required module provides package github.com/org/repo/app1/docs: go.mod file not found in current directory or any parent directory; see 'go help modules'
vladopajic commented 5 months ago

@pixo-wnobrien hey, sorry for late response. could you read this https://github.com/vladopajic/go-test-coverage/issues/55, and try two options that have been explained here 1) use v2.8.1 version 2) set local prefix for both apps (github.com/org/repo/app1 and github.com/org/repo/app2)

please let me know if this has resolved your issue.

snicol commented 4 months ago

I'm having a similar issue, with a monorepo containing a single Go module nested a few directories inside. I think the issue is similar to in @pixo-wnobrien's case, github.com/org/repo/app1 and github.com/org/repo/app2 likely have their own separate go.mod files and aren't discoverable, neither is there a go.work file present.

Setting local-prefixes also doesn't seem to solve the issue in my case as the relative path to the module and the module name is not 1:1.

vladopajic commented 4 months ago

hello gents,

i have started to work on feature that will allow user to set source directory. this feature should allow use of this tool in monorepos. could you please test it out and give me insights?

to test it you can use 028b5bedcf5d71e9e59fcc5ca7219ec772197382 version.

in ci:

- name: check test coverage (app 1)
  uses: vladopajic/go-test-coverage@028b5bedcf5d71e9e59fcc5ca7219ec772197382
  with:
    config: ./.testcoverage_app1.yml
     source-dir: ./app1

- name: check test coverage (app 2)
  uses: vladopajic/go-test-coverage@028b5bedcf5d71e9e59fcc5ca7219ec772197382
  with:
    config: ./.testcoverage_app2.yml
     source-dir: ./app2

locally:

go install github.com/vladopajic/go-test-coverage/v2@028b5bedcf5d71e9e59fcc5ca7219ec772197382
go-test-coverage -config=./.testcoverage_app1.yml -source-dir=app1
shettyh commented 4 months ago

@vladopajic tried with the commit provided, it still doesnt work after providing source-dir also

go.mod file not found in current directory or any parent directory
snicol commented 4 months ago

Same error here, have altered the full output to make it clearer (new lines my own):

failed to generate coverage statistics: could not find file [github.com/org/repo/subdir/module/package/example.go]: 
can't find file "github.com/org/repo/subdir/module/package/example.go": no required module provides package github.com/org/repo/subdir/module/package: 
go.mod file not found in current directory or any parent directory; see 'go help modules'

In the above, the go.mod lives in github.com/org/repo/subdir/module, and the coverage stats generation fails when analysing the cover output of a sub-package of the module (github.com/org/repo/subdir/module/package).

vladopajic commented 4 months ago

hello gents,

not sure how do you actually want to setup you repositories. it would require me to see in the details your project setup to understand what is happening.

please feel free to this example https://github.com/vladopajic/monorepo-covrage where coverage check is configured to work with monorepo. would something like this work for you?

krhubert commented 2 weeks ago

@vladopajic The in monorepo-covrage lacks the github-action setup. Please try to set github actions in this repo for both projects and you will see the problem that issue describes.

jobs:
  backend-unit-test:
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: proja
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-go@v4

      - name: run tests
        run: go test -v -cover -coverprofile=test.cover.out ./...

      - name: check test coverage
        # run go-test-coverage as a go command - this is a workaround I use today
        # to make sure go-test-coverage is executed inside proja dir
        run: go run github.com/vladopajic/go-test-coverage/v2 -c .testcoverage.yml

        # ideal solution would be to pass a working-directory and go-test-coverage
        # change the dir and execute the cover change there. 
        uses: vladopajic/go-test-coverage@v2
        with:
          config: .testcoverage.yml
          working-directory: proja