Blame view

node_modules/less/test/browser/generator/generate.js 2.14 KB
7820380e   “wangming”   1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
  const template = require('./template')
  let config
  const fs = require('fs-extra')
  const path = require('path')
  const globby = require('globby')
  const { runner } = require('mocha-headless-chrome')
  
  
  if (process.argv[2]) {
      config = require(`./${process.argv[2]}.config`)
  } else {
      config = require('./runner.config')
  }
  
  /**
   * Generate templates and run tests
   */
  const tests = []
  const cwd = process.cwd()
  const tmpDir = path.join(cwd, 'tmp', 'browser')
  fs.ensureDirSync(tmpDir)
  fs.copySync(path.join(cwd, 'test', 'browser', 'common.js'), path.join(tmpDir, 'common.js'))
  
  let numTests = 0
  let passedTests = 0
  let failedTests = 0
  
  /** Will run the runners in a series */
  function runSerial(tasks) {
      var result = Promise.resolve()
      start = Date.now()
      tasks.forEach(task => {
          result = result.then(result => {
              if (result && result.result && result.result.stats) {
                  const stats = result.result.stats
                  numTests += stats.tests
                  passedTests += stats.passes
                  failedTests += stats.failures
              }
              return task()
          }, err => {
              console.log(err)
              failedTests += 1
          })
      })
      return result
  }
  
  Object.entries(config).forEach(entry => {
      const test = entry[1]
      const paths = globby.sync(test.src)
      const templateString = template(paths, test.options.helpers, test.options.specs)
      fs.writeFileSync(path.join(cwd, test.options.outfile), templateString)
      tests.push(() => {
          const file = 'http://localhost:8081/' + test.options.outfile
          console.log(file)
          return runner({
              file,
              timeout: 3500,
              args: ['disable-web-security']
          })
      })
  })
  
  module.exports = () => runSerial(tests).then(() => {
      if (failedTests > 0) {
          process.stderr.write(failedTests + ' Failed, ' + passedTests + ' passed\n');
      } else {
          process.stdout.write('All Passed ' + passedTests + ' run\n');
      }
      if (failedTests) {
          process.on('exit', function() { process.reallyExit(1); });
      }
      process.exit()
  }, err => {
      process.stderr.write(err.message);
      process.exit()
  })