2aa9d73325
集中保留原版权声明,移除文件头检查和全局测试忽略规则。验证:全量单测、受影响用例复测及静态检查通过。
85 lines
3.1 KiB
JavaScript
85 lines
3.1 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { mkdtemp, mkdir, writeFile, rm } from 'node:fs/promises';
|
|
import { tmpdir } from 'node:os';
|
|
import path from 'node:path';
|
|
import { createServer } from 'node:net';
|
|
import { spawn } from 'node:child_process';
|
|
import { fileURLToPath } from 'node:url';
|
|
import { verifyBuild } from '../../scripts/verify-build.mjs';
|
|
|
|
test('static build validation rejects missing entry resources', async () => {
|
|
const directory = await mkdtemp(path.join(tmpdir(), 'monkey-pw-preview-'));
|
|
try {
|
|
await assert.rejects(verifyBuild(directory), /ENOENT/);
|
|
await writeFile(
|
|
path.join(directory, 'index.html'),
|
|
'<script src="/assets/app.js"></script>',
|
|
);
|
|
await assert.rejects(verifyBuild(directory), /ENOENT/);
|
|
await mkdir(path.join(directory, 'assets'));
|
|
await writeFile(path.join(directory, 'assets/app.js'), 'export {}');
|
|
assert.equal((await verifyBuild(directory)).assets, 1);
|
|
await writeFile(path.join(directory, 'logo.svg'), '<svg/>');
|
|
await writeFile(
|
|
path.join(directory, 'index.html'),
|
|
'<link rel="icon" href="__WL_BRAND_FAVICON__"><script src="/assets/app.js"></script><script>const example = \'<img src="/not-a-build-resource.png">\'</script>',
|
|
);
|
|
assert.equal((await verifyBuild(directory)).assets, 2);
|
|
await writeFile(
|
|
path.join(directory, 'index.html'),
|
|
'<script src="../outside.js"></script>',
|
|
);
|
|
await assert.rejects(verifyBuild(directory), /escapes output/);
|
|
} finally {
|
|
await rm(directory, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
test('preview refuses an occupied port instead of silently choosing the next port', async () => {
|
|
const directory = await mkdtemp(path.join(tmpdir(), 'monkey-pw-preview-'));
|
|
const blocker = createServer();
|
|
try {
|
|
await writeFile(
|
|
path.join(directory, 'index.html'),
|
|
'<script src="/app.js"></script>',
|
|
);
|
|
await writeFile(path.join(directory, 'app.js'), 'export {}');
|
|
await new Promise((resolve) => blocker.listen(0, '127.0.0.1', resolve));
|
|
const port = blocker.address().port;
|
|
const result = await new Promise((resolve, reject) => {
|
|
const child = spawn(
|
|
process.execPath,
|
|
[
|
|
fileURLToPath(
|
|
new URL('../../scripts/preview-dist.mjs', import.meta.url),
|
|
),
|
|
'--port',
|
|
String(port),
|
|
],
|
|
{
|
|
cwd: fileURLToPath(new URL('../../', import.meta.url)),
|
|
env: { ...process.env, PW_DIST_DIR: directory },
|
|
windowsHide: true,
|
|
stdio: ['ignore', 'pipe', 'pipe'],
|
|
timeout: 20000,
|
|
},
|
|
);
|
|
let output = '';
|
|
child.stdout.on('data', (chunk) => {
|
|
output += chunk;
|
|
});
|
|
child.stderr.on('data', (chunk) => {
|
|
output += chunk;
|
|
});
|
|
child.on('error', reject);
|
|
child.on('close', (code) => resolve({ code, output }));
|
|
});
|
|
assert.notEqual(result.code, 0);
|
|
assert.match(result.output, /already in use/);
|
|
} finally {
|
|
await new Promise((resolve) => blocker.close(resolve));
|
|
await rm(directory, { recursive: true, force: true });
|
|
}
|
|
});
|