microsoft/typespec
Publicmirrored from https://github.com/microsoft/typespecAvailable
e2e/e2e-tests.js
138lines · modecode
| 1 | /* eslint-disable no-console */ |
| 2 | // @ts-check |
| 3 | import { existsSync, readdirSync, rmSync, writeFileSync } from "fs"; |
| 4 | import { join } from "path"; |
| 5 | import { repoRoot } from "../eng/common/scripts/helpers.js"; |
| 6 | import { runOrExit } from "../packages/internal-build-utils/dist/src/index.js"; |
| 7 | |
| 8 | const e2eTestDir = join(repoRoot, "e2e"); |
| 9 | const npxCmd = process.platform === "win32" ? "npx.cmd" : "npx"; |
| 10 | |
| 11 | async 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 | } |
| 26 | await main(); |
| 27 | |
| 28 | function printInfo() { |
| 29 | console.log("-".repeat(100)); |
| 30 | console.log("Npm Version: "); |
| 31 | runOrExit("npm", ["-v"]); |
| 32 | console.log("-".repeat(100)); |
| 33 | } |
| 34 | |
| 35 | async function cleanE2EDirectory() { |
| 36 | await runOrExit("git", ["clean", "-xfd"], { cwd: e2eTestDir }); |
| 37 | } |
| 38 | |
| 39 | async 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 | |
| 63 | async function runTypeSpec(compilerTgz, args, options) { |
| 64 | await runOrExit(npxCmd, ["-y", "-p", compilerTgz, "tsp", ...args], { ...options }); |
| 65 | } |
| 66 | |
| 67 | async 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 | |
| 91 | async 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 | |
| 131 | function 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 | } |