Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions src/commands/createNewProject/dotnetSteps/DotnetRuntimeStep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,17 @@ export class DotnetRuntimeStep extends AzureWizardPromptStep<IProjectWizardConte
public static async createStep(context: IProjectWizardContext): Promise<DotnetRuntimeStep> {
if (context.targetFramework) {
context.targetFramework = typeof context.targetFramework === 'string' ? [context.targetFramework] : context.targetFramework;
const runtimes = await getRuntimes(context);
const runtimes = (await getRuntimes(context));
// if a targetFramework was provided from createNewProject
const workerRuntime = runtimes.find(runtime => context.targetFramework?.includes(runtime.targetFramework));
const filteredRuntimes = runtimes.filter(runtime => context.targetFramework?.includes(runtime.targetFramework));
let workerRuntime: cliFeedUtils.IWorkerRuntime | undefined = undefined;
if (filteredRuntimes.length > 1) {
const placeHolder: string = localize('selectWorkerRuntime', 'Select a .NET runtime');
workerRuntime = (await context.ui.showQuickPick(new DotnetRuntimeStep().getPicks(context, filteredRuntimes), { placeHolder })).data;
} else if (filteredRuntimes.length === 1) {
workerRuntime = filteredRuntimes[0];
}

if (!workerRuntime) {
throw new Error(localize('unknownFramework', 'Unrecognized target frameworks: "{0}". Available frameworks: {1}.',
context.targetFramework.map(tf => `"${tf}"`).join(', '),
Expand Down Expand Up @@ -49,8 +57,11 @@ export class DotnetRuntimeStep extends AzureWizardPromptStep<IProjectWizardConte
return !context.workerRuntime;
}

private async getPicks(context: IProjectWizardContext): Promise<IAzureQuickPickItem<cliFeedUtils.IWorkerRuntime | undefined>[]> {
const runtimes = await getRuntimes(context);
private async getPicks(context: IProjectWizardContext, runtimes?: cliFeedUtils.IWorkerRuntime[]): Promise<IAzureQuickPickItem<cliFeedUtils.IWorkerRuntime | undefined>[]> {
if (!runtimes) {
runtimes = await getRuntimes(context);
}

const picks: IAzureQuickPickItem<cliFeedUtils.IWorkerRuntime | undefined>[] = [];
for (const runtime of runtimes) {
picks.push({
Expand Down
49 changes: 47 additions & 2 deletions test/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import { runWithInputs } from '@microsoft/vscode-azext-dev';
import { type apiUtils } from "@microsoft/vscode-azext-utils";
import * as path from 'path';
import { extensions, type Extension } from "vscode";
import { ProjectLanguage, extensionId, nonNullValue, registerOnActionStartHandler } from '../extension.bundle';
import { FuncVersion, ProjectLanguage, extensionId, nonNullValue, registerOnActionStartHandler } from '../extension.bundle';
// eslint-disable-next-line no-restricted-imports
import { type AzureFunctionsExtensionApi } from '../src/vscode-azurefunctions.api';
import { getTestWorkspaceFolder, testFolderPath } from './global.test';
import { getJavaScriptValidateOptions, validateProject, type IValidateProjectOptions } from './project/validateProject';
import { getCSharpValidateOptions, getJavaScriptValidateOptions, validateProject, type IValidateProjectOptions } from './project/validateProject';

suite(`AzureFunctionsExtensionApi`, () => {
let api: AzureFunctionsExtensionApi;
Expand Down Expand Up @@ -73,4 +73,49 @@ suite(`AzureFunctionsExtensionApi`, () => {
);
await validateProject(folderPath, validateOptions);
});

test('createFunction dotnet with targetFramework', async () => {
const functionName: string = 'endpoint1';
const language: string = ProjectLanguage.CSharp;
const workspaceFolder = getTestWorkspaceFolder();
const projectSubpath = 'api';
const folderPath: string = path.join(workspaceFolder, projectSubpath);

await runWithInputs('api.createFunction', [language, /6/i, 'Company.Function', 'Anonymous'], registerOnActionStartHandler, async () => {
await api.createFunction({
folderPath,
functionName,
templateId: 'HttpTrigger',
languageFilter: /^C\#$/i,
functionSettings: { authLevel: 'anonymous' },
targetFramework: ['net8.0', 'net7.0', 'net6.0']
Comment thread
alexweininger marked this conversation as resolved.
});
});

const validateOptions: IValidateProjectOptions = getCSharpValidateOptions('net6.0', FuncVersion.v4, 1, projectSubpath, workspaceFolder);
// Exclude .git because the test workspace folders are already inside a git repo so we don't do git init.
validateOptions.excludedPaths?.push('.git');
await validateProject(folderPath, validateOptions);
});

// Intentionally pass a version (8) that hasn't been specified in targetFramework (6 & 7) to verify it isn't a possible pick. In the correct case (when 8 isn't a pick) we throw an error. api.createFunction swallows the error and returns undefined.
// In the incorrect case (when 8 is a pick) the test fails since the 2 provided test inputs have already been used, but there are more prompts.
test('createFunction with language not in targetFramework', async () => {
const functionName: string = 'endpoint1';
const language: string = ProjectLanguage.CSharp;
const workspaceFolder = getTestWorkspaceFolder();
const projectSubpath = 'api';
const folderPath: string = path.join(workspaceFolder, projectSubpath);

await runWithInputs('api.createFunction', [language, /8/i], registerOnActionStartHandler, async () => {
await api.createFunction({
folderPath,
functionName,
templateId: 'HttpTrigger',
languageFilter: /^C\#$/i,
functionSettings: { authLevel: 'anonymous' },
targetFramework: ['net7.0', 'net6.0']
})
});
});
});
11 changes: 6 additions & 5 deletions test/project/validateProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,25 +81,25 @@ export function getTypeScriptValidateOptions(options?: { version?: FuncVersion,
return result;
}

export function getCSharpValidateOptions(targetFramework: string, version: FuncVersion = defaultTestFuncVersion, numCsproj: number = 1): IValidateProjectOptions {
export function getCSharpValidateOptions(targetFramework: string, version: FuncVersion = defaultTestFuncVersion, numCsproj: number = 1, projectSubpath?: string, workspaceFolder?: string): IValidateProjectOptions {
return {
language: ProjectLanguage.CSharp,
version,
expectedSettings: {
'azureFunctions.projectLanguage': ProjectLanguage.CSharp,
'azureFunctions.projectRuntime': version,
'azureFunctions.preDeployTask': 'publish (functions)',
'azureFunctions.deploySubpath': `bin/Release/${targetFramework}/publish`,
'azureFunctions.deploySubpath': path.join(projectSubpath ?? '', `bin/Release/${targetFramework}/publish`),
'debug.internalConsoleOptions': 'neverOpen',
},
expectedPaths: [
{ globPattern: '*.csproj', numMatches: numCsproj }
{ globPattern: path.join(projectSubpath ?? '', '*.csproj'), numMatches: numCsproj }
],
expectedExtensionRecs: [
'ms-dotnettools.csharp'
],
excludedPaths: [
'.funcignore'
path.join(projectSubpath ?? '', '.funcignore')
],
expectedDebugConfigs: [
'Attach to .NET Functions'
Expand All @@ -110,7 +110,8 @@ export function getCSharpValidateOptions(targetFramework: string, version: FuncV
'clean release (functions)',
'publish (functions)',
'host start'
]
],
workspaceFolder
};
}

Expand Down