Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix TCGC broken with latest compiler and add e2e tests #818

Merged
merged 14 commits into from
May 10, 2024
7 changes: 7 additions & 0 deletions .chronus/changes/add_e2e_test-2024-4-10-13-19-5.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: internal
packages:
- "@azure-tools/typespec-client-generator-core"
---

add ci step to check against latest released typespec compiler
5 changes: 5 additions & 0 deletions packages/typespec-client-generator-core/basic-latest/main.tsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import "@typespec/rest";
import "@typespec/openapi3";
import "@azure-tools/typespec-client-generator-core";

op ping(): void;
15 changes: 15 additions & 0 deletions packages/typespec-client-generator-core/basic-latest/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "@azure-tools/tcgc-test-basic-latest",
"dependencies": {
"@typespec/compiler": "latest",
"@typespec/rest": "latest",
"@typespec/http": "latest",
"@typespec/versioning": "latest",
"@typespec/openapi": "latest",
"@typespec/openapi3": "latest",
"@azure-tools/typespec-azure-core": "latest",
"@azure-tools/typespec-autorest": "latest",
"@azure-tools/typespec-azure-resource-manager": "latest"
},
"private": true
}
93 changes: 93 additions & 0 deletions packages/typespec-client-generator-core/e2e-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/* eslint-disable no-console */

import { readdirSync, rmSync } from "fs";
import { join } from "path";
import { coreRepoRoot, repoRoot, run } from "../../eng/scripts/helpers.js";

const tcgcTestDir = join(repoRoot, "packages", "typespec-client-generator-core");
const npxCmd = process.platform === "win32" ? "npx.cmd" : "npx";

function main() {
printInfo();
cleanTcgcDirectory();
const packages = packPackages();
testBasicLatest(packages);
}
main();

function printInfo() {
console.log("-".repeat(100)); //
console.log("Npm Version: ");
run("npm", ["-v"]);
console.log("-".repeat(100));
}

function cleanTcgcDirectory() {
run("git", ["clean", "-xfd"], { cwd: tcgcTestDir });
}

function packPackages() {
run("pnpm", ["-w", "pack:all"], { cwd: repoRoot });
run("pnpm", ["-w", "pack:all"], { cwd: coreRepoRoot });
const azureOutputFolder = join(repoRoot, "/temp/artifacts");
const coreOutputFolder = join(repoRoot, "/core/temp/artifacts");
const files = readdirSync(azureOutputFolder).concat(readdirSync(coreOutputFolder));

console.log("Built packages:", files);

function resolvePackage(start) {
const pkgName = files.find((x) => x.startsWith(start));
if (pkgName === undefined) {
throw new Error(`Cannot resolve package starting with "${start}"`);
}
const outputFolder = start.startsWith("azure-tools") ? azureOutputFolder : coreOutputFolder;
return join(outputFolder, pkgName);
}

return {
"@typespec/compiler": resolvePackage("typespec-compiler-"),
"@typespec/openapi": resolvePackage("typespec-openapi-"),
"@typespec/openapi3": resolvePackage("typespec-openapi3-"),
"@typespec/http": resolvePackage("typespec-http-"),
"@typespec/rest": resolvePackage("typespec-rest-"),
"@typespec/versioning": resolvePackage("typespec-versioning-"),
"@azure-tools/typespec-azure-core": resolvePackage("azure-tools-typespec-azure-core-"),
"@azure-tools/typespec-autorest": resolvePackage("azure-tools-typespec-autorest-"),
"@azure-tools/typespec-azure-resource-manager": resolvePackage(
"azure-tools-typespec-azure-resource-manager-"
),
};
}

function runTypeSpec(compilerTgz, args, options) {
run(npxCmd, ["-y", "-p", compilerTgz, "tsp", ...args], { ...options });
}

function testBasicLatest(packages) {
const basicLatestDir = join(tcgcTestDir, "basic-latest");
const outputDir = join(basicLatestDir, "tsp-output");
console.log("Clearing basic-latest output");
rmSync(outputDir, { recursive: true, force: true });
console.log("Cleared basic-latest output");

console.log("Installing basic-latest dependencies");
runTypeSpec(packages["@typespec/compiler"], ["install"], { cwd: basicLatestDir });
console.log("Installed basic-latest dependencies");

console.log("Running tsp compile .");
runTypeSpec(packages["@typespec/compiler"], ["compile", ".", "--emit", "@typespec/openapi3"], {
cwd: basicLatestDir,
});
console.log("Completed tsp compile .");

expectOpenApiOutput(outputDir);
}

function expectOpenApiOutput(outputDir) {
const expectedOutputFile = join(outputDir, "@typespec/openapi3/openapi.yaml");
if (existsSync(expectedOutputFile)) {
console.log("Output created successfully.");
} else {
throw new Error(`Test failed to produce openapi output at "${expectedOutputFile}"`);
}
}
1 change: 1 addition & 0 deletions packages/typespec-client-generator-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"test:watch": "vitest -w",
"test:ui": "vitest --ui",
"test:ci": "vitest run --coverage --reporter=junit --reporter=default",
"test:e2e": "node ./e2e-tests.js",
"lint": "eslint . --max-warnings=0",
"lint:fix": "eslint . --fix ",
"regen-docs": "tspd doc . --enable-experimental --output-dir ../../docs/libraries/typespec-client-generator-core/reference"
Expand Down
Loading