Skip to content
This repository has been archived by the owner on Jan 10, 2023. It is now read-only.

Commit

Permalink
fix: support .codecov.yml/codecov.yml files for token (#108)
Browse files Browse the repository at this point in the history
* fix: support `.codecov.yml`/`codecov.yml` files for token

* fix: added more test coverage for gitlab

* fix: cleanup env vars to avoid leaking into other tests

* fix: updated test to use process.cwd()

* package bumps
  • Loading branch information
hellatan authored and eddiemoore committed Sep 4, 2018
1 parent 1fff4dc commit 7c1f16e
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 32 deletions.
29 changes: 21 additions & 8 deletions lib/codecov.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var fs = require('fs')
var path = require('path')
var request = require('request')
var urlgrey = require('urlgrey')
var jsYaml = require('js-yaml')
var walk = require('ignore-walk')
var execSync = require('child_process').execSync

Expand Down Expand Up @@ -258,20 +259,22 @@ var upload = function(args, on_success, on_failure) {
version
)

query.yaml = [yamlFile, '.codecov.yml'].reduce(function(result, file) {
return (
result ||
(fs.existsSync(path.resolve(process.cwd(), file)) ? file : undefined)
)
}, undefined)

if ((args.options.disable || '').split(',').indexOf('detect') === -1) {
console.log('==> Detecting CI Provider')
query = detectProvider()
} else {
debug.push('disabled detect')
}

query.yaml = [yamlFile, '.codecov.yml'].reduce(function(result, file) {
return (
result ||
(fs.existsSync(path.resolve(process.cwd(), file))
? path.resolve(process.cwd(), file)
: undefined)
)
}, undefined)

if (args.options.build) {
query.build = args.options.build
}
Expand All @@ -294,8 +297,18 @@ var upload = function(args, on_success, on_failure) {
query.flags = flags
}

var yamlToken
try {
var loadedYamlFile = jsYaml.safeLoad(fs.readFileSync(query.yaml, 'utf8'))
yamlToken = loadedYamlFile && loadedYamlFile.codecov && loadedYamlFile.codecov.token
} catch (e) {
// silently fail
}
var token =
args.options.token || process.env.codecov_token || process.env.CODECOV_TOKEN
args.options.token ||
yamlToken ||
process.env.codecov_token ||
process.env.CODECOV_TOKEN
if (token) {
query.token = token
}
Expand Down
29 changes: 15 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"dependencies": {
"argv": "^0.0.2",
"ignore-walk": "^3.0.1",
"js-yaml": "^3.12.0",
"request": "^2.87.0",
"urlgrey": "^0.4.4"
},
Expand All @@ -38,6 +39,7 @@
"husky": "^0.14.3",
"lint-staged": "^7.2.0",
"mocha": "^5.2.0",
"mock-fs": "^4.6.0",
"nyc": "^12.0.2",
"prettier": "^1.13.7"
},
Expand Down
95 changes: 85 additions & 10 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var fs = require('fs')
var mockFs = require('mock-fs')
var codecov = require('../lib/codecov')

var isWindows =
Expand Down Expand Up @@ -29,6 +30,7 @@ describe('Codecov', function() {
expect(codecov.upload({ options: { dump: true } }).query.token).to.eql(
'ABC123'
)
delete process.env.CODECOV_TOKEN
})

