microsoft/typespec

Public

mirrored from https://github.com/microsoft/typespecAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
3040a83d6de0cc6876163b48ec9be61eefa3ebdd

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

e2e/e2e-tests.js

137lines · modecode

1// @ts-check
2import { existsSync, readdirSync, rmSync, writeFileSync } from "fs";
3import { join } from "path";
4import { repoRoot } from "../eng/scripts/helpers.js";
5import { runOrExit } from "../packages/internal-build-utils/dist/src/index.js";
6
7const e2eTestDir = join(repoRoot, "e2e");
8const npxCmd = process.platform === "win32" ? "npx.cmd" : "npx";
9
10async function main() {
11 printInfo();
12 await cleanE2EDirectory();
13 const packages = await packPackages();
14
15 console.log("Check packages exists");
16 await runOrExit("ls", [`${repoRoot}/temp/artifacts`]);
17
18 console.log("Check cli is working");
19 await runTypeSpec(packages["@typespec/compiler"], ["--help"], { cwd: e2eTestDir });
20 console.log("Cli is working");
21
22 await testBasicLatest(packages);
23 await testBasicCurrentTgz(packages);
24}
25await main();
26
27function printInfo() {
28 console.log("-".repeat(100));
29 console.log("Npm Version: ");
30 runOrExit("npm", ["-v"]);
31 console.log("-".repeat(100));
32}
33
34async function cleanE2EDirectory() {
35 await runOrExit("git", ["clean", "-xfd"], { cwd: e2eTestDir });
36}
37
38async function packPackages() {
39 await runOrExit("pnpm", ["-w", "pack:all"], { cwd: repoRoot });
40 const outputFolder = join(repoRoot, "/temp/artifacts");
41 const files = readdirSync(outputFolder);
42 console.log("Built packages:", files);
43
44 function resolvePackage(start) {
45 const pkgName = files.find((x) => x.startsWith(start));
46 if (pkgName === undefined) {
47 throw new Error(`Cannot resolve package starting with "${start}"`);
48 }
49 return join(outputFolder, pkgName);
50 }
51
52 return {
53 "@typespec/compiler": resolvePackage("typespec-compiler-"),
54 "@typespec/openapi": resolvePackage("typespec-openapi-"),
55 "@typespec/openapi3": resolvePackage("typespec-openapi3-"),
56 "@typespec/http": resolvePackage("typespec-http-"),
57 "@typespec/rest": resolvePackage("typespec-rest-"),
58 "@typespec/versioning": resolvePackage("typespec-versioning-"),
59 };
60}
61
62async function runTypeSpec(compilerTgz, args, options) {
63 await runOrExit(npxCmd, ["-y", "-p", compilerTgz, "tsp", ...args], { ...options });
64}
65
66async function testBasicLatest(packages) {
67 const basicLatestDir = join(e2eTestDir, "basic-latest");
68 const outputDir = join(basicLatestDir, "tsp-output");
69 console.log("Clearing basic-latest output");
70 rmSync(outputDir, { recursive: true, force: true });
71 console.log("Cleared basic-latest output");
72
73 console.log("Installing basic-latest dependencies");
74 await runTypeSpec(packages["@typespec/compiler"], ["install"], { cwd: basicLatestDir });
75 console.log("Installed basic-latest dependencies");
76
77 console.log("Running tsp compile .");
78 await runTypeSpec(
79 packages["@typespec/compiler"],
80 ["compile", ".", "--emit", "@typespec/openapi3"],
81 {
82 cwd: basicLatestDir,
83 }
84 );
85 console.log("Completed tsp compile .");
86
87 expectOpenApiOutput(outputDir);
88}
89
90async function testBasicCurrentTgz(packages) {
91 const basicCurrentDir = join(e2eTestDir, "basic-current");
92 const outputDir = join(basicCurrentDir, "tsp-output");
93 console.log("Clearing basic-current");
94 rmSync(outputDir, { recursive: true, force: true });
95 console.log("Cleared basic-current");
96
97 console.log("Generating package.json for basic-current");
98 const packageJson = {
99 name: "@typespec/e2e-test-basic-current",
100 dependencies: {
101 "@typespec/compiler": packages["@typespec/compiler"],
102 "@typespec/http": packages["@typespec/http"],
103 "@typespec/rest": packages["@typespec/rest"],
104 "@typespec/openapi": packages["@typespec/openapi"],
105 "@typespec/openapi3": packages["@typespec/openapi3"],
106 "@typespec/versioning": packages["@typespec/versioning"],
107 },
108 private: true,
109 };
110 writeFileSync(join(basicCurrentDir, "package.json"), JSON.stringify(packageJson, null, 2));
111 console.log("Generated package.json for basic-current");
112
113 console.log("Installing basic-current dependencies");
114 await runTypeSpec(packages["@typespec/compiler"], ["install"], { cwd: basicCurrentDir });
115 console.log("Installed basic-current dependencies");
116
117 console.log(`Running tsp compile . in "${basicCurrentDir}"`);
118 await runTypeSpec(
119 packages["@typespec/compiler"],
120 ["compile", ".", "--emit", "@typespec/openapi3"],
121 {
122 cwd: basicCurrentDir,
123 }
124 );
125 console.log("Completed tsp compile .");
126
127 expectOpenApiOutput(outputDir);
128}
129
130function expectOpenApiOutput(outputDir) {
131 const expectedOutputFile = join(outputDir, "@typespec/openapi3/openapi.yaml");
132 if (existsSync(expectedOutputFile)) {
133 console.log("Output created successfully.");
134 } else {
135 throw new Error(`Test failed to produce openapi output at "${expectedOutputFile}"`);
136 }
137}