From ed18958f237b06483511c818414429edf81b2eec Mon Sep 17 00:00:00 2001 From: Timothee Guerin Date: Mon, 29 Sep 2025 13:32:45 -0700 Subject: [PATCH 1/2] Migrate autorest canonical to tester v2 --- .../src/emitter.ts | 16 ++--- .../typespec-autorest-canonical/src/index.ts | 3 +- .../typespec-autorest-canonical/src/lib.ts | 2 +- .../src/testing/index.ts | 10 --- .../test/test-host.ts | 63 ------------------- .../test/tester.ts | 31 +++++++++ .../test/versioning.test.ts | 2 +- .../typespec-autorest-canonical/tsconfig.json | 3 +- 8 files changed, 45 insertions(+), 85 deletions(-) delete mode 100644 packages/typespec-autorest-canonical/src/testing/index.ts delete mode 100644 packages/typespec-autorest-canonical/test/test-host.ts create mode 100644 packages/typespec-autorest-canonical/test/tester.ts diff --git a/packages/typespec-autorest-canonical/src/emitter.ts b/packages/typespec-autorest-canonical/src/emitter.ts index 17dfe4a4c6..9c4bb472ac 100644 --- a/packages/typespec-autorest-canonical/src/emitter.ts +++ b/packages/typespec-autorest-canonical/src/emitter.ts @@ -1,24 +1,24 @@ import { - AutorestDocumentEmitterOptions, - AutorestEmitterContext, + type AutorestDocumentEmitterOptions, + type AutorestEmitterContext, getOpenAPIForService, sortOpenAPIDocument, } from "@azure-tools/typespec-autorest"; import { isArmCommonType } from "@azure-tools/typespec-azure-resource-manager"; import { createTCGCContext, type TCGCContext } from "@azure-tools/typespec-client-generator-core"; import { - EmitContext, + type EmitContext, emitFile, getDirectoryPath, getNamespaceFullName, interpolatePath, listServices, - Namespace, + type Namespace, navigateType, - Program, + type Program, resolvePath, - Service, - Type, + type Service, + type Type, } from "@typespec/compiler"; import { getRenamedFrom, @@ -26,7 +26,7 @@ import { getTypeChangedFrom, getVersion, } from "@typespec/versioning"; -import { AutorestCanonicalEmitterOptions, reportDiagnostic } from "./lib.js"; +import { type AutorestCanonicalEmitterOptions, reportDiagnostic } from "./lib.js"; const defaultOptions = { "output-file": "{azure-resource-provider-folder}/{service-name}/canonical/openapi.json", diff --git a/packages/typespec-autorest-canonical/src/index.ts b/packages/typespec-autorest-canonical/src/index.ts index a26a428169..d16a04e49f 100644 --- a/packages/typespec-autorest-canonical/src/index.ts +++ b/packages/typespec-autorest-canonical/src/index.ts @@ -1,2 +1,3 @@ export { $onEmit } from "./emitter.js"; -export { $lib, AutorestCanonicalEmitterOptions } from "./lib.js"; +export { $lib } from "./lib.js"; +export type { AutorestCanonicalEmitterOptions } from "./lib.js"; diff --git a/packages/typespec-autorest-canonical/src/lib.ts b/packages/typespec-autorest-canonical/src/lib.ts index 0122ea8e62..9c80847158 100644 --- a/packages/typespec-autorest-canonical/src/lib.ts +++ b/packages/typespec-autorest-canonical/src/lib.ts @@ -1,4 +1,4 @@ -import { createTypeSpecLibrary, JSONSchemaType, paramMessage } from "@typespec/compiler"; +import { createTypeSpecLibrary, type JSONSchemaType, paramMessage } from "@typespec/compiler"; export interface AutorestCanonicalEmitterOptions { /** diff --git a/packages/typespec-autorest-canonical/src/testing/index.ts b/packages/typespec-autorest-canonical/src/testing/index.ts deleted file mode 100644 index 953b97e30d..0000000000 --- a/packages/typespec-autorest-canonical/src/testing/index.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { - TypeSpecTestLibrary, - createTestLibrary, - findTestPackageRoot, -} from "@typespec/compiler/testing"; - -export const AutorestCanonicalTestLibrary: TypeSpecTestLibrary = createTestLibrary({ - name: "@azure-tools/typespec-autorest-canonical", - packageRoot: await findTestPackageRoot(import.meta.url), -}); diff --git a/packages/typespec-autorest-canonical/test/test-host.ts b/packages/typespec-autorest-canonical/test/test-host.ts deleted file mode 100644 index 15a15cd348..0000000000 --- a/packages/typespec-autorest-canonical/test/test-host.ts +++ /dev/null @@ -1,63 +0,0 @@ -import { - TestHost, - createTestHost, - createTestWrapper, - expectDiagnosticEmpty, - resolveVirtualPath, -} from "@typespec/compiler/testing"; -import { HttpTestLibrary } from "@typespec/http/testing"; -import { OpenAPITestLibrary } from "@typespec/openapi/testing"; -import { RestTestLibrary } from "@typespec/rest/testing"; -import { VersioningTestLibrary } from "@typespec/versioning/testing"; -import { AutorestCanonicalEmitterOptions } from "../src/lib.js"; -import { AutorestCanonicalTestLibrary } from "../src/testing/index.js"; - -export async function createAutorestCanonicalTestHost() { - return createTestHost({ - libraries: [ - HttpTestLibrary, - RestTestLibrary, - OpenAPITestLibrary, - AutorestCanonicalTestLibrary, - VersioningTestLibrary, - ], - }); -} - -export async function createAutorestCanonicalTestRunner(host?: TestHost) { - host ??= await createAutorestCanonicalTestHost(); - return createTestWrapper(host, { - autoUsings: ["Versioning", "Http", "Rest", "OpenAPI"], - }); -} - -export async function openApiFor(code: string, options: AutorestCanonicalEmitterOptions = {}) { - const runner = await createAutorestCanonicalTestRunner(); - const diagnostics = await runner.diagnose(code, { - noEmit: false, - emit: ["@azure-tools/typespec-autorest-canonical"], - options: { - [AutorestCanonicalTestLibrary.name]: { - ...options, - "emitter-output-dir": resolveVirtualPath("tsp-output"), - }, - }, - }); - - expectDiagnosticEmpty(diagnostics); - const outPath = resolveVirtualPath("tsp-output", "canonical", "openapi.json"); - return JSON.parse(runner.fs.get(outPath)!); -} - -export async function diagnoseOpenApiFor( - code: string, - options: AutorestCanonicalEmitterOptions = {}, -) { - const runner = await createAutorestCanonicalTestRunner(); - return await runner.diagnose(code, { - emit: ["@azure-tools/typespec-autorest-canonical"], - options: { - "@azure-tools/typespec-autorest-canonical": options as any, - }, - }); -} diff --git a/packages/typespec-autorest-canonical/test/tester.ts b/packages/typespec-autorest-canonical/test/tester.ts new file mode 100644 index 0000000000..22b6fc23ce --- /dev/null +++ b/packages/typespec-autorest-canonical/test/tester.ts @@ -0,0 +1,31 @@ +import { resolvePath } from "@typespec/compiler"; +import { createTester } from "@typespec/compiler/testing"; + +export const ApiTester = createTester(resolvePath(import.meta.dirname, ".."), { + libraries: [ + "@typespec/http", + "@typespec/rest", + "@typespec/openapi", + "@azure-tools/typespec-autorest-canonical", + "@typespec/versioning", + ], +}); + +export const Tester = ApiTester.import( + "@typespec/http", + "@typespec/rest", + "@typespec/openapi", + "@typespec/versioning", +) + .using("Http", "Rest", "OpenAPI", "Versioning") + .emit("@azure-tools/typespec-autorest-canonical"); + +export async function openApiFor(code: string) { + const runner = await Tester.createInstance(); + const results = await runner.compile(code); + return JSON.parse(results.outputs["canonical/openapi.json"]); +} + +export async function diagnoseOpenApiFor(code: string) { + return Tester.diagnose(code); +} diff --git a/packages/typespec-autorest-canonical/test/versioning.test.ts b/packages/typespec-autorest-canonical/test/versioning.test.ts index a604f32e54..516526a717 100644 --- a/packages/typespec-autorest-canonical/test/versioning.test.ts +++ b/packages/typespec-autorest-canonical/test/versioning.test.ts @@ -2,7 +2,7 @@ import { expectDiagnostics } from "@typespec/compiler/testing"; import { deepStrictEqual, strictEqual } from "assert"; import { it } from "vitest"; import { canonicalVersion } from "../src/emitter.js"; -import { diagnoseOpenApiFor, openApiFor } from "./test-host.js"; +import { diagnoseOpenApiFor, openApiFor } from "./tester.js"; it("works with models", async () => { const v = await openApiFor( diff --git a/packages/typespec-autorest-canonical/tsconfig.json b/packages/typespec-autorest-canonical/tsconfig.json index c2be90093c..9261b75d40 100644 --- a/packages/typespec-autorest-canonical/tsconfig.json +++ b/packages/typespec-autorest-canonical/tsconfig.json @@ -11,7 +11,8 @@ "compilerOptions": { "outDir": "dist", "rootDir": ".", - "tsBuildInfoFile": "temp/tsconfig.tsbuildinfo" + "tsBuildInfoFile": "temp/tsconfig.tsbuildinfo", + "verbatimModuleSyntax": true }, "include": ["src/**/*.ts", "test/**/*.ts"] } From 8ae9093982eb2435cdcfb7f60ccc410c5829d52a Mon Sep 17 00:00:00 2001 From: Timothee Guerin Date: Mon, 29 Sep 2025 13:33:24 -0700 Subject: [PATCH 2/2] Changelog --- ...grate-autorest-canonical-tester-v2-2025-8-29-13-33-14.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .chronus/changes/migrate-autorest-canonical-tester-v2-2025-8-29-13-33-14.md diff --git a/.chronus/changes/migrate-autorest-canonical-tester-v2-2025-8-29-13-33-14.md b/.chronus/changes/migrate-autorest-canonical-tester-v2-2025-8-29-13-33-14.md new file mode 100644 index 0000000000..b1f6464de9 --- /dev/null +++ b/.chronus/changes/migrate-autorest-canonical-tester-v2-2025-8-29-13-33-14.md @@ -0,0 +1,6 @@ +--- +changeKind: internal +packages: + - "@azure-tools/typespec-autorest-canonical" +--- +