|
1 | 1 | import { expect } from 'chai' |
2 | 2 | import path from 'path' |
| 3 | +import { readFileSync } from 'fs' |
3 | 4 |
|
4 | 5 | import { run } from './jq' |
5 | 6 | import { FILTER_UNDEFINED_ERROR } from './command' |
@@ -74,6 +75,31 @@ describe('jq core', () => { |
74 | 75 | }) |
75 | 76 | }) |
76 | 77 |
|
| 78 | + it('should catch errors from child process stdin', done => { |
| 79 | + // This is a very specific case of error, only triggered if: |
| 80 | + // 1) The jq process exits early (e.g. due to an invalid filter) |
| 81 | + // AND |
| 82 | + // 2) We are trying to send a large amount of data over stdin, |
| 83 | + // (which will take longer to do than jq takes to exit). |
| 84 | + const largeJsonString = JSON.parse(readFileSync(PATH_LARGE_JSON_FIXTURE)) |
| 85 | + run(FILTER_INVALID, largeJsonString, { input: 'json' }) |
| 86 | + .then(result => { |
| 87 | + done('Expected an error to be thrown from child process stdin') |
| 88 | + }) |
| 89 | + .catch(error => { |
| 90 | + expect(error).to.be.an.instanceof(Error) |
| 91 | + // On Mac/Linux, the error code is "EPIPE". |
| 92 | + // On Windows, the equivalent code is "EOF". |
| 93 | + expect(error.message).to.be.oneOf(['write EPIPE', 'write EOF']) |
| 94 | + expect(error.code).to.be.oneOf(['EPIPE', 'EOF']) |
| 95 | + expect(error.syscall).to.equal('write') |
| 96 | + done() |
| 97 | + }) |
| 98 | + .catch(error => { |
| 99 | + done(error) |
| 100 | + }) |
| 101 | + }) |
| 102 | + |
77 | 103 | it('should fail on an undefined filter', done => { |
78 | 104 | run(undefined, PATH_JSON_FIXTURE) |
79 | 105 | .catch(error => { |
|
0 commit comments