ShtukbaseStandard
файл пакета→ .shtukbase/packages/std-owasp-secrets/checks/lib/std.test.ts

checks/lib/std.test.ts

84 строки, 4.1 КБ.

Файл как есть в пакете: код и комментарии в нём не переводятся.

  1. import { expect, test } from 'bun:test';
  2. import { spawnSync } from 'node:child_process';
  3. import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
  4. import { tmpdir } from 'node:os';
  5. import { dirname, join } from 'node:path';
  6. const copies = ['std-conventional-commits', 'std-keep-a-changelog', 'std-owasp-secrets'];
  7. const packages = join(import.meta.dir, '../../..');
  8. for (const pkg of copies) {
  9. test.each([false, true])(`${pkg} lists the project and places (Git: %s)`, (withGit) => {
  10. const repo = mkdtempSync(join(tmpdir(), 'shtuk-std-'));
  11. try {
  12. const project = join(repo, 'shtuks');
  13. function write(path: string, content: string): void {
  14. const file = join(repo, path);
  15. mkdirSync(dirname(file), { recursive: true });
  16. writeFileSync(file, content);
  17. }
  18. write('shtuks/local.txt', 'local');
  19. write('shtuks/nested/child/.shtukbase/packages.lock', '');
  20. write('shtuks/nested/child/base/packages.md', '---\nfolder: Child project\n---\n');
  21. write('shtuks/nested/child/index.md', '---\nplaces: [apps/site/child]\n---\n');
  22. write('shtuks/nested/child/child.txt', 'nested');
  23. write('shtuks/nested/child-other/sibling.txt', 'sibling');
  24. write('shtuks/norms/rules/fixture/example.md', '---\nexcept: [Child project]\n---\n');
  25. write('apps/site/site.txt', 'place');
  26. write('apps/site/child/child.txt', 'nested place');
  27. write('apps/other/other.txt', 'second place');
  28. write('.gitignore', 'ignored.txt\n');
  29. write('apps/site/ignored.txt', 'ignored');
  30. write('shtuks/ignored.txt', 'ignored');
  31. if (withGit) expect(spawnSync('git', ['init', '-q'], { cwd: repo }).status).toBe(0);
  32. const lib = join(packages, pkg, 'checks/lib/std.ts');
  33. const env = { ...process.env };
  34. delete env.SHTUK_ROOT;
  35. delete env.SHTUK_PLACES;
  36. function run(code: string, withPlaces = true): Record<string, string> {
  37. const result = spawnSync(process.execPath, ['-e', `
  38. import { norm, projectFiles, readTextFile } from ${JSON.stringify(lib)};
  39. ${code}
  40. if (new Set(files).size !== files.length) throw new Error('Duplicate file names');
  41. console.log(JSON.stringify(Object.fromEntries(files.map((file) => [file, readTextFile(file)]))));
  42. `], {
  43. cwd: project,
  44. encoding: 'utf8',
  45. env: withPlaces ? { ...env, SHTUK_ROOT: repo, SHTUK_PLACES: 'apps/site:apps/site/child:apps/other' } : env,
  46. });
  47. if (result.status !== 0) throw new Error(result.stderr || result.error?.message);
  48. return JSON.parse(result.stdout);
  49. }
  50. const local = {
  51. 'local.txt': 'local',
  52. 'nested/child/base/packages.md': '---\nfolder: Child project\n---\n',
  53. 'nested/child/index.md': '---\nplaces: [apps/site/child]\n---\n',
  54. 'nested/child/child.txt': 'nested',
  55. 'nested/child-other/sibling.txt': 'sibling',
  56. 'norms/rules/fixture/example.md': '---\nexcept: [Child project]\n---\n',
  57. ...(!withGit && { 'ignored.txt': 'ignored' }),
  58. };
  59. expect(run('const files = projectFiles();', false)).toEqual(local);
  60. expect(run('const files = projectFiles();')).toEqual({
  61. ...local,
  62. 'apps/site/site.txt': 'place',
  63. 'apps/site/child/child.txt': 'nested place',
  64. 'apps/other/other.txt': 'second place',
  65. ...(!withGit && { 'apps/site/ignored.txt': 'ignored' }),
  66. });
  67. const filtered = run("norm('fixture', 'example', 'enforce'); const files = projectFiles();");
  68. expect(Object.keys(filtered).some((file) => file.startsWith('nested/child/') || file.startsWith('apps/site/child/'))).toBe(false);
  69. expect(filtered['nested/child-other/sibling.txt']).toBe('sibling');
  70. expect(filtered['apps/site/site.txt']).toBe('place');
  71. expect(filtered['apps/other/other.txt']).toBe('second place');
  72. write('shtuks/apps/site/site.txt', 'same relative name');
  73. const collision = run('const files = projectFiles();');
  74. expect(collision['shtuks/apps/site/site.txt']).toBe('same relative name');
  75. expect(collision['apps/site/site.txt']).toBe('place');
  76. } finally {
  77. rmSync(repo, { recursive: true, force: true });
  78. }
  79. });
  80. }