|
1 | 1 | 'use strict' |
2 | 2 |
|
3 | 3 | const assert = require('node:assert/strict') |
| 4 | +const fs = require('node:fs') |
| 5 | +const os = require('node:os') |
| 6 | +const path = require('node:path') |
4 | 7 | const { promisify } = require('node:util') |
5 | 8 |
|
6 | 9 | const dc = require('dc-polyfill') |
@@ -326,7 +329,6 @@ describe('child process', () => { |
326 | 329 | abortController: sinon.match.instanceOf(AbortController), |
327 | 330 | shell: true, |
328 | 331 | }) |
329 | | - sinon.assert.calledOnce(start) |
330 | 332 | sinon.assert.calledWithMatch(asyncFinish, { |
331 | 333 | command: 'echo', |
332 | 334 | file: 'echo', |
@@ -701,6 +703,130 @@ describe('child process', () => { |
701 | 703 | }) |
702 | 704 | }) |
703 | 705 | }) |
| 706 | + |
| 707 | + describe('fork', () => { |
| 708 | + let tmpScript |
| 709 | + |
| 710 | + before(() => { |
| 711 | + tmpScript = path.join(os.tmpdir(), `dd-trace-test-fork-${Date.now()}.js`) |
| 712 | + fs.writeFileSync(tmpScript, 'process.exit(0)') |
| 713 | + }) |
| 714 | + |
| 715 | + after(() => { |
| 716 | + try { fs.unlinkSync(tmpScript) } catch (e) { /* ignore */ } |
| 717 | + }) |
| 718 | + |
| 719 | + it('should execute success callbacks', (done) => { |
| 720 | + const child = childProcess.fork(tmpScript) |
| 721 | + |
| 722 | + child.once('close', () => { |
| 723 | + sinon.assert.calledOnce(start) |
| 724 | + sinon.assert.calledWithMatch(start, { |
| 725 | + command: tmpScript, |
| 726 | + file: tmpScript, |
| 727 | + shell: false, |
| 728 | + abortController: sinon.match.instanceOf(AbortController), |
| 729 | + }) |
| 730 | + sinon.assert.calledOnce(asyncFinish) |
| 731 | + sinon.assert.calledWithMatch(asyncFinish, { |
| 732 | + command: tmpScript, |
| 733 | + file: tmpScript, |
| 734 | + shell: false, |
| 735 | + result: 0, |
| 736 | + }) |
| 737 | + sinon.assert.notCalled(error) |
| 738 | + done() |
| 739 | + }) |
| 740 | + }) |
| 741 | + |
| 742 | + it('should publish arguments', (done) => { |
| 743 | + const child = childProcess.fork(tmpScript, ['--flag']) |
| 744 | + |
| 745 | + child.once('close', () => { |
| 746 | + sinon.assert.calledOnce(start) |
| 747 | + sinon.assert.calledWithMatch(start, { |
| 748 | + command: `${tmpScript} --flag`, |
| 749 | + file: tmpScript, |
| 750 | + fileArgs: ['--flag'], |
| 751 | + shell: false, |
| 752 | + abortController: sinon.match.instanceOf(AbortController), |
| 753 | + }) |
| 754 | + done() |
| 755 | + }) |
| 756 | + }) |
| 757 | + |
| 758 | + it('should execute error callback for non-existent module', (done) => { |
| 759 | + const child = childProcess.fork('non_existent_module_test.js') |
| 760 | + |
| 761 | + child.once('error', () => {}) |
| 762 | + |
| 763 | + child.once('close', () => { |
| 764 | + sinon.assert.calledOnce(start) |
| 765 | + sinon.assert.calledWithMatch(start, { |
| 766 | + command: 'non_existent_module_test.js', |
| 767 | + file: 'non_existent_module_test.js', |
| 768 | + shell: false, |
| 769 | + }) |
| 770 | + sinon.assert.calledOnce(error) |
| 771 | + done() |
| 772 | + }) |
| 773 | + }) |
| 774 | + }) |
| 775 | + |
| 776 | + describe('callArgs on context', () => { |
| 777 | + function injectTestEnv (context) { |
| 778 | + if (!context.callArgs) return |
| 779 | + const args = context.callArgs |
| 780 | + const opts = args[2] != null && typeof args[2] === 'object' ? args[2] : {} |
| 781 | + args[2] = { ...opts, env: { ...process.env, DD_TEST_VAR: 'injected' } } |
| 782 | + } |
| 783 | + |
| 784 | + it('should include callArgs for async methods', (done) => { |
| 785 | + const child = childProcess.spawn('echo', ['hello']) |
| 786 | + |
| 787 | + child.once('close', () => { |
| 788 | + sinon.assert.calledOnce(start) |
| 789 | + const context = start.firstCall.firstArg |
| 790 | + assert.ok(Array.isArray(context.callArgs)) |
| 791 | + assert.strictEqual(context.callArgs[0], 'echo') |
| 792 | + assert.deepStrictEqual(context.callArgs[1], ['hello']) |
| 793 | + done() |
| 794 | + }) |
| 795 | + }) |
| 796 | + |
| 797 | + it('should include callArgs for sync methods', () => { |
| 798 | + childProcess.spawnSync('echo', ['hello']) |
| 799 | + |
| 800 | + sinon.assert.calledOnce(start) |
| 801 | + const context = start.firstCall.firstArg |
| 802 | + assert.ok(Array.isArray(context.callArgs)) |
| 803 | + assert.strictEqual(context.callArgs[0], 'echo') |
| 804 | + assert.deepStrictEqual(context.callArgs[1], ['hello']) |
| 805 | + }) |
| 806 | + |
| 807 | + it('should allow subscribers to mutate callArgs for async methods', (done) => { |
| 808 | + childProcessChannel.subscribe({ start: injectTestEnv }) |
| 809 | + |
| 810 | + const script = 'process.exit(process.env.DD_TEST_VAR === "injected" ? 0 : 1)' |
| 811 | + const child = childProcess.spawn('node', ['-e', script]) |
| 812 | + |
| 813 | + child.once('close', (code) => { |
| 814 | + childProcessChannel.unsubscribe({ start: injectTestEnv }) |
| 815 | + assert.strictEqual(code, 0) |
| 816 | + done() |
| 817 | + }) |
| 818 | + }) |
| 819 | + |
| 820 | + it('should allow subscribers to mutate callArgs for sync methods', () => { |
| 821 | + childProcessChannel.subscribe({ start: injectTestEnv }) |
| 822 | + |
| 823 | + const script = 'process.exit(process.env.DD_TEST_VAR === "injected" ? 0 : 1)' |
| 824 | + const result = childProcess.spawnSync('node', ['-e', script]) |
| 825 | + |
| 826 | + childProcessChannel.unsubscribe({ start: injectTestEnv }) |
| 827 | + assert.strictEqual(result.status, 0) |
| 828 | + }) |
| 829 | + }) |
704 | 830 | }) |
705 | 831 | }) |
706 | 832 | }) |
0 commit comments