Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

Commit

Permalink
Add option to include a yaml config file (#24)
Browse files Browse the repository at this point in the history
Add option to include a JSON config file
  • Loading branch information
eastenluis authored Nov 23, 2018
1 parent 42336f9 commit 17cc7a8
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 20 deletions.
9 changes: 9 additions & 0 deletions client/config.example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"testDir": "./example",
"outputDir": "./output",
"var": {
"TEST_NAME": "some name",
"TEST_NUM": 1
},
"lambdaFunction": "sanity-dev-sanityLauncher"
}
1 change: 1 addition & 0 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"commander": "^2.15.1",
"fs-extra": "^6.0.1",
"glob": "^7.1.2",
"lodash": "^4.17.11",
"pkg": "^4.3.3",
"promise-retry": "^1.1.1",
"xml2js": "^0.4.19"
Expand Down
54 changes: 35 additions & 19 deletions client/src/cli.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
#!/usr/bin/env node
const path = require('path')

const AWS = require('aws-sdk')
const fs = require('fs-extra')
const glob = require('glob')
const program = require('commander')

const EXIT_CODES = require('./exit-codes')
const { collectVariables } = require('./utils')
const { collectVariables, retrieveConfigurations } = require('./utils')
const runTests = require('./run-tests')

const CONFIG_OPTIONS = [
'lambdaFunction',
'outputDir',
'testDir',
'testPathPattern',
'var',
]
const DEFAULT_FUNCTION_NAME = 'sanity-dev-sanityLauncher'
const DEFAULT_TEST_DIR = '.'
const DEFAULT_OUTPUT_DIR = './output'

program
.version(require('../package.json').version)
.arguments('[options] [testPathPattern]')
.option('--config <path>', 'The path to a sanity runner ' +
'configuration file, in the JSON syntax. It specifies how to find ' +
'and execute tests. It will overridden if the corresponding flag values.')
.option('--test-dir <directory>', 'Test suites directory')
.option('--test-path-pattern <regexForTestFiles>', 'Run only tests with paths matching the given regex')
.option(
'--lambda-function [functionName]',
`The AWS Lambda function name. Default to ${DEFAULT_FUNCTION_NAME} if omitted.`
Expand All @@ -26,37 +39,40 @@ program
collectVariables,
{}
)
.parse(process.argv)

program.parse(process.argv)

if (!program.testDir) {
console.log('Missing test directory.')
process.exit(EXIT_CODES.INVALID_ARGUMENT)
}
if (!program.outputDir) {
console.log('Missing result output directory.')
process.exit(EXIT_CODES.INVALID_ARGUMENT)
}

const testDir = path.resolve(process.cwd(), program.testDir)
const baseConfiguration = {}
if (program.args.length > 0)
baseConfiguration.testPathPattern = program.args[0]
const configuration = retrieveConfigurations(program, CONFIG_OPTIONS, baseConfiguration)
const testDir = path.resolve(process.cwd(), configuration.testDir || DEFAULT_TEST_DIR)
const testFileNames = glob.sync('**/*.js', { cwd: testDir })

console.log(`Reading test files in ${testDir}...`)

const testPathsRegex = configuration.testPathPattern ? new RegExp(configuration.testPathPattern) : null
const testFiles = testFileNames.reduce((payload, filename) => {
console.log(`- ${filename}`)
const filePath = path.join(testDir, filename)
if (testPathsRegex && !testPathsRegex.test(filePath))
return payload

console.log(`- ${filename}`)
const fileContent = fs.readFileSync(filePath, 'utf8')
return Object.assign(payload, { [filename]: fileContent })
}, {})

if (Object.keys(testFiles).length === 0) {
console.log(`No test file is found in "${testDir}".`)
if (testPathsRegex) {
console.log(`No test file is found matching "${configuration.testPathPattern}".`)
} else {
console.log(`No test file is found.`)
}
process.exit(EXIT_CODES.INVALID_ARGUMENT)
}

const functionName = program.lambdaFunction || DEFAULT_FUNCTION_NAME
const outputDir = path.resolve(program.outputDir || DEFAULT_OUTPUT_DIR)
const testVariables = program.var
const functionName = configuration.lambdaFunction || DEFAULT_FUNCTION_NAME
const outputDir = path.resolve(configuration.outputDir || DEFAULT_OUTPUT_DIR)
const testVariables = configuration.var

runTests(functionName, testFiles, outputDir, testVariables).then(function(testsPassed) {
console.log('All test suites ran.')
Expand Down
23 changes: 23 additions & 0 deletions client/src/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
const fs = require('fs-extra')

const _ = require('lodash')
const chalk = require('chalk')
const xml2js = require('xml2js')

Expand All @@ -15,6 +18,25 @@ const collectVariables = async (variable, variableMap) => {
return Object.assign({}, variableMap, {[key]: value})
}

/**
* Retrieve test configuration from the Commander program object
* @param {*} program
* @param {Array} acceptedConfigs
* @param {Object?} baseConfiguration
*/
const retrieveConfigurations = (program, acceptedConfigs, baseConfiguration={}) => {
const configuration = baseConfiguration
if (program.config) {
const jsonConfigs = _.pick(
JSON.parse(fs.readFileSync(program.config, 'utf8')),
acceptedConfigs,
)
_.merge(configuration, jsonConfigs)
}
const flagConfigs = _.pick(program, acceptedConfigs)
return _.merge(configuration, flagConfigs)
}

/**
* Convert JUnit XML string into object
* @param {string} resultXML JUnit result for the
Expand Down Expand Up @@ -145,4 +167,5 @@ const formatTestResults = async results => {
module.exports = {
collectVariables,
formatTestResults,
retrieveConfigurations,
}
6 changes: 5 additions & 1 deletion client/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,10 @@ lodash@^4.0.0:
version "4.17.10"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.10.tgz#1b7793cf7259ea38fb3661d4d38b3260af8ae4e7"

lodash@^4.17.11:
version "4.17.11"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.11.tgz#b39ea6229ef607ecd89e2c8df12536891cac9b8d"

map-cache@^0.2.2:
version "0.2.2"
resolved "https://registry.yarnpkg.com/map-cache/-/map-cache-0.2.2.tgz#c32abd0bd6525d9b051645bb4f26ac5dc98a0dbf"
Expand Down Expand Up @@ -1441,7 +1445,7 @@ xml2js@^0.4.19:

xmlbuilder@^4.1.0:
version "4.2.1"
resolved "https://registry.yarnpkg.com/xmlbuilder/-/xmlbuilder-4.2.1.tgz#aa58a3041a066f90eaa16c2f5389ff19f3f461a5"
resolved "http://registry.npmjs.org/xmlbuilder/-/xmlbuilder-4.2.1.tgz#aa58a3041a066f90eaa16c2f5389ff19f3f461a5"
dependencies:
lodash "^4.0.0"

Expand Down

0 comments on commit 17cc7a8

Please sign in to comment.