build: discover owned runtime modules in the formatter gate

This commit is contained in:
Sameh Khamis committed 2026-09-15 07:26:32 -07:00
1 parent 76c20be277
commit ae99843c14
13 files changed
+193 -36

No files matched your search

+90
View File
@@ -1,4 +1,5 @@
import test from 'node:test';
import { execFileSync } from 'node:child_process';
import assert from 'node:assert/strict';
import {
mkdtemp,
@@ -93,3 +94,92 @@ test(
);
},
);
test('runtime discovery includes new nested modules and respects Git and formatter exclusions', async (t) => {
const root = await fixture(t);
execFileSync('git', ['init', '-q'], { cwd: root });
await mkdir(path.join(root, 'src/nested'), { recursive: true });
await mkdir(path.join(root, 'vendor'), { recursive: true });
await writeFile(
path.join(root, 'scripts/format-runtime.json'),
JSON.stringify({ roots: ['src'] }),
);
await writeFile(path.join(root, '.gitignore'), 'src/ignored.js\n');
await writeFile(path.join(root, '.prettierignore'), 'src/generated.js\n');
const source = 'export const value="yes"';
for (const name of [
'src/nested/new.js',
'src/module.mjs',
'src/check.test.mjs',
'src/ignored.js',
'src/generated.js',
'vendor/other.js',
])
await writeFile(path.join(root, name), source);
const expected = ['adopted.js', 'src/module.mjs', 'src/nested/new.js'];
assert.deepEqual(
(await formatAdoptedFiles(root, '--check')).changed,
expected,
);
await formatAdoptedFiles(root, '--write');
assert.deepEqual((await formatAdoptedFiles(root, '--check')).changed, []);
for (const name of [
'src/check.test.mjs',
'src/ignored.js',
'src/generated.js',
'vendor/other.js',
])
assert.equal(await readFile(path.join(root, name), 'utf8'), source);
});
test('runtime scope rejects unsafe roots before formatting existing entries', async (t) => {
for (const roots of [
[],
['src', 'src'],
['../outside'],
['/absolute'],
['.'],
['src/*'],
]) {
const root = await fixture(t);
await writeFile(
path.join(root, 'scripts/format-runtime.json'),
JSON.stringify({ roots }),
);
await assert.rejects(
formatAdoptedFiles(root, '--write'),
/Runtime formatting roots/,
);
assert.equal(
await readFile(path.join(root, 'adopted.js'), 'utf8'),
'const value="yes"\r\n',
);
}
});
test(
'discovered runtime symlinks cannot escape the repository',
{ skip: process.platform === 'win32' },
async (t) => {
const root = await fixture(t);
const other = await fixture(t);
execFileSync('git', ['init', '-q'], { cwd: root });
await mkdir(path.join(root, 'src'));
await writeFile(
path.join(root, 'scripts/format-runtime.json'),
JSON.stringify({ roots: ['src'] }),
);
await symlink(
path.join(other, 'adopted.js'),
path.join(root, 'src/outside.js'),
);
await assert.rejects(
formatAdoptedFiles(root, '--write'),
/repository files/,
);
assert.equal(
await readFile(path.join(root, 'adopted.js'), 'utf8'),
'const value="yes"\r\n',
);
},
);