diff options
| author | Achilleas Pipinellis <axil@gitlab.com> | 2019-08-20 20:22:43 +0200 |
|---|---|---|
| committer | Achilleas Pipinellis <axil@gitlab.com> | 2019-08-20 20:22:43 +0200 |
| commit | e61308ce1d82e12e5087371469baea4a452875d1 (patch) | |
| tree | 62551a3ae4eab75e5af7e3b35358c07a51b2132f /scripts/frontend | |
| parent | 4f323bb62fbe71a4352de25cab141f361a3fe1a6 (diff) | |
| parent | 2989ed078c1d45b0959dcecb1bc3c8f4740a3c0d (diff) | |
| download | gitlab-ce-docs-patch-71.tar.gz | |
Merge branch 'master' into docs-patch-71docs-patch-71
Diffstat (limited to 'scripts/frontend')
| -rwxr-xr-x | scripts/frontend/file_test_coverage.js | 88 | ||||
| -rwxr-xr-x | scripts/frontend/test.js | 23 |
2 files changed, 104 insertions, 7 deletions
diff --git a/scripts/frontend/file_test_coverage.js b/scripts/frontend/file_test_coverage.js new file mode 100755 index 00000000000..7d1eb45d4bc --- /dev/null +++ b/scripts/frontend/file_test_coverage.js @@ -0,0 +1,88 @@ +#!/usr/bin/env node + +/** + * Counts the number of frontend test files and compares them against the number of application files. + * + * Example output: + * + * Source files: 1551 + * Test files: 716 + * Coverage: 46.16% + */ + +const fs = require('fs'); +const path = require('path'); + +const sourceDirectories = ['app/assets/javascripts']; +const testDirectories = ['spec/javascripts', 'spec/frontend']; + +if (fs.existsSync('ee')) { + sourceDirectories.forEach(dir => { + sourceDirectories.push(`ee/${dir}`); + }); + + testDirectories.forEach(dir => { + testDirectories.push(`ee/${dir}`); + }); +} + +let numSourceFiles = 0; +let numTestFiles = 0; + +const isVerbose = process.argv.some(arg => arg === '-v'); + +const countSourceFiles = path => + forEachFileIn(path, fileName => { + if (fileName.endsWith('.vue') || fileName.endsWith('.js')) { + if (isVerbose) { + console.log(`source file: ${fileName}`); + } + + numSourceFiles += 1; + } + }); + +const countTestFiles = path => + forEachFileIn(path, fileName => { + if (fileName.endsWith('_spec.js')) { + if (isVerbose) { + console.log(`test file: ${fileName}`); + } + + numTestFiles += 1; + } + }); + +function forEachFileIn(dirPath, callback) { + fs.readdir(dirPath, (err, files) => { + if (err) { + console.error(err); + } + + if (!files) { + return; + } + + files.forEach(fileName => { + const absolutePath = path.join(dirPath, fileName); + const stats = fs.statSync(absolutePath); + if (stats.isFile()) { + callback(absolutePath); + } else if (stats.isDirectory()) { + forEachFileIn(absolutePath, callback); + } + }); + }); +} + +console.log(`Source directories: ${sourceDirectories.join(', ')}`); +console.log(`Test directories: ${testDirectories.join(', ')}`); + +sourceDirectories.forEach(countSourceFiles); +testDirectories.forEach(countTestFiles); + +process.on('exit', () => { + console.log(`Source files: ${numSourceFiles}`); + console.log(`Test files: ${numTestFiles}`); + console.log(`Coverage: ${((100 * numTestFiles) / numSourceFiles).toFixed(2)}%`); +}); diff --git a/scripts/frontend/test.js b/scripts/frontend/test.js index dab7176f8c1..71a8bebf0f2 100755 --- a/scripts/frontend/test.js +++ b/scripts/frontend/test.js @@ -5,19 +5,24 @@ const { EOL } = require('os'); const program = require('commander'); const chalk = require('chalk'); +const SUCCESS_CODE = 0; const JEST_ROUTE = 'spec/frontend'; const KARMA_ROUTE = 'spec/javascripts'; const COMMON_ARGS = ['--colors']; -const JEST_ARGS = ['--passWithNoTests']; -const KARMA_ARGS = ['--no-fail-on-empty-test-suite']; -const SUCCESS_CODE = 0; +const jestArgs = [...COMMON_ARGS, '--passWithNoTests']; +const karmaArgs = [...COMMON_ARGS, '--no-fail-on-empty-test-suite']; program - .version('0.1.0') .usage('[options] <file ...>') .option('-p, --parallel', 'Run tests suites in parallel') + .option( + '-w, --watch', + 'Rerun tests when files change (tests will be run in parallel if this enabled)', + ) .parse(process.argv); +const shouldParallelize = program.parallel || program.watch; + const isSuccess = code => code === SUCCESS_CODE; const combineExitCodes = codes => { @@ -31,7 +36,7 @@ const skipIfFail = fn => code => (isSuccess(code) ? fn() : code); const endWithEOL = str => (str[str.length - 1] === '\n' ? str : `${str}${EOL}`); const runTests = paths => { - if (program.parallel) { + if (shouldParallelize) { return Promise.all([runJest(paths), runKarma(paths)]).then(combineExitCodes); } else { return runJest(paths).then(skipIfFail(() => runKarma(paths))); @@ -73,11 +78,11 @@ const spawnYarnScript = (cmd, args) => { }; const runJest = args => { - return spawnYarnScript('jest', [...JEST_ARGS, ...COMMON_ARGS, ...toJestArgs(args)]); + return spawnYarnScript('jest', [...jestArgs, ...toJestArgs(args)]); }; const runKarma = args => { - return spawnYarnScript('karma', [...KARMA_ARGS, ...COMMON_ARGS, ...toKarmaArgs(args)]); + return spawnYarnScript('karma', [...karmaArgs, ...toKarmaArgs(args)]); }; const replacePath = to => path => @@ -96,6 +101,10 @@ const toKarmaArgs = paths => paths.reduce((acc, path) => acc.concat('-f', replacePathForKarma(path)), []); const main = paths => { + if (program.watch) { + jestArgs.push('--watch'); + karmaArgs.push('--single-run', 'false', '--auto-watch'); + } runTests(paths).then(code => { console.log('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'); if (isSuccess(code)) { |