it('can get a token passed in cli', function() {
Expand All @@ -37,6 +39,69 @@ describe('Codecov', function() {
).to.eql('qwerty')
})

it('can read a codecov.yml file', function() {
mockFs({
'codecov.yml': 'codecov:\n token: fake-token',
})
expect(codecov.upload({ options: { dump: true } }).query.token).to.eql(
'fake-token'
)
mockFs.restore()
})
it('can read a .codecov.yml file', function() {
mockFs({
'.codecov.yml': 'codecov:\n token: fake-token-dotfile',
})
expect(codecov.upload({ options: { dump: true } }).query.token).to.eql(
'fake-token-dotfile'
)
mockFs.restore()
})
it('should have no token if yaml file does not supplied', function() {
mockFs({
'.codecov.yml': 'codecov:\n noconfig: true',
})
expect(codecov.upload({ options: { dump: true } }).query.token).to.eql(
undefined
)
mockFs.restore()
})

it('token precedence should be respected', function() {
// options.token || .codecov.yml/codecov.yml file || codecov_token || CODECOV_TOKEN
mockFs({
'.codecov.yml': 'codecov:\n token: fake-token-dotfile',
})
var upload = codecov.upload({ options: { dump: true, token: 'qwerty' } })
expect(upload.query.token).to.eql('qwerty')
mockFs.restore()

process.env.codecov_token = 'abc123'
upload = codecov.upload({ options: { dump: true, token: 'qwerty2' } })
expect(upload.query.token).to.eql('qwerty2')
delete process.env.codecov_token

process.env.CODECOV_TOKEN = 'ABC123'
upload = codecov.upload({ options: { dump: true, token: 'qwerty3' } })
expect(upload.query.token).to.eql('qwerty3')
delete process.env.CODECOV_TOKEN

mockFs({
'.codecov.yml': 'codecov:\n token: fake-token-dotfile',
})
process.env.codecov_token = 'abc123'
upload = codecov.upload({ options: { dump: true } })
expect(upload.query.token).to.eql('fake-token-dotfile')
mockFs.restore()

process.env.codecov_token = 'abc123'
process.env.CODECOV_TOKEN = 'ABC123'
upload = codecov.upload({ options: { dump: true } })
expect(upload.query.token).to.eql('abc123')
delete process.env.codecov_token
delete process.env.CODECOV_TOKEN
})

it('can auto detect reports', function() {
var res = codecov.upload({ options: { dump: true } })
expect(res.files[0].split(pathSeparator).pop()).to.eql(
Expand Down Expand Up @@ -125,6 +190,7 @@ describe('Codecov', function() {
var res = codecov.upload({ options: { dump: true, env: 'HELLO,VAR1' } })
expect(res.body).to.contain('HELLO=world\n')
expect(res.body).to.contain('VAR1=\n')
delete process.env.HELLO
})

it('can include env in env', function() {
Expand All @@ -134,6 +200,8 @@ describe('Codecov', function() {
expect(res.body).to.contain('HELLO=world\n')
expect(res.body).to.contain('VAR1=\n')
expect(res.body).to.contain('VAR2=\n')
delete process.env.HELLO
delete process.env.CODECOV_ENV
})

it('can have custom args for gcov', function() {
Expand Down Expand Up @@ -163,33 +231,40 @@ describe('Codecov', function() {
})

it('Should use codecov.yml via env variable', function() {
var CWD = process.cwd()
expect(
codecov.upload({ options: { dump: true, disable: 'detect' } }).query.yaml
).to.eql('codecov.yml')
).to.eql(CWD + '/codecov.yml')

fs.writeFileSync('foo.yml', '')
mockFs({
'foo.yml': '',
})
process.env.codecov_yml = 'foo.yml'
expect(
codecov.upload({ options: { dump: true, disable: 'detect' } }).query.yaml
).to.eql('foo.yml')
fs.unlinkSync('foo.yml')
).to.eql(CWD + '/foo.yml')
mockFs.restore()
delete process.env.codecov_yml

fs.writeFileSync('FOO.yml', '')
mockFs({
'FOO.yml': '',
})
process.env.CODECOV_YML = 'FOO.yml'
expect(
codecov.upload({ options: { dump: true, disable: 'detect' } }).query.yaml
).to.eql('FOO.yml')
fs.unlinkSync('FOO.yml')
).to.eql(CWD + '/FOO.yml')
mockFs.restore()
delete process.env.CODECOV_YML
})

it('can get config from cli args', function() {
fs.writeFileSync('foo.yml', '')
mockFs({
'foo.yml': '',
})
var res = codecov.upload({
options: { dump: true, yml: 'foo.yml', disable: 'detect' },
})
expect(res.query.yaml).to.eql('foo.yml')
fs.unlinkSync('foo.yml')
expect(res.query.yaml).to.eql(process.cwd() + '/foo.yml')
mockFs.restore()
})
})
24 changes: 24 additions & 0 deletions test/services/gitlab.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ describe('Gitlab CI Provider', function() {
expect(gitlab.detect()).to.be(true)
})

it('cannot detect gitlab', function() {
delete process.env.GITLAB_CI
expect(gitlab.detect()).to.be(false)
})

it('can get service env info', function() {
process.env.CI_BUILD_ID = '1234'
process.env.CI_BUILD_REPO = 'https://gitlab.com/owner/repo.git'
Expand All @@ -20,5 +25,24 @@ describe('Gitlab CI Provider', function() {
slug: 'owner/repo',
branch: 'master',
})
delete process.env.CI_BUILD_REPO
process.env.CI_REPOSITORY_URL = 'https://gitlab.com/owner/repo2.git'
expect(gitlab.configuration()).to.eql({
service: 'gitlab',
build: '1234',
root: '/',
commit: '5678',
slug: 'owner/repo2',
branch: 'master',
})
delete process.env.CI_REPOSITORY_URL
expect(gitlab.configuration()).to.eql({
service: 'gitlab',
build: '1234',
root: '/',
commit: '5678',
slug: '',
branch: 'master',
})
})
})

0 comments on commit 7c1f16e

Please sign in to comment.