Skip to content

Commit

Permalink
fix: exit with non-zero if piped data is error (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
pudkrong committed Mar 14, 2023
1 parent f69a015 commit ec5f305
Show file tree
Hide file tree
Showing 7 changed files with 6,381 additions and 9,757 deletions.
3 changes: 2 additions & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx lint-staged
npx lint-staged
npm run test:unit
57 changes: 57 additions & 0 deletions features/console-reporter.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { spawn } from 'node:child_process'
import { createReadStream } from 'node:fs'
import { join } from 'node:path'

describe('console-reporter', () => {
it('should exit with code 0 if the previous process runs successfully', (done) => {
const consoleReporter = spawn(
'npx',
['tsx', 'features/console-reporter.ts'],
{
cwd: process.cwd(),
},
)

const data = createReadStream(
join(process.cwd(), 'features/fixtures/report-success.json'),
)
data
.on('data', (data) => {
consoleReporter.stdin.write(data)
})
.on('end', () => {
consoleReporter.stdin.end()
})

consoleReporter.on('close', (code) => {
expect(code).toBe(0)
done()
})
})

it('should exit with code 1 if the previous process is failed', (done) => {
const consoleReporter = spawn(
'npx',
['tsx', 'features/console-reporter.ts'],
{
cwd: process.cwd(),
},
)

const data = createReadStream(
join(process.cwd(), 'features/fixtures/report-fail.json'),
)
data
.on('data', (data) => {
consoleReporter.stdin.write(data)
})
.on('end', () => {
consoleReporter.stdin.end()
})

consoleReporter.on('close', (code) => {
expect(code).toBe(1)
done()
})
})
})
11 changes: 10 additions & 1 deletion features/console-reporter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { consoleReporter } from '@nordicsemiconductor/bdd-markdown'

const chunks: string[] = []

process.stdin.on('data', (data) => {
consoleReporter(JSON.parse(data.toString()), console.log)
chunks.push(data.toString())
})

process.stdin.on('end', () => {
const jsonData = JSON.parse(chunks.join(''))
consoleReporter(jsonData, console.log)

if (jsonData.ok !== true) process.exit(1)
})
95 changes: 95 additions & 0 deletions features/fixtures/report-fail.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
{
"ok": false,
"results": [
[
{
"root": "/",
"dir": "/path/to/features",
"base": "Calculator.feature.md",
"ext": ".md",
"name": "Calculator.feature"
},
{
"ok": false,
"results": [
[
{
"keyword": "Scenario",
"title": "Adding",
"line": 30,
"steps": [
{
"keyword": "When",
"title": "I add 1 and 2",
"line": 32
},
{
"keyword": "Then",
"title": "the result should be 3",
"line": 34
}
]
},
{
"ok": false,
"results": [
[
{
"keyword": "When",
"title": "I add 1 and 2",
"line": 32
},
{
"logs": [],
"ok": true,
"duration": 102,
"executed": {
"keyword": "When",
"title": "I add 1 and 2",
"line": 32
},
"tries": 1,
"skipped": false
}
],
[
{
"keyword": "Then",
"title": "the result should be 3",
"line": 34
},
{
"logs": [],
"ok": false,
"duration": 651,
"executed": {
"keyword": "Then",
"title": "the result should be 3",
"line": 34
},
"tries": 1,
"skipped": false
}
]
],
"duration": 753,
"logs": [],
"skipped": false
}
]
],
"duration": 771,
"logs": [],
"skipped": false
}
]
],
"summary": {
"total": 1,
"passed": 0,
"failed": 1,
"duration": 3083,
"skipped": 0
},
"name": "Calculator"
}
95 changes: 95 additions & 0 deletions features/fixtures/report-success.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
{
"ok": true,
"results": [
[
{
"root": "/",
"dir": "/path/to/features",
"base": "Calculator.feature.md",
"ext": ".md",
"name": "Calculator.feature"
},
{
"ok": true,
"results": [
[
{
"keyword": "Scenario",
"title": "Adding",
"line": 30,
"steps": [
{
"keyword": "When",
"title": "I add 1 and 2",
"line": 32
},
{
"keyword": "Then",
"title": "the result should be 3",
"line": 34
}
]
},
{
"ok": false,
"results": [
[
{
"keyword": "When",
"title": "I add 1 and 2",
"line": 32
},
{
"logs": [],
"ok": true,
"duration": 102,
"executed": {
"keyword": "When",
"title": "I add 1 and 2",
"line": 32
},
"tries": 1,
"skipped": false
}
],
[
{
"keyword": "Then",
"title": "the result should be 3",
"line": 34
},
{
"logs": [],
"ok": false,
"duration": 651,
"executed": {
"keyword": "Then",
"title": "the result should be 3",
"line": 34
},
"tries": 1,
"skipped": false
}
]
],
"duration": 753,
"logs": [],
"skipped": false
}
]
],
"duration": 771,
"logs": [],
"skipped": false
}
]
],
"summary": {
"total": 1,
"passed": 1,
"failed": 0,
"duration": 3083,
"skipped": 0
},
"name": "Calculator"
}
Loading

0 comments on commit ec5f305

Please sign in to comment.