-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(server): add stdin option to API (#2186)
* feat(server): add stdin for api * test(stdin): switch to async await tests for stdin * test(cli): use await timer
- Loading branch information
1 parent
3673232
commit 17dec00
Showing
10 changed files
with
190 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
'use strict'; | ||
|
||
function handleStdin(options) { | ||
if (options.stdin) { | ||
// listening for this event only once makes testing easier, | ||
// since it prevents event listeners from hanging open | ||
process.stdin.once('end', () => { | ||
// eslint-disable-next-line no-process-exit | ||
process.exit(0); | ||
}); | ||
|
||
process.stdin.resume(); | ||
} | ||
} | ||
|
||
module.exports = handleStdin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
'use strict'; | ||
|
||
const config = require('../fixtures/simple-config/webpack.config'); | ||
const testServer = require('../helpers/test-server'); | ||
const timer = require('../helpers/timer'); | ||
const port = require('../ports-map')['stdin-option']; | ||
|
||
describe('stdin', () => { | ||
// eslint-disable-next-line no-unused-vars | ||
let server; | ||
let exitSpy; | ||
|
||
beforeAll(() => { | ||
exitSpy = jest.spyOn(process, 'exit').mockImplementation(() => {}); | ||
}); | ||
|
||
afterEach((done) => { | ||
server = null; | ||
exitSpy.mockReset(); | ||
process.stdin.removeAllListeners('end'); | ||
testServer.close(done); | ||
}); | ||
|
||
describe('enabled', () => { | ||
beforeAll((done) => { | ||
server = testServer.start( | ||
config, | ||
{ | ||
port, | ||
stdin: true, | ||
}, | ||
done | ||
); | ||
}); | ||
|
||
it('should exit process', async () => { | ||
process.stdin.emit('end'); | ||
await timer(1000); | ||
process.stdin.pause(); | ||
expect(exitSpy.mock.calls[0]).toEqual([0]); | ||
}); | ||
}); | ||
|
||
describe('disabled (default)', () => { | ||
beforeAll((done) => { | ||
server = testServer.start( | ||
config, | ||
{ | ||
port, | ||
}, | ||
done | ||
); | ||
}); | ||
|
||
it('should not exit process', async () => { | ||
process.stdin.emit('end'); | ||
await timer(1000); | ||
process.stdin.pause(); | ||
expect(exitSpy.mock.calls.length).toEqual(0); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
'use strict'; | ||
|
||
const timer = require('../../helpers/timer'); | ||
const handleStdin = require('../../../lib/utils/handleStdin'); | ||
|
||
describe('handleStdin', () => { | ||
let exitSpy; | ||
|
||
beforeAll(() => { | ||
exitSpy = jest.spyOn(process, 'exit').mockImplementation(() => {}); | ||
}); | ||
|
||
afterEach(() => { | ||
process.stdin.removeAllListeners('end'); | ||
exitSpy.mockReset(); | ||
}); | ||
|
||
describe('enabled', () => { | ||
it('should exit process', async () => { | ||
handleStdin({ | ||
stdin: true, | ||
}); | ||
process.stdin.emit('end'); | ||
|
||
await timer(1000); | ||
process.stdin.pause(); | ||
expect(exitSpy.mock.calls[0]).toEqual([0]); | ||
}); | ||
}); | ||
|
||
describe('disabled (default)', () => { | ||
it('should not exit process', async () => { | ||
handleStdin({}); | ||
process.stdin.emit('end'); | ||
|
||
await timer(1000); | ||
process.stdin.pause(); | ||
expect(exitSpy.mock.calls.length).toEqual(0); | ||
}); | ||
}); | ||
}); |