h2non / gock

HTTP traffic mocking and testing made easy in Go ΰΌΌΚ˜ΜšΩ„ΝœΚ˜ΜšΰΌ½
https://pkg.go.dev/github.com/h2non/gock
MIT License
2.04k stars 106 forks source link

My tests fail with GitHub Actions: mocks seem to be ignored 🀷 #98

Closed nicokosi closed 2 years ago

nicokosi commented 2 years ago

I cannot make tests pass in GitHub Actions for this pull request (see this comment): mocks seem OK (tests pass locally) but fail in GitHub Actions.

Any hint/tips? 😊

nicokosi commented 2 years ago

I did some debugging, I now understand that this is a test environment issue. The go-gh REST client library visibly needs some configuration:

RESTClient builds a client to send requests to GitHub REST API endpoints. As part of the configuration a hostname, auth token, and default set of headers are resolved from the gh environment configuration. These behaviors can be overridden using the opts argument.

See https://github.com/cli/go-gh/blob/c41a1271d97cf42ba0dcf94289da729cdb1554eb/gh.go#L59

Therefore, I close this issue.

PS: tests succeed locally because I have configured the GitHub CLI authentication:

$ gh auth status
github.com
  βœ“ Logged in to github.com as nicokosi (GITHUB_TOKEN)
  βœ“ Git operations for github.com configured to use ssh protocol.
  βœ“ Token: *******************

Which is used by the GitHub REST Client:

$ go test
GET /repos/acme/buzz/readme HTTP/1.1
Host: api.github.com
User-Agent: go-gh v0.0.3
Accept: application/vnd.github.merge-info-preview+json, application/vnd.github.nebula-preview, application/vnd.github.antiope-preview, application/vnd.github.shadow-cat-preview
Authorization: token *******************
Content-Type: application/json; charset=utf-8
Time-Zone: Europe/Amsterdam
Accept-Encoding: gzip
nicokosi commented 2 years ago

For info, here is my environment fix: https://github.com/nicokosi/gh-collab-scanner/pull/8/commits/4854d1ffc2986b61bae276bdaa01b3841eb734cf

See the documentation: https://docs.github.com/en/actions/using-workflows/using-github-cli-in-workflows. πŸ“–