UNPKG

@lcap/nasl-parser

Version:

Take Nasl text to Nasl AST with the help of generalized parsing.

215 lines (207 loc) 6.78 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const fs_1 = __importDefault(require("fs")); const nasl_parser_1 = require("./nasl-parser"); const assert_1 = require("assert"); const to_nasl_ast_1 = require("./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) => { 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) => { 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) => { 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) => { 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_1.default.existsSync('tests/e2e/')) { fs_1.default.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_1.default.readFile(fPath, 'utf8', (err, code) => { if (err) { console.error(err); return; } console.log('processing', fName, fPath); const parsedCode = (0, nasl_parser_1.parse)(code); (0, assert_1.strict)(parsedCode.results.length === 1); const nasl = (0, to_nasl_ast_1.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_1.default.readFile(fPath, 'utf8', (err, code) => { if (err) { console.error(err); return; } const parsedCode = (0, nasl_parser_1.parse)(code); (0, assert_1.strict)(parsedCode.results.length === 1); const nasl = (0, to_nasl_ast_1.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_1.default.readFile(fPath, 'utf8', (err, code) => { if (err) { console.error(err); return; } const parsedCode = (0, nasl_parser_1.parse)(code); (0, assert_1.strict)(parsedCode.results.length === 1); const nasl = (0, to_nasl_ast_1.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_1.default.readFile(fPath, 'utf8', (err, code) => { if (err) { console.error(err); return; } const parsedCode = (0, nasl_parser_1.parse)(code); (0, assert_1.strict)(parsedCode.results.length === 1); const nasl = (0, to_nasl_ast_1.toNaslAST)(parsedCode.results[0].nasl); genJestFiles('apps', fName, nasl); }); }); } function genJestFiles(kind, fName, nasl) { 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_1.default.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_1.default.existsSync(`${finalPath}/real/`)) { fs_1.default.mkdirSync(`${finalPath}/real/`, { recursive: true }); } fs_1.default.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(); //# sourceMappingURL=prepare-tests.js.map