diff --git a/src/get-sha.js b/src/get-sha.js index 331c1fe..1b65088 100644 --- a/src/get-sha.js +++ b/src/get-sha.js @@ -6,7 +6,9 @@ module.exports = function getCommitSHA(core, github) { // Get GitHub-event-relevant contextual details, like commit SHA const ctx = github.context; - core.debug(`event is ${ctx.eventName} with payload ${JSON.stringify(ctx.payload, null, 2)}`); + core.debug( + `event is ${ctx.eventName} with payload ${JSON.stringify(ctx.payload, null, 2)}`, + ); let sha; // Depending on the exact GitHub event that triggers the action, we need to report the status on different payload // fields representing the correct SHA @@ -21,7 +23,9 @@ module.exports = function getCommitSHA(core, github) { sha = ctx.sha; } if (!sha) { - throw new Error(`Could not determine SHA from GitHub context payload: ${JSON.stringify(ctx, null, 2)}`); + throw new Error( + `Could not determine SHA from GitHub context payload: ${JSON.stringify(ctx, null, 2)}`, + ); } core.info(`Using SHA: ${sha}`); return sha; diff --git a/src/health-score.js b/src/health-score.js index b71ca1f..edfd89a 100644 --- a/src/health-score.js +++ b/src/health-score.js @@ -25,11 +25,13 @@ module.exports = { if (extensions.length === 0) { core.error('Extensions not specified'); } else { - if (includes.length === 0) core.warning('Directories to be included not specified'); + if (includes.length === 0) + core.warning('Directories to be included not specified'); com = module.exports.grep(core, extensions, includes, excludes); // to-do et al comments } return { - comments: com, coverageMisses: misses, + comments: com, + coverageMisses: misses, }; }, grep: findProblematicComments, diff --git a/src/helpers/helper-functions.js b/src/helpers/helper-functions.js index 5c1bced..95676e3 100644 --- a/src/helpers/helper-functions.js +++ b/src/helpers/helper-functions.js @@ -15,7 +15,10 @@ module.exports = { .map((item) => item.trim().replace(/^- */, '')) .filter(Boolean) .map((item) => { - if ((item.startsWith('"') && item.endsWith('"')) || (item.startsWith("'") && item.endsWith("'"))) { + if ( + (item.startsWith('"') && item.endsWith('"')) || + (item.startsWith("'") && item.endsWith("'")) + ) { return item.slice(1, -1); } return item; @@ -32,7 +35,9 @@ module.exports = { start_line: c.line_no ? c.line_no : 1, end_line: c.line_no ? c.line_no : 1, annotation_level: 'warning', - message: c.commentType ? (`Problematic comment ("${c.commentType}") identified`) : ('Problematic comment ("TODO", "HACK", "FIXME") identified'), + message: c.commentType + ? `Problematic comment ("${c.commentType}") identified` + : 'Problematic comment ("TODO", "HACK", "FIXME") identified', })); }, }; diff --git a/src/report.js b/src/report.js index 4bfc469..67265c1 100644 --- a/src/report.js +++ b/src/report.js @@ -14,23 +14,25 @@ const PROBLEMATIC_COMMENT_PENALTY = 100; module.exports = async function reportStatus(startTime, core, github, score) { const gh = core.getInput('github_token'); if (!gh) { - core.warning('No GitHub token found; will not report score on commit status.'); + core.warning( + 'No GitHub token found; will not report score on commit status.', + ); return 0; } core.info(JSON.stringify(score, null, 2)); // Calculate score - const points = ( - (score.comments.length * PROBLEMATIC_COMMENT_PENALTY) - + (score.coverageMisses * UNCOVERED_LINE_PENALTY) - ) * -1; + const points = + (score.comments.length * PROBLEMATIC_COMMENT_PENALTY + + score.coverageMisses * UNCOVERED_LINE_PENALTY) * + -1; // Report the thing const ctx = github.context; const octokit = github.getOctokit(gh); let details = `# Score Breakdown `; - if (score.comments && score.comments.length) { + if (score.comments?.length) { details += ` ## Problematic Comments diff --git a/src/score_components/coverage.js b/src/score_components/coverage.js index 2770797..779cb68 100644 --- a/src/score_components/coverage.js +++ b/src/score_components/coverage.js @@ -10,9 +10,14 @@ const getSHA = require('../get-sha'); module.exports = async function retrieveCodeCoverage(core, github) { // See if we can get a coverage overview for this commit from codecov const codecovToken = core.getInput('codecov_token'); - const maxAttempts = parseInt(core.getInput('codecov_max_attempts'), 10) > 0 ? parseInt(core.getInput('codecov_max_attempts'), 10) : 10; - const retryDelay = parseInt(core.getInput('codecov_retry_delay'), 10) || 10000; - const treatTimeoutAsError = core.getInput('codecov_treat_timeout_as_error') === 'true'; + const maxAttempts = + Number.parseInt(core.getInput('codecov_max_attempts'), 10) > 0 + ? Number.parseInt(core.getInput('codecov_max_attempts'), 10) + : 10; + const retryDelay = + Number.parseInt(core.getInput('codecov_retry_delay'), 10) || 10000; + const treatTimeoutAsError = + core.getInput('codecov_treat_timeout_as_error') === 'true'; let misses = 0; let attempts = 1; @@ -30,18 +35,21 @@ module.exports = async function retrieveCodeCoverage(core, github) { repo_name: ctx.repo.repo, commitid: sha, }); - core.debug(`codecov api response: ${JSON.stringify(coverage, null, 2)}`); - if (coverage && coverage.data) { - if (coverage.data.totals && coverage.data.totals.misses) { + core.debug( + `codecov api response: ${JSON.stringify(coverage, null, 2)}`, + ); + if (coverage?.data) { + if (coverage.data.totals?.misses) { misses = coverage.data.totals.misses; core.info(`${misses} uncovered lines according to codecov`); break; - } else { - core.info('codecov response data present but missing totals, delaying'); - // if totals are missing, probably codecov has not compiled the coverage info yet; delay and try again. - attempts += 1; - await sleep(retryDelay); } + core.info( + 'codecov response data present but missing totals, delaying', + ); + // if totals are missing, probably codecov has not compiled the coverage info yet; delay and try again. + attempts += 1; + await sleep(retryDelay); } else { core.info('codecov response data missing, delaying'); // if totals are missing, probably codecov has not compiled the coverage info yet; delay and try again. diff --git a/src/score_components/find-problematic-comments.js b/src/score_components/find-problematic-comments.js index 1f84079..c4c6964 100644 --- a/src/score_components/find-problematic-comments.js +++ b/src/score_components/find-problematic-comments.js @@ -1,5 +1,5 @@ -const fs = require('fs'); -const child_process = require('child_process'); +const fs = require('node:fs'); +const child_process = require('node:child_process'); const CommentType = Object.freeze({ TODO: 'TODO', @@ -7,12 +7,17 @@ const CommentType = Object.freeze({ HACK: 'HACK', }); -module.exports = function grepForProblematicComments(core, ext, include, exclude) { +module.exports = function grepForProblematicComments( + core, + ext, + include, + exclude, +) { let find = 'find'; - if (include && include.length) { - include.forEach((i) => { + if (include?.length) { + for (const i of include) { find += ` ${i}`; - }); + } } else { find += ' .'; } @@ -21,14 +26,17 @@ module.exports = function grepForProblematicComments(core, ext, include, exclude if (conditions) find += ` \\( ${conditions} \\) `; let ignores = []; - if (exclude && exclude.length) { + if (exclude?.length) { ignores = exclude; } else if (fs.existsSync('.gitignore')) { - ignores = fs.readFileSync('.gitignore').toString().split('\n') + ignores = fs + .readFileSync('.gitignore') + .toString() + .split('\n') .filter(Boolean) .map((entry) => entry.trim()); } - ignores.forEach((ex) => { + for (const ex of ignores) { const isDirectory = ex.endsWith('/'); if (isDirectory) { const dirPath = ex.slice(0, -1); @@ -36,7 +44,7 @@ module.exports = function grepForProblematicComments(core, ext, include, exclude } else { find += ` -not -path "*/${ex}"`; } - }); + } const commentPattern = `\\s*(//|/\\*|\\*).*\\b(${CommentType.TODO}|${CommentType.HACK}|${CommentType.FIXME})\\b`; // Modify the grep command to search within comments only @@ -48,18 +56,26 @@ module.exports = function grepForProblematicComments(core, ext, include, exclude core.error(e); core.error('child_process execSync failed to execute'); } - const result = output.split('\n').filter(Boolean).map((line) => { - // example line output = "./src/report.js:47: // TODO: handle API call erroring out" - const [path, lineNo, ...data] = line.split(':'); + const result = output + .split('\n') + .filter(Boolean) + .map((line) => { + // example line output = "./src/report.js:47: // TODO: handle API call erroring out" + const [path, lineNo, ...data] = line.split(':'); - const lineData = data.join(':'); - const [_code, ...rawCommentData] = lineData.split('//'); + const lineData = data.join(':'); + const [_code, ...rawCommentData] = lineData.split('//'); - const commentData = rawCommentData.join('//'); - const commentType = getCommentType(commentData); + const commentData = rawCommentData.join('//'); + const commentType = getCommentType(commentData); - return { path: path.trim(), line_no: parseInt(lineNo, 10), comment: `//${commentData}`.trim(), commentType }; - }); + return { + path: path.trim(), + line_no: Number.parseInt(lineNo, 10), + comment: `//${commentData}`.trim(), + commentType, + }; + }); return result; }; diff --git a/test/health-score-test.js b/test/health-score-test.js index d5b50ff..6910c40 100644 --- a/test/health-score-test.js +++ b/test/health-score-test.js @@ -1,6 +1,11 @@ const { assert } = require('chai'); const sinon = require('sinon'); -const { fakeCore, fakeGithub, fakeComments, fakeCoverage } = require('./stubs/stubs'); +const { + fakeCore, + fakeGithub, + fakeComments, + fakeCoverage, +} = require('./stubs/stubs'); const hs = require('../src/health-score'); const contextValue = { @@ -31,7 +36,11 @@ describe('health-score', () => { fakeCore.getInput.withArgs('exclude').returns('test'); fakeGithub.context = contextValue; await hs.compile(fakeCore, fakeGithub); - assert(fakeCore.warning.calledWith(sinon.match('Directories to be included not specified'))); + assert( + fakeCore.warning.calledWith( + sinon.match('Directories to be included not specified'), + ), + ); }); it('should check for invalid extensions', async () => { fakeCore.getInput.withArgs('extension').returns(''); @@ -39,7 +48,9 @@ describe('health-score', () => { fakeCore.getInput.withArgs('exclude').returns(null); fakeGithub.context = contextValue; await hs.compile(fakeCore, fakeGithub); - assert(fakeCore.error.calledWith(sinon.match('Extensions not specified'))); + assert( + fakeCore.error.calledWith(sinon.match('Extensions not specified')), + ); }); }); it('should take single input', async () => { @@ -66,21 +77,35 @@ describe('health-score', () => { await hs.compile(fakeCore, fakeGithub); - assert(fakeComments.calledWith(fakeCore, ['js', 'ts'], ['src', 'lib'], ['test', 'dist'])); + assert( + fakeComments.calledWith( + fakeCore, + ['js', 'ts'], + ['src', 'lib'], + ['test', 'dist'], + ), + ); assert(fakeCoverage.calledWith(fakeCore, fakeGithub)); }); it('should handle inputs in flow format', async () => { - fakeCore.getInput.withArgs('extension').returns('[\'js\', \'ts\']'); - fakeCore.getInput.withArgs('include').returns('[\'src\', \'lib\']'); - fakeCore.getInput.withArgs('exclude').returns('[\'test\', \'dist\']'); + fakeCore.getInput.withArgs('extension').returns("['js', 'ts']"); + fakeCore.getInput.withArgs('include').returns("['src', 'lib']"); + fakeCore.getInput.withArgs('exclude').returns("['test', 'dist']"); fakeGithub.context = contextValue; fakeComments.onCall().returns(['']); fakeCoverage.onCall().returns(0); await hs.compile(fakeCore, fakeGithub); - assert(fakeComments.calledWith(fakeCore, ['js', 'ts'], ['src', 'lib'], ['test', 'dist'])); + assert( + fakeComments.calledWith( + fakeCore, + ['js', 'ts'], + ['src', 'lib'], + ['test', 'dist'], + ), + ); assert(fakeCoverage.calledWith(fakeCore, fakeGithub)); }); it('should handle combined and unformatted inputs', async () => { @@ -93,7 +118,14 @@ describe('health-score', () => { await hs.compile(fakeCore, fakeGithub); - assert(fakeComments.calledWith(fakeCore, ['js', 'ts'], ['src', 'lib'], ['test', 'dist'])); + assert( + fakeComments.calledWith( + fakeCore, + ['js', 'ts'], + ['src', 'lib'], + ['test', 'dist'], + ), + ); assert(fakeCoverage.calledWith(fakeCore, fakeGithub)); }); }); @@ -108,14 +140,24 @@ describe('health-score', () => { const startTime = new Date(); const points = await hs.report(startTime, fakeCore, fakeGithub, score); assert( - fakeCore.warning.calledWith(sinon.match('No GitHub token found; will not report score on commit status.')), + fakeCore.warning.calledWith( + sinon.match( + 'No GitHub token found; will not report score on commit status.', + ), + ), ); assert.deepEqual(points, 0); }); it('should calculate points accurately', async () => { fakeCore.getInput.withArgs('github_token').returns('test'); fakeGithub.context = contextValue; - const score = { comments: [{ path: 'path', line_no: 10, comment: '// TODO: random stuff' }, { path: 'path2', line_no: 15, comment: '// FIXME: random stuff' }], coverageMisses: 5 }; + const score = { + comments: [ + { path: 'path', line_no: 10, comment: '// TODO: random stuff' }, + { path: 'path2', line_no: 15, comment: '// FIXME: random stuff' }, + ], + coverageMisses: 5, + }; const startTime = new Date(); const points = await hs.report(startTime, fakeCore, fakeGithub, score); assert.deepEqual(points, -205); diff --git a/test/helpers/helpers-test.js b/test/helpers/helpers-test.js index 753c586..b3529cb 100644 --- a/test/helpers/helpers-test.js +++ b/test/helpers/helpers-test.js @@ -1,5 +1,8 @@ const { assert } = require('chai'); -const { getAnnotations, parseYamlArray } = require('../../src/helpers/helper-functions'); +const { + getAnnotations, + parseYamlArray, +} = require('../../src/helpers/helper-functions'); describe('helpers', () => { it('should have a parseYamlArray function', async () => { @@ -71,44 +74,77 @@ describe('helpers', () => { assert.deepEqual(getAnnotations(comments), []); }); it('should handle find outputs of different formats', async () => { - assert.deepEqual(getAnnotations([{ path: 'path', line_no: 10, comment: '// TODO: random stuff', commentType: 'TODO' }]), [ - { - path: 'path', - start_line: 10, - end_line: 10, - annotation_level: 'warning', - message: 'Problematic comment ("TODO") identified', - }, - ]); - assert.deepEqual(getAnnotations([ - { path: 'path', line_no: 10, comment: '// FIXME: random stuff', commentType: 'FIXME' }, - { path: 'path2', line_no: 15, comment: '// random FIXME comment', commmentType: null }]), [ - { - path: 'path', - start_line: 10, - end_line: 10, - annotation_level: 'warning', - message: 'Problematic comment ("FIXME") identified', - }, - { - path: 'path2', - start_line: 15, - end_line: 15, - annotation_level: 'warning', - message: 'Problematic comment ("TODO", "HACK", "FIXME") identified', - }, - ]); + assert.deepEqual( + getAnnotations([ + { + path: 'path', + line_no: 10, + comment: '// TODO: random stuff', + commentType: 'TODO', + }, + ]), + [ + { + path: 'path', + start_line: 10, + end_line: 10, + annotation_level: 'warning', + message: 'Problematic comment ("TODO") identified', + }, + ], + ); + assert.deepEqual( + getAnnotations([ + { + path: 'path', + line_no: 10, + comment: '// FIXME: random stuff', + commentType: 'FIXME', + }, + { + path: 'path2', + line_no: 15, + comment: '// random FIXME comment', + commmentType: null, + }, + ]), + [ + { + path: 'path', + start_line: 10, + end_line: 10, + annotation_level: 'warning', + message: 'Problematic comment ("FIXME") identified', + }, + { + path: 'path2', + start_line: 15, + end_line: 15, + annotation_level: 'warning', + message: 'Problematic comment ("TODO", "HACK", "FIXME") identified', + }, + ], + ); }); it('should default to line 1 for outputs without line number', async () => { - assert.deepEqual(getAnnotations([{ path: 'path', comment: '// TODO: random stuff', commentType: 'TODO' }]), [ - { - path: 'path', - start_line: 1, - end_line: 1, - annotation_level: 'warning', - message: 'Problematic comment ("TODO") identified', - }, - ]); + assert.deepEqual( + getAnnotations([ + { + path: 'path', + comment: '// TODO: random stuff', + commentType: 'TODO', + }, + ]), + [ + { + path: 'path', + start_line: 1, + end_line: 1, + annotation_level: 'warning', + message: 'Problematic comment ("TODO") identified', + }, + ], + ); }); }); }); diff --git a/test/score_components/coverage-test.js b/test/score_components/coverage-test.js index 43d61c0..31ffb3a 100644 --- a/test/score_components/coverage-test.js +++ b/test/score_components/coverage-test.js @@ -59,7 +59,9 @@ describe('score component: code coverage', () => { repo: 'slack-health-score', }, }; - fakeCodecov.repos_commits_retrieve.onCall(9).resolves({ data: { totals: { misses: 42 } } }); + fakeCodecov.repos_commits_retrieve + .onCall(9) + .resolves({ data: { totals: { misses: 42 } } }); const res = await cov(fakeCore, fakeGithub); assert.strictEqual(res, 42); }); @@ -79,7 +81,9 @@ describe('score component: code coverage', () => { }, }; fakeCodecov.repos_commits_retrieve.onFirstCall().resolves({ data: {} }); - fakeCodecov.repos_commits_retrieve.onSecondCall().resolves({ data: { totals: { misses: 42 } } }); + fakeCodecov.repos_commits_retrieve + .onSecondCall() + .resolves({ data: { totals: { misses: 42 } } }); const startTime = performance.now(); const res = await cov(fakeCore, fakeGithub); @@ -106,18 +110,26 @@ describe('score component: code coverage', () => { }, }; fakeCodecov.repos_commits_retrieve.onFirstCall().resolves({ data: {} }); - fakeCodecov.repos_commits_retrieve.onSecondCall().resolves({ data: { totals: { misses: 42 } } }); + fakeCodecov.repos_commits_retrieve + .onSecondCall() + .resolves({ data: { totals: { misses: 42 } } }); const res = await cov(fakeCore, fakeGithub); assert.equal(res, 0); assert.equal(fakeCodecov.repos_commits_retrieve.callCount, 1); - assert(fakeCore.warning.calledWithMatch(sinon.match('Reached maximum attempts'))); + assert( + fakeCore.warning.calledWithMatch( + sinon.match('Reached maximum attempts'), + ), + ); }); it('should call actions/core.error if `codecov_treat_timeout_as_error` set to true and `codecov_max_attempts` exceeded', async () => { fakeCore.getInput.withArgs('codecov_token').returns('abcd1234'); fakeCore.getInput.withArgs('codecov_max_attempts').returns('1'); fakeCore.getInput.withArgs('codecov_retry_delay').returns('10'); - fakeCore.getInput.withArgs('codecov_treat_timeout_as_error').returns('true'); + fakeCore.getInput + .withArgs('codecov_treat_timeout_as_error') + .returns('true'); fakeGithub.context = { eventName: 'pull_request', @@ -130,9 +142,13 @@ describe('score component: code coverage', () => { }, }; fakeCodecov.repos_commits_retrieve.onFirstCall().resolves({ data: {} }); - fakeCodecov.repos_commits_retrieve.onSecondCall().resolves({ data: { totals: { misses: 42 } } }); + fakeCodecov.repos_commits_retrieve + .onSecondCall() + .resolves({ data: { totals: { misses: 42 } } }); await cov(fakeCore, fakeGithub); - assert(fakeCore.error.calledWith(sinon.match('Reached maximum attempts'))); + assert( + fakeCore.error.calledWith(sinon.match('Reached maximum attempts')), + ); }); }); }); diff --git a/test/score_components/problematic-comments-test.js b/test/score_components/problematic-comments-test.js index 488e9c7..794bfbc 100644 --- a/test/score_components/problematic-comments-test.js +++ b/test/score_components/problematic-comments-test.js @@ -9,15 +9,25 @@ describe('score component: problematic comments', () => { sinon.reset(); }); it('should throw error incase execSync fails', async () => { - const score = { comments: grepForProblematicComments(fakeCore, ['js'], [], []), coverageMisses: 0 }; + const score = { + comments: grepForProblematicComments(fakeCore, ['js'], [], []), + coverageMisses: 0, + }; fakeChildProcess.execSync.throws('error'); fakeFs.returns(null); - assert(fakeCore.error.calledWith(sinon.match('child_process execSync failed to execute'))); + assert( + fakeCore.error.calledWith( + sinon.match('child_process execSync failed to execute'), + ), + ); assert.deepEqual(score.comments, []); }); describe('should handle different inputs', async () => { it('should default to . if includes not provided', async () => { - const score = { comments: grepForProblematicComments(fakeCore, ['js'], [], []), coverageMisses: 0 }; + const score = { + comments: grepForProblematicComments(fakeCore, ['js'], [], []), + coverageMisses: 0, + }; fakeChildProcess.execSync.returns(''); fakeFs.returns(null); let find = 'find .'; @@ -27,14 +37,25 @@ describe('score component: problematic comments', () => { assert.deepEqual(score.comments, []); }); it('should default to gitignore if excludes not provided', async () => { - const score = { comments: grepForProblematicComments(fakeCore, ['js'], [], []), coverageMisses: 0 }; + const score = { + comments: grepForProblematicComments(fakeCore, ['js'], [], []), + coverageMisses: 0, + }; fakeChildProcess.execSync.returns(''); fakeFs.returns(null); assert(fakeFs.calledWith('.gitignore')); assert.deepEqual(score.comments, []); }); it('should handle both includes and excludes', async () => { - const score = { comments: grepForProblematicComments(fakeCore, ['js'], ['src'], ['test/']), coverageMisses: 0 }; + const score = { + comments: grepForProblematicComments( + fakeCore, + ['js'], + ['src'], + ['test/'], + ), + coverageMisses: 0, + }; fakeChildProcess.execSync.returns(''); fakeFs.returns(null); let find = 'find src'; @@ -45,7 +66,15 @@ describe('score component: problematic comments', () => { assert.deepEqual(score.comments, []); }); it('should handle both multiples includes, excludes and extensions', async () => { - const score = { comments: grepForProblematicComments(fakeCore, ['js', 'ts'], ['src', 'dir'], ['test/', 'dist']), coverageMisses: 5 }; + const score = { + comments: grepForProblematicComments( + fakeCore, + ['js', 'ts'], + ['src', 'dir'], + ['test/', 'dist'], + ), + coverageMisses: 5, + }; fakeChildProcess.execSync.returns(''); fakeFs.returns(null); let find = 'find src dir'; @@ -59,33 +88,104 @@ describe('score component: problematic comments', () => { describe('should generate JSON in required format with grep results', async () => { it('should handle empty grep results', async () => { fakeChildProcess.execSync.returns(''); - const score = { comments: grepForProblematicComments(fakeCore, ['js'], ['src'], []), coverageMisses: 0 }; + const score = { + comments: grepForProblematicComments(fakeCore, ['js'], ['src'], []), + coverageMisses: 0, + }; assert.deepEqual(score.comments, []); }); it('should handle grep results', async () => { fakeChildProcess.execSync.returns('path:10: // TODO: random stuff'); - const score = { comments: grepForProblematicComments(fakeCore, ['js'], ['src'], []), coverageMisses: 0 }; - assert.deepEqual(score.comments, [{ path: 'path', line_no: 10, comment: '// TODO: random stuff', commentType: 'TODO' }]); + const score = { + comments: grepForProblematicComments(fakeCore, ['js'], ['src'], []), + coverageMisses: 0, + }; + assert.deepEqual(score.comments, [ + { + path: 'path', + line_no: 10, + comment: '// TODO: random stuff', + commentType: 'TODO', + }, + ]); }); it('should handle multiple grep results', async () => { - fakeChildProcess.execSync.returns('path:10: // TODO: random stuff \n path2:15: // FIXME: random stuff'); - const score = { comments: grepForProblematicComments(fakeCore, ['js'], ['src'], []), coverageMisses: 0 }; - assert.deepEqual(score.comments, [{ path: 'path', line_no: 10, comment: '// TODO: random stuff', commentType: 'TODO' }, { path: 'path2', line_no: 15, comment: '// FIXME: random stuff', commentType: 'FIXME' }]); + fakeChildProcess.execSync.returns( + 'path:10: // TODO: random stuff \n path2:15: // FIXME: random stuff', + ); + const score = { + comments: grepForProblematicComments(fakeCore, ['js'], ['src'], []), + coverageMisses: 0, + }; + assert.deepEqual(score.comments, [ + { + path: 'path', + line_no: 10, + comment: '// TODO: random stuff', + commentType: 'TODO', + }, + { + path: 'path2', + line_no: 15, + comment: '// FIXME: random stuff', + commentType: 'FIXME', + }, + ]); }); it('should handle grep results not in the correct format', async () => { - fakeChildProcess.execSync.returns('path:10: // TODO: random stuff \n path2:15: // random things TODO'); - const score = { comments: grepForProblematicComments(fakeCore, ['js'], ['src'], []), coverageMisses: 0 }; - assert.deepEqual(score.comments, [{ path: 'path', line_no: 10, comment: '// TODO: random stuff', commentType: 'TODO' }, { path: 'path2', line_no: 15, comment: '// random things TODO', commentType: null }]); + fakeChildProcess.execSync.returns( + 'path:10: // TODO: random stuff \n path2:15: // random things TODO', + ); + const score = { + comments: grepForProblematicComments(fakeCore, ['js'], ['src'], []), + coverageMisses: 0, + }; + assert.deepEqual(score.comments, [ + { + path: 'path', + line_no: 10, + comment: '// TODO: random stuff', + commentType: 'TODO', + }, + { + path: 'path2', + line_no: 15, + comment: '// random things TODO', + commentType: null, + }, + ]); }); it('should handle grep results that have types without colons', async () => { fakeChildProcess.execSync.returns('path:10: // TODO random stuff'); - const score = { comments: grepForProblematicComments(fakeCore, ['js'], ['src'], []), coverageMisses: 0 }; - assert.deepEqual(score.comments, [{ path: 'path', line_no: 10, comment: '// TODO random stuff', commentType: 'TODO' }]); + const score = { + comments: grepForProblematicComments(fakeCore, ['js'], ['src'], []), + coverageMisses: 0, + }; + assert.deepEqual(score.comments, [ + { + path: 'path', + line_no: 10, + comment: '// TODO random stuff', + commentType: 'TODO', + }, + ]); }); it('should handle grep results that starts with code', async () => { - fakeChildProcess.execSync.returns('path:10: const result = "hello:world" // TODO: random stuff'); - const score = { comments: grepForProblematicComments(fakeCore, ['js'], ['src'], []), coverageMisses: 0 }; - assert.deepEqual(score.comments, [{ path: 'path', line_no: 10, comment: '// TODO: random stuff', commentType: 'TODO' }]); + fakeChildProcess.execSync.returns( + 'path:10: const result = "hello:world" // TODO: random stuff', + ); + const score = { + comments: grepForProblematicComments(fakeCore, ['js'], ['src'], []), + coverageMisses: 0, + }; + assert.deepEqual(score.comments, [ + { + path: 'path', + line_no: 10, + comment: '// TODO: random stuff', + commentType: 'TODO', + }, + ]); }); }); }); diff --git a/test/stubs/stubs.js b/test/stubs/stubs.js index cfa29b7..ef0cf42 100644 --- a/test/stubs/stubs.js +++ b/test/stubs/stubs.js @@ -2,8 +2,8 @@ const sinon = require('sinon'); const core = require('@actions/core'); const github = require('@actions/github'); const codecov = require('@api/codecov'); -const child_process = require('child_process'); -const fs = require('fs'); +const child_process = require('node:child_process'); +const fs = require('node:fs'); const hs = require('../../src/health-score'); const fakeCore = sinon.stub(core); @@ -13,4 +13,12 @@ const fakeChildProcess = sinon.stub(child_process); const fakeComments = sinon.stub(hs, 'grep'); const fakeCoverage = sinon.stub(hs, 'coverage'); const fakeFs = sinon.stub(fs, 'existsSync'); -module.exports = { fakeCore, fakeGithub, fakeCodecov, fakeChildProcess, fakeComments, fakeCoverage, fakeFs }; +module.exports = { + fakeCore, + fakeGithub, + fakeCodecov, + fakeChildProcess, + fakeComments, + fakeCoverage, + fakeFs, +};