microsoft/typespec

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
4ddf43ae0dffb11cf982bebe006f37d5524bee69

Branches

Tags

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

Clone

HTTPS

Download ZIP

e2e/e2e-tests.js

138lines · modecode

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