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