run-tests.mjs 1.2 KB

1234567891011121314151617181920212223242526272829303132
  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 = [
  20. ...collect(path.join(root, 'mcp', 'src')),
  21. ...collect(path.join(root, 'test'))
  22. ].sort();
  23. if (!tests.length) throw new Error('No tests found.');
  24. const before = workspaceTemps();
  25. const result = spawnSync(process.execPath, ['--test', ...tests], { cwd: root, stdio: 'inherit' });
  26. const created = [...workspaceTemps()].filter(name => !before.has(name));
  27. if (created.length) {
  28. console.error(`Tests polluted the workspace root: ${created.join(', ')}`);
  29. process.exit(1);
  30. }
  31. process.exit(result.status || 0);