archivo del paquete→ .shtukbase/packages/std-owasp-secrets/checks/lib/std.test.ts
checks/lib/std.test.ts
84 líneas, 4.1 KB.
El archivo tal como está en el paquete: el código y sus comentarios no se traducen.
- import { expect, test } from 'bun:test';
- import { spawnSync } from 'node:child_process';
- import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
- import { tmpdir } from 'node:os';
- import { dirname, join } from 'node:path';
- const copies = ['std-conventional-commits', 'std-keep-a-changelog', 'std-owasp-secrets'];
- const packages = join(import.meta.dir, '../../..');
- for (const pkg of copies) {
- test.each([false, true])(`${pkg} lists the project and places (Git: %s)`, (withGit) => {
- const repo = mkdtempSync(join(tmpdir(), 'shtuk-std-'));
- try {
- const project = join(repo, 'shtuks');
- function write(path: string, content: string): void {
- const file = join(repo, path);
- mkdirSync(dirname(file), { recursive: true });
- writeFileSync(file, content);
- }
- write('shtuks/local.txt', 'local');
- write('shtuks/nested/child/.shtukbase/packages.lock', '');
- write('shtuks/nested/child/base/packages.md', '---\nfolder: Child project\n---\n');
- write('shtuks/nested/child/index.md', '---\nplaces: [apps/site/child]\n---\n');
- write('shtuks/nested/child/child.txt', 'nested');
- write('shtuks/nested/child-other/sibling.txt', 'sibling');
- write('shtuks/norms/rules/fixture/example.md', '---\nexcept: [Child project]\n---\n');
- write('apps/site/site.txt', 'place');
- write('apps/site/child/child.txt', 'nested place');
- write('apps/other/other.txt', 'second place');
- write('.gitignore', 'ignored.txt\n');
- write('apps/site/ignored.txt', 'ignored');
- write('shtuks/ignored.txt', 'ignored');
- if (withGit) expect(spawnSync('git', ['init', '-q'], { cwd: repo }).status).toBe(0);
- const lib = join(packages, pkg, 'checks/lib/std.ts');
- const env = { ...process.env };
- delete env.SHTUK_ROOT;
- delete env.SHTUK_PLACES;
- function run(code: string, withPlaces = true): Record<string, string> {
- const result = spawnSync(process.execPath, ['-e', `
- import { norm, projectFiles, readTextFile } from ${JSON.stringify(lib)};
- ${code}
- if (new Set(files).size !== files.length) throw new Error('Duplicate file names');
- console.log(JSON.stringify(Object.fromEntries(files.map((file) => [file, readTextFile(file)]))));
- `], {
- cwd: project,
- encoding: 'utf8',
- env: withPlaces ? { ...env, SHTUK_ROOT: repo, SHTUK_PLACES: 'apps/site:apps/site/child:apps/other' } : env,
- });
- if (result.status !== 0) throw new Error(result.stderr || result.error?.message);
- return JSON.parse(result.stdout);
- }
- const local = {
- 'local.txt': 'local',
- 'nested/child/base/packages.md': '---\nfolder: Child project\n---\n',
- 'nested/child/index.md': '---\nplaces: [apps/site/child]\n---\n',
- 'nested/child/child.txt': 'nested',
- 'nested/child-other/sibling.txt': 'sibling',
- 'norms/rules/fixture/example.md': '---\nexcept: [Child project]\n---\n',
- ...(!withGit && { 'ignored.txt': 'ignored' }),
- };
- expect(run('const files = projectFiles();', false)).toEqual(local);
- expect(run('const files = projectFiles();')).toEqual({
- ...local,
- 'apps/site/site.txt': 'place',
- 'apps/site/child/child.txt': 'nested place',
- 'apps/other/other.txt': 'second place',
- ...(!withGit && { 'apps/site/ignored.txt': 'ignored' }),
- });
- const filtered = run("norm('fixture', 'example', 'enforce'); const files = projectFiles();");
- expect(Object.keys(filtered).some((file) => file.startsWith('nested/child/') || file.startsWith('apps/site/child/'))).toBe(false);
- expect(filtered['nested/child-other/sibling.txt']).toBe('sibling');
- expect(filtered['apps/site/site.txt']).toBe('place');
- expect(filtered['apps/other/other.txt']).toBe('second place');
- write('shtuks/apps/site/site.txt', 'same relative name');
- const collision = run('const files = projectFiles();');
- expect(collision['shtuks/apps/site/site.txt']).toBe('same relative name');
- expect(collision['apps/site/site.txt']).toBe('place');
- } finally {
- rmSync(repo, { recursive: true, force: true });
- }
- });
- }