Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Heroku CI rails app test coverage #226

Open
a-b opened this issue Sep 19, 2017 · 21 comments
Open

Heroku CI rails app test coverage #226

a-b opened this issue Sep 19, 2017 · 21 comments

Comments

@a-b
Copy link

a-b commented Sep 19, 2017

Please include Heroku CI specific manual, how to run test coverage for rails app.
Inspired by codeclimate/ruby-test-reporter#183

@toddmazierski
Copy link
Contributor

Thanks, @a-b. We're still working on getting the required environment variables from Heroku CI as mentioned in this comment. Please stand by!

@brynary
Copy link
Member

brynary commented Oct 3, 2017

Update: Heroku now supports a HEROKU_TEST_RUN_COMMIT_VERSION env variable. I think we can get this working with that.

(codeclimate/ruby-test-reporter#183 (comment))

@efueger
Copy link
Member

efueger commented Oct 4, 2017

Cross-posting this comment:

@dblandin mentioned: Hey @joshwlewis,

Thank you for the update! Out of those three, HEROKU_TEST_RUN_COMMITTED_AT_TIMESTAMP would definitely be the most important as it's a required value, either from the git repo itself (which is unavailable during Heroku CI runs) or the environment.

If we had that value available, we could address and close this issue easily. Thanks!

@dblandin
Copy link
Contributor

dblandin commented Oct 4, 2017

Thanks @efueger!

@brynary I think we're still blocked until we have the git committed at value available.

@dblandin
Copy link
Contributor

dblandin commented Oct 9, 2017

Hey @a-b,

We've discussed this internally and while we're waiting on Heroku to support this environment variable via Heroku CI, you can use the following (not entirely recommended) workaround:

$ cc-test-reporter before-build
$ GIT_COMMITTED_AT="$(date +%s)" cc-test-reporter after-build

This fakes the git commit "committed at" data point with the current time.

Note that this might produce undesirable behavior when rendering test coverage information on codeclimate.com or annotations on github.com (via the Code Climate browser extension) where we depend upon accurate committed at data, but that should be fairly uncommon.

@dja
Copy link

dja commented Oct 25, 2017

Is there a recommended way to install/setup cc-test-reporter for Heroku, and give it all the env variables we can until they expose a better GIT_COMMITTED_AT variable?

@dblandin
Copy link
Contributor

Hey @dja, you can use the workaround posted in #226 (comment). Just be aware that there might be some weirdness rendering data if the generated data and commited_at date are significantly different.

@dja
Copy link

dja commented Nov 20, 2017

I have this almost fully working, except getting this issue when passing the workaround GIT_COMMITTED_AT="$(date +%s)": Error: strconv.Atoi: parsing "$(date +%s)": invalid syntax

The date as formatted is 1511140351

I'm setting variables as so:

GIT_BRANCH=HEROKU_TEST_RUN_BRANCH
GIT_COMMIT_SHA=HEROKU_TEST_RUN_COMMIT_VERSION
CI_NAME='HEROKU'
CI_BUILD_ID=HEROKU_TEST_RUN_ID
GIT_COMMITTED_AT='$(date +%s)'

@dblandin
Copy link
Contributor

Hey @dja,

It looks like your subshell value isn't excuting correctly. I think that's because you're wrapping the subshell command in single quotes which doesn't trigger interpolation.

I'm also wondering if you're GIT_BRANCH and GIT_COMMIT_SHA values are correct as they're missing the leading $.

Could you try the following?

export CI_BUILD_ID="$HEROKU_TEST_RUN_ID"
export CI_NAME="heroku"
export GIT_BRANCH="$HEROKU_TEST_RUN_BRANCH"
export GIT_COMMIT_SHA="$HEROKU_TEST_RUN_COMMIT_VERSION"
export GIT_COMMITTED_AT="$(date +%s)"

@dja
Copy link

dja commented Nov 20, 2017

That helped, but getting this error:

== Run Code Climate after-build ==
fatal: Not a git repository (or any parent up to mount point /app)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
Error: exit status 128

@TaylorBriggs
Copy link

As far as I can tell, this setup is working on Heroku CI. We have an rspec test suite with simplecov and a mocha/chai/sinon/enzyme test suite run by karma and reporting via istanbul.

app.json:

{
  "environments": {
    "test": {
      "addons": [
        "heroku-postgresql"
      ],
      "buildpacks": [
        { "url": "heroku/nodejs" },
        { "url": "https://github.com/heroku/heroku-buildpack-google-chrome" },
        { "url": "heroku/ruby" }
      ],
      "env": {
        "NODE_ENV": "test",
        "RAILS_ENV": "test"
      },
      "scripts": {
        "test-setup": "./script/ci_setup",
        "test": "./script/ci"
      }
    }
  }
}

script/ci_setup:

#!/bin/bash

# download the codeclimate test reporter
curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
chmod +x ./cc-test-reporter

./cc-test-reporter before-build

script/ci:

#!/bin/bash

export CI_BUILD_ID="$HEROKU_TEST_RUN_ID"
export CI_NAME="heroku"
export GIT_BRANCH="$HEROKU_TEST_RUN_BRANCH"
export GIT_COMMIT_SHA="$HEROKU_TEST_RUN_COMMIT_VERSION"
export GIT_COMMITTED_AT="$(date +%s)"

ruby_filename="coverage/codeclimate.rb.json"
js_filename="coverage/codeclimate.js.json"

# run the ruby test suite
bundle exec rake spec
# format ruby coverage
./cc-test-reporter format-coverage --output $ruby_filename

# run the JS test suite
yarn test
# format js coverage
./cc-test-reporter format-coverage --input-type lcov --output $js_filename coverage/karma/lcov.info

# sum code coverage and send to codeclimate
./cc-test-reporter sum-coverage $ruby_filename $js_filename
./cc-test-reporter upload-coverage

P.S. 👋 @dblandin

dblandin added a commit that referenced this issue Feb 19, 2018
Heroku CI currently exposes the following environment variables during
CI builds:

- `HEROKU_TEST_RUN_BRANCH`: A string representing the branch of the commit under test
- `HEROKU_TEST_RUN_COMMIT_VERSION`: A string representing the commit version under test (This is the SHA in most cases)
- `HEROKU_TEST_RUN_ID`: A string uuid representing the unique ID of the test run

source: https://devcenter.heroku.com/articles/heroku-ci#configuration-using-app-json

The Code Climate test reporter relies on a git committed at timestamp to
reliably render accurate test coverage annotations on codeclimate.com
and on github.com via the browser extension.

For now, we can automatically apply the environment variables supported
by Heroku CI, but a couple others will have to be manually supplied by
the user:

```
export CI_NAME="heroku"
export GIT_COMMITTED_AT="$(date +%s)"
```

Related to: #226
dblandin added a commit that referenced this issue Feb 19, 2018
Heroku CI currently exposes the following environment variables during
CI builds:

- `HEROKU_TEST_RUN_BRANCH`: A string representing the branch of the commit under test
- `HEROKU_TEST_RUN_COMMIT_VERSION`: A string representing the commit version under test (This is the SHA in most cases)
- `HEROKU_TEST_RUN_ID`: A string uuid representing the unique ID of the test run

source: https://devcenter.heroku.com/articles/heroku-ci#configuration-using-app-json

The Code Climate test reporter relies on a git committed at timestamp to
reliably render accurate test coverage annotations on codeclimate.com
and on github.com via the browser extension.

For now, we can automatically apply the environment variables supported
by Heroku CI, but a couple others will have to be manually supplied by
the user:

```
export CI_NAME="heroku"
export GIT_COMMITTED_AT="$(date +%s)"
```

Related to: #226
@dblandin
Copy link
Contributor

Hey @TaylorBriggs!! 👋

I just merged and released #305 which brings in support for the environment variables currently available during Heroku CI builds.

Not all the way there yet, but at least you can remove a few of those export lines now 😄

@joshwlewis Any update on support for the CI_NAME and/or GIT_COMMITTED_AT environment variables?

@TaylorBriggs
Copy link

Thanks @dblandin!

@niclas-ahden
Copy link

niclas-ahden commented Sep 14, 2018

@joshwlewis @appleton We just switched to HerokuCI and noticed the incomplete support for Code Climate. Any update on the CI_NAME and GIT_COMMITTED_AT environment variables?

Details in this issue and:
codeclimate/ruby-test-reporter#183 (comment)
#305

Thanks!

@sergiopantoja
Copy link

@TaylorBriggs's solution worked perfectly for code coverage, but we found that Heroku CI wouldn't report failing tests with that. Not sure if it's our RSpec config, but just want to give a heads up so others can make sure their test failures are being reported properly.

Will send another update if we figure it out.

@TaylorBriggs
Copy link

@sergiopantoja I ran into the issue with Heroku CI not reporting the test failures. I updated my script as follows to catch the error code and exit the script:

#!/bin/bash

exit_if_error() {
  if [ $1 -ne 0 ]; then
    exit 1
  fi
}

export CI_NAME="heroku"
export GIT_COMMITTED_AT="$(date +%s)"

ruby_filename="coverage/codeclimate.rb.json"
js_filename="coverage/codeclimate.js.json"

# lint the ruby code
bin/rubocop
exit_if_error $?

# ruby test suite
bin/rake spec
exit_if_error $?

./cc-test-reporter format-coverage --output $ruby_filename

# JS linting and test suite
yarn test
exit_if_error $?

./cc-test-reporter format-coverage --input-type lcov --output $js_filename coverage/karma/lcov.info

# sum code coverage and send to codeclimate
./cc-test-reporter sum-coverage $ruby_filename $js_filename
./cc-test-reporter upload-coverage

@TaylorBriggs
Copy link

I haven't been working on that project since March, but it was working as of then. Maybe Heroku CI has changed since so you may need to adjust accordingly.

@wfleming
Copy link
Contributor

wfleming commented Oct 1, 2018

I'd suggest considering adding set -e to the top of the bash script as an alternative to exit_if_error. set -e will exit the script if any command exits non-zero. AFAICT the only important commands you're not explicitly checking are the test-reporter invocations. Personally I think I'd want my CI to error if those were erroring to catch potential misconfigurations, but if you're alright with those commands erroring you could use set +x to put the shell back in a mode where erroring commands don't abort the script, which would still be a bit less code to write.

e.g.

#!/bin/bash
set -e

# do stuff that must exit 0

set +x

# do stuff that is allowed to exit non-zero without failing CI

@sergiopantoja
Copy link

sergiopantoja commented Oct 1, 2018

@TaylorBriggs @wfleming Man you guys are great! Thanks so much for the quick response and help. 🙏

@rosscooperman
Copy link

@sergiopantoja you can also pass an exit status to the reporter like cc-test-reporter --exit-code $EXIT_CODE. So, something like:

./run-tests.sh
RETURN_VALUE=$?
 ./cc-test-reporter after-build --exit-code $RETURN_VALUE

or what we do (since ^^ seems unecessary)

#!/bin/bash

export CI_NAME="heroku"
export GIT_COMMITTED_AT="$(date +%s)"

bin/rspec
RETURN_VALUE=$?
./cc-test-reporter after-build
exit $RETURN_VALUE

The downside of set -e is that you won't get any coverage measured if there are test failures which may not be what you want (though some test runners don't run coverage when there are failures anyway).

@fedegl
Copy link

fedegl commented Feb 24, 2021

Any updates on how to setup up CodeClimate reporting on Heroku CI? I am having some of the same issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests