Skip to content

Commit

Permalink
fix: by default compile the scenarios to a temp directory
Browse files Browse the repository at this point in the history
fixes #1
  • Loading branch information
tpluscode committed Dec 10, 2019
1 parent e0c6680 commit 7784de3
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 10 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,23 @@ The base URI can also be changed from the run command
docker-compose run e2e-tests --baseUri http://dev-env.my.app/
```

Finally, tests can be filtered by a regular expression which is matched against the relative path to test files within
#### Filtering tests to run

Tests can be filtered by a regular expression which is matched against the relative path to test files within
the `tests` directory.
For example, running from the above `docker-compose.yml`:

```bash
docker-compose run e2e-tests --grep ^ProductsCollectionEndpoint/PostRequest_
```

#### Compile scenarios in-place

By default the tests will be compiled in a temporary path. An optional flag
can be set to have the test JSON files generated adjacent to the input files.

```
docker-compose run e2e-tests --compileInPlace
```

[hypertest]: https://testing.hypermedia.app/dsl/
3 changes: 3 additions & 0 deletions hypertest/entrypoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ if (grep) {
if (argv.dir) {
args.push('--dir', `${argv.dir}`)
}
if (argv.compileInPlace) {
args.push('--compileInPlace')
}

const analyser = spawn(
'node',
Expand Down
23 changes: 22 additions & 1 deletion hypertest/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions hypertest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@
"@fcostarodrigo/walk": "^4.0.2",
"@hydrofoil/hypertest": "^0.7",
"commander": "^4.0.1",
"copy-dir": "^1.2.0",
"hydra-validator": "^1.1.1",
"hydra-validator-e2e": "^0.8.2",
"temp": "^0.9.1",
"typescript": "^3.7.2",
"yargs": "^15.0.2"
},
"devDependencies": {
"@tpluscode/eslint-config": "0.0.3",
"@types/temp": "^0.8.34",
"@types/yargs": "^13.0.3",
"@typescript-eslint/eslint-plugin": "^2.9.0",
"@typescript-eslint/parser": "^2.9.0",
Expand Down
45 changes: 37 additions & 8 deletions hypertest/run.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { spawn } from 'child_process'
import { mkdir } from 'temp'
import copydir from 'copy-dir'
import program from 'commander'
import walk from '@fcostarodrigo/walk'

Expand All @@ -15,6 +17,7 @@ interface Scenario {
program.option('--dir <pattern>', 'Directory to run tests from', '/tests')
program.option('--grep <pattern>', 'RegExp to filter the test cases')
program.option('--baseUri <baseUri>', 'Base resource URI')
program.option('--compileInPlace', 'Compile in the same directory', false)

program.parse(process.argv)

Expand All @@ -26,26 +29,51 @@ ${texts.map(text => ` ${text}`).join('\n')}
`)
}

function parseScenarios() {
function copyScenarios(): Promise<string> {
return new Promise((resolve, reject) => {
headerLog('Compiling test scenarios', `Directory used: ${program.dir}`)
const childProcess = spawn('node_modules/.bin/hypertest-compiler', [program.dir], { stdio: 'inherit' })
if (program.compileInPlace) {
resolve(program.dir)
return
}

mkdir('', (err, tempDir) => {
if (err) {
reject(err)
return
}

copydir.sync(program.dir, tempDir)

resolve(tempDir)
})
})
}

function parseScenarios(dir: string): Promise<string> {
return new Promise((resolve, reject) => {
const headerMessages = ['Compiling test scenarios', `Directory used: ${program.dir}`]
if (dir !== program.dir) {
headerMessages.push(`Using temp directory for compiled output: ${dir}`)
}

headerLog(...headerMessages)
const childProcess = spawn('node_modules/.bin/hypertest-compiler', [dir], { stdio: 'inherit' })

childProcess.on('exit', code => {
if (code === 0) {
resolve()
resolve(dir)
}

reject(new Error('Failed to compile test scenarios'))
})
})
}

async function filterScenarios() {
async function filterScenarios(dir: string) {
const scenarios: Scenario[] = []
const skipped: string[] = []
for await (const file of walk(program.dir)) {
const matches = file.match(new RegExp(`${program.dir}/(.+)\\..+\\.hypertest\\.json$`))
for await (const file of walk(dir)) {
const matches = file.match(new RegExp(`${dir}/(.+)\\..+\\.hypertest\\.json$`))

if (!matches) {
continue
Expand Down Expand Up @@ -116,7 +144,8 @@ function summary(summary: Summary) {
process.exit(summary.failures.length)
}

parseScenarios()
copyScenarios()
.then(parseScenarios)
.then(filterScenarios)
.then(runScenarios)
.then(summary)
Expand Down

0 comments on commit 7784de3

Please sign in to comment.