Skip to content

Commit ce14f26

Browse files
feat: add getToken helper
1 parent f6580e6 commit ce14f26

File tree

6 files changed

+100
-21
lines changed

6 files changed

+100
-21
lines changed

src/helpers/files.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const { log } = require('./logger')
88
/**
99
*
1010
* @param {string} projectRoot
11+
* @param {Object} args
1112
* @returns {Promise<string>}
1213
*/
1314
async function getFileListing(projectRoot, args) {
@@ -161,12 +162,14 @@ function coverageFilePatterns() {
161162
* @returns {string[]}
162163
*/
163164
function getCoverageFiles(projectRoot, coverageFilePatterns) {
164-
return coverageFilePatterns.flatMap(pattern => {
165-
return glob.sync(`**/${pattern}`, {
166-
cwd: projectRoot,
167-
ignore: globBlacklist(),
168-
})
169-
})
165+
return coverageFilePatterns.flatMap(
166+
/** @type {string} */ pattern => {
167+
return glob.sync(`**/${pattern}`, {
168+
cwd: projectRoot,
169+
ignore: globBlacklist(),
170+
})
171+
},
172+
)
170173
}
171174

172175
/**
@@ -221,6 +224,7 @@ function parseGitIgnore(projectRoot) {
221224
*
222225
* @param {string} projectRoot Root of the project
223226
* @param {string} dirPath Directory to search in
227+
* @param {Object} args
224228
* @param {string[]} arrayOfFiles
225229
* @returns {string[]}
226230
*/
@@ -285,6 +289,11 @@ function endFileMarker() {
285289
return '<<<<<< EOF\n'
286290
}
287291

292+
/**
293+
*
294+
* @param {string} filePath
295+
* @returns string
296+
*/
288297
function fileHeader(filePath) {
289298
return `# path=${filePath}\n`
290299
}
@@ -314,6 +323,11 @@ function getFilePath(projectRoot, filePath) {
314323
return path.join(projectRoot, filePath)
315324
}
316325

326+
/**
327+
*
328+
* @param {string} projectRoot
329+
* @param {string} filePath
330+
*/
317331
function removeFile(projectRoot, filePath) {
318332
fs.unlink(getFilePath(projectRoot, filePath), err => {
319333
if (err) {

src/helpers/logger.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
/**
44
*
5-
* @param {string|Object} message
5+
* @param {string} message
66
* @param {Object} [options]
7-
* @param {'debug'|'error'} [options.level]
7+
* @param {string} [options.level]
88
* @param {Object} [options.args]
9+
* @param {boolean} [options.args.verbose]
910
* @returns void
1011
*/
1112
function log(message, options) {

src/helpers/validate.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
const validator = require('validator')
22

3+
/**
4+
*
5+
* @param {string} token
6+
* @returns boolean
7+
*/
38
function validateToken(token) {
49
return validator.isAlphanumeric(token)
510
}
@@ -25,15 +30,33 @@ function validateFileNamePath(path) {
2530
* @param {number} requestedLength
2631
* @returns {boolean}
2732
*/
28-
const GIT_SHA_LENGTH = 40;
33+
const GIT_SHA_LENGTH = 40
2934

3035
function validateSHA(commitSHA, requestedLength = GIT_SHA_LENGTH) {
3136
return (
3237
commitSHA.length === requestedLength && validator.isAlphanumeric(commitSHA)
3338
)
3439
}
3540

41+
function getToken(args) {
42+
// Token gets set in the following order:
43+
// * args.token
44+
// * process.env.CODECOV_TOKEN
45+
// * ''
46+
let token = ''
47+
if (args.token && validateToken(args.token)) {
48+
token = args.token
49+
} else if (
50+
process.env.CODECOV_TOKEN &&
51+
validateToken(process.env.CODECOV_TOKEN)
52+
) {
53+
token = process.env.CODECOV_TOKEN
54+
}
55+
return token
56+
}
57+
3658
module.exports = {
59+
getToken,
3760
validateToken,
3861
validateURL,
3962
validateFlags,

src/index.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const providers = require('./ci_providers')
1313
* @param {string} token
1414
* @param {string} query
1515
* @param {string} uploadFile
16+
* @param {string} source
1617
*/
1718
function dryRun(uploadHost, token, query, uploadFile, source) {
1819
log('==> Dumping upload file (no upload)')
@@ -47,6 +48,7 @@ function dryRun(uploadHost, token, query, uploadFile, source) {
4748
* @param {string} args.url Change the upload host (Enterprise use)
4849
* @param {boolean} args.clean Move discovered coverage reports to the trash
4950
* @param {string} args.feature Toggle features
51+
* @param {string} args.source Track wrappers of the uploader
5052
*/
5153
async function main(args) {
5254
/*
@@ -68,14 +70,8 @@ async function main(args) {
6870
const uploadHost = validateHelpers.validateURL(args.url)
6971
? args.url
7072
: 'https://codecov.io'
71-
let token = validateHelpers.validateToken(args.token) ? args.token : ''
72-
if (token === '') {
73-
token = process.env.CODECOV_TOKEN || ''
74-
token = validateHelpers.validateToken(token)
75-
? process.env.CODECOV_TOKEN
76-
: ''
77-
}
7873

74+
const token = validateHelpers.getToken(args)
7975
log(generateHeader(getVersion()))
8076

8177
// == Step 2: detect if we are in a git repo
@@ -172,7 +168,7 @@ async function main(args) {
172168

173169
// Environment variables
174170
if (args.env || envs.CODECOV_ENV) {
175-
const environmentVars = args.env || envs.CODECOV_ENV
171+
const environmentVars = args.env || envs.CODECOV_ENV || ''
176172
const vars = environmentVars
177173
.split(',')
178174
.filter(Boolean)
@@ -243,7 +239,7 @@ async function main(args) {
243239
{ level: 'debug', args },
244240
)
245241
const result = await webHelpers.uploadToCodecovPUT(uploadURL, gzippedFile)
246-
log(result)
242+
log(JSON.stringify(result))
247243
return result
248244
} catch (error) {
249245
throw new Error(`Error uploading to ${uploadHost}: ${error}`)

test/helpers/logger.test.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// @ts-check
22
const logger = require('../../src/helpers/logger')
3+
const { expect, it } = require('@jest/globals')
34

45
describe('Logger Helper', () => {
56
afterEach(() => {
@@ -23,7 +24,9 @@ describe('Logger Helper', () => {
2324
})
2425

2526
it('Should call logger with options.level = debug and verbose set', () => {
26-
jest.spyOn(console, 'debug').mockImplementation(() => {})
27+
jest.spyOn(console, 'debug').mockImplementation(() => {
28+
// Intentionally empty
29+
})
2730
logger.log('message with debug level and verbose', {
2831
level: 'debug',
2932
args: { verbose: true },
@@ -34,15 +37,19 @@ describe('Logger Helper', () => {
3437
})
3538

3639
it('Should call logger with options.level = error', () => {
37-
jest.spyOn(console, 'error').mockImplementation(() => {})
40+
jest.spyOn(console, 'error').mockImplementation(() => {
41+
// Intentionally empty
42+
})
3843
logger.log('message with error level', { level: 'error' })
3944
expect(console.error).toHaveBeenCalledWith(
4045
expect.stringMatching(/error level/),
4146
)
4247
})
4348

4449
it('Should call logger with unsupported options.level ', () => {
45-
jest.spyOn(console, 'log').mockImplementation(() => {})
50+
jest.spyOn(console, 'log').mockImplementation(() => {
51+
// Intentionally empty
52+
})
4653
logger.log('message with error level of foobar', { level: 'foobar' })
4754
expect(console.log).toHaveBeenCalledWith(expect.stringMatching(/of foobar/))
4855
})

test/helpers/validate.test.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
const validate = require('../../src/helpers/validate')
22

3+
// Backup the env
4+
const realEnv = { ...process.env }
5+
36
describe('Input Validators', () => {
47
describe('Tokens', () => {
58
it('Returns true with a valid token', () => {
@@ -65,5 +68,40 @@ describe('Input Validators', () => {
6568
it('should pass with correct content and length', () => {
6669
expect(validate.validateSHA('abc123', 6)).toBe(true)
6770
})
71+
72+
it('should pass with correct content and default length', () => {
73+
expect(
74+
validate.validateSHA('1732d84b7ef2425e979d6034a3e3bb5633da344a'),
75+
).toBe(true)
76+
})
77+
})
78+
79+
describe('getToken()', () => {
80+
beforeEach(() => {
81+
delete process.env.CODECOV_TOKEN
82+
})
83+
84+
afterEach(() => {
85+
process.env = realEnv
86+
})
87+
88+
it('should return an empty string if neither args or env are set', () => {
89+
const args = {}
90+
expect(validate.getToken(args)).toEqual('')
91+
})
92+
93+
it('should return the value of the env if env is set, and args are not set', () => {
94+
process.env.CODECOV_TOKEN = 'testingEnvToken'
95+
const args = {}
96+
expect(validate.getToken(args)).toEqual('testingEnvToken')
97+
})
98+
99+
it('should return the value of the arg if env is set, and args are set', () => {
100+
process.env.CODECOV_TOKEN = 'testingEnvToken'
101+
const args = {
102+
token: 'testingArgToken',
103+
}
104+
expect(validate.getToken(args)).toEqual('testingArgToken')
105+
})
68106
})
69107
})

0 commit comments

Comments
 (0)