|
| 1 | +import { execa } from "execa"; |
| 2 | +import { access, constants, mkdir, rm } from "fs/promises"; |
| 3 | +import { dirname, join } from "path"; |
| 4 | +import { ExpectStatic, test } from "vitest"; |
| 5 | + |
| 6 | +const repoRoot = join(__dirname, "..", "..", "..", ".."); |
| 7 | + |
| 8 | +async function tspClient(...args: string[]) { |
| 9 | + const allArgs = ["exec", "--no", "--", "tsp-client"].concat(args); |
| 10 | + |
| 11 | + console.log(`${repoRoot}$ npm ${allArgs.join(" ")}`); |
| 12 | + |
| 13 | + return await execa("npm", allArgs, { all: true, cwd: repoRoot, reject: false }); |
| 14 | +} |
| 15 | + |
| 16 | +async function convert(expect: ExpectStatic, readme: string) { |
| 17 | + const specFolder = dirname(dirname(join(repoRoot, readme))); |
| 18 | + const outputFolder = join(specFolder, "Test.TspClientConvert"); |
| 19 | + |
| 20 | + try { |
| 21 | + await mkdir(outputFolder); |
| 22 | + } catch { |
| 23 | + // Delete and retry |
| 24 | + await rm(outputFolder, { recursive: true, force: true }); |
| 25 | + await mkdir(outputFolder); |
| 26 | + } |
| 27 | + |
| 28 | + try { |
| 29 | + const { stdout, all, exitCode } = await tspClient( |
| 30 | + "convert", |
| 31 | + "--no-prompt", |
| 32 | + "--swagger-readme", |
| 33 | + readme, |
| 34 | + "-o", |
| 35 | + outputFolder, |
| 36 | + readme.includes("resource-manager") ? "--arm" : "", |
| 37 | + ); |
| 38 | + |
| 39 | + expect(stdout).toContain("Converting"); |
| 40 | + expect(exitCode, all).toBe(0); |
| 41 | + |
| 42 | + const tspConfigYaml = join(outputFolder, "tspconfig.yaml"); |
| 43 | + await access(tspConfigYaml, constants.R_OK); |
| 44 | + console.log(`File exists: ${tspConfigYaml}`); |
| 45 | + |
| 46 | + const mainTsp = join(outputFolder, "main.tsp"); |
| 47 | + await access(mainTsp, constants.R_OK); |
| 48 | + console.log(`File exists: ${mainTsp}`); |
| 49 | + } finally { |
| 50 | + await rm(outputFolder, { recursive: true, force: true }); |
| 51 | + } |
| 52 | + |
| 53 | + // Ensure outputFolder is deleted |
| 54 | + expect(() => access(outputFolder)).rejects.toThrowError(); |
| 55 | +} |
| 56 | + |
| 57 | +// TODO: Convert to `test.concurrent()` once Azure/azure-sdk-tools#8610 is merged, |
| 58 | +// which should fix race condition bug calling `npx autorest`. |
| 59 | +test("Usage", async ({ expect }) => { |
| 60 | + const { stdout, exitCode } = await tspClient(); |
| 61 | + |
| 62 | + expect(stdout).toContain("Usage"); |
| 63 | + expect(exitCode).not.toBe(0); |
| 64 | +}); |
| 65 | + |
| 66 | +test("Convert keyvault/data-plane", async ({ expect }) => { |
| 67 | + await convert(expect, "specification/keyvault/data-plane/readme.md"); |
| 68 | +}); |
| 69 | + |
| 70 | +test("Convert sphere/resource-manager", async ({ expect }) => { |
| 71 | + await convert(expect, "specification/sphere/resource-manager/readme.md"); |
| 72 | +}); |
0 commit comments