Skip to content

Commit

Permalink
[tsp-client] Add acceptance tests (#29811)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeharder authored Jul 16, 2024
1 parent 1aa9126 commit ac1f4e8
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 4 deletions.
11 changes: 10 additions & 1 deletion .github/workflows/_reusable-eng-tools-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ on:
description: Name of package under eng/tools
required: true
type: string
sparse-checkout-paths:
description: Paths for sparse checkout
type: string

jobs:
test:
Expand All @@ -24,9 +27,15 @@ jobs:
runs-on: ${{ matrix.os }}

steps:
- if: runner.os == 'Windows'
run: git config --global core.longpaths true
shell: pwsh

- uses: actions/checkout@v4
with:
sparse-checkout: eng
sparse-checkout: |
eng
${{ inputs.sparse-checkout-paths }}
- name: Use Node ${{ matrix.node-version }}.x
uses: actions/setup-node@v4
Expand Down
28 changes: 28 additions & 0 deletions .github/workflows/tsp-client-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: tsp-client - Test

on:
push:
branches:
- main
pull_request:
paths:
- package-lock.json
- package.json
- tsconfig.json
- .github/workflows/_reusable-eng-tools-test.yaml
- .github/workflows/tsp-client-test.yaml
- eng/tools/package.json
- eng/tools/tsconfig.json
- eng/tools/tsp-client/**
- specification/keyvault
- specification/sphere

jobs:
tsp-client:
uses: ./.github/workflows/_reusable-eng-tools-test.yaml
with:
package: tsp-client-tests
sparse-checkout-paths: |
specification/common-types
specification/keyvault
specification/sphere
1 change: 1 addition & 0 deletions eng/tools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "azure-rest-api-specs-eng-tools",
"devDependencies": {
"@azure-tools/suppressions": "file:suppressions",
"@azure-tools/tsp-client-tests": "file:tsp-client-tests",
"@azure-tools/typespec-requirement": "file:typespec-requirement",
"@azure-tools/typespec-validation": "file:typespec-validation"
},
Expand Down
20 changes: 20 additions & 0 deletions eng/tools/tsp-client-tests/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "@azure-tools/tsp-client-tests",
"private": true,
"type": "module",
"devDependencies": {
"@types/node": "^18.19.31",
"execa": "^9.3.0",
"typescript": "~5.4.5",
"vitest": "^1.6.0"
},
"scripts": {
"build": "tsc",
"postinstall": "npm run build",
"test": "vitest",
"test:ci": "vitest run --reporter=verbose"
},
"engines": {
"node": ">= 18.0.0"
}
}
72 changes: 72 additions & 0 deletions eng/tools/tsp-client-tests/test/tsp-client.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { execa } from "execa";
import { access, constants, mkdir, rm } from "fs/promises";
import { dirname, join } from "path";
import { ExpectStatic, test } from "vitest";

const repoRoot = join(__dirname, "..", "..", "..", "..");

async function tspClient(...args: string[]) {
const allArgs = ["exec", "--no", "--", "tsp-client"].concat(args);

console.log(`${repoRoot}$ npm ${allArgs.join(" ")}`);

return await execa("npm", allArgs, { all: true, cwd: repoRoot, reject: false });
}

async function convert(expect: ExpectStatic, readme: string) {
const specFolder = dirname(dirname(join(repoRoot, readme)));
const outputFolder = join(specFolder, "Test.TspClientConvert");

try {
await mkdir(outputFolder);
} catch {
// Delete and retry
await rm(outputFolder, { recursive: true, force: true });
await mkdir(outputFolder);
}

try {
const { stdout, all, exitCode } = await tspClient(
"convert",
"--no-prompt",
"--swagger-readme",
readme,
"-o",
outputFolder,
readme.includes("resource-manager") ? "--arm" : "",
);

expect(stdout).toContain("Converting");
expect(exitCode, all).toBe(0);

const tspConfigYaml = join(outputFolder, "tspconfig.yaml");
await access(tspConfigYaml, constants.R_OK);
console.log(`File exists: ${tspConfigYaml}`);

const mainTsp = join(outputFolder, "main.tsp");
await access(mainTsp, constants.R_OK);
console.log(`File exists: ${mainTsp}`);
} finally {
await rm(outputFolder, { recursive: true, force: true });
}

// Ensure outputFolder is deleted
expect(() => access(outputFolder)).rejects.toThrowError();
}

// TODO: Convert to `test.concurrent()` once Azure/azure-sdk-tools#8610 is merged,
// which should fix race condition bug calling `npx autorest`.
test("Usage", async ({ expect }) => {
const { stdout, exitCode } = await tspClient();

expect(stdout).toContain("Usage");
expect(exitCode).not.toBe(0);
});

test("Convert keyvault/data-plane", async ({ expect }) => {
await convert(expect, "specification/keyvault/data-plane/readme.md");
});

test("Convert sphere/resource-manager", async ({ expect }) => {
await convert(expect, "specification/sphere/resource-manager/readme.md");
});
6 changes: 6 additions & 0 deletions eng/tools/tsp-client-tests/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "./dist",
}
}
7 changes: 7 additions & 0 deletions eng/tools/tsp-client-tests/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from "vite";

export default defineConfig({
test: {
testTimeout: 240000,
},
});
3 changes: 1 addition & 2 deletions eng/tools/typespec-requirement/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"type": "module",
"devDependencies": {
"@types/node": "^18.19.31",
"@vitest/coverage-v8": "^1.6.0",
"execa": "^9.3.0",
"typescript": "~5.4.5",
"vitest": "^1.6.0"
Expand All @@ -13,7 +12,7 @@
"build": "tsc",
"postinstall": "npm run build",
"test": "vitest",
"test:ci": "vitest run --coverage --reporter=verbose"
"test:ci": "vitest run --reporter=verbose"
},
"engines": {
"node": ">= 18.0.0"
Expand Down
20 changes: 19 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ac1f4e8

Please sign in to comment.