Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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))
Comment thread
alexweininger marked this conversation as resolved.
Outdated
// 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
45 changes: 45 additions & 0 deletions test/MockIProjectWizardContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* eslint-disable no-restricted-imports */
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need this still?

/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.md in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { type TestUserInput } from "@microsoft/vscode-azext-dev";
import { type Uri, type WorkspaceFolder } from "vscode";
import { type FuncVersion, type TemplateSchemaVersion } from "../extension.bundle";
import { type OpenBehavior } from "../src/commands/createNewProject/IProjectWizardContext";
import { type ProjectLanguage } from "../src/constants";
import { type cliFeedUtils } from "../src/utils/cliFeedUtils";

export interface MockIProjectWizardContext {
projectPath: string;
workspacePath: string;
workspaceFolder: WorkspaceFolder | undefined;

language?: ProjectLanguage;
languageModel?: number;
languageFilter?: RegExp;

version: FuncVersion;
templateSchemaVersion?: TemplateSchemaVersion;
projectTemplateKey: string | undefined;
workerRuntime?: cliFeedUtils.IWorkerRuntime;
openBehavior?: OpenBehavior;

generateFromOpenAPI?: boolean;
openApiSpecificationFile?: Uri[];

targetFramework?: string | string[];

containerizedProject?: boolean;

telemetry: {
properties: { [key: string]: string | undefined; }
measurements: { [key: string]: number | undefined; }
};
errorHandling: {
issueProperties: {}
};
valuesToMask: string[];
ui: TestUserInput;
}
46 changes: 44 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,46 @@ 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);
await validateProject(folderPath, validateOptions);
});

// When passing in a language version that is not in the target framework the last two inputs should not be prompted if they are this test will fail
Copy link
Copy Markdown
Member

@alexweininger alexweininger Jun 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is ensuring that target framework property is properly used to filter the picks from all available framework versions.

From this comment, it's not clear to me:

  1. Why api.createFunction doesn't throw here, since selecting version 8 shouldn't be possible
  2. What last two inputs do you mean, do you mean the next steps in the createFunction wizard?
  3. How do you know this test will fail if 8 is a valid pick?

I think a better comment would start like:

// 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)... And in the incorrect case (when 8 is available)...

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']
})
});
});
});