-
Notifications
You must be signed in to change notification settings - Fork 137
/
Copy pathaddBinding.test.ts
86 lines (74 loc) · 4.19 KB
/
addBinding.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { createTestActionContext, runWithTestActionContext } from '@microsoft/vscode-azext-dev';
import { AzExtFsExtra, type AzExtTreeItem } from '@microsoft/vscode-azext-utils';
import * as assert from 'assert';
import * as path from 'path';
import { Uri } from 'vscode';
import { ProjectLanguage, addBinding, createNewProjectInternal, ext, getRandomHexString, type IFunctionBinding, type IFunctionJson } from '../extension.bundle';
import { cleanTestWorkspace, getTestWorkspaceFolder } from './global.test';
suite('Add Binding', () => {
let functionJsonPath: string;
const functionName: string = 'HttpTriggerTest';
let initialBindingsCount: number;
suiteSetup(async () => {
await cleanTestWorkspace();
const testWorkspacePath = getTestWorkspaceFolder();
await runWithTestActionContext('createNewProject', async (context) => {
await context.ui.runWithInputs([testWorkspacePath, ProjectLanguage.JavaScript, 'Model V3', /http\s*trigger/i, functionName, 'Anonymous'], async () => {
await createNewProjectInternal(context, {});
});
})
functionJsonPath = path.join(testWorkspacePath, functionName, 'function.json');
assert.ok(await AzExtFsExtra.pathExists(functionJsonPath), 'Failed to create project');
initialBindingsCount = await getBindingsCount();
});
suiteTeardown(async () => {
const finalBindingsCount: number = await getBindingsCount();
assert.equal(finalBindingsCount, initialBindingsCount + /* 3
https://github.com/microsoft/vscode-azurefunctions/issues/3266 */
1, 'Not all expected bindings were added.');
});
test('Command Palette', async function (this: Mocha.Context): Promise<void> {
// https://github.com/microsoft/vscode-azurefunctions/issues/3266
this.skip();
this.timeout(30 * 1000);
const userInputs: string[] = [functionName];
userInputs.unshift('Local Project');
await validateAddBinding(undefined, userInputs);
});
test('Uri', async () => {
await validateAddBinding(Uri.parse(functionJsonPath), []);
});
test('Tree', async function (this: Mocha.Context): Promise<void> {
// https://github.com/microsoft/vscode-azurefunctions/issues/3266
this.skip();
const treeItem: AzExtTreeItem | undefined = await ext.rgApi.workspaceResourceTree.findTreeItem(`/localProject0/functions/${functionName}`, await createTestActionContext());
assert.ok(treeItem, 'Failed to find tree item');
await validateAddBinding(treeItem, []);
});
async function getBindingsCount(): Promise<number> {
const data: IFunctionJson = await AzExtFsExtra.readJSON<IFunctionJson>(functionJsonPath);
return (data.bindings || []).length;
}
async function validateAddBinding(commandInput: any, userInputs: string[]): Promise<void> {
const bindingType: string = 'HTTP';
const bindingDirection: string = 'out';
const bindingName: string = 'binding' + getRandomHexString();
await runWithTestActionContext('addBinding', async (context) => {
await context.ui.runWithInputs([...userInputs, bindingDirection, bindingType, bindingName], async () => {
await addBinding(context, commandInput as Uri | undefined);
});
});
const data: IFunctionJson = await AzExtFsExtra.readJSON<IFunctionJson>(functionJsonPath);
const binding: IFunctionBinding | undefined = data.bindings && data.bindings.find(b => b.name === bindingName);
if (!binding) {
assert.fail(`Failed to find binding "${bindingName}".`);
} else {
assert.equal(binding.direction, bindingDirection);
assert.equal(binding.type, bindingType.toLowerCase()); // For no particular reason, the type is lowercase in function.json
}
}
});