|
1 | 1 | const test = require('node:test'); |
2 | 2 | const assert = require('node:assert/strict'); |
3 | 3 | const path = require('path'); |
| 4 | +const childProcess = require('child_process'); |
4 | 5 |
|
5 | 6 | const helpersPath = path.join(__dirname, '..', 'lib', 'validation-helpers.js'); |
6 | | -const childProcessId = require.resolve('child_process'); |
7 | 7 |
|
8 | | -test('getAuthToken passes --allow-no-subscriptions to az', () => { |
9 | | - const originalChildProcess = require.cache[childProcessId]; |
| 8 | +test('getAuthToken passes --allow-no-subscriptions to az', (t) => { |
| 9 | + const originalExecSync = childProcess.execSync; |
10 | 10 | let capturedCommand = null; |
11 | 11 |
|
12 | | - require.cache[childProcessId] = { |
13 | | - id: childProcessId, |
14 | | - filename: childProcessId, |
15 | | - loaded: true, |
16 | | - exports: { |
17 | | - execSync: (command, options) => { |
18 | | - capturedCommand = command; |
19 | | - const out = 'fake-token-value\n'; |
20 | | - return options && options.encoding ? out : Buffer.from(out); |
21 | | - }, |
22 | | - }, |
| 12 | + childProcess.execSync = (command, options) => { |
| 13 | + capturedCommand = command; |
| 14 | + const out = 'fake-token-value\n'; |
| 15 | + return options && options.encoding ? out : Buffer.from(out); |
23 | 16 | }; |
24 | 17 | delete require.cache[require.resolve(helpersPath)]; |
25 | 18 |
|
26 | | - try { |
27 | | - const { getAuthToken } = require(helpersPath); |
28 | | - const token = getAuthToken('https://example.crm.dynamics.com'); |
29 | | - |
30 | | - assert.equal(token, 'fake-token-value'); |
31 | | - assert.match(capturedCommand, /^az account get-access-token /); |
32 | | - assert.match(capturedCommand, /--allow-no-subscriptions/); |
33 | | - assert.match(capturedCommand, /--resource "https:\/\/example\.crm\.dynamics\.com"/); |
34 | | - } finally { |
35 | | - if (originalChildProcess) require.cache[childProcessId] = originalChildProcess; |
36 | | - else delete require.cache[childProcessId]; |
| 19 | + t.after(() => { |
| 20 | + childProcess.execSync = originalExecSync; |
37 | 21 | delete require.cache[require.resolve(helpersPath)]; |
38 | | - } |
| 22 | + }); |
| 23 | + |
| 24 | + const { getAuthToken } = require(helpersPath); |
| 25 | + const token = getAuthToken('https://example.crm.dynamics.com'); |
| 26 | + |
| 27 | + assert.equal(token, 'fake-token-value'); |
| 28 | + assert.match(capturedCommand, /^az account get-access-token /); |
| 29 | + assert.match(capturedCommand, /--allow-no-subscriptions/); |
| 30 | + assert.match(capturedCommand, /--resource "https:\/\/example\.crm\.dynamics\.com"/); |
39 | 31 | }); |
0 commit comments