mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-10 19:52:53 +00:00
feat: tests
This commit is contained in:
@@ -46,7 +46,6 @@ Inside of a script in `package.json` or if the npm package is installed globally
|
||||
openstapps-core-tools schema src/core lib/schema
|
||||
```
|
||||
|
||||
|
||||
## How to use the validator?
|
||||
|
||||
### Using the validator programatically
|
||||
|
||||
@@ -27,26 +27,25 @@
|
||||
},
|
||||
"scripts": {
|
||||
"build": "tsup --dts",
|
||||
"format": "prettier .",
|
||||
"format:fix": "prettier --write .",
|
||||
"format": "prettier . --ignore-path ../../.gitignore",
|
||||
"format:fix": "prettier --write . --ignore-path ../../.gitignore",
|
||||
"lint": "eslint --ext .ts src/",
|
||||
"lint:fix": "eslint --fix --ext .ts src/",
|
||||
"plantuml-restart": "docker restart plantuml-server",
|
||||
"plantuml-start": "docker run --name plantuml-server -d -p 8080:8080 registry.gitlab.com/openstapps/core-tools:latest",
|
||||
"plantuml-stop": "docker stop plantuml-server",
|
||||
"test": "nyc mocha 'test/**/*.spec.ts'"
|
||||
"test": "c8 mocha"
|
||||
},
|
||||
"dependencies": {
|
||||
"@openstapps/collection-utils": "workspace:*",
|
||||
"@openstapps/logger": "workspace:*",
|
||||
"@openstapps/easy-ast": "workspace:*",
|
||||
"@openstapps/logger": "workspace:*",
|
||||
"ajv": "8.12.0",
|
||||
"re2": "1.18.0",
|
||||
"better-ajv-errors": "1.2.0",
|
||||
"chai": "4.3.7",
|
||||
"commander": "10.0.0",
|
||||
"deepmerge": "4.3.1",
|
||||
"del": "6.1.1",
|
||||
"eslint": "8.33.0",
|
||||
"flatted": "3.2.7",
|
||||
"fs-extra": "10.1.0",
|
||||
"glob": "10.2.1",
|
||||
@@ -56,18 +55,14 @@
|
||||
"mustache": "4.2.0",
|
||||
"openapi-types": "12.1.0",
|
||||
"plantuml-encoder": "1.4.0",
|
||||
"re2": "1.18.0",
|
||||
"toposort": "2.0.2",
|
||||
"ts-json-schema-generator": "1.2.0",
|
||||
"ts-node": "10.9.1",
|
||||
"typescript": "4.8.4"
|
||||
"ts-json-schema-generator": "1.2.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@openstapps/eslint-config": "workspace:*",
|
||||
"@openstapps/nyc-config": "workspace:*",
|
||||
"@openstapps/prettier-config": "workspace:*",
|
||||
"@openstapps/tsconfig": "workspace:*",
|
||||
"@testdeck/mocha": "0.3.3",
|
||||
"@types/chai": "4.3.4",
|
||||
"@types/fs-extra": "9.0.13",
|
||||
"@types/glob": "8.0.1",
|
||||
@@ -75,10 +70,13 @@
|
||||
"@types/mocha": "10.0.1",
|
||||
"@types/mustache": "4.2.2",
|
||||
"@types/node": "18.15.3",
|
||||
"chai": "4.3.7",
|
||||
"mocha": "10.2.0",
|
||||
"c8": "7.13.0",
|
||||
"nock": "13.3.0",
|
||||
"tsup": "6.7.0",
|
||||
"typedoc": "0.23.28"
|
||||
"ts-node": "10.9.1",
|
||||
"typescript": "4.8.4"
|
||||
},
|
||||
"tsup": {
|
||||
"entry": [
|
||||
@@ -99,8 +97,5 @@
|
||||
"eslintIgnore": [
|
||||
"resources",
|
||||
"openapi"
|
||||
],
|
||||
"nyc": {
|
||||
"extends": "@openstapps/nyc-config"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
@@ -17,7 +17,6 @@ import {Command} from 'commander';
|
||||
import {existsSync, readFileSync, writeFileSync} from 'fs';
|
||||
import {copy} from 'fs-extra';
|
||||
import path from 'path';
|
||||
import {mkdirPromisified, readFilePromisified} from './common.js';
|
||||
import {lightweightDefinitionsFromPath, lightweightProjectFromPath} from '@openstapps/easy-ast';
|
||||
import {openapi3Template} from './resources/openapi-303-template.js';
|
||||
import {gatherRouteInformation, generateOpenAPIForRoute} from './routes.js';
|
||||
@@ -27,6 +26,7 @@ import {UMLConfig} from './uml/uml-config.js';
|
||||
import {capitalize} from './util/string.js';
|
||||
import {validateFiles, writeReport} from './validate.js';
|
||||
import {fileURLToPath} from 'url';
|
||||
import {mkdir, readFile} from 'fs/promises';
|
||||
|
||||
// handle unhandled promise rejections
|
||||
process.on('unhandledRejection', async (reason: unknown) => {
|
||||
@@ -42,7 +42,7 @@ const commander = new Command('openstapps-core-tools');
|
||||
// eslint-disable-next-line unicorn/prefer-module
|
||||
commander.version(
|
||||
JSON.parse(
|
||||
readFileSync(path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', 'package.json')).toString(),
|
||||
readFileSync(path.join(path.dirname(fileURLToPath(import.meta.url)), '..', 'package.json')).toString(),
|
||||
).version,
|
||||
);
|
||||
|
||||
@@ -100,7 +100,7 @@ commander
|
||||
// copy schema json schema files
|
||||
try {
|
||||
if (!existsSync(outDirectorySchemasPath)) {
|
||||
await mkdirPromisified(outDirectorySchemasPath, {
|
||||
await mkdir(outDirectorySchemasPath, {
|
||||
recursive: true,
|
||||
});
|
||||
}
|
||||
@@ -134,7 +134,7 @@ commander.command('schema <srcPath> <schemaPath>').action(async (relativeSourceP
|
||||
|
||||
Logger.info(`Found ${validatableTypes.length} type(s) to generate schemas for.`);
|
||||
|
||||
await mkdirPromisified(schemaPath, {
|
||||
await mkdir(schemaPath, {
|
||||
recursive: true,
|
||||
});
|
||||
|
||||
@@ -150,7 +150,7 @@ commander.command('schema <srcPath> <schemaPath>').action(async (relativeSourceP
|
||||
|
||||
Logger.info(`Using ${corePackageJsonPath} to determine version for schemas.`);
|
||||
|
||||
const buffer = await readFilePromisified(corePackageJsonPath);
|
||||
const buffer = await readFile(corePackageJsonPath);
|
||||
const corePackageJson = JSON.parse(buffer.toString());
|
||||
const coreVersion = corePackageJson.version;
|
||||
|
||||
|
||||
@@ -13,17 +13,8 @@
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import {Logger} from '@openstapps/logger';
|
||||
import {existsSync, mkdir, readFile, unlink, writeFile} from 'fs';
|
||||
import glob from 'glob';
|
||||
import {platform} from 'os';
|
||||
import {promisify} from 'util';
|
||||
import path from 'path';
|
||||
|
||||
export const globPromisified = promisify(glob.Glob);
|
||||
export const mkdirPromisified = promisify(mkdir);
|
||||
export const readFilePromisified = promisify(readFile);
|
||||
export const writeFilePromisified = promisify(writeFile);
|
||||
export const unlinkPromisified = promisify(unlink);
|
||||
import {existsSync} from 'fs';
|
||||
|
||||
/**
|
||||
* Get path that contains a tsconfig.json
|
||||
@@ -33,21 +24,15 @@ export const unlinkPromisified = promisify(unlink);
|
||||
export function getTsconfigPath(startPath: string): string {
|
||||
let tsconfigPath = startPath;
|
||||
|
||||
// see https://stackoverflow.com/questions/9652043/identifying-the-file-system-root-with-node-js
|
||||
const root = platform() === 'win32' ? process.cwd().split(path.sep)[0] : '/';
|
||||
|
||||
// repeat until a tsconfig.json is found
|
||||
while (!existsSync(path.join(tsconfigPath, 'tsconfig.json'))) {
|
||||
if (tsconfigPath === root) {
|
||||
const parent = path.resolve(tsconfigPath, '..');
|
||||
if (tsconfigPath === parent) {
|
||||
throw new Error(
|
||||
`Reached file system root ${root} while searching for 'tsconfig.json' in ${startPath}!`,
|
||||
`Reached file system root ${parent} while searching for 'tsconfig.json' in ${startPath}!`,
|
||||
);
|
||||
}
|
||||
|
||||
// pop last directory
|
||||
const tsconfigPathParts = tsconfigPath.split(path.sep);
|
||||
tsconfigPathParts.pop();
|
||||
tsconfigPath = tsconfigPathParts.join(path.sep);
|
||||
tsconfigPath = parent;
|
||||
}
|
||||
|
||||
Logger.info(`Using 'tsconfig.json' from ${tsconfigPath}.`);
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
export * from './validate.js'
|
||||
export * from './types/validator.js'
|
||||
export * from './validate.js';
|
||||
export * from './types/validator.js';
|
||||
|
||||
export * from './uml/uml-config.js'
|
||||
export * from './uml/create-diagram.js'
|
||||
export * from './uml/uml-config.js';
|
||||
export * from './uml/create-diagram.js';
|
||||
|
||||
export * from './routes.js'
|
||||
export * from './types/routes.js'
|
||||
export * from './routes.js';
|
||||
export * from './types/routes.js';
|
||||
|
||||
export * from './schema.js'
|
||||
export * from './types/schema.js'
|
||||
export * from './schema.js';
|
||||
export * from './types/schema.js';
|
||||
|
||||
@@ -13,7 +13,11 @@
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import {OpenAPIV3} from 'openapi-types';
|
||||
import {isLightweightClass, lightweightProjectFromPath, LightweightProjectWithIndex} from '@openstapps/easy-ast';
|
||||
import {
|
||||
isLightweightClass,
|
||||
lightweightProjectFromPath,
|
||||
LightweightProjectWithIndex,
|
||||
} from '@openstapps/easy-ast';
|
||||
import {RouteInstanceWithMeta, RouteWithMetaInformation} from './types/routes.js';
|
||||
import {rejectNil} from './util/collections.js';
|
||||
import {capitalize} from './util/string.js';
|
||||
|
||||
@@ -22,7 +22,7 @@ import {getTsconfigPath} from './common.js';
|
||||
import {definitionsOf, lightweightProjectFromPath} from '@openstapps/easy-ast';
|
||||
import {isSchemaWithDefinitions} from './util/guards.js';
|
||||
import path from 'path';
|
||||
import re2 from './types/re2.js';
|
||||
import re2 from 're2';
|
||||
|
||||
/**
|
||||
* StAppsCore converter
|
||||
@@ -64,7 +64,7 @@ export class Converter {
|
||||
this.generator = new SchemaGenerator(program, createParser(program, config), createFormatter(config));
|
||||
|
||||
// create Ajv instance
|
||||
this.schemaValidator = new Ajv.default({code: {regExp: re2}});
|
||||
this.schemaValidator = new Ajv.default({code: {regExp: re2 as never}});
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
import re2 from 're2';
|
||||
|
||||
type Re2 = typeof re2 & {code: string};
|
||||
(re2 as Re2).code = 'require("lib/types/re2").default';
|
||||
|
||||
export default re2 as Re2;
|
||||
@@ -13,7 +13,6 @@
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import {Logger} from '@openstapps/logger';
|
||||
import {createWriteStream} from 'fs';
|
||||
import * as request from 'got';
|
||||
import {
|
||||
expandTypeValue,
|
||||
@@ -22,9 +21,10 @@ import {
|
||||
LightweightClassDefinition,
|
||||
LightweightDefinition,
|
||||
LightweightProperty,
|
||||
LightweightType
|
||||
LightweightType,
|
||||
} from '@openstapps/easy-ast';
|
||||
import {UMLConfig} from './uml-config.js';
|
||||
import {writeFile} from 'fs/promises';
|
||||
|
||||
/**
|
||||
* Converts the lightweight class/enum definitions according to the configuration,
|
||||
@@ -81,8 +81,8 @@ export async function createDiagramFromString(
|
||||
plantUmlBaseURL: string,
|
||||
outputFile = `Diagram-${new Date().toISOString()}`,
|
||||
) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires,unicorn/prefer-module
|
||||
const plantumlEncoder = require('plantuml-encoder');
|
||||
// @ts-expect-error no declarations
|
||||
const plantumlEncoder = await import('plantuml-encoder');
|
||||
const plantUMLCode = plantumlEncoder.encode(`@startuml\n${modelPlantUMLCode}\n@enduml`);
|
||||
const url = `${plantUmlBaseURL}/svg/${plantUMLCode}`;
|
||||
let response;
|
||||
@@ -100,13 +100,10 @@ export async function createDiagramFromString(
|
||||
throw error;
|
||||
}
|
||||
// attach file extension
|
||||
const fileName = `${outputFile}.svg`;
|
||||
try {
|
||||
createWriteStream(fileName).write(response.body);
|
||||
Logger.log(`Writen data to file: ${fileName}`);
|
||||
} catch {
|
||||
throw new Error('Could not write file. Are you missing permissions?');
|
||||
}
|
||||
const fileName = `${outputFile.replace(/[^\w-]/g, '_')}.svg`;
|
||||
|
||||
await writeFile(fileName, response.body);
|
||||
Logger.log(`Writen data to file: ${fileName}`);
|
||||
|
||||
return fileName;
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
*/
|
||||
export function rejectNil<T>(array: Array<T | undefined | null>): T[] {
|
||||
// eslint-disable-next-line unicorn/no-null
|
||||
return array.filter(it => it == null) as T[];
|
||||
return array.filter(it => it != null) as T[];
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -15,16 +15,17 @@
|
||||
import {Logger} from '@openstapps/logger';
|
||||
import Ajv from 'ajv';
|
||||
import betterAjvErrors, {IOutputError} from 'better-ajv-errors';
|
||||
import {PathLike} from 'fs';
|
||||
import type {PathLike} from 'fs';
|
||||
import {readFile, writeFile} from 'fs/promises';
|
||||
import {JSONSchema7} from 'json-schema';
|
||||
import mustache from 'mustache';
|
||||
import {Schema} from 'ts-json-schema-generator';
|
||||
import {globPromisified, readFilePromisified, writeFilePromisified} from './common.js';
|
||||
import {ExpectedValidationErrors, ValidationError, ValidationResult} from './types/validator.js';
|
||||
import {isThingWithType} from './util/guards.js';
|
||||
import path from 'path';
|
||||
import re2 from './types/re2.js';
|
||||
import {toPosixPath} from './util/posix-path.js';
|
||||
import re2 from 're2';
|
||||
import {glob} from 'glob';
|
||||
import {fileURLToPath} from 'url';
|
||||
|
||||
/**
|
||||
* StAppsCore validator
|
||||
@@ -35,13 +36,13 @@ export class Validator {
|
||||
*/
|
||||
private readonly ajv = new Ajv.default({
|
||||
verbose: true,
|
||||
code: {regExp: re2},
|
||||
code: {regExp: re2 as never},
|
||||
});
|
||||
|
||||
/**
|
||||
* Map of schema names to schemas
|
||||
*/
|
||||
private readonly schemas: {[type: string]: Schema} = {};
|
||||
private readonly schemas: { [type: string]: Schema } = {};
|
||||
|
||||
/**
|
||||
* A wrapper function for Ajv that transforms the error into the compatible old error
|
||||
@@ -58,8 +59,9 @@ export class Validator {
|
||||
*
|
||||
* @param schemaDirectory Path to directory that contains schema files
|
||||
*/
|
||||
public async addSchemas(schemaDirectory: PathLike): Promise<string[]> {
|
||||
const schemaFiles = await globPromisified(path.posix.join(toPosixPath(schemaDirectory), '*.json'));
|
||||
public async addSchemas(schemaDirectory: string): Promise<string[]> {
|
||||
const searchGlob = path.posix.join(schemaDirectory.replaceAll(path.sep, path.posix.sep), '*.json');
|
||||
const schemaFiles = await glob(searchGlob);
|
||||
|
||||
if (schemaFiles.length === 0) {
|
||||
throw new Error(`No schema files in ${schemaDirectory.toString()}!`);
|
||||
@@ -70,7 +72,7 @@ export class Validator {
|
||||
await Promise.all(
|
||||
schemaFiles.map(async (file: string) => {
|
||||
// read schema file
|
||||
const buffer = await readFilePromisified(file);
|
||||
const buffer = await readFile(file);
|
||||
|
||||
// add schema to map
|
||||
this.schemas[path.basename(file, '.json')] = JSON.parse(buffer.toString());
|
||||
@@ -92,7 +94,7 @@ export class Validator {
|
||||
if (schema === undefined) {
|
||||
if (isThingWithType(instance)) {
|
||||
// schema name can be inferred from type string
|
||||
const schemaSuffix = (instance as {type: string}).type
|
||||
const schemaSuffix = (instance as { type: string }).type
|
||||
.split(' ')
|
||||
.map((part: string) => {
|
||||
return part.slice(0, 1).toUpperCase() + part.slice(1);
|
||||
@@ -175,8 +177,8 @@ export async function validateFiles(
|
||||
const v = new Validator();
|
||||
await v.addSchemas(schemaDirectory);
|
||||
|
||||
// get list of files to test
|
||||
const testFiles = await globPromisified(path.join(resourcesDirectory, '*.json'));
|
||||
// get a list of files to test
|
||||
const testFiles = await glob(path.posix.join(resourcesDirectory.replaceAll(path.sep, path.posix.sep), '*.json'), {absolute: true});
|
||||
|
||||
if (testFiles.length === 0) {
|
||||
throw new Error(`No test files in ${resourcesDirectory}!`);
|
||||
@@ -191,7 +193,7 @@ export async function validateFiles(
|
||||
testFiles.map(async (testFile: string) => {
|
||||
const testFileName = path.basename(testFile);
|
||||
|
||||
const buffer = await readFilePromisified(path.join(resourcesDirectory, testFileName));
|
||||
const buffer = await readFile(testFile);
|
||||
|
||||
// read test description from file
|
||||
const testDescription = JSON.parse(buffer.toString());
|
||||
@@ -260,12 +262,14 @@ export async function validateFiles(
|
||||
* @param errors Errors that occurred in validation
|
||||
*/
|
||||
export async function writeReport(reportPath: PathLike, errors: ExpectedValidationErrors): Promise<void> {
|
||||
// eslint-disable-next-line unicorn/prefer-module
|
||||
let buffer = await readFilePromisified(path.resolve(__dirname, '..', 'resources', 'file.html.mustache'));
|
||||
let buffer = await readFile(
|
||||
path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', 'resources', 'file.html.mustache'),
|
||||
);
|
||||
const fileTemplate = buffer.toString();
|
||||
|
||||
// eslint-disable-next-line unicorn/prefer-module
|
||||
buffer = await readFilePromisified(path.resolve(__dirname, '..', 'resources', 'error.html.mustache'));
|
||||
buffer = await readFile(
|
||||
path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', 'resources', 'error.html.mustache'),
|
||||
);
|
||||
const errorTemplate = buffer.toString();
|
||||
|
||||
let output = '';
|
||||
@@ -295,11 +299,12 @@ export async function writeReport(reportPath: PathLike, errors: ExpectedValidati
|
||||
});
|
||||
}
|
||||
|
||||
// eslint-disable-next-line unicorn/prefer-module
|
||||
buffer = await readFilePromisified(path.resolve(__dirname, '..', 'resources', 'report.html.mustache'));
|
||||
buffer = await readFile(
|
||||
path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', 'resources', 'report.html.mustache'),
|
||||
);
|
||||
const reportTemplate = buffer.toString();
|
||||
|
||||
await writeFilePromisified(
|
||||
await writeFile(
|
||||
reportPath,
|
||||
mustache.render(reportTemplate, {
|
||||
report: output,
|
||||
|
||||
@@ -15,9 +15,9 @@
|
||||
*/
|
||||
import {Logger} from '@openstapps/logger';
|
||||
import {expect} from 'chai';
|
||||
import {slow, suite, test, timeout} from '@testdeck/mocha';
|
||||
import {cwd} from 'process';
|
||||
import {getTsconfigPath} from '../src/common.js';
|
||||
import path from 'path';
|
||||
import {fileURLToPath} from 'url';
|
||||
|
||||
process.on('unhandledRejection', (reason: unknown): void => {
|
||||
if (reason instanceof Error) {
|
||||
@@ -26,10 +26,10 @@ process.on('unhandledRejection', (reason: unknown): void => {
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
@suite(timeout(20_000), slow(10_000))
|
||||
export class CommonSpec {
|
||||
@test
|
||||
async getTsconfigPath() {
|
||||
expect(getTsconfigPath(__dirname)).to.be.equal(cwd());
|
||||
}
|
||||
}
|
||||
describe('common', function () {
|
||||
describe('getTsconfigPath', function () {
|
||||
it('should get tsconfig path', function () {
|
||||
expect(getTsconfigPath(path.dirname(fileURLToPath(import.meta.url)))).to.be.equal(process.cwd());
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -14,36 +14,32 @@
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import {expect} from 'chai';
|
||||
import {existsSync, unlinkSync} from 'fs';
|
||||
import {slow, suite, test, timeout} from '@testdeck/mocha';
|
||||
import {createDiagram, createDiagramFromString} from '../src/uml/create-diagram.js';
|
||||
import {UMLConfig} from '../src/uml/uml-config.js';
|
||||
import {LightweightDefinition, lightweightDefinitionsFromPath} from '@openstapps/easy-ast';
|
||||
import {unlink} from 'fs/promises';
|
||||
import {createDiagram, createDiagramFromString} from '../src/index.js';
|
||||
import {UMLConfig} from '../src/index.js';
|
||||
import {lightweightDefinitionsFromPath} from '@openstapps/easy-ast';
|
||||
import nock = require('nock');
|
||||
import path from 'path';
|
||||
import {existsSync} from 'fs';
|
||||
import {fileURLToPath} from 'url';
|
||||
|
||||
@suite(timeout(15_000), slow(5000))
|
||||
export class CreateDiagramSpec {
|
||||
plantUmlConfig: UMLConfig;
|
||||
describe('CreateDiagram', function () {
|
||||
this.timeout(15_000);
|
||||
this.slow(5000);
|
||||
|
||||
definitions: LightweightDefinition[];
|
||||
const plantUmlConfig: UMLConfig = {
|
||||
definitions: [],
|
||||
showAssociations: true,
|
||||
showEnumValues: true,
|
||||
showInheritance: true,
|
||||
showInheritedProperties: true,
|
||||
showOptionalProperties: true,
|
||||
showProperties: true,
|
||||
};
|
||||
|
||||
constructor() {
|
||||
this.plantUmlConfig = {
|
||||
definitions: [],
|
||||
showAssociations: true,
|
||||
showEnumValues: true,
|
||||
showInheritance: true,
|
||||
showInheritedProperties: true,
|
||||
showOptionalProperties: true,
|
||||
showProperties: true,
|
||||
};
|
||||
const definitions = lightweightDefinitionsFromPath('./test/model');
|
||||
|
||||
this.definitions = lightweightDefinitionsFromPath('./test/model');
|
||||
}
|
||||
|
||||
@test
|
||||
async shouldRefuseRequest() {
|
||||
it('should refuse request', async function () {
|
||||
const testPlantUmlCode = 'class Test{\n}';
|
||||
try {
|
||||
await createDiagramFromString(testPlantUmlCode, 'http://plantuml:8080');
|
||||
@@ -54,7 +50,7 @@ export class CreateDiagramSpec {
|
||||
new Error('getaddrinfo ENOTFOUND plantuml').message,
|
||||
]).to.include((error as NodeJS.ErrnoException).message);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* This test will only test the functionality of the method
|
||||
@@ -63,26 +59,25 @@ export class CreateDiagramSpec {
|
||||
* - Writing the response to a file
|
||||
* This test will not check the file content
|
||||
*/
|
||||
@test
|
||||
async shouldCreateDiagrams() {
|
||||
it('should create diagrams', async function () {
|
||||
nock('http://plantuml:8080')
|
||||
.persist()
|
||||
.get(() => true)
|
||||
.reply(200, 'This will be the file content');
|
||||
|
||||
let fileName = await createDiagram(this.definitions, this.plantUmlConfig, 'http://plantuml:8080');
|
||||
let filePath = path.resolve(__dirname, '..', fileName);
|
||||
expect(await existsSync(filePath)).to.equal(true);
|
||||
let fileName = await createDiagram(definitions, plantUmlConfig, 'http://plantuml:8080');
|
||||
let filePath = path.join(path.dirname(fileURLToPath(import.meta.url)), '..', fileName);
|
||||
expect(existsSync(filePath)).to.be.true;
|
||||
|
||||
await unlinkSync(fileName);
|
||||
this.plantUmlConfig.showAssociations = false;
|
||||
await unlink(fileName);
|
||||
plantUmlConfig.showAssociations = false;
|
||||
|
||||
this.plantUmlConfig.showInheritance = false;
|
||||
fileName = await createDiagram(this.definitions, this.plantUmlConfig, 'http://plantuml:8080');
|
||||
filePath = path.resolve(__dirname, '..', fileName);
|
||||
expect(await existsSync(filePath)).to.equal(true);
|
||||
await unlinkSync(fileName);
|
||||
plantUmlConfig.showInheritance = false;
|
||||
fileName = await createDiagram(definitions, plantUmlConfig, 'http://plantuml:8080');
|
||||
filePath = path.join(path.dirname(fileURLToPath(import.meta.url)), '..', fileName);
|
||||
expect(existsSync(filePath)).to.be.true;
|
||||
await unlink(fileName);
|
||||
|
||||
nock.cleanAll();
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -15,9 +15,9 @@
|
||||
*/
|
||||
import {Logger} from '@openstapps/logger';
|
||||
import {expect} from 'chai';
|
||||
import {slow, suite, test, timeout} from '@testdeck/mocha';
|
||||
import {Converter} from '../src/schema.js';
|
||||
import path from 'path';
|
||||
import {fileURLToPath} from 'url';
|
||||
|
||||
process.on('unhandledRejection', (error: unknown) => {
|
||||
if (error instanceof Error) {
|
||||
@@ -26,11 +26,14 @@ process.on('unhandledRejection', (error: unknown) => {
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
@suite(timeout(40_000), slow(10_000))
|
||||
export class SchemaSpec {
|
||||
@test
|
||||
async getSchema() {
|
||||
const converter = new Converter(path.join(__dirname, '..', 'src', 'resources'));
|
||||
describe('Schema', function () {
|
||||
this.timeout(40_000);
|
||||
this.slow(10_000);
|
||||
|
||||
it('should create schema', function () {
|
||||
const converter = new Converter(
|
||||
path.join(path.dirname(fileURLToPath(import.meta.url)), '..', 'src', 'resources'),
|
||||
);
|
||||
|
||||
const schema = converter.getSchema('Foo', '0.0.1');
|
||||
expect(schema).to.be.deep.equal({
|
||||
@@ -77,5 +80,5 @@ export class SchemaSpec {
|
||||
required: ['lorem', 'type'],
|
||||
type: 'object',
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -15,14 +15,14 @@
|
||||
*/
|
||||
import {Logger} from '@openstapps/logger';
|
||||
import {expect} from 'chai';
|
||||
import {existsSync, mkdirSync, writeFileSync} from 'fs';
|
||||
import {existsSync} from 'fs';
|
||||
import {JSONSchema7 as Schema} from 'json-schema';
|
||||
import {slow, suite, test, timeout} from '@testdeck/mocha';
|
||||
import rimraf from 'rimraf';
|
||||
import {Foo} from '../src/resources/foo.js';
|
||||
import {Converter} from '../src/schema.js';
|
||||
import {Validator} from '../src/validate.js';
|
||||
import path from 'path';
|
||||
import {fileURLToPath} from 'url';
|
||||
import {rm, mkdir, writeFile} from 'fs/promises';
|
||||
import {Converter} from '../src/index.js';
|
||||
|
||||
process.on('unhandledRejection', (error: unknown) => {
|
||||
if (error instanceof Error) {
|
||||
@@ -31,57 +31,55 @@ process.on('unhandledRejection', (error: unknown) => {
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
const tmpdir = path.join(__dirname, 'tmp');
|
||||
const tmpdir = path.join(path.dirname(fileURLToPath(import.meta.url)), 'tmp');
|
||||
const fooInstance: Foo = {
|
||||
lorem: 'ipsum',
|
||||
type: 'Foo',
|
||||
};
|
||||
|
||||
@suite(timeout(40_000), slow(5000))
|
||||
export class ValidateSpec {
|
||||
static converter: Converter;
|
||||
describe('Validator', function () {
|
||||
this.timeout(40_000);
|
||||
this.slow(5000);
|
||||
|
||||
static schema: Schema;
|
||||
let schema: Schema;
|
||||
let converter: Converter;
|
||||
|
||||
static before() {
|
||||
this.converter = new Converter(path.join(__dirname, '..', 'src', 'resources'));
|
||||
this.schema = this.converter.getSchema('Foo', '0.0.1');
|
||||
beforeEach(async function () {
|
||||
converter = new Converter(
|
||||
path.join(path.dirname(fileURLToPath(import.meta.url)), '..', 'src', 'resources'),
|
||||
);
|
||||
schema = converter.getSchema('Foo', '0.0.1');
|
||||
if (!existsSync(tmpdir)) {
|
||||
mkdirSync(tmpdir);
|
||||
await mkdir(tmpdir);
|
||||
}
|
||||
writeFileSync(path.join(tmpdir, 'SCFoo.json'), JSON.stringify(this.schema, undefined, 2));
|
||||
}
|
||||
await writeFile(path.join(tmpdir, 'SCFoo.json'), JSON.stringify(schema, undefined, 2));
|
||||
});
|
||||
|
||||
static after() {
|
||||
rimraf(tmpdir, error => {
|
||||
// tslint:disable-next-line: no-unused-expression
|
||||
afterEach(async function () {
|
||||
try {
|
||||
await rm(tmpdir, {recursive: true});
|
||||
} catch (error) {
|
||||
expect(error, `Unable to remove temporary directory for tests at: ${tmpdir}`).to.be.null;
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@test
|
||||
async validateBySchemaIdentifyingString() {
|
||||
it('should validate by schema identifying string', async function () {
|
||||
const validator = new Validator();
|
||||
await validator.addSchemas(tmpdir);
|
||||
const validationResult = validator.validate(fooInstance, 'SCFoo');
|
||||
// tslint:disable-next-line: no-unused-expression
|
||||
expect(validationResult.errors, JSON.stringify(validationResult.errors, undefined, 2)).to.be.empty;
|
||||
}
|
||||
});
|
||||
|
||||
@test
|
||||
async validateBySchemaInstance() {
|
||||
it('should validate by schema instance', async function () {
|
||||
const validator = new Validator();
|
||||
const validationResult = validator.validate(fooInstance, ValidateSpec.schema);
|
||||
// tslint:disable-next-line: no-unused-expression
|
||||
const validationResult = validator.validate(fooInstance, schema);
|
||||
expect(validationResult.errors, JSON.stringify(validationResult.errors, undefined, 2)).to.be.empty;
|
||||
}
|
||||
});
|
||||
|
||||
@test
|
||||
async validateIntrinsic() {
|
||||
it('should validate intrinsic', async function () {
|
||||
const validator = new Validator();
|
||||
await validator.addSchemas(tmpdir);
|
||||
const validationResult = validator.validate(fooInstance);
|
||||
// tslint:disable-next-line: no-unused-expression
|
||||
expect(validationResult.errors, JSON.stringify(validationResult.errors, undefined, 2)).to.be.empty;
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user