feat(testfile-directives): Add per-test file directives to control runtime permissions and flags (Deno, Node, Bun, Chromium)
This commit is contained in:
153
test/test.directives.node.ts
Normal file
153
test/test.directives.node.ts
Normal file
@@ -0,0 +1,153 @@
|
||||
import { expect, tap } from '../ts_tapbundle/index.js';
|
||||
import {
|
||||
parseDirectivesFromContent,
|
||||
mergeDirectives,
|
||||
directivesToRuntimeOptions,
|
||||
hasDirectives,
|
||||
} from '../ts/tstest.classes.testfile.directives.js';
|
||||
|
||||
tap.test('parseDirectivesFromContent - deno allowAll', async () => {
|
||||
const content = `// tstest:deno:allowAll
|
||||
import { tap } from '../tapbundle/index.js';
|
||||
`;
|
||||
const directives = parseDirectivesFromContent(content);
|
||||
expect(directives.deno.length).toEqual(1);
|
||||
expect(directives.deno[0].key).toEqual('allowAll');
|
||||
expect(directives.deno[0].scope).toEqual('deno');
|
||||
});
|
||||
|
||||
tap.test('parseDirectivesFromContent - multiple deno directives', async () => {
|
||||
const content = `// tstest:deno:allowRun
|
||||
// tstest:deno:allowFfi
|
||||
import { tap } from '../tapbundle/index.js';
|
||||
`;
|
||||
const directives = parseDirectivesFromContent(content);
|
||||
expect(directives.deno.length).toEqual(2);
|
||||
expect(directives.deno[0].key).toEqual('allowRun');
|
||||
expect(directives.deno[1].key).toEqual('allowFfi');
|
||||
});
|
||||
|
||||
tap.test('parseDirectivesFromContent - flag directive with value', async () => {
|
||||
const content = `// tstest:deno:flag:--unstable-ffi
|
||||
import { tap } from '../tapbundle/index.js';
|
||||
`;
|
||||
const directives = parseDirectivesFromContent(content);
|
||||
expect(directives.deno.length).toEqual(1);
|
||||
expect(directives.deno[0].key).toEqual('flag');
|
||||
expect(directives.deno[0].value).toEqual('--unstable-ffi');
|
||||
});
|
||||
|
||||
tap.test('parseDirectivesFromContent - node flag directive', async () => {
|
||||
const content = `// tstest:node:flag:--max-old-space-size=4096
|
||||
import { tap } from '../tapbundle/index.js';
|
||||
`;
|
||||
const directives = parseDirectivesFromContent(content);
|
||||
expect(directives.node.length).toEqual(1);
|
||||
expect(directives.node[0].key).toEqual('flag');
|
||||
expect(directives.node[0].value).toEqual('--max-old-space-size=4096');
|
||||
});
|
||||
|
||||
tap.test('parseDirectivesFromContent - empty lines before directives', async () => {
|
||||
const content = `
|
||||
// tstest:deno:allowAll
|
||||
import { tap } from '../tapbundle/index.js';
|
||||
`;
|
||||
const directives = parseDirectivesFromContent(content);
|
||||
expect(directives.deno.length).toEqual(1);
|
||||
expect(directives.deno[0].key).toEqual('allowAll');
|
||||
});
|
||||
|
||||
tap.test('parseDirectivesFromContent - stops at first non-comment line', async () => {
|
||||
const content = `// tstest:deno:allowRun
|
||||
import { tap } from '../tapbundle/index.js';
|
||||
// tstest:deno:allowFfi
|
||||
`;
|
||||
const directives = parseDirectivesFromContent(content);
|
||||
// Should only find allowRun, not allowFfi (after import)
|
||||
expect(directives.deno.length).toEqual(1);
|
||||
expect(directives.deno[0].key).toEqual('allowRun');
|
||||
});
|
||||
|
||||
tap.test('parseDirectivesFromContent - no directives returns empty', async () => {
|
||||
const content = `import { tap } from '../tapbundle/index.js';
|
||||
tap.test('foo', async () => {});
|
||||
`;
|
||||
const directives = parseDirectivesFromContent(content);
|
||||
expect(hasDirectives(directives)).toEqual(false);
|
||||
});
|
||||
|
||||
tap.test('parseDirectivesFromContent - regular comments are skipped', async () => {
|
||||
const content = `// This is a regular comment
|
||||
// tstest:deno:allowAll
|
||||
import { tap } from '../tapbundle/index.js';
|
||||
`;
|
||||
const directives = parseDirectivesFromContent(content);
|
||||
expect(directives.deno.length).toEqual(1);
|
||||
});
|
||||
|
||||
tap.test('parseDirectivesFromContent - mixed runtime directives', async () => {
|
||||
const content = `// tstest:deno:allowRun
|
||||
// tstest:bun:flag:--smol
|
||||
import { tap } from '../tapbundle/index.js';
|
||||
`;
|
||||
const directives = parseDirectivesFromContent(content);
|
||||
expect(directives.deno.length).toEqual(1);
|
||||
expect(directives.bun.length).toEqual(1);
|
||||
expect(directives.bun[0].key).toEqual('flag');
|
||||
expect(directives.bun[0].value).toEqual('--smol');
|
||||
});
|
||||
|
||||
tap.test('directivesToRuntimeOptions - deno allowAll', async () => {
|
||||
const content = `// tstest:deno:allowAll
|
||||
import { tap } from '../tapbundle/index.js';
|
||||
`;
|
||||
const directives = parseDirectivesFromContent(content);
|
||||
const options = directivesToRuntimeOptions(directives, 'deno') as any;
|
||||
expect(options).toBeTruthy();
|
||||
expect(options.permissions).toContain('--allow-all');
|
||||
expect(options.permissions).not.toContain('--allow-read');
|
||||
});
|
||||
|
||||
tap.test('directivesToRuntimeOptions - deno extra permissions', async () => {
|
||||
const content = `// tstest:deno:allowRun
|
||||
// tstest:deno:allowFfi
|
||||
import { tap } from '../tapbundle/index.js';
|
||||
`;
|
||||
const directives = parseDirectivesFromContent(content);
|
||||
const options = directivesToRuntimeOptions(directives, 'deno') as any;
|
||||
expect(options).toBeTruthy();
|
||||
expect(options.permissions).toContain('--allow-run');
|
||||
expect(options.permissions).toContain('--allow-ffi');
|
||||
// Should still contain defaults
|
||||
expect(options.permissions).toContain('--allow-read');
|
||||
expect(options.permissions).toContain('--allow-env');
|
||||
});
|
||||
|
||||
tap.test('directivesToRuntimeOptions - no directives returns undefined', async () => {
|
||||
const directives = parseDirectivesFromContent('import { tap } from "tapbundle";');
|
||||
const options = directivesToRuntimeOptions(directives, 'deno');
|
||||
expect(options).toBeUndefined();
|
||||
});
|
||||
|
||||
tap.test('directivesToRuntimeOptions - node flag directive', async () => {
|
||||
const content = `// tstest:node:flag:--max-old-space-size=4096
|
||||
import { tap } from '../tapbundle/index.js';
|
||||
`;
|
||||
const directives = parseDirectivesFromContent(content);
|
||||
const options = directivesToRuntimeOptions(directives, 'node');
|
||||
expect(options).toBeTruthy();
|
||||
expect(options.extraArgs).toContain('--max-old-space-size=4096');
|
||||
});
|
||||
|
||||
tap.test('mergeDirectives - combines directives from init and test file', async () => {
|
||||
const init = parseDirectivesFromContent(`// tstest:deno:allowRun
|
||||
`);
|
||||
const testFile = parseDirectivesFromContent(`// tstest:deno:allowFfi
|
||||
`);
|
||||
const merged = mergeDirectives(init, testFile);
|
||||
expect(merged.deno.length).toEqual(2);
|
||||
expect(merged.deno[0].key).toEqual('allowRun');
|
||||
expect(merged.deno[1].key).toEqual('allowFfi');
|
||||
});
|
||||
|
||||
export default tap.start();
|
||||
Reference in New Issue
Block a user