48 lines
2.8 KiB
JavaScript
48 lines
2.8 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import { execFileSync } from 'node:child_process';
|
|
import { copyFileSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync } from 'node:fs';
|
|
import { tmpdir } from 'node:os';
|
|
import { basename, dirname, join, resolve } from 'node:path';
|
|
import { test } from 'node:test';
|
|
import { pathToFileURL } from 'node:url';
|
|
import { projectRoot, resolveProjectPath } from './project-paths.mjs';
|
|
|
|
test('relative paths use the relocated script root instead of the caller directory', async () => {
|
|
const fixture = mkdtempSync(join(tmpdir(), 'linkport-path-test-'));
|
|
try {
|
|
const relocated = join(fixture, 'moved project [spaces]');
|
|
mkdirSync(join(relocated, 'scripts'), { recursive: true });
|
|
const module = join(relocated, 'scripts', 'project-paths.mjs');
|
|
copyFileSync(resolveProjectPath('scripts/project-paths.mjs'), module);
|
|
const { resolveProjectPath: resolveMoved } = await import(pathToFileURL(module));
|
|
assert.equal(resolveMoved('bin/linkport.exe'), join(relocated, 'bin', 'linkport.exe'));
|
|
assert.equal(resolveMoved(join(fixture, 'external.exe')), join(fixture, 'external.exe'));
|
|
const output = execFileSync(process.execPath, ['--input-type=module', '-e',
|
|
`import { resolveProjectPath } from ${JSON.stringify(pathToFileURL(module).href)}; console.log(resolveProjectPath('bin/linkport.exe'));`],
|
|
{ cwd: tmpdir(), encoding: 'utf8', windowsHide: true });
|
|
assert.equal(output.trim(), join(relocated, 'bin', 'linkport.exe'));
|
|
} finally {
|
|
assert.equal(dirname(resolve(fixture)), resolve(tmpdir()));
|
|
assert(basename(fixture).startsWith('linkport-path-test-'));
|
|
rmSync(fixture, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
test('verification and browser launcher resolve binary arguments from the project', () => {
|
|
assert.equal(resolveProjectPath(''), projectRoot);
|
|
for (const name of ['verify-package.mjs', 'verify-desktop.mjs', 'verify-random-port.mjs']) {
|
|
assert.match(readFileSync(resolveProjectPath(`scripts/${name}`), 'utf8'), /resolveProjectPath\(process\.argv\[2\]\)/);
|
|
}
|
|
assert.match(readFileSync(resolveProjectPath('web/tests/start-server.mjs'), 'utf8'), /resolveProjectPath\(process\.env\.LINKPORT_E2E_BINARY/);
|
|
});
|
|
|
|
test('maintained scripts contain no fixed Windows installation or developer paths', () => {
|
|
const scripts = readdirSync(resolveProjectPath('scripts')).filter(name => /\.(ps1|mjs|sh)$/.test(name) && !name.includes('.test.'));
|
|
const paths = [...scripts.map(name => `scripts/${name}`), 'start-dev.cmd', 'package.cmd', 'web/tests/start-server.mjs'];
|
|
for (const path of paths) {
|
|
const source = readFileSync(resolveProjectPath(path), 'utf8');
|
|
assert.doesNotMatch(source, /\b[A-Za-z]:[\\/]/, `Fixed drive path in ${path}`);
|
|
assert.doesNotMatch(source, /\$env:(?:ProgramFiles|ProgramW6432|LOCALAPPDATA|USERPROFILE)/i, `Machine installation scan in ${path}`);
|
|
}
|
|
});
|