github.com/yrj2011/jx-test-infra@v0.0.0-20190529031832-7a2065ee98eb/triage/node_modules/jasmine/lib/command.js (about) 1 var path = require('path'), 2 fs = require('fs'); 3 4 exports = module.exports = Command; 5 6 var subCommands = { 7 init: { 8 description: 'initialize jasmine', 9 action: initJasmine 10 }, 11 examples: { 12 description: 'install examples', 13 action: installExamples 14 }, 15 help: { 16 description: 'show help', 17 action: help, 18 alias: '-h' 19 }, 20 version: { 21 description: 'show jasmine and jasmine-core versions', 22 action: version, 23 alias: '-v' 24 } 25 }; 26 27 function Command(projectBaseDir, examplesDir, print) { 28 this.projectBaseDir = projectBaseDir; 29 this.specDir = path.join(projectBaseDir, 'spec'); 30 31 var command = this; 32 33 this.run = function(jasmine, commands) { 34 setEnvironmentVariables(commands); 35 36 var commandToRun; 37 Object.keys(subCommands).forEach(function(cmd) { 38 var commandObject = subCommands[cmd]; 39 if (commands.indexOf(cmd) >= 0) { 40 commandToRun = commandObject; 41 } else if(commandObject.alias && commands.indexOf(commandObject.alias) >= 0) { 42 commandToRun = commandObject; 43 } 44 }); 45 46 if (commandToRun) { 47 commandToRun.action({jasmine: jasmine, projectBaseDir: command.projectBaseDir, specDir: command.specDir, examplesDir: examplesDir, print: print}); 48 } else { 49 var env = parseOptions(commands); 50 if (env.unknownOptions.length > 0) { 51 print('Unknown options: ' + env.unknownOptions.join(', ')); 52 print(''); 53 help({print: print}); 54 } else { 55 runJasmine(jasmine, env); 56 } 57 } 58 }; 59 } 60 61 function isFileArg(arg) { 62 return arg.indexOf('--') !== 0 && !isEnvironmentVariable(arg); 63 } 64 65 function parseOptions(argv) { 66 var files = [], 67 helpers = [], 68 unknownOptions = [], 69 color = process.stdout.isTTY || false, 70 configPath, 71 filter, 72 stopOnFailure, 73 random, 74 seed; 75 76 argv.forEach(function(arg) { 77 if (arg === '--no-color') { 78 color = false; 79 } else if (arg.match("^--filter=")) { 80 filter = arg.match("^--filter=(.*)")[1]; 81 } else if (arg.match("^--helper=")) { 82 helpers.push(arg.match("^--helper=(.*)")[1]); 83 } else if (arg.match("^--stop-on-failure=")) { 84 stopOnFailure = arg.match("^--stop-on-failure=(.*)")[1] === 'true'; 85 } else if (arg.match("^--random=")) { 86 random = arg.match("^--random=(.*)")[1] === 'true'; 87 } else if (arg.match("^--seed=")) { 88 seed = arg.match("^--seed=(.*)")[1]; 89 } else if (arg.match("^--config=")) { 90 configPath = arg.match("^--config=(.*)")[1]; 91 } else if (isFileArg(arg)) { 92 files.push(arg); 93 } else if (!isEnvironmentVariable(arg)) { 94 unknownOptions.push(arg); 95 } 96 }); 97 return { 98 color: color, 99 configPath: configPath, 100 filter: filter, 101 stopOnFailure: stopOnFailure, 102 helpers: helpers, 103 files: files, 104 random: random, 105 seed: seed, 106 unknownOptions: unknownOptions 107 }; 108 } 109 110 function runJasmine(jasmine, env) { 111 jasmine.loadConfigFile(env.configPath || process.env.JASMINE_CONFIG_PATH); 112 if (env.stopOnFailure !== undefined) { 113 jasmine.stopSpecOnExpectationFailure(env.stopOnFailure); 114 } 115 if (env.seed !== undefined) { 116 jasmine.seed(env.seed); 117 } 118 if (env.random !== undefined) { 119 jasmine.randomizeTests(env.random); 120 } 121 if (env.helpers !== undefined && env.helpers.length) { 122 jasmine.addHelperFiles(env.helpers); 123 } 124 jasmine.showColors(env.color); 125 jasmine.execute(env.files, env.filter); 126 } 127 128 function initJasmine(options) { 129 var print = options.print; 130 var specDir = options.specDir; 131 makeDirStructure(path.join(specDir, 'support/')); 132 if(!fs.existsSync(path.join(specDir, 'support/jasmine.json'))) { 133 fs.writeFileSync(path.join(specDir, 'support/jasmine.json'), fs.readFileSync(path.join(__dirname, '../lib/examples/jasmine.json'), 'utf-8')); 134 } 135 else { 136 print('spec/support/jasmine.json already exists in your project.'); 137 } 138 } 139 140 function installExamples(options) { 141 var specDir = options.specDir; 142 var projectBaseDir = options.projectBaseDir; 143 var examplesDir = options.examplesDir; 144 145 makeDirStructure(path.join(specDir, 'support')); 146 makeDirStructure(path.join(specDir, 'jasmine_examples')); 147 makeDirStructure(path.join(specDir, 'helpers', 'jasmine_examples')); 148 makeDirStructure(path.join(projectBaseDir, 'lib', 'jasmine_examples')); 149 150 copyFiles( 151 path.join(examplesDir, 'spec', 'helpers', 'jasmine_examples'), 152 path.join(specDir, 'helpers', 'jasmine_examples'), 153 new RegExp(/[Hh]elper\.js/) 154 ); 155 156 copyFiles( 157 path.join(examplesDir, 'lib', 'jasmine_examples'), 158 path.join(projectBaseDir, 'lib', 'jasmine_examples'), 159 new RegExp(/\.js/) 160 ); 161 162 copyFiles( 163 path.join(examplesDir, 'spec', 'jasmine_examples'), 164 path.join(specDir, 'jasmine_examples'), 165 new RegExp(/[Ss]pec.js/) 166 ); 167 } 168 169 function help(options) { 170 var print = options.print; 171 print('Usage: jasmine [command] [options] [files]'); 172 print(''); 173 print('Commands:'); 174 Object.keys(subCommands).forEach(function(cmd) { 175 var commandNameText = cmd; 176 if(subCommands[cmd].alias) { 177 commandNameText = commandNameText + ',' + subCommands[cmd].alias; 178 } 179 print('%s\t%s', lPad(commandNameText, 10), subCommands[cmd].description); 180 }); 181 print(''); 182 print('If no command is given, jasmine specs will be run'); 183 print(''); 184 print(''); 185 186 print('Options:'); 187 print('%s\tturn off color in spec output', lPad('--no-color', 18)); 188 print('%s\tfilter specs to run only those that match the given string', lPad('--filter=', 18)); 189 print('%s\tload helper files that match the given string', lPad('--helper=', 18)); 190 print('%s\t[true|false] stop spec execution on expectation failure', lPad('--stop-on-failure=', 18)); 191 print('%s\tpath to your optional jasmine.json', lPad('--config=', 18)); 192 print(''); 193 print('The given arguments take precedence over options in your jasmine.json'); 194 print('The path to your optional jasmine.json can also be configured by setting the JASMINE_CONFIG_PATH environment variable'); 195 } 196 197 function version(options) { 198 var print = options.print; 199 print('jasmine v' + require('../package.json').version); 200 print('jasmine-core v' + options.jasmine.coreVersion()); 201 } 202 203 function lPad(str, length) { 204 if (str.length >= length) { 205 return str; 206 } else { 207 return lPad(' ' + str, length); 208 } 209 } 210 211 function copyFiles(srcDir, destDir, pattern) { 212 var srcDirFiles = fs.readdirSync(srcDir); 213 srcDirFiles.forEach(function(file) { 214 if (file.search(pattern) !== -1) { 215 fs.writeFileSync(path.join(destDir, file), fs.readFileSync(path.join(srcDir, file))); 216 } 217 }); 218 } 219 220 function makeDirStructure(absolutePath) { 221 var splitPath = absolutePath.split(path.sep); 222 splitPath.forEach(function(dir, index) { 223 if(index > 1) { 224 var fullPath = path.join(splitPath.slice(0, index).join('/'), dir); 225 if (!fs.existsSync(fullPath)) { 226 fs.mkdirSync(fullPath); 227 } 228 } 229 }); 230 } 231 232 function isEnvironmentVariable(command) { 233 var envRegExp = /(.*)=(.*)/; 234 return command.match(envRegExp); 235 } 236 237 function setEnvironmentVariables(commands) { 238 commands.forEach(function (command) { 239 var regExpMatch = isEnvironmentVariable(command); 240 if(regExpMatch) { 241 var key = regExpMatch[1]; 242 var value = regExpMatch[2]; 243 process.env[key] = value; 244 } 245 }); 246 }