|
| 1 | +import fs from 'fs' |
| 2 | +import path from 'path' |
| 3 | +import chalk from 'chalk' |
| 4 | +import less from 'less' |
| 5 | +import strip from 'strip-css-comments' |
| 6 | +const diff = require('diff') |
| 7 | + |
| 8 | +const INCLUDE_PATH = path.resolve(__dirname, '..') |
| 9 | +const SPEC_DIR = path.resolve(__dirname, 'specs') |
| 10 | + |
| 11 | +function logDiff (left, right) { |
| 12 | + diff.diffLines(left, right).forEach(item => { |
| 13 | + if (item.added || item.removed) { |
| 14 | + let text = item.value |
| 15 | + .replace('\n', '\u00b6\n') |
| 16 | + .replace('\ufeff', '[[BOM]]') |
| 17 | + log(chalk[item.added ? 'green' : 'red'](text)) |
| 18 | + } else { |
| 19 | + let value = item.value.replace('\ufeff', '[[BOM]]') |
| 20 | + let lines = value.split('\n') |
| 21 | + |
| 22 | + // max line count for each item |
| 23 | + let keepLines = 6 |
| 24 | + // lines to be omitted |
| 25 | + let omitLines = lines.length - keepLines |
| 26 | + if (lines.length > keepLines) { |
| 27 | + lines.splice( |
| 28 | + Math.floor(keepLines / 2), |
| 29 | + omitLines, |
| 30 | + chalk.gray('(...' + omitLines + ' lines omitted...)') |
| 31 | + ) |
| 32 | + } |
| 33 | + log(lines.join('\n')) |
| 34 | + } |
| 35 | + }) |
| 36 | + log('\n') |
| 37 | +} |
| 38 | + |
| 39 | +function extractName (file, ext) { |
| 40 | + let pattern = new RegExp('([^\\/\\\\]+)\\.' + ext + '$', 'i') |
| 41 | + let match = file.match(pattern) |
| 42 | + return match ? match[1] : null |
| 43 | +} |
| 44 | + |
| 45 | +function log (msg) { |
| 46 | + process.stdout.write(msg) |
| 47 | +} |
| 48 | + |
| 49 | +function logLine (msg) { |
| 50 | + log(msg + '\n') |
| 51 | +} |
| 52 | + |
| 53 | +function getTests (specDir) { |
| 54 | + /** |
| 55 | + * Get all modules |
| 56 | + */ |
| 57 | + let srcDir = path.resolve(__dirname, '../src/components') |
| 58 | + let modules = [ |
| 59 | + 'global', |
| 60 | + ...fs |
| 61 | + .readdirSync(srcDir) |
| 62 | + .map(moduleFile => extractName(moduleFile, 'less')) |
| 63 | + .filter(m => m) |
| 64 | + ] |
| 65 | + |
| 66 | + /** |
| 67 | + * Get test suites |
| 68 | + */ |
| 69 | + let suites = [] |
| 70 | + let noTests = [] |
| 71 | + |
| 72 | + modules.forEach(module => { |
| 73 | + let moduleDir = path.join(specDir, module) |
| 74 | + if (!fs.existsSync(moduleDir)) { |
| 75 | + noTests.push(module) |
| 76 | + return |
| 77 | + } |
| 78 | + if (fs.statSync(moduleDir).isDirectory()) { |
| 79 | + let files = fs.readdirSync(moduleDir) |
| 80 | + if (files.length === 0) { |
| 81 | + noTests.push(module) |
| 82 | + } |
| 83 | + files.forEach(partFile => { |
| 84 | + let part = extractName(partFile, 'less') |
| 85 | + if (!part) { |
| 86 | + // .css files |
| 87 | + return |
| 88 | + } |
| 89 | + let src = fs.readFileSync(path.resolve(moduleDir, partFile), { |
| 90 | + encoding: 'utf8' |
| 91 | + }) |
| 92 | + |
| 93 | + let expected = '' |
| 94 | + if (fs.existsSync(path.resolve(moduleDir, part + '.css'))) { |
| 95 | + expected = fs.readFileSync(path.resolve(moduleDir, part) + '.css', { |
| 96 | + encoding: 'utf8' |
| 97 | + }) |
| 98 | + } |
| 99 | + |
| 100 | + suites.push({ |
| 101 | + part: chalk.bold(module) + chalk.white('.') + part, |
| 102 | + src, |
| 103 | + expected |
| 104 | + }) |
| 105 | + }) |
| 106 | + } |
| 107 | + }) |
| 108 | + if (noTests.length) { |
| 109 | + logLine( |
| 110 | + '\u2731 No test specs found for the following module' + |
| 111 | + (noTests.length > 1 ? 's' : '') + |
| 112 | + ':\n' + |
| 113 | + noTests.join('\n') + |
| 114 | + '\n' |
| 115 | + ) |
| 116 | + } else { |
| 117 | + logLine('\u2731 Great. Each module has got test specs.\n') |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Prepare tests |
| 122 | + */ |
| 123 | + return suites.map(suite => { |
| 124 | + return done => { |
| 125 | + less |
| 126 | + .render(suite.src, { |
| 127 | + paths: [INCLUDE_PATH], |
| 128 | + javascriptEnabled: true |
| 129 | + }) |
| 130 | + .then( |
| 131 | + result => { |
| 132 | + let passed = true |
| 133 | + let actual = strip(result.css, { preserve: false }) |
| 134 | + .replace(/\n+/g, '\n') |
| 135 | + .replace(/^\n/, '') |
| 136 | + let expected = suite.expected |
| 137 | + if (actual !== expected) { |
| 138 | + logLine(chalk.red('\u2718 ' + suite.part)) |
| 139 | + logDiff(actual, expected) |
| 140 | + passed = false |
| 141 | + } else { |
| 142 | + logLine(chalk.green('\u2714 ' + suite.part)) |
| 143 | + } |
| 144 | + done(passed) |
| 145 | + }, |
| 146 | + err => { |
| 147 | + logLine(chalk.red('\u2718 ' + suite.part)) |
| 148 | + logLine('Less compile error:') |
| 149 | + logLine(err) |
| 150 | + done(false) |
| 151 | + } |
| 152 | + ) |
| 153 | + } |
| 154 | + }) |
| 155 | +} |
| 156 | + |
| 157 | +class TestRunner { |
| 158 | + constructor (tests, endCallback) { |
| 159 | + this.tests = tests |
| 160 | + this.total = tests.length |
| 161 | + this.failed = 0 |
| 162 | + this.endCallback = endCallback |
| 163 | + } |
| 164 | + |
| 165 | + next () { |
| 166 | + let runner = this.tests.shift() |
| 167 | + if (runner) { |
| 168 | + runner(passed => { |
| 169 | + if (!passed) { |
| 170 | + this.failed++ |
| 171 | + } |
| 172 | + this.next() |
| 173 | + }) |
| 174 | + return |
| 175 | + } |
| 176 | + this.end() |
| 177 | + } |
| 178 | + |
| 179 | + end () { |
| 180 | + logLine('\n--------\n') |
| 181 | + if (!this.failed) { |
| 182 | + logLine( |
| 183 | + 'All ' + this.total + ' spec' + (this.total > 1 ? 's' : '') + ' passed.' |
| 184 | + ) |
| 185 | + } else { |
| 186 | + let passed = this.total - this.failed |
| 187 | + logLine( |
| 188 | + passed + |
| 189 | + ' spec' + |
| 190 | + (passed > 1 ? 's' : '') + |
| 191 | + ' passed, ' + |
| 192 | + this.failed + |
| 193 | + ' spec' + |
| 194 | + (this.failed > 1 ? 's' : '') + |
| 195 | + ' failed.' |
| 196 | + ) |
| 197 | + |
| 198 | + // exit with code 1 |
| 199 | + process.on('exit', () => { |
| 200 | + process.reallyExit(1) |
| 201 | + }) |
| 202 | + } |
| 203 | + |
| 204 | + this.endCallback && this.endCallback() |
| 205 | + } |
| 206 | + |
| 207 | + start () { |
| 208 | + this.next() |
| 209 | + } |
| 210 | +} |
| 211 | + |
| 212 | +new TestRunner(getTests(SPEC_DIR)).start() |
0 commit comments