run-tests.mjs 1.1 KB

1234567891011121314151617181920212223242526272829
  1. import fs from 'node:fs';
  2. import path from 'node:path';
  3. import { spawnSync } from 'node:child_process';
  4. const root = path.resolve(import.meta.dirname, '..');
  5. const workspaceRoot = path.resolve(root, '..');
  6. function workspaceTemps() {
  7. return new Set(fs.readdirSync(workspaceRoot, { withFileTypes: true })
  8. .filter(entry => entry.name.startsWith('.tmp-'))
  9. .map(entry => entry.name));
  10. }
  11. function collect(directory, found = []) {
  12. for (const entry of fs.readdirSync(directory, { withFileTypes: true })) {
  13. const file = path.join(directory, entry.name);
  14. if (entry.isDirectory()) collect(file, found);
  15. else if (entry.name.endsWith('.test.mjs')) found.push(file);
  16. }
  17. return found;
  18. }
  19. const tests = collect(path.join(root, 'mcp', 'src')).sort();
  20. if (!tests.length) throw new Error('No tests found.');
  21. const before = workspaceTemps();
  22. const result = spawnSync(process.execPath, ['--test', ...tests], { cwd: root, stdio: 'inherit' });
  23. const created = [...workspaceTemps()].filter(name => !before.has(name));
  24. if (created.length) {
  25. console.error(`Tests polluted the workspace root: ${created.join(', ')}`);
  26. process.exit(1);
  27. }
  28. process.exit(result.status || 0);