From 7b2bfcae00b5cfc05ec4ff9adf8d9c8eb5ae0de0 Mon Sep 17 00:00:00 2001 From: Brett Uglow Date: Sun, 27 Nov 2016 11:45:31 +1100 Subject: [PATCH] feat(testUnit): use consistent /coverage directory for Node & Browser projects Add integration test for coverage report to ensure it is created in the right place --- .../templates/config/prepublish.js.tpl | 46 ++++++++++++++++--- lib/buildTool/npm/npmResources.yml | 4 +- .../npm/testUnit/templates/istanbul.js | 2 +- .../testUnit/templates/karma.common.js.tpl | 2 +- .../testUnit/templates/karma.conf.js.tpl | 4 +- lib/core/resources.yml | 1 + package.json | 4 +- test/spec/integration/integration.spec.js | 2 +- test/spec/integration/testUnitTest.js | 34 +++++++++----- 9 files changed, 72 insertions(+), 27 deletions(-) diff --git a/lib/buildTool/npm/documentation/templates/config/prepublish.js.tpl b/lib/buildTool/npm/documentation/templates/config/prepublish.js.tpl index 9eab0bb..fd000ff 100644 --- a/lib/buildTool/npm/documentation/templates/config/prepublish.js.tpl +++ b/lib/buildTool/npm/documentation/templates/config/prepublish.js.tpl @@ -1,25 +1,57 @@ 'use strict'; -// START_CONFIT_GENERATED_CONTENT -// Update the swanky.config.yaml file's version number +// START_CONFIT_GENERATED_CONTENT<% +var configPath = paths.config.configDir + resources.documentation.configSubDir; +var relativePath = configPath.replace(/([^/]+)/g, '..'); +%> +const path = require('path'); +const basePath = path.join(__dirname, '/<%= relativePath %>'); const latestVersion = require('latest-version'); const fs = require('fs'); const pkgName = '<%- pkg.name %>'; const pkgRepo = '<%- pkg.repository.url.substr(pkg.repository.url.lastIndexOf('/') + 1).replace('.git', '') %>'; + +const IS_DRYRUN = process.argv.indexOf('-d') > -1; // Allow a dry-run to see what changes would be made + +let FILES_TO_PROCESS = [ + // Update the swanky.config.yaml file's version number and server path + { + name: '<%= documentation.srcDir %>swanky.config.yaml', + searchRE: /^version:.*$/m, + replacement: (version) => `version: ${version}` + }, + { + name: '<%= documentation.srcDir %>swanky.config.yaml', + searchRE: /^serverPath:.*$/m, + replacement: () => `serverPath: ${pkgRepo}` + } +]; + +// In the next section you can modify FILES_TO_PROCESS to meet your needs... // END_CONFIT_GENERATED_CONTENT + // START_CONFIT_GENERATED_CONTENT latestVersion(pkgName).then(version => { console.log(`Latest version of ${pkgName}: ${version}`); - // Read Swanky config YAML file, modify the version and server-path, then save it again - let text = fs.readFileSync('<%= documentation.srcDir %>swanky.config.yaml', 'utf8'); + // Patch the files + FILES_TO_PROCESS.forEach(file => { + let fileName = path.join(basePath, file.name); + let text = fs.readFileSync(fileName, 'utf8'); - text = text.replace(/^version:.*$/m, `version: ${version}`); - text = text.replace(/^serverPath:.*$/m, `serverPath: ${pkgRepo}`); + text = text.replace(file.searchRE, file.replacement(version)); - fs.writeFileSync('<%= documentation.srcDir %>swanky.config.yaml', text, 'utf8'); + if (IS_DRYRUN) { + console.info(`Updated version in ${file.name}:`); + console.info(text); + console.info('---------------'); + } else { + fs.writeFileSync(fileName, text, 'utf8'); + console.log(`Updated version in ${file.name}`); + } + }); }); // END_CONFIT_GENERATED_CONTENT diff --git a/lib/buildTool/npm/npmResources.yml b/lib/buildTool/npm/npmResources.yml index e25cfe9..63bb942 100644 --- a/lib/buildTool/npm/npmResources.yml +++ b/lib/buildTool/npm/npmResources.yml @@ -192,7 +192,7 @@ release: - condition: <%- release.checkCodeCoverage && app.repositoryType === 'GitHub' %> tasks: - name: upload-coverage - tasks: ['cat <%- paths.output.reportDir %>lcov/lcov.info | ./node_modules/coveralls/bin/coveralls.js'] + tasks: ['cat <%- paths.output.reportDir + resources.testUnit.coverageReportSubDir %>lcov/lcov.info | ./node_modules/coveralls/bin/coveralls.js'] description: Uploads code-coverage metrics to Coveralls.io features: - Setup - https://coveralls.zendesk.com/hc/en-us/articles/201347419-Coveralls-currently-supports @@ -281,7 +281,7 @@ testUnit: - Code coverage - name: test:check-coverage - tasks: ['cross-env NODE_ENV=test istanbul check-coverage <%- paths.output.reportDir %>coverage.json --config <%- paths.config.configDir + resources.testUnit.configSubDir %>istanbul.js'] + tasks: ['cross-env NODE_ENV=test istanbul check-coverage <%- paths.output.reportDir + resources.testUnit.coverageReportSubDir %>coverage.json --config <%- paths.config.configDir + resources.testUnit.configSubDir %>istanbul.js'] description: features: diff --git a/lib/buildTool/npm/testUnit/templates/istanbul.js b/lib/buildTool/npm/testUnit/templates/istanbul.js index 133046f..2640137 100644 --- a/lib/buildTool/npm/testUnit/templates/istanbul.js +++ b/lib/buildTool/npm/testUnit/templates/istanbul.js @@ -6,7 +6,7 @@ var coverageConfig = { check: require('./thresholds.json'), reporting: { print: 'both', - dir: '<%= paths.output.reportDir %>', + dir: '<%- paths.output.reportDir + resources.testUnit.coverageReportSubDir %>', reports: [ 'cobertura', 'html', diff --git a/lib/buildTool/webpack/testUnit/templates/karma.common.js.tpl b/lib/buildTool/webpack/testUnit/templates/karma.common.js.tpl index 45bd7e9..47daff4 100644 --- a/lib/buildTool/webpack/testUnit/templates/karma.common.js.tpl +++ b/lib/buildTool/webpack/testUnit/templates/karma.common.js.tpl @@ -82,7 +82,7 @@ let karmaConfig = { reporters: ['progress', 'junit', 'coverage', 'threshold'], coverageReporter: { - dir: '<%- paths.output.reportDir %>coverage', + dir: '<%- paths.output.reportDir + resources.testUnit.coverageReportSubDir %>', reporters: [ { type: 'cobertura', subdir: 'cobertura' }, { type: 'lcovonly', subdir: 'lcov' }, diff --git a/lib/buildTool/webpack/testUnit/templates/karma.conf.js.tpl b/lib/buildTool/webpack/testUnit/templates/karma.conf.js.tpl index 7114965..6fff462 100644 --- a/lib/buildTool/webpack/testUnit/templates/karma.conf.js.tpl +++ b/lib/buildTool/webpack/testUnit/templates/karma.conf.js.tpl @@ -5,7 +5,7 @@ var commonConfig = require('./karma.common.js'); var webpackHelpers = require('../webpack/webpackHelpers.js')(); var debugMode = process.argv.indexOf('--debug') > -1; -var noCoverage = process.argv.indexOf('--no-coverage') > -1; +var noThresholdCheck = process.argv.indexOf('--no-threshold-check') > -1; function getConfitConfig(config) { // level of logging @@ -34,7 +34,7 @@ function getConfitConfig(config) { }); } - if (noCoverage) { + if (noThresholdCheck) { commonConfig.reporters = commonConfig.reporters.filter(function(reporter) { return reporter !== 'threshold'; }); diff --git a/lib/core/resources.yml b/lib/core/resources.yml index 054a8fa..b2e308e 100644 --- a/lib/core/resources.yml +++ b/lib/core/resources.yml @@ -341,6 +341,7 @@ testSystem: testUnit: configSubDir: testUnit/ + coverageReportSubDir: coverage/ testVisualRegression: configSubDir: testVisualRegression/ diff --git a/package.json b/package.json index 2d4bdba..184dd4d 100644 --- a/package.json +++ b/package.json @@ -61,10 +61,10 @@ "docs:serve": "http-server website/ -o", "semantic-release": "semantic-release pre && npm publish && semantic-release post && npm run docs:publish", "start": "npm run dev", - "test": "npm-run-all updateFixtures test:unit:once test:seq", + "test": "npm-run-all updateFixtures test:unit:once test:int", "test:check-coverage": "cross-env NODE_ENV=test istanbul check-coverage reports/coverage.json --config config/testUnit/istanbul.js", "test:coverage": "npm-run-all test:unit:once test:check-coverage --silent", - "test:seq": "node test/testRunner.js --sequence --MAX_LOG=true", + "test:int": "node test/testRunner.js --sequence --MAX_LOG=true", "test:unit": "chokidar 'lib/**/*.js' 'test/**/*.js' -c 'npm run test:unit:once' --initial --silent", "test:unit:once": "cross-env NODE_ENV=test istanbul cover --config config/testUnit/istanbul.js _mocha -- --opts config/testUnit/mocha.opts", "updateFixtures": "node test/updateFixtures.js", diff --git a/test/spec/integration/integration.spec.js b/test/spec/integration/integration.spec.js index f9b79ec..cbf24b3 100644 --- a/test/spec/integration/integration.spec.js +++ b/test/spec/integration/integration.spec.js @@ -32,7 +32,7 @@ describe('test "' + fixtureFileName + '"', () => { require('./testBuildServe')(confitConfig, SERVER_MAX_WAIT_TIME); require('./testVerify')(confitConfig, srcDir, hasCSS); // Execute unit tests but do not fail if coverage is too low - require('./testUnitTest')(confitConfig, unitTestDir, 'npm run test:unit:once -- --no-coverage', true); + require('./testUnitTest')(confitConfig, unitTestDir, 'npm run test:unit:once -- --no-threshold-check', true); // Execute debug unit tests and ensure that there is NO coverage report require('./testUnitTest')(confitConfig, unitTestDir, 'npm run test:unit:debug:once', false); require('./testDocumentation')(confitConfig); diff --git a/test/spec/integration/testUnitTest.js b/test/spec/integration/testUnitTest.js index dff6e7b..b5a98a4 100644 --- a/test/spec/integration/testUnitTest.js +++ b/test/spec/integration/testUnitTest.js @@ -23,22 +23,34 @@ function runCommand(cmd) { module.exports = function(confitConfig, unitTestPath, commandToRun, hasCodeCoverage) { describe(commandToRun, () => { - it(`should pass the unit tests in the sampleApp code ${hasCodeCoverage ? 'WITH' : 'WITHOUT'} code coverage`, function() { + it.only(`should pass the unit tests in the sampleApp code ${hasCodeCoverage ? 'WITH' : 'WITHOUT'} code coverage`, function() { assert.doesNotThrow(() => runCommand(commandToRun)); + + if (hasCodeCoverage) { + let reportDir = path.join(process.env.TEST_DIR, confitConfig.paths.output.reportDir); + fs.readdirSync(reportDir + 'coverage/').forEach((file) => console.log(file)); + + assert(fs.existsSync(reportDir + 'coverage/') === true, 'Coverage dir exists'); + assert(fs.existsSync(reportDir + 'coverage/lcov/lcov.info') === true, 'lcov/lcov.info exists'); + fs.removeSync(reportDir); // Remove the directory + } else { + assert(fs.existsSync(reportDir + 'coverage/') === false, 'Coverage directory does NOT exist'); + } }); - // We get a table of results like this, which we need to filter to find 'File ' and 'All files ' - // ----------------|----------|----------|----------|----------|----------------| - // File | % Stmts | % Branch | % Funcs | % Lines |Uncovered Lines | - // ----------------|----------|----------|----------|----------|----------------| - // demoModule/ | 80.95 | 100 | 33.33 | 76.47 | | - // app.js | 78.57 | 100 | 25 | 72.73 | 15,26,27 | - // demoModule.js | 85.71 | 100 | 50 | 83.33 | 9 | - // ----------------|----------|----------|----------|----------|----------------| - // All files | 80.95 | 100 | 33.33 | 76.47 | | - // ----------------|----------|----------|----------|----------|----------------| it('should have 100% branch coverage for the test files', () => { + // We get a table of results like this, which we need to filter to find 'File ' and 'All files ' + // ----------------|----------|----------|----------|----------|----------------| + // File | % Stmts | % Branch | % Funcs | % Lines |Uncovered Lines | + // ----------------|----------|----------|----------|----------|----------------| + // demoModule/ | 80.95 | 100 | 33.33 | 76.47 | | + // app.js | 78.57 | 100 | 25 | 72.73 | 15,26,27 | + // demoModule.js | 85.71 | 100 | 50 | 83.33 | 9 | + // ----------------|----------|----------|----------|----------|----------------| + // All files | 80.95 | 100 | 33.33 | 76.47 | | + // ----------------|----------|----------|----------|----------|----------------| + let result = runCommand(commandToRun).toString(); console.log(result);