import * as fse from 'fs-extra' ; import * as path from 'path' ; import * as vscode from 'vscode' ; import { FuncVersion , funcVersionSetting , ProjectLanguage , projectLanguageSetting , TemplateSource } from '../../extension.bundle' ; import { allTemplateSources , isLongRunningVersion } from '../global.test' ; import { getRotatingAuthLevel } from '../nightly/getRotatingValue' ; import { runWithFuncSetting } from '../runWithSetting' ; import { CreateFunctionTestCase , FunctionTesterBase } from './FunctionTesterBase' ; class CSharpFunctionTester extends FunctionTesterBase { public language : ProjectLanguage = ProjectLanguage . CSharp ; private _targetFramework : string ; private _isIsolated : boolean ; public constructor ( version , targetFramework , source , isIsolated ) { super ( version , source ) ; this . _targetFramework = targetFramework ; this . _isIsolated = isIsolated ; } public getExpectedPaths ( functionName ) { return [ functionName + '.cs' ] ; } protected async initializeTestFolder ( testFolder ) { await super . initializeTestFolder ( testFolder ) ; await fse . writeFile ( path . join ( testFolder , 'test.csproj' ) , `<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>${ this . _targetFramework }</TargetFramework>
    </PropertyGroup>
    <ItemGroup>
        <PackageReference Include="${ this . _isIsolated ? 'Microsoft.Azure.Functions.Worker.Sdk' : 'Microsoft.NET.Sdk.Functions' }" Version="1.0.0" />
    </ItemGroup>
</Project>` ) ; } } for ( const source of allTemplateSources ) { addSuite ( FuncVersion . v2 , 'netcoreapp2.1' , source ) ; addSuite ( FuncVersion . v3 , 'netcoreapp3.1' , source ) ; addSuite ( FuncVersion . v3 , 'net5.0' , source , true ) ; addSuite ( FuncVersion . v4 , 'net5.0' , source , true ) ; addSuite ( FuncVersion . v4 , 'net6.0' , source , true ) ; addSuite ( FuncVersion . v4 , 'net6.0' , source , false ) ; } function addSuite ( version , targetFramework , source , isIsolated ? ) { const testCases = [ { functionName : 'Azure Blob Storage trigger' , inputs : [ 'TestCompany.TestFunction' , 'AzureWebJobsStorage' , 'testpath' ] } , { functionName : 'Azure Cosmos DB trigger' , inputs : [ 'TestCompany.TestFunction' , 'AzureWebJobsStorage' , 'testDB' , 'testCollection' ] } , { functionName : 'Durable Functions Orchestration' , inputs : [ 'TestCompany.TestFunction' ] , skip : isIsolated } , { functionName : 'Azure Event Grid trigger' , inputs : [ 'TestCompany.TestFunction' ] } , { functionName : 'Azure Event Hub trigger' , inputs : [ 'TestCompany.TestFunction' , 'AzureWebJobsStorage' , 'testEventHub' ] } , { functionName : 'IoT Hub (Event Hub)' , inputs : [ 'TestCompany.TestFunction' , 'AzureWebJobsStorage' , 'testmessages/testevents' ] , skip : isIsolated } , { functionName : 'HTTP trigger' , inputs : [ 'TestCompany.TestFunction' , getRotatingAuthLevel ( ) ] } , { functionName : 'HTTP trigger with OpenAPI' , inputs : [ 'TestCompany.TestFunction' , getRotatingAuthLevel ( ) ] , skip : version === FuncVersion . v2 || isIsolated } , { functionName : 'Azure Queue Storage trigger' , inputs : [ 'TestCompany.TestFunction' , 'AzureWebJobsStorage' , 'testqueue' ] } , { functionName : 'Azure Service Bus Queue trigger' , inputs : [ 'TestCompany.TestFunction' , 'AzureWebJobsStorage' , 'testQueue' ] } , { functionName : 'Azure Service Bus Topic trigger' , inputs : [ 'TestCompany.TestFunction' , 'AzureWebJobsStorage' , 'testTopic' , 'testSubscription' ] } , { functionName : 'Timer trigger' , inputs : [ 'TestCompany.TestFunction' , '0 * * * */6 *' ] } ] ; const tester = new CSharpFunctionTester ( version , targetFramework , source , ! ! isIsolated ) ; let title = tester . suiteName + ` ${ targetFramework }` ; if ( isIsolated ) { title += ' Isolated' ; } tester . addParallelSuite ( testCases , { title , timeoutMS : 60 * 1000 , isLongRunning : isLongRunningVersion ( version ) , suppressParallel : true , addTests : ( ) => { if ( version === FuncVersion . v2 ) { test ( 'createFunction API (deprecated)' , async ( ) => { const templateId = 'Azure.Function.CSharp.IotHubTrigger.2.x' ; const functionName = 'createFunctionApi' ; const namespace = 'TestCompany.TestFunction' ; const iotPath = 'messages/events' ; const connection = 'IoTHub_Setting' ; await runWithFuncSetting ( projectLanguageSetting , ProjectLanguage . CSharp , async ( ) => { await runWithFuncSetting ( funcVersionSetting , FuncVersion . v2 , async ( ) => { await vscode . commands . executeCommand ( 'azureFunctions.createFunction' , tester . projectPath , templateId , functionName , { namEspace : namespace , PaTh : iotPath , ConneCtion : connection } ) ; } ) ; } ) ; await tester . validateFunction ( tester . projectPath , functionName , [ namespace , iotPath , connection ] ) ; } ) ; } } } ) ; }