1+ import { AzureCoreTestLibrary } from "@azure-tools/typespec-azure-core/testing" ;
2+ import {
3+ createSdkContext ,
4+ CreateSdkContextOptions ,
5+ } from "@azure-tools/typespec-client-generator-core" ;
6+ import { SdkTestLibrary } from "@azure-tools/typespec-client-generator-core/testing" ;
7+ import {
8+ CompilerOptions ,
9+ EmitContext ,
10+ Program
11+ } from "@typespec/compiler" ;
12+ import { createTestHost , TestHost } from "@typespec/compiler/testing" ;
13+ import { CSharpEmitterContext , CSharpEmitterOptions , Logger , LoggerLevel } from "@typespec/http-client-csharp" ;
14+ import { HttpTestLibrary } from "@typespec/http/testing" ;
15+ import { RestTestLibrary } from "@typespec/rest/testing" ;
16+ import { VersioningTestLibrary } from "@typespec/versioning/testing" ;
17+ import { XmlTestLibrary } from "@typespec/xml/testing" ;
18+
19+ export async function createEmitterTestHost ( ) : Promise < TestHost > {
20+ return createTestHost ( {
21+ libraries : [
22+ RestTestLibrary ,
23+ HttpTestLibrary ,
24+ VersioningTestLibrary ,
25+ AzureCoreTestLibrary ,
26+ SdkTestLibrary ,
27+ XmlTestLibrary ,
28+ ] ,
29+ } ) ;
30+ }
31+
32+ export interface TypeSpecCompileOptions {
33+ IsNamespaceNeeded ?: boolean ;
34+ IsAzureCoreNeeded ?: boolean ;
35+ IsTCGCNeeded ?: boolean ;
36+ IsXmlNeeded ?: boolean ;
37+ AuthDecorator ?: string ;
38+ }
39+
40+ export async function typeSpecCompile (
41+ content : string ,
42+ host : TestHost ,
43+ options ?: TypeSpecCompileOptions ,
44+ ) {
45+ const needNamespaces = options ?. IsNamespaceNeeded ?? true ;
46+ const needAzureCore = options ?. IsAzureCoreNeeded ?? false ;
47+ const needTCGC = options ?. IsTCGCNeeded ?? false ;
48+ const needXml = options ?. IsXmlNeeded ?? false ;
49+ const authDecorator =
50+ options ?. AuthDecorator ?? `@useAuth(ApiKeyAuth<ApiKeyLocation.header, "api-key">)` ;
51+ const namespace = `
52+ @versioned(Versions)
53+ ${ authDecorator }
54+ @service({
55+ title: "Azure Csharp emitter Testing",
56+ })
57+
58+ namespace Azure.Csharp.Testing;
59+
60+ enum Versions {
61+ ${ needAzureCore ? "@useDependency(Azure.Core.Versions.v1_0_Preview_1)" : "" }
62+ "2023-01-01-preview"
63+ }
64+
65+ ` ;
66+ const fileContent = `
67+ import "@typespec/rest";
68+ import "@typespec/http";
69+ import "@typespec/versioning";
70+ ${ needXml ? 'import "@typespec/xml";' : "" }
71+ ${ needAzureCore ? 'import "@azure-tools/typespec-azure-core";' : "" }
72+ ${ needTCGC ? 'import "@azure-tools/typespec-client-generator-core";' : "" }
73+ using TypeSpec.Rest;
74+ using TypeSpec.Http;
75+ using TypeSpec.Versioning;
76+ ${ needXml ? "using TypeSpec.Xml;" : "" }
77+ ${ needAzureCore ? "using Azure.Core;\nusing Azure.Core.Traits;" : "" }
78+ ${ needTCGC ? "using Azure.ClientGenerator.Core;" : "" }
79+
80+ ${ needNamespaces ? namespace : "" }
81+ ${ content }
82+ ` ;
83+ host . addTypeSpecFile ( "main.tsp" , fileContent ) ;
84+ const cliOptions = {
85+ warningAsError : false ,
86+ } as CompilerOptions ;
87+ await host . compile ( "./" , cliOptions ) ;
88+ return host . program ;
89+ }
90+
91+ export function createEmitterContext ( program : Program ) : EmitContext < CSharpEmitterOptions > {
92+ return {
93+ program : program ,
94+ emitterOutputDir : "./" ,
95+ options : {
96+ outputFile : "tspCodeModel.json" ,
97+ logFile : "log.json" ,
98+ "new-project" : false ,
99+ "clear-output-folder" : false ,
100+ "save-inputs" : false ,
101+ "generate-protocol-methods" : true ,
102+ "generate-convenience-methods" : true ,
103+ "package-name" : undefined ,
104+ } ,
105+ } as EmitContext < CSharpEmitterOptions > ;
106+ }
107+
108+ /* We always need to pass in the emitter name now that it is required so making a helper to do this. */
109+ export async function createCSharpSdkContext (
110+ program : EmitContext < CSharpEmitterOptions > ,
111+ sdkContextOptions : CreateSdkContextOptions = { } ,
112+ ) : Promise < CSharpEmitterContext > {
113+ const context = await createSdkContext (
114+ program ,
115+ "@typespec/http-client-csharp" ,
116+ sdkContextOptions ,
117+ ) ;
118+ return {
119+ ...context ,
120+ logger : new Logger ( program . program , LoggerLevel . INFO ) ,
121+ __typeCache : {
122+ types : new Map ( ) ,
123+ models : new Map ( ) ,
124+ enums : new Map ( ) ,
125+ } ,
126+ } ;
127+ }
0 commit comments