-
-
Notifications
You must be signed in to change notification settings - Fork 53
feat: add support for stdin JSON input #135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
46456f0
6ff9634
75ac031
32d38ce
1380e08
64424bf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -152,5 +152,211 @@ test('Test cli flags', (t) => { | |
| }) | ||
| }) | ||
|
|
||
| t.test('test stdin with valid JSON', (tt) => { | ||
| const validCommit = { | ||
| id: '2b98d02b52', | ||
| message: 'stream: make null an invalid chunk to write in object mode\n\nthis harmonizes behavior between readable, writable, and transform\nstreams so that they all handle nulls in object mode the same way by\nconsidering them invalid chunks.\n\nPR-URL: https://github.com/nodejs/node/pull/6170\nReviewed-By: James M Snell <jasnell@gmail.com>\nReviewed-By: Matteo Collina <matteo.collina@gmail.com>' | ||
| } | ||
| const input = JSON.stringify([validCommit]) | ||
|
|
||
| const ls = spawn('./bin/cmd.js', ['-']) | ||
| let compiledData = '' | ||
| let errorData = '' | ||
|
|
||
| ls.stdout.on('data', (data) => { | ||
| compiledData += data | ||
| }) | ||
|
|
||
| ls.stderr.on('data', (data) => { | ||
| errorData += data | ||
| }) | ||
|
|
||
| ls.stdin.write(input) | ||
| ls.stdin.end() | ||
|
|
||
| ls.on('close', (code) => { | ||
| tt.equal(code, 0, 'CLI exits with zero code on success') | ||
| tt.match(compiledData, /[^0-9a-f]2b98d02b52[^0-9a-f]/, 'output contains commit id') | ||
| tt.equal(errorData, '', 'no error output') | ||
| tt.end() | ||
| }) | ||
| }) | ||
|
|
||
| t.test('test stdin with invalid commit (missing subsystem)', (tt) => { | ||
| const invalidCommit = { | ||
| id: 'def456', | ||
| message: 'this is a bad commit message without subsystem\n\nPR-URL: https://github.com/nodejs/node/pull/1234\nReviewed-By: Someone <someone@example.com>' | ||
| } | ||
| const input = JSON.stringify([invalidCommit]) | ||
|
|
||
| const ls = spawn('./bin/cmd.js', ['-']) | ||
| let compiledData = '' | ||
|
|
||
| ls.stdout.on('data', (data) => { | ||
| compiledData += data | ||
| }) | ||
|
|
||
| ls.stdin.write(input) | ||
| ls.stdin.end() | ||
|
|
||
| ls.on('close', (code) => { | ||
| tt.notEqual(code, 0, 'CLI exits with non-zero code on failure') | ||
| tt.match(compiledData, /def456/, 'output contains commit id') | ||
| tt.match(compiledData, /title-format/, 'output mentions the rule violation') | ||
| tt.end() | ||
| }) | ||
| }) | ||
|
|
||
| t.test('test stdin with multiple commits', (tt) => { | ||
| const commits = [ | ||
| { | ||
| id: 'commit1', | ||
| message: 'doc: update README\n\nPR-URL: https://github.com/nodejs/node/pull/1111\nReviewed-By: Someone <someone@example.com>' | ||
| }, | ||
| { | ||
| id: 'commit2', | ||
| message: 'test: add new test case\n\nPR-URL: https://github.com/nodejs/node/pull/2222\nReviewed-By: Someone <someone@example.com>' | ||
| } | ||
| ] | ||
| const input = JSON.stringify(commits) | ||
|
|
||
| const ls = spawn('./bin/cmd.js', ['-']) | ||
| let compiledData = '' | ||
|
|
||
| ls.stdout.on('data', (data) => { | ||
| compiledData += data | ||
| }) | ||
|
|
||
| ls.stdin.write(input) | ||
| ls.stdin.end() | ||
|
|
||
| ls.on('close', (code) => { | ||
| tt.equal(code, 0, 'CLI exits with zero code on success') | ||
| tt.match(compiledData, /commit1/, 'output contains first commit id') | ||
| tt.match(compiledData, /commit2/, 'output contains second commit id') | ||
| tt.end() | ||
| }) | ||
| }) | ||
|
|
||
| t.test('test stdin with TAP output', (tt) => { | ||
| const validCommit = { | ||
| id: '69435db261', | ||
| message: 'chore: update tested node release lines (#94)' | ||
| } | ||
| const input = JSON.stringify([validCommit]) | ||
|
|
||
| const ls = spawn('./bin/cmd.js', ['--no-validate-metadata', '--tap', '-']) | ||
| let compiledData = '' | ||
|
|
||
| ls.stdout.on('data', (data) => { | ||
| compiledData += data | ||
| }) | ||
|
|
||
| ls.stdin.write(input) | ||
| ls.stdin.end() | ||
|
|
||
| ls.on('close', (code) => { | ||
| const output = compiledData.trim() | ||
| tt.match(output, | ||
| /# 69435db261/, | ||
| 'TAP output contains the sha of the commit being linted') | ||
| tt.match(output, | ||
| /not ok \d+ subsystem: Invalid subsystem: "chore" \(chore: update tested node release lines \(#94\)\)/, | ||
| 'TAP output contains failure for subsystem') | ||
| tt.match(output, | ||
| /# fail\s+\d+/, | ||
| 'TAP output contains total failures') | ||
| tt.match(output, | ||
| /# Please review the commit message guidelines:\s# https:\/\/github.com\/nodejs\/node\/blob\/HEAD\/doc\/contributing\/pull-requests.md#commit-message-guidelines/, | ||
| 'TAP output contains pointer to commit message guidelines') | ||
| tt.equal(code, 1, 'CLI exits with non-zero code on failure') | ||
| tt.end() | ||
| }) | ||
| }) | ||
|
|
||
| t.test('test stdin with invalid JSON', (tt) => { | ||
| const input = 'this is not valid JSON' | ||
|
|
||
| const ls = spawn('./bin/cmd.js', ['-']) | ||
| let errorData = '' | ||
|
|
||
| ls.stderr.on('data', (data) => { | ||
| errorData += data | ||
| }) | ||
|
|
||
| ls.stdin.write(input) | ||
| ls.stdin.end() | ||
|
|
||
| ls.on('close', (code) => { | ||
| tt.equal(code, 1, 'CLI exits with non-zero code on error') | ||
| tt.match(errorData, /Error parsing JSON input/, 'error message is shown') | ||
| tt.end() | ||
| }) | ||
| }) | ||
|
|
||
| t.test('test stdin with non-array JSON', (tt) => { | ||
| const input = JSON.stringify({ id: 'test', message: 'test' }) | ||
|
|
||
| const ls = spawn('./bin/cmd.js', ['-']) | ||
| let errorData = '' | ||
|
|
||
| ls.stderr.on('data', (data) => { | ||
| errorData += data | ||
| }) | ||
|
|
||
| ls.stdin.write(input) | ||
| ls.stdin.end() | ||
|
|
||
| ls.on('close', (code) => { | ||
| tt.equal(code, 1, 'CLI exits with non-zero code on error') | ||
| tt.match(errorData, /Input must be an array/, 'error message is shown') | ||
|
Comment on lines
+310
to
+312
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems likely to create bad DX. If it's not an array, just wrap it in an array.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If there's a use-case for passing a non-array, we can consider it. Until then, it's probably more helpful to throw an error. |
||
| tt.end() | ||
| }) | ||
| }) | ||
|
|
||
| t.test('test stdin with missing properties', (tt) => { | ||
| const input = JSON.stringify([{ id: 'test' }]) // missing 'message' | ||
|
|
||
| const ls = spawn('./bin/cmd.js', ['-']) | ||
| let errorData = '' | ||
|
|
||
| ls.stderr.on('data', (data) => { | ||
| errorData += data | ||
| }) | ||
|
|
||
| ls.stdin.write(input) | ||
| ls.stdin.end() | ||
|
|
||
| ls.on('close', (code) => { | ||
| tt.equal(code, 1, 'CLI exits with non-zero code on error') | ||
| tt.match(errorData, /must have "id" and "message" properties/, 'error message is shown') | ||
| tt.end() | ||
| }) | ||
| }) | ||
|
|
||
| t.test('test stdin with --no-validate-metadata', (tt) => { | ||
| const commit = { | ||
| id: 'novalidate', | ||
| message: 'doc: update README\n\nThis commit has no PR-URL or reviewers' | ||
| } | ||
| const input = JSON.stringify([commit]) | ||
|
|
||
| const ls = spawn('./bin/cmd.js', ['--no-validate-metadata', '-']) | ||
| let compiledData = '' | ||
|
|
||
| ls.stdout.on('data', (data) => { | ||
| compiledData += data | ||
| }) | ||
|
|
||
| ls.stdin.write(input) | ||
| ls.stdin.end() | ||
|
|
||
| ls.on('close', (code) => { | ||
| tt.equal(code, 0, 'CLI exits with zero code when metadata validation is disabled') | ||
| tt.match(compiledData, /novalidate/, 'output contains commit id') | ||
| tt.end() | ||
| }) | ||
| }) | ||
|
|
||
| t.end() | ||
| }) | ||
Uh oh!
There was an error while loading. Please reload this page.