microsoft/typespec
Publicmirrored fromhttps://github.com/microsoft/typespecAvailable
e2e/e2e-tests.js
155lines · modecode
| 1 | /* eslint-disable no-console */ |
| 2 | // @ts-check |
| 3 | import { existsSync, readdirSync, rmSync, writeFileSync } from "fs"; |
| 4 | import { readdir } from "fs/promises"; |
| 5 | import { join } from "path"; |
| 6 | import { repoRoot } from "../eng/common/scripts/helpers.js"; |
| 7 | import { runOrExit } from "../packages/internal-build-utils/dist/src/index.js"; |
| 8 | |
| 9 | const e2eTestDir = join(repoRoot, "e2e"); |
| 10 | const npxCmd = process.platform === "win32" ? "npx.cmd" : "npx"; |
| 11 | |
| 12 | async 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 | } |
| 27 | await main(); |
| 28 | |
| 29 | /** |
| 30 | * @param {string} dir |
| 31 | */ |
| 32 | async 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 | |
| 41 | function printInfo() { |
| 42 | console.log("-".repeat(100)); |
| 43 | console.log("Npm Version: "); |
| 44 | runOrExit("npm", ["-v"]); |
| 45 | console.log("-".repeat(100)); |
| 46 | } |
| 47 | |
| 48 | async function cleanE2EDirectory() { |
| 49 | await runOrExit("git", ["clean", "-xfd"], { cwd: e2eTestDir }); |
| 50 | } |
| 51 | |
| 52 | async 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 | "@typespec/asset-emitter": resolvePackage("typespec-asset-emitter-"), |
| 74 | }; |
| 75 | } |
| 76 | |
| 77 | async function runTypeSpec(compilerTgz, args, options) { |
| 78 | await runOrExit(npxCmd, ["-y", "-p", compilerTgz, "tsp", ...args], { ...options }); |
| 79 | } |
| 80 | |
| 81 | async function testBasicLatest(packages) { |
| 82 | const basicLatestDir = join(e2eTestDir, "basic-latest"); |
| 83 | const outputDir = join(basicLatestDir, "tsp-output"); |
| 84 | console.log("Clearing basic-latest output"); |
| 85 | rmSync(outputDir, { recursive: true, force: true }); |
| 86 | console.log("Cleared basic-latest output"); |
| 87 | |
| 88 | console.log("Installing basic-latest dependencies"); |
| 89 | await runTypeSpec(packages["@typespec/compiler"], ["install"], { |
| 90 | cwd: basicLatestDir, |
| 91 | }); |
| 92 | console.log("Installed basic-latest dependencies"); |
| 93 | |
| 94 | console.log("Running tsp compile ."); |
| 95 | await runTypeSpec( |
| 96 | packages["@typespec/compiler"], |
| 97 | ["compile", ".", "--emit", "@typespec/openapi3"], |
| 98 | { |
| 99 | cwd: basicLatestDir, |
| 100 | }, |
| 101 | ); |
| 102 | console.log("Completed tsp compile ."); |
| 103 | |
| 104 | expectOpenApiOutput(outputDir); |
| 105 | } |
| 106 | |
| 107 | async function testBasicCurrentTgz(packages) { |
| 108 | const basicCurrentDir = join(e2eTestDir, "basic-current"); |
| 109 | const outputDir = join(basicCurrentDir, "tsp-output"); |
| 110 | console.log("Clearing basic-current"); |
| 111 | rmSync(outputDir, { recursive: true, force: true }); |
| 112 | console.log("Cleared basic-current"); |
| 113 | |
| 114 | console.log("Generating package.json for basic-current"); |
| 115 | const packageJson = { |
| 116 | name: "@typespec/e2e-test-basic-current", |
| 117 | dependencies: { |
| 118 | "@typespec/compiler": packages["@typespec/compiler"], |
| 119 | "@typespec/http": packages["@typespec/http"], |
| 120 | "@typespec/rest": packages["@typespec/rest"], |
| 121 | "@typespec/openapi": packages["@typespec/openapi"], |
| 122 | "@typespec/openapi3": packages["@typespec/openapi3"], |
| 123 | "@typespec/versioning": packages["@typespec/versioning"], |
| 124 | "@typespec/asset-emitter": packages["@typespec/asset-emitter"], |
| 125 | }, |
| 126 | private: true, |
| 127 | }; |
| 128 | writeFileSync(join(basicCurrentDir, "package.json"), JSON.stringify(packageJson, null, 2)); |
| 129 | console.log("Generated package.json for basic-current"); |
| 130 | |
| 131 | console.log("Installing basic-current dependencies"); |
| 132 | await runTypeSpec(packages["@typespec/compiler"], ["install"], { cwd: basicCurrentDir }); |
| 133 | console.log("Installed basic-current dependencies"); |
| 134 | |
| 135 | console.log(`Running tsp compile . in "${basicCurrentDir}"`); |
| 136 | await runTypeSpec( |
| 137 | packages["@typespec/compiler"], |
| 138 | ["compile", ".", "--emit", "@typespec/openapi3"], |
| 139 | { |
| 140 | cwd: basicCurrentDir, |
| 141 | }, |
| 142 | ); |
| 143 | console.log("Completed tsp compile ."); |
| 144 | |
| 145 | expectOpenApiOutput(outputDir); |
| 146 | } |
| 147 | |
| 148 | function expectOpenApiOutput(outputDir) { |
| 149 | const expectedOutputFile = join(outputDir, "@typespec/openapi3/openapi.yaml"); |
| 150 | if (existsSync(expectedOutputFile)) { |
| 151 | console.log("Output created successfully."); |
| 152 | } else { |
| 153 | throw new Error(`Test failed to produce openapi output at "${expectedOutputFile}"`); |
| 154 | } |
| 155 | } |
| 156 | |