Skip to content

Commit 52a66fd

Browse files
Merge pull request #235 from codecov/better-log
Better logs
2 parents 300eb11 + 11b7bcc commit 52a66fd

18 files changed

+284
-137
lines changed

.circleci/config.yml

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ jobs:
2626
# Download and cache dependencies
2727
- restore_cache:
2828
keys:
29-
- v1-dependencies-{{ checksum "npm-shrinkwrap.json" }}
29+
- v2-dependencies-{{ checksum "npm-shrinkwrap.json" }}
3030

3131
- run: npm install
3232

3333
- save_cache:
3434
paths:
3535
- node_modules
36-
key: v1-dependencies-{{ checksum "npm-shrinkwrap.json" }}
36+
key: v2-dependencies-{{ checksum "npm-shrinkwrap.json" }}
3737

3838
# run tests!
3939
- run: make test
@@ -133,7 +133,7 @@ jobs:
133133
- run:
134134
name: Run Alpine binary -f (dry run)
135135
command: |
136-
out/codecov-alpine -f /root/project/coverage-alpine/cobertura-coverage.xml -F alpine -d -Z -e CIRCLE_BRANCH >> output_alpine.txt
136+
NODE_DEBUG=vm out/codecov-alpine -f /root/project/coverage-alpine/cobertura-coverage.xml -F alpine -d -Z -e CIRCLE_BRANCH >> output_alpine.txt
137137
- run:
138138
name: Run Alpine binary auto-detect (dry run)
139139
command: |
@@ -143,6 +143,9 @@ jobs:
143143
command: |
144144
out/codecov-alpine -F alpine -Z -e CIRCLE_BRANCH
145145
146+
- store_artifacts:
147+
path: output_alpine.txt
148+
146149
- persist_to_workspace:
147150
root: .
148151
paths:
@@ -181,7 +184,7 @@ jobs:
181184
- run:
182185
name: Run Linux binary -f (dry run)
183186
command: |
184-
out/codecov-linux -f /home/circleci/project/coverage/cobertura-coverage.xml -F linux -d -Z -e CIRCLE_BRANCH >> output_linux.txt
187+
out/codecov-linux -f /home/circleci/project/coverage/cobertura-coverage.xml -F linux -d -Z -v -e CIRCLE_BRANCH >> output_linux.txt
185188
- run:
186189
name: Run Linux binary auto-detect (dry run)
187190
command: |
@@ -232,7 +235,7 @@ jobs:
232235
- run:
233236
name: Run MacOS binary -f (dry-run)
234237
command: |
235-
out/codecov-macos -f /Users/distiller/project/coverage/cobertura-coverage.xml -F macos -d -Z -e CIRCLE_BRANCH >> output_osx.txt
238+
out/codecov-macos -f /Users/distiller/project/coverage/cobertura-coverage.xml -F macos -d -Z -v -e CIRCLE_BRANCH >> output_osx.txt
236239
- run:
237240
name: Run MacOS binary auto-detect (dry-run)
238241
command: |
@@ -282,7 +285,7 @@ jobs:
282285
command: |
283286
dir C:\Users\circleci\project\coverage\
284287
attrib C:\Users\circleci\project\coverage\cobertura-coverage.xml
285-
.\out\codecov.exe -f C:\Users\circleci\project\coverage\cobertura-coverage.xml -F windows -d -Z -e CIRCLE_BRANCH >> output_win.txt
288+
.\out\codecov.exe -f C:\Users\circleci\project\coverage\cobertura-coverage.xml -F windows -d -Z -v -e CIRCLE_BRANCH >> output_win.txt
286289
shell: cmd.exe
287290
- run:
288291
name: Run Windows binary auto-detect (dry-run)

