UNPKG

@testim/testim-cli

Version:

Command line interface for running Testing on you CI

121 lines (107 loc) 4.55 kB
var program = require('commander'); var Promise = require('promise'); function camelizeHifenValues(prop){ return prop.replace(/-([a-z])/g, function (m, w) {return w.toUpperCase();}); } function collect(val, col){ col.push(val); return col; } function setHostAndPortForSauceLab(){ if(!program.host){ program.host = 'ondemand.saucelabs.com'; } if(!program.port){ program.port = 80; } } function setSauceUser(val){ setHostAndPortForSauceLab(); program.sauce = program.sauce || {}; program.sauce.user = val; return val; } function setSauceKey(val){ setHostAndPortForSauceLab(); program.sauce = program.sauce || {}; program.sauce.key = val; return val; } function printUsage(){ function isDefaultHelpLine(line){ return line.indexOf("-h, --help") !== -1; } function isExtOnlyOption(line){ return line.indexOf("--ext") !== -1; } program.help(function(txt){ var lines = txt.split('\n'); return lines.filter(function(ln){ return !isDefaultHelpLine(ln) && !isExtOnlyOption(ln); }).join('\n'); }); } program .description('Testim.io CLI') .option('--help', 'output usage information', printUsage) .option('-o --options-file [options-file.json]', '') .option('-t, --testId [test-id]', 'test id to run', collect, []) .option('-l, --label [label]', 'labels to run', collect, []) .option('--project <project-id>', 'project id') .option('-r, --report-file [report xml path]', 'where to save results (if not specified result will be printed to output)') .option('-h, --host <host-name>', 'host name or ip containing the selenium grid') .option('-p, --port <host-port>', 'host port') .option('--sauce-user [sauce-lab-user]', 'user to connect to sauce labs', setSauceUser) .option('--sauce-key [sauce-lab-key]', 'key to use when connecting to sauce labs', setSauceKey) .option('--base-url [base-url]', 'change al test base-url to a specified url') .option('--token <token>', 'identification token to testim') .option('--ext [extension src path]', 'use extension from path (default it \'/..\')') .parse(process.argv); module.exports = { process : function(moreOptions) { if (moreOptions['options-file']) { program.optionsFile = moreOptions['options-file']; } // merge options from file try { var optionsFile = program.optionsFile ? require(require('path').join(process.cwd(), program.optionsFile)) : {}; function mergeValues(first, second){ return (!first || first.length === 0) ? second : first; } Object.keys(optionsFile).forEach(function(prop){ var safePropName = camelizeHifenValues(prop); program[safePropName] = mergeValues(program[safePropName], optionsFile[prop]); }); } catch(err) { console.log(err); return Promise.reject("unable to read options file"); } // merge more options Object.assign(program, moreOptions); if(!program.label && !program.testId){ return Promise.reject("missing tests to run, add either --label <label-name> or --testId <test-id>"); } if(!program.project){ return Promise.reject("missing project-id info, use --project <project-id>"); } // TODO if user using saucelab as grid we can probably use their host and if(!program.host || !program.port){ return Promise.reject("missing remote grid address parameters, specify both --host <host-name-or-ip> and --port <port-number>"); } if(!program.token){ return Promise.reject("missing Testim Access Token, use --token <testim-access-token>, contact info@testim.io if you need a new one."); } return Promise.resolve({ "testId" : program.testId || [], "label" : [].concat(program.label), "reportFile" : program.reportFile, "project" : program.project, "host" : program.host, "port" : program.port, "saucelab" : program.sauce, "baseUrl" : program.baseUrl, "token" : program.token, "ext" : program.ext }); } };