From 5488967ec193cbe4bcffcf5dad01696ae2e1c321 Mon Sep 17 00:00:00 2001 From: Ameer A Date: Tue, 24 Dec 2019 16:08:54 +0200 Subject: [PATCH] feat(options): migrate API to use glob patterns BREAKING CHANGE: old API (reportDir and rootDir) has been dropped in favor of the new Glob patterns allowing further configurability for report file names BREAKING CHANGE: drops support for 10.0.0 > Node >= 8.6.0 --- .circleci/config.yml | 4 ++-- .eslintignore | 1 + README.md | 13 +++++-------- bin/mochawesome-merge.js | 20 +++++++++++--------- lib/index.js | 28 ++++++++++++---------------- package.json | 4 ++-- tests/merge.js | 34 ++++++++++++++++------------------ yarn.lock | 12 ++++++++++++ 8 files changed, 61 insertions(+), 55 deletions(-) create mode 100644 .eslintignore diff --git a/.circleci/config.yml b/.circleci/config.yml index 1638e2a..709628a 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -6,7 +6,7 @@ version: 2 jobs: test: docker: - - image: circleci/node:8 + - image: circleci/node:10 environment: CODECOV_TOKEN: f8ed9d20-3500-440b-bf81-896d29d1393e steps: @@ -34,7 +34,7 @@ jobs: release: docker: - - image: circleci/node:8 + - image: circleci/node:10 steps: - checkout - run: yarn install diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 0000000..4ebc8ae --- /dev/null +++ b/.eslintignore @@ -0,0 +1 @@ +coverage diff --git a/README.md b/README.md index c34dd36..8f522dd 100644 --- a/README.md +++ b/README.md @@ -27,9 +27,9 @@ const { merge } = require('mochawesome-merge') // See Options below const options = { - rootDir: process.cwd(), - reportDir: 'report', + reportsGlob: './report/*.json', } + merge(options).then(report => { console.log(report) }) @@ -38,15 +38,12 @@ merge(options).then(report => { ## CLI ``` -$ mochawesome-merge --rootDir [rootDirectory] --reportDir [directory] > output.json +$ mochawesome-merge [reportsGlob] > output.json ``` -## Options - -- `rootDir` (optional) — source mochawesome JSON reports root directory. Defaults to `process.cwd()`. -- `reportDir` (optional) — source mochawesome JSON reports directory. Defaults to `mochawesome-report`. +## Arguments -The final mochawesome JSON reports will be in `path.resolve(rootDir, reportDir)`. +- `reportsGlob`: glob pattern for source mochawesome JSON reports. [string] [default: "./mochawesome-report/mochawesome*.json"] ## [Cypress](https://github.com/cypress-io/cypress) diff --git a/bin/mochawesome-merge.js b/bin/mochawesome-merge.js index 830cfef..e56cad1 100755 --- a/bin/mochawesome-merge.js +++ b/bin/mochawesome-merge.js @@ -1,15 +1,17 @@ #!/usr/bin/env node + const { merge } = require('../lib/index') -const { argv } = require('yargs') - .option('rootDir', { - type: 'string', - description: 'source mochawesome JSON reports root directory.', - }) - .option('reportDir', { - type: 'string', - description: 'source mochawesome JSON reports directory.', - }) +const { argv } = require('yargs').command( + '$0 [reportsGlob]', + 'Merge several Mochawesome JSON reports', + yargs => + yargs.positional('reportsGlob', { + type: 'string', + default: './mochawesome-report/mochawesome*.json', + description: 'glob pattern for source mochawesome JSON reports.', + }) +) merge(argv).then( report => { diff --git a/lib/index.js b/lib/index.js index 9dc1f05..a614c5e 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,6 +1,5 @@ const fse = require('fs-extra') -const minimatch = require('minimatch') -const path = require('path') +const glob = require('glob') const { flatMap } = require('./utils') function generateStats(suites) { @@ -28,16 +27,14 @@ function generateStats(suites) { } } -async function collectReportFiles(dir) { - const files = await fse.readdir(dir).catch(() => { - throw new Error(`Directory ${dir} does not exist`) - }) - return Promise.all( - files - .filter(file => minimatch(file, 'mochawesome*.json')) - .map(filename => path.resolve(dir, filename)) - .map(filename => fse.readJson(filename)) - ) +function collectReportFiles(reportsGlob) { + const files = glob.sync(reportsGlob) + + if (files.length === 0) { + throw new Error(`Pattern ${reportsGlob} matched no report files`) + } + + return Promise.all(files.map(filename => fse.readJson(filename))) } const collectReportSuites = flatMap(report => @@ -50,12 +47,11 @@ const getAllTests = flatMap(suite => [ ]) exports.merge = async function merge({ - rootDir = process.cwd(), - reportDir = 'mochawesome-report', + reportsGlob = './mochawesome-report/mochawesome*.json', } = {}) { - const dir = path.resolve(rootDir, reportDir) - const reports = await collectReportFiles(dir) + const reports = await collectReportFiles(reportsGlob) const suites = collectReportSuites(reports) + return { stats: generateStats(suites), results: suites, diff --git a/package.json b/package.json index dbd86c4..bccffa7 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "author": "Anton Telesh ", "license": "MIT", "engines": { - "node": ">=8.6.0" + "node": ">=10.0.0" }, "scripts": { "test": "is-ci && jest --coverage && codecov || jest --coverage", @@ -36,7 +36,7 @@ }, "dependencies": { "fs-extra": "^7.0.1", - "minimatch": "^3.0.4", + "glob": "^7.1.6", "uuid": "^3.3.2", "yargs": "^12.0.5" }, diff --git a/tests/merge.js b/tests/merge.js index aa2b812..b7d9d9d 100644 --- a/tests/merge.js +++ b/tests/merge.js @@ -1,11 +1,9 @@ const { merge } = require('../lib') -const path = require('path') describe('merge', () => { test('merges configs', async () => { const report = await merge({ - rootDir: process.cwd(), - reportDir: path.resolve(__dirname, './mochawesome-report'), + reportsGlob: `./tests/mochawesome-report/mochawesome*.json`, }) const suites = report.results @@ -15,31 +13,31 @@ describe('merge', () => { }) expect(suites.length).toBe(3) - expect(suites[0].tests.length).toBe(2) - expect(suites[0].passes.length).toBe(2) - expect(suites[0].failures.length).toBe(0) + expect(suites[0].tests.length).toBe(1) + expect(suites[0].passes.length).toBe(0) + expect(suites[0].failures.length).toBe(1) - expect(suites[1].tests.length).toBe(1) - expect(suites[1].passes.length).toBe(0) - expect(suites[1].failures.length).toBe(1) + expect(suites[1].tests.length).toBe(3) + expect(suites[1].passes.length).toBe(1) + expect(suites[1].failures.length).toBe(2) - expect(suites[2].tests.length).toBe(3) - expect(suites[2].passes.length).toBe(1) - expect(suites[2].failures.length).toBe(2) + expect(suites[2].tests.length).toBe(2) + expect(suites[2].passes.length).toBe(2) + expect(suites[2].failures.length).toBe(0) }) test('throws when invalid directory provided', async () => { - const dirname = './invalid-directory' - const dir = path.resolve(process.cwd(), dirname) - await expect(merge({ reportDir: dirname })).rejects.toEqual( - new Error(`Directory ${dir} does not exist`) + const reportsGlob = './invalid-directory/mochawesome*.json' + + await expect(merge({ reportsGlob })).rejects.toEqual( + new Error(`Pattern ${reportsGlob} matched no report files`) ) }) test('defaults to mochawesome-report directory', async () => { - const dir = path.resolve(process.cwd(), 'mochawesome-report') + const reportsGlob = './mochawesome-report/mochawesome*.json' await expect(merge()).rejects.toEqual( - new Error(`Directory ${dir} does not exist`) + new Error(`Pattern ${reportsGlob} matched no report files`) ) }) }) diff --git a/yarn.lock b/yarn.lock index 2aed83c..dda31c1 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2421,6 +2421,18 @@ glob@^7.1.3: once "^1.3.0" path-is-absolute "^1.0.0" +glob@^7.1.6: + version "7.1.6" + resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.6.tgz#141f33b81a7c2492e125594307480c46679278a6" + integrity sha512-LwaxwyZ72Lk7vZINtNNrywX0ZuLyStrdDtabefZKAY5ZGJhVtgdznluResxNmPitE0SAO+O26sWTHeKSI2wMBA== + dependencies: + fs.realpath "^1.0.0" + inflight "^1.0.4" + inherits "2" + minimatch "^3.0.4" + once "^1.3.0" + path-is-absolute "^1.0.0" + global-dirs@^0.1.0: version "0.1.1" resolved "https://registry.yarnpkg.com/global-dirs/-/global-dirs-0.1.1.tgz#b319c0dd4607f353f3be9cca4c72fc148c49f445"