This commit is contained in:
qiaozha 2020-01-17 13:29:37 +08:00
Родитель d2d738b08d
Коммит 90f00fcae7
8 изменённых файлов: 266 добавлений и 4 удалений

Просмотреть файл

@ -43,6 +43,7 @@
"@azure-tools/codemodel": "~3.0.0",
"@azure-tools/linq": "^3.1.212",
"js-yaml": "^3.13.1",
"mocha-typescript": "^1.1.17",
"node-yaml": "^3.2.0"
},
"files": [

Просмотреть файл

@ -4,7 +4,7 @@ import { serialize, deserialize } from '@azure-tools/codegen';
import { values, items, length, Dictionary } from '@azure-tools/linq';
import { changeCamelToDash } from '../utils/helper';
class AzNamer {
export class AzNamer {
codeModel: CodeModel;
constructor(protected session: Session<CodeModel>) {

Просмотреть файл

@ -106,7 +106,7 @@ function hasSpecialChars(str: string): boolean {
return !/^[a-zA-Z0-9]+$/.test(str);
}
class Modifiers {
export class Modifiers {
codeModel: CodeModel;
constructor(protected session: Session<CodeModel>) {
@ -191,7 +191,7 @@ export async function processRequest(host: Host) {
try {
const session = await startSession<CodeModel>(host, {}, codeModelSchema);
const plugin = await new Modifiers(session);
const plugin = new Modifiers(session);
const result = await plugin.process();
host.WriteFile("modifiers-temp-output.yaml", serialize(result));
} catch (E) {

60
src/test/main.ts Normal file
Просмотреть файл

@ -0,0 +1,60 @@
import { suite, test } from 'mocha-typescript';
import * as assert from 'assert';
import { readFile, writeFile, readdir, mkdir } from '@azure-tools/async-io';
import { deserialize, serialize, fail } from '@azure-tools/codegen';
import { CodeModel, codeModelSchema } from '@azure-tools/codemodel';
import { createTestSession, createPassThruSession } from './utils/test-helper';
import { AzNamer } from '../plugins/aznamer';
import { Modifiers } from '../plugins/modifiers';
require('source-map-support').install();
@suite class Process {
@test async 'acceptance-suite'() {
const folders = await readdir(`${__dirname}/../../test/scenarios/`);
for (const each of folders) {
if ([
'body-formdata',
'body-formdata-urlencoded',
].indexOf(each) > -1) {
console.log(`Skipping: ${each}`);
continue;
}
/* if ('body-complex' !== each) {
console.log(`Skipping: ${each}`);
continue;
} */
console.log(`Processing: ${each}`);
const cfg = {
modelerfour: { 'flatten-models': true, 'flatten-payloads': true },
'payload-flattening-threshold': 2
}
const session = await createTestSession<CodeModel>(cfg, `${__dirname}/../../test/scenarios/${each}`, ['openapi-document.json'], []);
// process OAI model
const modeler = new AzNamer(session);
// go!
const codeModel = await modeler.process();
const yaml = serialize(codeModel, codeModelSchema);
await mkdir(`${__dirname}/../../test/scenarios/${each}`);
await (writeFile(`${__dirname}/../../test/scenarios/${each}/modeler.yaml`, yaml));
const aznamer = new AzNamer(await createPassThruSession(cfg, yaml, 'code-model-v4'));
const model = await aznamer.process();
const aznameryaml = serialize(model, codeModelSchema);
await (writeFile(`${__dirname}/../../test/scenarios/${each}/aznamer.yaml`, aznameryaml));
const modifiers = new Modifiers(await createPassThruSession(cfg, aznameryaml, 'code-model-v4'));
const modified = await modifiers.process();
const modifieryaml = serialize(modified, codeModelSchema);
await (writeFile(`${__dirname}/../../test/scenarios/${each}/modifier.yaml`, modifieryaml));
}
}
}

Просмотреть файл

@ -0,0 +1,41 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { suite, test } from 'mocha-typescript';
import * as assert from 'assert';
import { readFile, writeFile, readdir, mkdir } from '@azure-tools/async-io';
import { deserialize, serialize, fail } from '@azure-tools/codegen';
import { CodeModel, codeModelSchema } from '@azure-tools/codemodel';
import { createTestSession } from '../utils/test-helper';
import { Modifiers } from '../../plugins/modifiers';
require('source-map-support').install();
const resources = `${__dirname}/../../test/resources`;
const supposes = `${__dirname}/../../test/supposes`;
@suite class Process {
@test async 'simple model test'() {
const session = await createTestSession<CodeModel>({}, resources, ['input2.yaml'], ['output1.yaml']);
// process OAI model
const modeler = new Modifiers(session);
// go!
const codeModel = await modeler.process();
// console.log(serialize(codeModel))
const yaml = serialize(codeModel, codeModelSchema);
//await (writeFile(`${__dirname}/../../output.yaml`, yaml));
const cms = deserialize<CodeModel>(yaml, 'foo.yaml', codeModelSchema);
assert.strictEqual(true, cms instanceof CodeModel, 'Type Info is maintained in deserialization.');
}
}

Просмотреть файл

@ -0,0 +1,42 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { suite, test } from 'mocha-typescript';
import * as assert from 'assert';
import { readFile, writeFile, readdir, mkdir } from '@azure-tools/async-io';
import { deserialize, serialize, fail } from '@azure-tools/codegen';
import { CodeModel, codeModelSchema } from '@azure-tools/codemodel';
import { createTestSession, createPassThruSession } from '../utils/test-helper';
import { AzNamer } from '../../plugins/aznamer';
require('source-map-support').install();
const resources = `${__dirname}/../../test/resources/process`;
@suite class Process {
@test async 'simple model test'() {
const session = await createTestSession<CodeModel>({}, resources, ['input2.yaml'], ['output1.yaml']);
// process OAI model
const modeler = new AzNamer(session);
// go!
const codeModel = await modeler.process();
// console.log(serialize(codeModel))
const yaml = serialize(codeModel, codeModelSchema);
//await (writeFile(`${__dirname}/../../output.yaml`, yaml));
const cms = deserialize<CodeModel>(yaml, 'foo.yaml', codeModelSchema);
assert.strictEqual(true, cms instanceof CodeModel, 'Type Info is maintained in deserialization.');
}
}

Просмотреть файл

@ -0,0 +1,117 @@
import { readFile, writeFile, readdir, mkdir } from '@azure-tools/async-io';
import { deserialize, serialize, fail } from '@azure-tools/codegen';
import { startSession } from '@azure-tools/autorest-extension-base';
import { values } from '@azure-tools/linq';
import { CodeModel } from '@azure-tools/codemodel';
import { codeModelSchema } from '@azure-tools/codemodel';
require('source-map-support').install();
export async function readData(folder: string, ...files: Array<string>): Promise<Array<{ model: any; filename: string; content: string }>> {
const results = [];
for (const filename of files) {
const content = await readFile(`${folder}/${filename}`);
const model = deserialize<any>(content, filename);
results.push({
model,
filename,
content
});
}
return results;
}
export async function cts<TInputModel>(config: any, filename: string, content: string) {
const ii = [{
model: deserialize<any>(content, filename),
filename,
content
}];
return await startSession<TInputModel>({
ReadFile: async (filename: string): Promise<string> => (values(ii).first(each => each.filename === filename) || fail(`missing input '${filename}'`)).content,
GetValue: async (key: string): Promise<any> => {
if (!key) {
return config;
}
return config[key];
},
ListInputs: async (artifactType?: string): Promise<Array<string>> => ii.map(each => each.filename),
ProtectFiles: async (path: string): Promise<void> => {
// test
},
WriteFile: (filename: string, content: string, sourceMap?: any, artifactType?: string): void => {
// test
},
Message: (message: any): void => {
// test
console.error(message);
},
UpdateConfigurationFile: (filename: string, content: string): void => {
// test
},
GetConfigurationFile: async (filename: string): Promise<string> => '',
});
}
export async function createTestSession<TInputModel>(config: any, folder: string, inputs: Array<string>, outputs: Array<string>) {
const ii = await readData(folder, ...inputs);
const oo = await readData(folder, ...outputs);
return await startSession<TInputModel>({
ReadFile: async (filename: string): Promise<string> => (values(ii).first(each => each.filename === filename) || fail(`missing input '${filename}'`)).content,
GetValue: async (key: string): Promise<any> => {
if (!key) {
return config;
}
return config[key];
},
ListInputs: async (artifactType?: string): Promise<Array<string>> => ii.map(each => each.filename),
ProtectFiles: async (path: string): Promise<void> => {
// test
},
WriteFile: (filename: string, content: string, sourceMap?: any, artifactType?: string): void => {
// test
},
Message: (message: any): void => {
// test
console.error(message);
},
UpdateConfigurationFile: (filename: string, content: string): void => {
// test
},
GetConfigurationFile: async (filename: string): Promise<string> => '',
});
}
export async function createPassThruSession(config: any, input: string, inputArtifactType: string) {
return await startSession<CodeModel>({
ReadFile: async (filename: string): Promise<string> => input,
GetValue: async (key: string): Promise<any> => {
if (!key) {
return config;
}
return config[key];
},
ListInputs: async (artifactType?: string): Promise<Array<string>> => [inputArtifactType],
ProtectFiles: async (path: string): Promise<void> => {
// test
},
WriteFile: (filename: string, content: string, sourceMap?: any, artifactType?: string): void => {
// test
},
Message: (message: any): void => {
// test
console.error(message);
},
UpdateConfigurationFile: (filename: string, content: string): void => {
// test
},
GetConfigurationFile: async (filename: string): Promise<string> => '',
}, {}, codeModelSchema);
}

Просмотреть файл

@ -9,7 +9,8 @@
"types": [
"node"
],
"target": "es2016"
"target": "es2016",
"experimentalDecorators": true
},
"include": [
"src"