From 8be63a652e3dda91655013142f261e407a1c10cd Mon Sep 17 00:00:00 2001 From: Ed Moore Date: Wed, 12 May 2021 22:34:21 +1000 Subject: [PATCH] Coverage directory -s (#89) * coverage directory * fix tests --- bin/codecov | 4 ++++ src/index.js | 5 +++-- test/fixtures/coverage.txt | 1 + test/fixtures/other/coverage.txt | 1 + test/index.test.js | 27 +++++++++++++++++++++++++++ 5 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/coverage.txt create mode 100644 test/fixtures/other/coverage.txt diff --git a/bin/codecov b/bin/codecov index a2e5bf5e4..b67ddf6d8 100755 --- a/bin/codecov +++ b/bin/codecov @@ -43,6 +43,10 @@ var argv = require("yargs") // eslint-disable-line alias: "P", description: "Specify the pull request number mannually" }, + dir: { + alias: "s", + description: "Directory to search for coverage reports.\nAlready searches project root and current working directory" + }, token: { alias: "t", default: "", diff --git a/src/index.js b/src/index.js index 2a1a4ce81..49a6628d2 100644 --- a/src/index.js +++ b/src/index.js @@ -19,6 +19,7 @@ function dryRun (uploadHost, token, query, uploadFile) { * @param {Object} args * @param {string} args.build Specify the build number manually * @param {string} args.branch Specify the branch manually + * @param {string} args.dir Directory to search for coverage reports. * @param {string} args.env Specify environment variables to be included with this build * @param {string} args.sha Specify the commit SHA mannually * @param {string} args.file Target file(s) to upload @@ -82,7 +83,7 @@ async function main (args) { let coverageFilePaths = [] if (!args.file) { coverageFilePaths = fileHelpers.getCoverageFiles( - projectRoot, + args.dir || projectRoot, // TODO: Determine why this is so slow (I suspect it's walking paths it should not) fileHelpers.coverageFilePatterns() ) @@ -117,7 +118,7 @@ async function main (args) { for (let index = 0; index < coverageFilePaths.length; index++) { const coverageFile = coverageFilePaths[index] const fileContents = await fileHelpers.readCoverageFile( - '.', + args.dir || projectRoot, coverageFile ) uploadFile = uploadFile.concat(fileHelpers.fileHeader(coverageFile)) diff --git a/test/fixtures/coverage.txt b/test/fixtures/coverage.txt new file mode 100644 index 000000000..5e031f342 --- /dev/null +++ b/test/fixtures/coverage.txt @@ -0,0 +1 @@ +An example coverage root file diff --git a/test/fixtures/other/coverage.txt b/test/fixtures/other/coverage.txt new file mode 100644 index 000000000..00ac4d6da --- /dev/null +++ b/test/fixtures/other/coverage.txt @@ -0,0 +1 @@ +An example coverage other file diff --git a/test/index.test.js b/test/index.test.js index f42545c2f..108f7766c 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -109,4 +109,31 @@ describe('Uploader Core', function () { }) expect(result).toEqual({ status: 'success', resultURL: 'https://results.codecov.io' }) }, 30000) + + it('Can find all coverage from root dir', async function () { + jest.spyOn(process, 'exit').mockImplementation(() => {}) + const log = jest.spyOn(console, 'log') + await app.main({ + name: 'customname', + token: 'abcdefg', + url: 'https://codecov.io', + dryRun: true, + }) + expect(log).toHaveBeenCalledWith(expect.stringMatching(/An example coverage root file/)) + expect(log).toHaveBeenCalledWith(expect.stringMatching(/An example coverage other file/)) + }) + + it('Can find only coverage from custom dir', async function () { + jest.spyOn(process, 'exit').mockImplementation(() => {}) + const log = jest.spyOn(console, 'log') + await app.main({ + name: 'customname', + token: 'abcdefg', + url: 'https://codecov.io', + dryRun: true, + dir: './test/fixtures/other' + }) + expect(log).toHaveBeenCalledWith(expect.stringMatching(/An example coverage other file/)) + expect(log).not.toHaveBeenCalledWith(expect.stringMatching(/An example coverage root file/)) + }) })