bin/codecov

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ var argv = require('yargs') // eslint-disable-line
106106
alias: 'Q',
107107
type: 'string',
108108
description: `Used internally by Codecov, this argument helps track wrappers
109-
of the uploader (e.g. GitHub Action, CircleCI Orb)`
109+
of the uploader (e.g. GitHub Action, CircleCI Orb)`,
110110
},
111111
})
112112
.version()

src/ci_providers/provider_azurepipelines.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const childProcess = require('child_process')
2-
const { log } = require('../helpers/logger')
2+
const { error, info, verbose } = require('../helpers/logger')
33

44
function detect(envs) {
55
return !!envs.SYSTEM_TEAMFOUNDATIONSERVERURI
@@ -63,7 +63,7 @@ function _getSHA(inputs) {
6363
.trimRight()
6464
if (mergeCommitRegex.exec(mergeCommitMessage)) {
6565
const mergeCommit = mergeCommitMessage.split(' ')[1]
66-
log(` Fixing merge commit SHA ${commit} -> ${mergeCommit}`)
66+
info(` Fixing merge commit SHA ${commit} -> ${mergeCommit}`)
6767
commit = mergeCommit
6868
}
6969
}

src/ci_providers/provider_githubactions.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const childProcess = require('child_process')
2-
const { log } = require('../helpers/logger')
2+
const { error, info, verbose } = require('../helpers/logger')
33

44
function detect(envs) {
55
return envs.GITHUB_ACTIONS
@@ -70,10 +70,10 @@ function _getSHA(inputs) {
7070
.trimRight()
7171
if (mergeCommitRegex.exec(mergeCommitMessage)) {
7272
const mergeCommit = mergeCommitMessage.split(' ')[1]
73-
log(` Fixing merge commit SHA ${commit} -> ${mergeCommit}`)
73+
info(` Fixing merge commit SHA ${commit} -> ${mergeCommit}`)
7474
commit = mergeCommit
7575
} else if (mergeCommitMessage === '') {
76-
log(
76+
info(
7777
'-> Issue detecting commit SHA. Please run actions/checkout with fetch-depth > 1 or set to 0',
7878
)
7979
}

src/ci_providers/provider_local.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const childprocess = require('child_process')
22
const { parseSlug } = require('../helpers/git')
3+
const { logAndThrow } = require('../helpers/util')
34

45
function detect(envs) {
56
return !envs.CI
@@ -27,9 +28,7 @@ function _getBranch(inputs) {
2728
.trimRight()
2829
return branchName
2930
} catch (error) {
30-
throw new Error(
31-
`There was an error getting the branch name from git: ${error}`,
32-
)
31+
logAndThrow(`There was an error getting the branch name from git: ${error}`)
3332
}
3433
}
3534

@@ -66,9 +65,7 @@ function _getSHA(inputs) {
6665
.trimRight()
6766
return sha
6867
} catch (error) {
69-
throw new Error(
70-
`There was an error getting the commit SHA from git: ${error}`,
71-
)
68+
logAndThrow(`There was an error getting the commit SHA from git: ${error}`)
7269
}
7370
}
7471

@@ -84,7 +81,7 @@ function _getSlug(inputs) {
8481
.trimRight()
8582
return parseSlug(slug)
8683
} catch (error) {
87-
throw new Error(`There was an error getting the slug from git: ${error}`)
84+
logAndThrow(`There was an error getting the slug from git: ${error}`)
8885
}
8986
}
9087

src/ci_providers/provider_template.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
* @param {*} envs an object of enviromental variable key/value pairs
55
* @returns boolean
66
*/
7+
8+
const { logAndThrow } = require('../helpers/util')
9+
710
// eslint-disable-next-line no-unused-vars
811
function detect(envs) {
912
return false
@@ -42,9 +45,7 @@ function _getBranch(inputs) {
4245
try {
4346
return args.branch || ''
4447
} catch (error) {
45-
throw new Error(
46-
`There was an error getting the branch name from git: ${error}`,
47-
)
48+
logAndThrow(`There was an error getting the branch name from git: ${error}`)
4849
}
4950
}
5051

@@ -71,9 +72,7 @@ function _getPR(inputs) {
7172
try {
7273
return args.pr || ''
7374
} catch (error) {
74-
throw new Error(
75-
`There was an error getting the branch name from git: ${error}`,
76-
)
75+
logAndThrow(`There was an error getting the pr number: ${error}`)
7776
}
7877
}
7978

@@ -105,9 +104,7 @@ function _getSHA(inputs) {
105104
try {
106105
return args.sha || ''
107106
} catch (error) {
108-
throw new Error(
109-
`There was an error getting the commit SHA from git: ${error}`,
110-
)
107+
logAndThrow(`There was an error getting the commit SHA from git: ${error}`)
111108
}
112109
}
113110
/**
@@ -121,7 +118,7 @@ function _getSlug(inputs) {
121118
try {
122119
return args.slug || ''
123120
} catch (error) {
124-
throw new Error(`There was an error getting the slug from git: ${error}`)
121+
logAndThrow(`There was an error getting the slug from git: ${error}`)
125122
}
126123
}
127124
/**

src/helpers/files.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ const childProcess = require('child_process')
33
const fs = require('fs')
44
const glob = require('glob')
55
const path = require('path').posix
6-
7-
const { log } = require('./logger')
6+
const { logAndThrow } = require('./util')
7+
const { error, info, verbose } = require('./logger')
88

99
/**
1010
*
@@ -195,7 +195,8 @@ function fetchGitRoot() {
195195
process.cwd()
196196
).trimRight()
197197
} catch (error) {
198-
throw new Error('Error fetching git root. Please try using the -R flag.')
198+
logAndThrow('Error fetching git root. Please try using the -R flag.')
199+
return '.'
199200
}
200201
}
201202

@@ -206,11 +207,12 @@ function fetchGitRoot() {
206207
*/
207208
function parseGitIgnore(projectRoot) {
208209
const gitIgnorePath = path.join(projectRoot, '.gitignore')
209-
let lines
210+
/** @type {string[]} */
211+
let lines = []
210212
try {
211213
lines = readAllLines(gitIgnorePath) || []
212214
} catch (error) {
213-
throw new Error(`Unable to open ${gitIgnorePath}: ${error}`)
215+
logAndThrow(`Unable to open ${gitIgnorePath}: ${error}`)
214216
}
215217

216218
return lines.filter(line => {
@@ -226,11 +228,12 @@ function parseGitIgnore(projectRoot) {
226228
* @param {string} projectRoot Root of the project
227229
* @param {string} dirPath Directory to search in
228230
* @param {Object} args
231+
* @param {boolean} [args.verbose]
229232
* @param {string[]} arrayOfFiles
230233
* @returns {string[]}
231234
*/
232235
function getAllFiles(projectRoot, dirPath, args, arrayOfFiles = []) {
233-
log(`Searching for files in ${dirPath}`, { level: 'debug', args })
236+
verbose(`Searching for files in ${dirPath}`, args.verbose)
234237
const files = fs.readdirSync(dirPath)
235238

236239
files.forEach(function (file) {
@@ -250,7 +253,7 @@ function getAllFiles(projectRoot, dirPath, args, arrayOfFiles = []) {
250253
)
251254
}
252255
})
253-
log(`Search complete for files in ${dirPath}`, { level: 'debug', args })
256+
verbose(`Search complete for files in ${dirPath}`, args.verbose)
254257
return arrayOfFiles
255258
}
256259

@@ -278,8 +281,9 @@ function readCoverageFile(projectRoot, filePath) {
278281
encoding: 'utf-8',
279282
})
280283
} catch (error) {
281-
throw new Error(`There was an error reading the coverage file: ${error}`)
284+
logAndThrow(`There was an error reading the coverage file: ${error}`)
282285
}
286+
return ''
283287
}
284288

285289
function endNetworkMarker() {
@@ -332,7 +336,7 @@ function getFilePath(projectRoot, filePath) {
332336
function removeFile(projectRoot, filePath) {
333337
fs.unlink(getFilePath(projectRoot, filePath), err => {
334338
if (err) {
335-
log(`Error removing ${filePath} coverage file`)
339+
error(`Error removing ${filePath} coverage file`)
336340
}
337341
})
338342
}

src/helpers/git.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const childProcess = require('child_process')
2+
const { logAndThrow } = require('./util')
23

34
function parseSlug(slug) {
45
// origin https://github.com/torvalds/linux.git (fetch)
@@ -18,18 +19,16 @@ function parseSlug(slug) {
1819
const cleanSlug = slug.split(':')[1].replace('.git', '')
1920
return cleanSlug
2021
}
21-
throw new Error(`Unable to parse slug URL: ${slug}`)
22+
logAndThrow(`Unable to parse slug URL: ${slug}`)
2223
}
2324

2425
function parseSlugFromRemoteAddr(remoteAddr) {
2526
let slug = ''
2627
if (!remoteAddr) {
27-
remoteAddr = (childProcess
28-
.spawnSync('git', [
29-
'config',
30-
'--get',
31-
'remote.origin.url',
32-
]).stdout || '')
28+
remoteAddr = (
29+
childProcess.spawnSync('git', ['config', '--get', 'remote.origin.url'])
30+
.stdout || ''
31+
)
3332
.toString()
3433
.trimRight()
3534
}

src/helpers/logger.js

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
11
// @ts-check
22

33
/**
4-
*
4+
* We really only need three log levels
5+
* * Error
6+
* * Info
7+
* * Verbose
8+
* For the purposes of minimal refactor, I'm aliasing debug() to verbose()
9+
* I'm also mapping the log() options to the new calls and
10+
* deprecating log()
11+
*/
12+
13+
function _getTimestamp() {
14+
return new Date().toISOString()
15+
}
16+
17+
/**
18+
* @deprecated - Please use error(), info(), or verbose()
519
* @param {string} message
620
* @param {Object} [options]
721
* @param {string} [options.level]
@@ -11,24 +25,56 @@
1125
*/
1226
function log(message, options) {
1327
if (!options || !options.level) {
14-
return console.log(message)
28+
return info(message)
1529
}
1630
switch (options.level) {
1731
case 'debug':
1832
if (options.args && options.args.verbose) {
19-
console.debug(message)
33+
return verbose(message, true)
2034
}
2135

2236
break
2337
case 'error':
24-
console.error(message)
25-
break
38+
return error(message)
2639
default:
27-
console.log(message)
28-
break
40+
return info(message)
41+
}
42+
}
43+
44+
/**
45+
*
46+
* @param {string} message - message to log
47+
* @param {boolean} shouldVerbose - pass the value of the verbose flag
48+
* @return void
49+
*/
50+
function verbose(message, shouldVerbose = false) {
51+
if (shouldVerbose === true) {
52+
console.debug(`[${_getTimestamp()}] ['verbose'] ${message}`)
2953
}
3054
}
3155

56+
/**
57+
*
58+
* @param {string} message - message to log
59+
* @return void
60+
*/
61+
function error(message) {
62+
console.error(`[${_getTimestamp()}] ['error'] ${message}`)
63+
}
64+
65+
/**
66+
*
67+
* @param {string} message - message to log
68+
* @return void
69+
*/
70+
function info(message) {
71+
console.log(`[${_getTimestamp()}] ['info'] ${message}`)
72+
}
73+
3274
module.exports = {
75+
debug: verbose,
76+
error,
77+
info,
3378
log,
79+
verbose,
3480
}

0 commit comments

Comments
 (0)