|
5 | 5 | 'use strict' |
6 | 6 |
|
7 | 7 | const chproc = require('child_process') |
8 | | -const path = require('path') |
| 8 | +const pathModule = require('path') |
9 | 9 | const fs = require('fs') |
10 | 10 | // TODO: It shouldn't be necessary to disable n/no-extraneous-require - Research |
11 | 11 | // eslint-disable-next-line n/no-extraneous-require |
12 | 12 | const { assert } = require('chai') |
13 | 13 |
|
14 | | -const TEST_DIR = path.join(__dirname, '.') |
15 | | -process.chdir(TEST_DIR) |
| 14 | +// sub process must be executed inside TEST_DIR |
| 15 | +const TEST_DIR = pathModule.join(__dirname, '.') |
| 16 | +const execSync = (command, options) => chproc.execSync(command, { ...(options ?? {}), cwd: TEST_DIR }) |
| 17 | +const rmSync = (path, options) => fs.rmSync(pathModule.join(TEST_DIR, path), options) |
| 18 | +const readFileSync = (path, options) => fs.readFileSync(pathModule.join(TEST_DIR, path), options) |
16 | 19 |
|
17 | 20 | // This should switch to our withVersion helper. The order here currently matters. |
18 | 21 | const esbuildVersions = ['latest', '0.16.12'] |
19 | 22 |
|
20 | 23 | esbuildVersions.forEach((version) => { |
21 | 24 | describe(`esbuild ${version}`, () => { |
22 | 25 | before(() => { |
23 | | - chproc.execSync('npm install', { |
| 26 | + execSync('npm install', { |
24 | 27 | timeout: 1000 * 30 |
25 | 28 | }) |
26 | 29 | if (version !== 'latest') { |
27 | | - chproc.execSync(`npm install esbuild@${version}`, { |
| 30 | + execSync(`npm install esbuild@${version}`, { |
28 | 31 | timeout: 1000 * 30 |
29 | 32 | }) |
30 | 33 | } |
31 | 34 | }) |
32 | 35 |
|
33 | 36 | it('works', () => { |
34 | 37 | console.log('npm run build') |
35 | | - chproc.execSync('npm run build') |
| 38 | + execSync('npm run build') |
36 | 39 |
|
37 | 40 | console.log('npm run built') |
38 | 41 | try { |
39 | | - chproc.execSync('npm run built', { |
| 42 | + execSync('npm run built', { |
40 | 43 | timeout: 1000 * 30 |
41 | 44 | }) |
42 | 45 | } catch (err) { |
43 | 46 | console.error(err) |
44 | 47 | process.exit(1) |
45 | 48 | } finally { |
46 | | - fs.rmSync('./out.js', { force: true }) |
| 49 | + rmSync('./out.js', { force: true }) |
47 | 50 | } |
48 | 51 | }) |
49 | 52 |
|
50 | 53 | it('does not bundle modules listed in .external', () => { |
51 | 54 | const command = 'node ./build-and-test-skip-external.js' |
52 | 55 | console.log(command) |
53 | | - chproc.execSync(command, { |
| 56 | + execSync(command, { |
54 | 57 | timeout: 1000 * 30 |
55 | 58 | }) |
56 | 59 | }) |
57 | 60 |
|
58 | 61 | it('handles typescript apps that import without file extensions', () => { |
59 | 62 | const command = 'node ./build-and-test-typescript.mjs' |
60 | 63 | console.log(command) |
61 | | - chproc.execSync(command, { |
| 64 | + execSync(command, { |
62 | 65 | timeout: 1000 * 30 |
63 | 66 | }) |
64 | 67 | }) |
65 | 68 |
|
66 | 69 | it('handles the complex aws-sdk package with dynamic requires', () => { |
67 | 70 | const command = 'node ./build-and-test-aws-sdk.js' |
68 | 71 | console.log(command) |
69 | | - chproc.execSync(command, { |
| 72 | + execSync(command, { |
70 | 73 | timeout: 1000 * 30 |
71 | 74 | }) |
72 | 75 | }) |
73 | 76 |
|
74 | 77 | it('handles scoped node_modules', () => { |
75 | 78 | const command = 'node ./build-and-test-koa.mjs' |
76 | 79 | console.log(command) |
77 | | - chproc.execSync(command, { |
| 80 | + execSync(command, { |
78 | 81 | timeout: 1000 * 30 |
79 | 82 | }) |
80 | 83 | }) |
81 | 84 |
|
82 | 85 | it('handles instrumentations where the patching function is a property of the hook', () => { |
83 | 86 | const command = 'node ./build-and-test-openai.js' |
84 | 87 | console.log(command) |
85 | | - chproc.execSync(command, { |
| 88 | + execSync(command, { |
86 | 89 | timeout: 1000 * 30 |
87 | 90 | }) |
88 | 91 | }) |
89 | 92 |
|
90 | 93 | it('injects Git metadata into bundled applications', () => { |
91 | 94 | const command = 'node ./build-and-test-git-tags.js' |
92 | 95 | console.log(command) |
93 | | - chproc.execSync(command, { |
| 96 | + execSync(command, { |
94 | 97 | timeout: 1000 * 30 |
95 | 98 | }) |
96 | 99 | }) |
97 | 100 |
|
98 | 101 | describe('ESM', () => { |
99 | 102 | afterEach(() => { |
100 | | - fs.rmSync('./out.mjs', { force: true }) |
101 | | - fs.rmSync('./out.js', { force: true }) |
102 | | - fs.rmSync('./basic-test.mjs', { force: true }) |
| 103 | + rmSync('./out.mjs', { force: true }) |
| 104 | + rmSync('./out.js', { force: true }) |
| 105 | + rmSync('./basic-test.mjs', { force: true }) |
103 | 106 | }) |
104 | 107 |
|
105 | 108 | it('works', () => { |
106 | 109 | console.log('npm run build:esm') |
107 | | - chproc.execSync('npm run build:esm') |
| 110 | + execSync('npm run build:esm') |
108 | 111 | console.log('npm run built:esm') |
109 | | - chproc.execSync('npm run built:esm', { |
| 112 | + execSync('npm run built:esm', { |
110 | 113 | timeout: 1000 * 30 |
111 | 114 | }) |
112 | 115 | }) |
113 | 116 |
|
114 | 117 | it('should not override existing js banner', () => { |
115 | 118 | const command = 'node ./build-and-run.esm-unrelated-js-banner.mjs' |
116 | 119 | console.log(command) |
117 | | - chproc.execSync(command, { |
| 120 | + execSync(command, { |
118 | 121 | timeout: 1000 * 30 |
119 | 122 | }) |
120 | 123 |
|
121 | | - const builtFile = fs.readFileSync('./out.mjs').toString() |
| 124 | + const builtFile = readFileSync('./out.mjs').toString() |
122 | 125 | assert.include(builtFile, '/* js test */') |
123 | 126 | }) |
124 | 127 |
|
125 | 128 | it('should contain the definitions when esm is inferred from outfile', () => { |
126 | 129 | const command = 'node ./build-and-run.esm-relying-in-extension.mjs' |
127 | 130 | console.log(command) |
128 | | - chproc.execSync(command, { |
| 131 | + execSync(command, { |
129 | 132 | timeout: 1000 * 30 |
130 | 133 | }) |
131 | 134 |
|
132 | | - const builtFile = fs.readFileSync('./out.mjs').toString() |
| 135 | + const builtFile = readFileSync('./out.mjs').toString() |
133 | 136 | assert.include(builtFile, 'globalThis.__filename ??= $dd_fileURLToPath(import.meta.url);') |
134 | 137 | }) |
135 | 138 |
|
136 | 139 | it('should contain the definitions when esm is inferred from format', () => { |
137 | 140 | const command = 'node ./build-and-run.esm-relying-in-format.mjs' |
138 | 141 | console.log(command) |
139 | | - chproc.execSync(command, { |
| 142 | + execSync(command, { |
140 | 143 | timeout: 1000 * 30 |
141 | 144 | }) |
142 | 145 |
|
143 | | - const builtFile = fs.readFileSync('./out.mjs').toString() |
| 146 | + const builtFile = readFileSync('./out.mjs').toString() |
144 | 147 | assert.include(builtFile, 'globalThis.__filename ??= $dd_fileURLToPath(import.meta.url);') |
145 | 148 | }) |
146 | 149 |
|
147 | 150 | it('should contain the definitions when format is inferred from out extension', () => { |
148 | 151 | const command = 'node ./build-and-run.esm-relying-in-out-extension.mjs' |
149 | 152 | console.log(command) |
150 | | - chproc.execSync(command, { |
| 153 | + execSync(command, { |
151 | 154 | timeout: 1000 * 30 |
152 | 155 | }) |
153 | 156 |
|
154 | | - const builtFile = fs.readFileSync('./basic-test.mjs').toString() |
| 157 | + const builtFile = readFileSync('./basic-test.mjs').toString() |
155 | 158 | assert.include(builtFile, 'globalThis.__filename ??= $dd_fileURLToPath(import.meta.url);') |
156 | 159 | }) |
157 | 160 |
|
158 | 161 | it('should not contain the definitions when no esm is specified', () => { |
159 | 162 | const command = 'node ./build.js' |
160 | 163 | console.log(command) |
161 | | - chproc.execSync(command, { |
| 164 | + execSync(command, { |
162 | 165 | timeout: 1000 * 30 |
163 | 166 | }) |
164 | 167 |
|
165 | | - const builtFile = fs.readFileSync('./out.js').toString() |
| 168 | + const builtFile = readFileSync('./out.js').toString() |
166 | 169 | assert.notInclude(builtFile, 'globalThis.__filename ??= $dd_fileURLToPath(import.meta.url);') |
167 | 170 | }) |
168 | 171 |
|
169 | 172 | it('should not crash when it is already patched using global', () => { |
170 | 173 | const command = 'node ./build-and-run.esm-patched-global-banner.mjs' |
171 | 174 | console.log(command) |
172 | | - chproc.execSync(command, { |
| 175 | + execSync(command, { |
173 | 176 | timeout: 1000 * 30 |
174 | 177 | }) |
175 | 178 | }) |
176 | 179 |
|
177 | 180 | it('should not crash when it is already patched using const', () => { |
178 | 181 | const command = 'node ./build-and-run.esm-patched-const-banner.mjs' |
179 | 182 | console.log(command) |
180 | | - chproc.execSync(command, { |
| 183 | + execSync(command, { |
181 | 184 | timeout: 1000 * 30 |
182 | 185 | }) |
183 | 186 | }) |
|
0 commit comments