2020-11-24 06:07:36 +03:00
|
|
|
import assert from "assert";
|
|
|
|
import * as fs from "fs";
|
|
|
|
import * as path from "path";
|
|
|
|
import ExampleGenerator from "../lib/generator/exampleGenerator";
|
|
|
|
import { ModelValidationError } from "../lib/util/modelValidationError";
|
2021-01-07 05:52:02 +03:00
|
|
|
import { generateExamples } from "../lib/validate";
|
|
|
|
import { log } from "../lib/util/logging";
|
2020-11-24 06:07:36 +03:00
|
|
|
|
|
|
|
const payloadDir = `test/exampleGenerator/payloads`;
|
|
|
|
const specRepoDir = `azure-rest-api-specs`;
|
|
|
|
jest.setTimeout(999999);
|
|
|
|
describe.skip("mock examples", () => {
|
|
|
|
const specFilePaths = getSpecFilePaths(specRepoDir);
|
|
|
|
for (const specPath of specFilePaths) {
|
|
|
|
const specInfo = specPath.split("/");
|
|
|
|
const apiVersion = specInfo[specInfo.length - 2];
|
|
|
|
const rp = specInfo[specInfo.length - 1].split(".")[0];
|
|
|
|
it(`spec for ${rp + apiVersion}`, async () => {
|
|
|
|
const errors: readonly ModelValidationError[] = await new ExampleGenerator(
|
|
|
|
specPath,
|
|
|
|
path.resolve(payloadDir, rp + apiVersion)
|
|
|
|
).generateAll();
|
|
|
|
if (errors.length > 0) {
|
|
|
|
console.error(errors);
|
|
|
|
}
|
|
|
|
assert.strictEqual(errors.length, 0);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-01-18 11:54:38 +03:00
|
|
|
describe("test generate example", () => {
|
2021-01-07 05:52:02 +03:00
|
|
|
const originalError = log.error;
|
|
|
|
//const originalLog = log.info;
|
|
|
|
const originalWarn = log.warn;
|
|
|
|
let consoleOutput: any[] = [];
|
2021-01-18 11:54:38 +03:00
|
|
|
const recordOutput = (output: any) => {
|
2021-01-07 05:52:02 +03:00
|
|
|
if (typeof output === "string" && output.indexOf("\\") !== -1) {
|
|
|
|
const result = output.replace(/\\/gi, "/");
|
|
|
|
consoleOutput.push(result);
|
|
|
|
} else {
|
|
|
|
consoleOutput.push(output);
|
|
|
|
}
|
2021-01-18 11:54:38 +03:00
|
|
|
};
|
2021-01-07 05:52:02 +03:00
|
|
|
//const mockedLog = (output: any) => recordOutput(output);
|
|
|
|
const mockedError = (output: any) => recordOutput(output);
|
|
|
|
const mockedWarn = (output: any) => recordOutput(output);
|
|
|
|
beforeAll(() => {
|
2021-01-18 11:54:38 +03:00
|
|
|
consoleOutput = [];
|
2021-01-07 05:52:02 +03:00
|
|
|
//log.info = mockedLog as any
|
2021-01-18 11:54:38 +03:00
|
|
|
log.error = mockedError as any;
|
2021-01-07 05:52:02 +03:00
|
|
|
log.warn = mockedWarn as any;
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
log.error = originalError;
|
|
|
|
//log.info = originalLog;
|
|
|
|
log.warn = originalWarn;
|
2021-01-18 11:54:38 +03:00
|
|
|
});
|
|
|
|
|
2021-01-07 05:52:02 +03:00
|
|
|
test.each<string[]>([
|
|
|
|
["sql", "package-pure-2020-02-preview"],
|
|
|
|
["signalr", "package-2020-05-01"],
|
2021-01-18 11:54:38 +03:00
|
|
|
["eventgrid", "package-2020-06"],
|
2021-01-07 05:52:02 +03:00
|
|
|
])(
|
|
|
|
"from payload,rp:%s",
|
|
|
|
async (resourceProviderName, tag) => {
|
|
|
|
await generateExamples(
|
|
|
|
"",
|
|
|
|
payloadDir,
|
|
|
|
undefined,
|
|
|
|
`test/exampleGenerator/specification/${resourceProviderName}/resource-manager/readme.md`,
|
|
|
|
tag
|
|
|
|
);
|
|
|
|
expect(consoleOutput).toMatchSnapshot(`,tag:${tag}`);
|
|
|
|
},
|
|
|
|
1000000
|
|
|
|
);
|
2021-01-18 11:54:38 +03:00
|
|
|
|
2021-01-07 05:52:02 +03:00
|
|
|
test.each<string[]>([
|
|
|
|
["sql", "package-pure-2020-02-preview"],
|
|
|
|
["signalr", "package-2020-05-01"],
|
2021-01-18 11:54:38 +03:00
|
|
|
["eventgrid", "package-2020-06"],
|
2021-01-07 05:52:02 +03:00
|
|
|
])(
|
|
|
|
"from mocker,readme:%s",
|
|
|
|
async (resourceProviderName, tag) => {
|
|
|
|
await generateExamples(
|
|
|
|
"",
|
|
|
|
undefined,
|
|
|
|
undefined,
|
|
|
|
`test/exampleGenerator/specification/${resourceProviderName}/resource-manager/readme.md`,
|
|
|
|
tag
|
|
|
|
);
|
|
|
|
expect(consoleOutput).toMatchSnapshot(`,tag:${tag}`);
|
|
|
|
},
|
|
|
|
1000000
|
|
|
|
);
|
2021-01-18 11:54:38 +03:00
|
|
|
});
|
2021-01-07 05:52:02 +03:00
|
|
|
|
2020-11-24 06:07:36 +03:00
|
|
|
export function getSpecFilePaths(repoDir: string) {
|
|
|
|
const rpList = fs.readdirSync(path.resolve(repoDir, "specification"));
|
|
|
|
|
|
|
|
const specPath: string[] = [];
|
|
|
|
rpList.forEach((rp: string) => {
|
|
|
|
if (!fs.existsSync(path.resolve(repoDir, "specification", rp, "resource-manager"))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const fullRps = fs
|
|
|
|
.readdirSync(path.resolve(repoDir, "specification", rp, "resource-manager"))
|
|
|
|
.filter((s) => s.startsWith("Microsoft."));
|
|
|
|
if (fullRps.length !== 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const stablePath = path.resolve(
|
|
|
|
repoDir,
|
|
|
|
"specification",
|
|
|
|
rp,
|
|
|
|
"resource-manager",
|
|
|
|
fullRps[0],
|
|
|
|
"stable"
|
|
|
|
);
|
|
|
|
if (!fs.existsSync(stablePath)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const versions = fs.readdirSync(stablePath).sort((i, j) => j.localeCompare(i));
|
|
|
|
if (versions.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const filePath = path.resolve(stablePath, versions[0], `${rp}.json`);
|
|
|
|
if (!fs.existsSync(filePath)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
specPath.push(filePath);
|
|
|
|
});
|
|
|
|
return specPath;
|
|
|
|
}
|