microsoft/typespec

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
db8de25e36b2ae9892be3bd9d56fc8f225a7ef6c

Branches

Tags

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

Clone

HTTPS

Download ZIP

e2e/e2e-tests.js

153lines · modecode

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