Skip to content

Commit

Permalink
chore: check if generated CTS match committed CTS (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
millotp authored Feb 2, 2022
1 parent e3b7ed8 commit a29c4d5
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 114 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -158,5 +158,8 @@ jobs:
- name: Generate CTS
run: yarn cts:generate

- name: Check diff with pushed CTS
run: exit $(git status --porcelain ./tests/output | wc -l)

- name: Run CTS
run: yarn cts:test
10 changes: 10 additions & 0 deletions tests/CTS/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"javascript": {
"extension": "test.ts",
"outputFolder": "tests"
},
"java": {
"extension": "test.java",
"outputFolder": "src/test/java/com/algolia"
}
}
2 changes: 1 addition & 1 deletion tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
],
"scripts": {
"build": "tsc",
"generate": "yarn generate:methods:requets ${0:-javascript} ${1:-search} && yarn generate:client ${0:-javascript} ${1:-search} && yarn format ${0:-javascript}",
"generate": "yarn generate:methods:requets ${0:-javascript} ${1:-search} && yarn generate:client ${0:-javascript} ${1:-search}",
"generate:methods:requets": "node dist/tests/src/methods/requests/main.js ${0:-javascript} ${1:-search}",
"generate:client": "node dist/tests/src/client/main.js ${0:-javascript} ${1:-search}",
"format": "../scripts/formatter.sh ${0:-javascript} tests/output/${0:-javascript} && yarn lint",
Expand Down
44 changes: 12 additions & 32 deletions tests/src/client/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ import Mustache from 'mustache';
import openapitools from '../../../openapitools.json';
import {
walk,
extensionForLanguage,
packageNames,
createClientName,
exists,
createOutputDir,
getOutputPath,
loadTemplates,
} from '../utils';

import type { TestsBlock, Test, ModifiedStepForMustache } from './types';

const testPath = 'client';

async function loadTests(client: string): Promise<TestsBlock[]> {
const testsBlocks: TestsBlock[] = [];
const clientPath = `./CTS/client/${client}`;
Expand Down Expand Up @@ -53,28 +57,6 @@ async function loadTests(client: string): Promise<TestsBlock[]> {
return testsBlocks;
}

async function loadTemplates(
language: string
): Promise<Record<string, string>> {
const templates: Record<string, string> = {};
const templatePath = `./CTS/client/templates/${language}`;

await exists(`./CTS/client/templates/javascript`);
if (!(await exists(templatePath))) {
return {};
}

for await (const file of walk(templatePath)) {
if (!file.name.endsWith('.mustache')) {
continue;
}
const type = file.name.replace('.mustache', '');
const fileContent = (await fsp.readFile(file.path)).toString();
templates[type] = fileContent;
}
return templates;
}

export async function generateTests(
language: string,
client: string
Expand All @@ -89,11 +71,12 @@ export async function generateTests(
return;
}

const outputPath = `output/${language}/tests/client/`;
await fsp.mkdir(outputPath, { recursive: true });
const { suite: template, ...partialTemplates } = await loadTemplates(
language
);
await createOutputDir({ language, testPath });

const { suite: template, ...partialTemplates } = await loadTemplates({
language,
testPath,
});

if (!template) {
// eslint-disable-next-line no-console
Expand All @@ -117,10 +100,7 @@ export async function generateTests(
},
partialTemplates
);
await fsp.writeFile(
`${outputPath}/${client}.${extensionForLanguage[language]}`,
code
);
await fsp.writeFile(getOutputPath({ language, client, testPath }), code);
}

function serializeParameters(parameters: any): string {
Expand Down
53 changes: 15 additions & 38 deletions tests/src/methods/requests/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,27 @@ import openapitools from '../../../../openapitools.json';
import {
createClientName,
packageNames,
extensionForLanguage,
sourcePathForLanguage,
capitalize,
getOutputPath,
createOutputDir,
loadTemplates,
} from '../../utils';

import { loadCTS } from './cts';
import { loadPartials, loadRequestsTemplate } from './templates';
import type { CTSBlock } from './types';

async function createOutputDir(language: string): Promise<void> {
await fsp.mkdir(`output/${language}/${sourcePathForLanguage[language]}`, {
recursive: true,
});
}
const testPath = 'methods/requests';

async function generateRequestsTests(
cts: CTSBlock[],
template: string,
export async function generateTests(
language: string,
client: string,
partials: Record<string, string>
client: string
): Promise<void> {
const { requests: template, ...partialTemplates } = await loadTemplates({
language,
testPath,
});
const cts = (await loadCTS(client)).requests;
await createOutputDir({ language, testPath });

if (cts.length === 0) {
return;
}
Expand All @@ -54,30 +53,8 @@ async function generateRequestsTests(
};
},
},
partials
partialTemplates
);

await fsp.writeFile(
`output/${language}/${sourcePathForLanguage[language]}/${client}.${extensionForLanguage[language]}`,
code
);
}

export async function generateTests(
language: string,
client: string
): Promise<void> {
const template = await loadRequestsTemplate(language);
const cts = await loadCTS(client);
const partials = await loadPartials(language);

await createOutputDir(language);

await generateRequestsTests(
cts.requests,
template,
language,
client,
partials
);
await fsp.writeFile(getOutputPath({ language, client, testPath }), code);
}
30 changes: 0 additions & 30 deletions tests/src/methods/requests/templates.ts

This file was deleted.

68 changes: 55 additions & 13 deletions tests/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fsp from 'fs/promises';
import path from 'path';

import openapitools from '../../openapitools.json';
import ctsConfig from '../CTS/config.json';

// For each generator, we map the packageName with the language and client
export const packageNames: Record<
Expand Down Expand Up @@ -90,19 +91,8 @@ export function removeEnumType(obj: any): any {
return obj;
}

// All those language dependents object should be defined in the CTS itself
export const extensionForLanguage: Record<string, string> = {
javascript: 'test.ts',
java: 'test.java',
};

export const sourcePathForLanguage: Record<string, string> = {
javascript: 'tests/methods/requests',
java: 'src/test/java/com/algolia',
};

/* eslint-disable no-console */
function printUsage(commandName: string): void {
/* eslint-disable no-console */
console.log(`usage: ${commandName} language client`);
// eslint-disable-next-line no-process-exit
process.exit(1);
Expand Down Expand Up @@ -130,10 +120,62 @@ export function parseCLI(
// eslint-disable-next-line no-process-exit
process.exit(1);
}
/* eslint-enable no-console */

return {
lang,
client,
};
}
/* eslint-enable no-console */

export async function createOutputDir({
language,
testPath,
}: {
language: string;
testPath: string;
}): Promise<void> {
await fsp.mkdir(
`output/${language}/${ctsConfig[language].outputFolder}/${testPath}`,
{
recursive: true,
}
);
}

export function getOutputPath({
language,
client,
testPath,
}: {
language: string;
client: string;
testPath: string;
}): string {
return `output/${language}/${ctsConfig[language].outputFolder}/${testPath}/${client}.${ctsConfig[language].extension}`;
}

export async function loadTemplates({
language,
testPath,
}: {
language: string;
testPath: string;
}): Promise<Record<string, string>> {
const templates: Record<string, string> = {};
const templatePath = `./CTS/${testPath}/templates/${language}`;

if (!(await exists(templatePath))) {
return {};
}

for await (const file of walk(templatePath)) {
if (!file.name.endsWith('.mustache')) {
continue;
}
const name = file.name.replace('.mustache', '');
const fileContent = (await fsp.readFile(file.path)).toString();
templates[name] = fileContent;
}
return templates;
}

0 comments on commit a29c4d5

Please sign in to comment.