Browse Source

test: refactor how spec files are collected (#23816)

(cherry picked from commit 3a7775fa73b37cb216553b600505ead67fd849d7)
(cherry picked from commit c0329a257ae5a3e1a19a17a3207422b6b96f148b)
Alexey Kuzmin 4 years ago
parent
commit
b181077654
3 changed files with 74 additions and 59 deletions
  1. 27 29
      spec-main/index.js
  2. 15 0
      spec/static/get-files.js
  3. 32 30
      spec/static/index.html

+ 27 - 29
spec-main/index.js

@@ -33,7 +33,7 @@ protocol.registerSchemesAsPrivileged([
   { scheme: 'no-fetch', privileges: { corsEnabled: true } }
 ])
 
-app.whenReady().then(() => {
+app.whenReady().then(async () => {
   require('ts-node/register')
 
   const argv = require('yargs')
@@ -65,34 +65,32 @@ app.whenReady().then(() => {
   if (argv.grep) mocha.grep(argv.grep)
   if (argv.invert) mocha.invert()
 
-  // Read all test files.
-  const walker = require('walkdir').walk(__dirname, {
-    no_recurse: true
-  })
-
-  // This allows you to run specific modules only:
-  // npm run test -match=menu
-  const moduleMatch = process.env.npm_config_match
-    ? new RegExp(process.env.npm_config_match, 'g')
-    : null
-
-  const testFiles = []
-  walker.on('file', (file) => {
-    if (/-spec\.[tj]s$/.test(file) &&
-        (!moduleMatch || moduleMatch.test(file))) {
-      testFiles.push(file)
+  const filter = (file) => {
+    if (!/-spec\.[tj]s$/.test(file)) {
+      return false
     }
-  })
-
-  walker.on('end', () => {
-    testFiles.sort()
-    testFiles.forEach((file) => mocha.addFile(file))
-    const cb = () => {
-      // Ensure the callback is called after runner is defined
-      process.nextTick(() => {
-        process.exit(runner.failures)
-      })
+
+    // This allows you to run specific modules only:
+    // npm run test -match=menu
+    const moduleMatch = process.env.npm_config_match
+      ? new RegExp(process.env.npm_config_match, 'g')
+      : null
+    if (moduleMatch && !moduleMatch.test(file)) {
+      return false
     }
-    const runner = mocha.run(cb)
-  })
+
+    return true
+  }
+
+  const getFiles = require('../spec/static/get-files')
+  const testFiles = await getFiles(__dirname, { filter })
+  testFiles.sort()
+  testFiles.forEach((file) => mocha.addFile(file))
+  const cb = () => {
+    // Ensure the callback is called after runner is defined
+    process.nextTick(() => {
+      process.exit(runner.failures)
+    })
+  }
+  const runner = mocha.run(cb)
 })

+ 15 - 0
spec/static/get-files.js

@@ -0,0 +1,15 @@
+async function getFiles (directoryPath, { filter = null } = {}) {
+  const files = [];
+  const walker = require('walkdir').walk(directoryPath, {
+    no_recurse: true
+  });
+  walker.on('file', (file) => {
+    if (!filter || filter(file)) {
+      files.push(file);
+    }
+  });
+  await new Promise((resolve) => walker.on('end', resolve));
+  return files;
+}
+
+module.exports = getFiles;

+ 32 - 30
spec/static/index.html

@@ -9,7 +9,7 @@
 <div id="mocha"></div>
 
 <script type="text/javascript" charset="utf-8">
-(function() {
+(async function() {
   // Deprecated APIs are still supported and should be tested.
   process.throwDeprecation = false
 
@@ -71,41 +71,43 @@
   if (query.grep) mocha.grep(query.grep)
   if (query.invert) mocha.invert()
 
-  // Read all test files.
-  const walker = require('walkdir').walk(path.dirname(__dirname), {
-    no_recurse: true
-  })
-
   const crashSpec = 'api-crash-reporter-spec.js'
 
-  // This allows you to run specific modules only:
-  // npm run test -match=menu
-  const moduleMatch = process.env.npm_config_match
-    ? new RegExp(process.env.npm_config_match, 'g')
-    : null
-
-  const testFiles = []
-  walker.on('file', (file) => {
-    if (/-spec\.js$/.test(file) && !file.includes(crashSpec) &&
-        (!moduleMatch || moduleMatch.test(file))) {
-      testFiles.push(file)
+  const filter = (file) => {
+    if (!/-spec\.js$/.test(file)) {
+      return false
     }
-  })
 
-  walker.on('end', () => {
-    testFiles.sort()
-    testFiles.forEach((file) => mocha.addFile(file))
-    if (!process.env.npm_config_match || new RegExp(process.env.npm_config_match, 'g').test(crashSpec)) {
-      mocha.addFile(path.resolve(__dirname, '..', crashSpec))
+    if (file.includes(crashSpec)) {
+      return false
     }
 
-    const runner = mocha.run(() => {
-      // Ensure the callback is called after runner is defined
-      setTimeout(() => {
-        Mocha.utils.highlightTags('code')
-        if (isCi) ipcRenderer.send('process.exit', runner.failures)
-      }, 0)
-    })
+    // This allows you to run specific modules only:
+    // npm run test -match=menu
+    const moduleMatch = process.env.npm_config_match
+      ? new RegExp(process.env.npm_config_match, 'g')
+      : null
+    if (moduleMatch && !moduleMatch.test(file)) {
+      return false
+    }
+
+    return true
+  }
+
+  const getFiles = require('./get-files')
+  const testFiles = await getFiles(path.dirname(__dirname), { filter })
+  testFiles.sort()
+  testFiles.forEach((file) => mocha.addFile(file))
+  if (!process.env.npm_config_match || new RegExp(process.env.npm_config_match, 'g').test(crashSpec)) {
+    mocha.addFile(path.resolve(__dirname, '..', crashSpec))
+  }
+
+  const runner = mocha.run(() => {
+    // Ensure the callback is called after runner is defined
+    setTimeout(() => {
+      Mocha.utils.highlightTags('code')
+      if (isCi) ipcRenderer.send('process.exit', runner.failures)
+    }, 0)
   })
 })()
 </script>