@lcap/nasl-parser
Version:
Take Nasl text to Nasl AST with the help of generalized parsing.
233 lines (191 loc) • 6.13 kB
text/typescript
import fs from 'fs';
import { parse } from './nasl-parser';
import { strict as assert } from 'assert';
import { inspect } from 'util';
import { toNaslAST } from './toAST/to-nasl-ast';
const logicUTNames = [
'arith',
'logical',
'comparison',
'if',
'switch',
'switch-complex',
'test-type',
'asgn',
'batch-asgn',
'filter',
'ai-test1',
'ai-test1-q',
'ai-test2',
'ai-test2-q',
'enum-short-ref',
'json-de-se',
'new-comp',
'map-transform',
'info',
'list-type-ref',
];
const enumUTNames = [
'enum-str',
'enum-int',
];
const structUTNames = [
'mut-struct',
];
const appUTNames = [
'enum-ref',
];
// const unitTestFiles = unitTestName.map(f => `./examples/${f}.nasl`);
const genLogicJestSpec = (fName : string) => {
const content =
`import { removeUnimportantProps } from '../../../ts/nasl-json-util';
import c from './controlled/${fName}.json';
import real from './real/${fName}.json';
describe('${fName}...', () => {
test('${fName}', async () => {
expect(removeUnimportantProps(real.logics[0])).toEqual(removeUnimportantProps(c))
});
});
`
return content;
}
const genEnumJestSpec = (fName : string) => {
const content =
`import { removeUnimportantProps } from '../../../ts/nasl-json-util';
import c from './controlled/${fName}.json';
import real from './real/${fName}.json';
describe('${fName}...', () => {
test('${fName}', async () => {
expect(removeUnimportantProps(real.enums[0])).toEqual(removeUnimportantProps(c))
});
});
`
return content;
}
const genStructJestSpec = (fName : string) => {
const content =
`import { removeUnimportantProps } from '../../../ts/nasl-json-util';
import c from './controlled/${fName}.json';
import real from './real/${fName}.json';
describe('${fName}...', () => {
test('${fName}', async () => {
expect(removeUnimportantProps(real.structures[0])).toEqual(removeUnimportantProps(c))
});
});
`
return content;
}
const genAppJestSpec = (fName : string) => {
const content =
`import { removeUnimportantProps } from '../../../ts/nasl-json-util';
import c from './controlled/${fName}.json';
import real from './real/${fName}.json';
describe('${fName}...', () => {
test('${fName}', async () => {
expect(removeUnimportantProps(real)).toEqual(removeUnimportantProps(c))
});
});
`
return content;
}
// read all files in /examples to produce test files
async function prepareTestFiles() {
if (!fs.existsSync('tests/e2e/')) {
fs.mkdirSync('tests/e2e/', { recursive: true });
}
// logic tests
logicUTNames.forEach(async fName => {
// skip some files
if (['query'].includes(fName)) {
return;
}
const fPath = `./examples/unit/logics/${fName}.nasl`
// read file
fs.readFile(fPath, 'utf8', (err, code) => {
if (err) { console.error(err); return; }
console.log('processing', fName, fPath);
const parsedCode = parse(code);
assert(parsedCode.results.length === 1);
const nasl = toNaslAST(parsedCode.results[0].nasl);
genJestFiles('logics', fName, nasl);
});
});
// enum tests
enumUTNames.forEach(async fName => {
// skip some files
if (['query'].includes(fName)) {
return;
}
const fPath = `./examples/unit/enums/${fName}.nasl`
// read file
fs.readFile(fPath, 'utf8', (err, code) => {
if (err) { console.error(err); return; }
const parsedCode = parse(code);
assert(parsedCode.results.length === 1);
const nasl = toNaslAST(parsedCode.results[0].nasl);
genJestFiles('enums', fName, nasl);
});
});
// struct tests
structUTNames.forEach(async fName => {
// skip some files
if (['query'].includes(fName)) {
return;
}
const fPath = `./examples/unit/structs/${fName}.nasl`
// read file
fs.readFile(fPath, 'utf8', (err, code) => {
if (err) { console.error(err); return; }
const parsedCode = parse(code);
assert(parsedCode.results.length === 1);
const nasl = toNaslAST(parsedCode.results[0].nasl);
genJestFiles('structs', fName, nasl);
});
});
// app tests
appUTNames.forEach(async fName => {
// skip some files
if (['query'].includes(fName)) {
return;
}
const fPath = `./examples/unit/apps/${fName}.nasl`
// read file
fs.readFile(fPath, 'utf8', (err, code) => {
if (err) { console.error(err); return; }
const parsedCode = parse(code);
assert(parsedCode.results.length === 1);
const nasl = toNaslAST(parsedCode.results[0].nasl);
genJestFiles('apps', fName, nasl);
});
});
}
function genJestFiles(kind: 'logics' | 'enums' | 'structs' | 'apps', fName: string, nasl: any) {
const finalPath = `tests/e2e/${kind}`;
let specContent = '';
if (kind === 'logics') {
specContent = genLogicJestSpec(fName);
} else if (kind === 'enums') {
specContent = genEnumJestSpec(fName);
} else if (kind === 'apps') {
specContent = genAppJestSpec(fName);
} else if (kind === 'structs') {
specContent = genStructJestSpec(fName);
} else {
throw new Error('Unsupported kind');
}
fs.writeFile(`${finalPath}/${fName}.spec.ts`, specContent, (err) => {
if (err) throw err;
console.log(`Jest spec file ${finalPath}/${fName}.spec.ts has been saved!`);
});
if (!fs.existsSync(`${finalPath}/real/`)) {
fs.mkdirSync(`${finalPath}/real/`, { recursive: true });
}
fs.writeFile(`${finalPath}/real/${fName}.json`,
JSON.stringify(nasl.toJSON(), null, 2),
(err) => {
if (err) throw err;
console.log(`Nasl ast file ${finalPath}/real/${fName}.json has been saved!`);
}
);
}
prepareTestFiles();