in apps/vs-code-designer/src/app/commands/workflows/unitTest/saveBlankUnitTest.ts [40:188]
export async function saveBlankUnitTest(context: IActionContext, node: vscode.Uri | undefined, unitTestDefinition: any): Promise<void> {
const startTime = Date.now();
// Initialize telemetry properties
logTelemetry(context, {
workspaceLocated: 'false',
projectRootLocated: 'false',
workflowNodeSelected: 'false',
multiRootWorkspaceValid: 'false',
unitTestNamePrompted: 'false',
directoriesEnsured: 'false',
csFileCreated: 'false',
csprojUpdated: 'false',
workspaceUpdated: 'false',
unitTestDefinitionParsed: 'false',
operationInfoExists: 'false',
outputParametersExists: 'false',
workflowNodePath: '',
workflowTestFolderPathResolved: 'false',
mockOutputsFolderPathCreated: 'false',
mockableOperationsFound: '0',
mockableOperationsProcessed: '0',
mockableTriggersProcessed: '0',
csFilesGenerated: '0',
csFileGenerationFailures: '0',
workspaceUpdatedStatus: 'false',
workspaceUpdateFailureReason: '',
unitTestSaveStatus: 'InProgress',
unitTestSaveFailureReason: '',
});
try {
if (!(await convertToWorkspace(context))) {
logTelemetry(context, {
multiRootWorkspaceValid: 'false',
});
ext.outputChannel.appendLog(
localize('createBlankUnitTestCancelled', 'Exiting blank unit test creation, a workspace is required to create blank unit tests.')
);
return;
}
logTelemetry(context, {
multiRootWorkspaceValid: 'true',
workspaceLocated: 'true',
projectRootLocated: 'true',
});
// Get parsed outputs
const parsedOutputs = await parseUnitTestOutputs(unitTestDefinition);
const operationInfo = parsedOutputs['operationInfo'];
const outputParameters = parsedOutputs['outputParameters'];
logTelemetry(context, {
operationInfoExists: operationInfo ? 'true' : 'false',
outputParametersExists: outputParameters ? 'true' : 'false',
});
// Determine workflow node
let workflowNode = getWorkflowNode(node) as vscode.Uri;
let projectPath: string | undefined;
if (workflowNode) {
const workspaceFolder = getWorkspacePath(workflowNode.fsPath);
projectPath = await tryGetLogicAppProjectRoot(context, workspaceFolder);
} else {
const workspaceFolder = await getWorkspaceFolder(context);
projectPath = await tryGetLogicAppProjectRoot(context, workspaceFolder);
workflowNode = await selectWorkflowNode(context, projectPath);
}
logTelemetry(context, {
workflowNodeSelected: 'true',
workflowNodePath: workflowNode ? workflowNode.fsPath : '',
});
try {
validateWorkflowPath(projectPath, workflowNode.fsPath);
} catch (error) {
vscode.window.showErrorMessage(`Workflow validation failed: ${error.message}`);
return;
}
const workflowName = path.basename(path.dirname(workflowNode.fsPath));
// Prompt for unit test name
const unitTestName = await promptForUnitTestName(context, projectPath, workflowName);
logTelemetry(context, {
unitTestNamePrompted: 'true',
});
ext.outputChannel.appendLog(localize('unitTestNameEntered', `Unit test name entered: ${unitTestName}`));
const { unitTestFolderPath, logicAppName, workflowTestFolderPath, logicAppTestFolderPath, testsDirectory } = getUnitTestPaths(
projectPath,
workflowName,
unitTestName
);
logTelemetry(context, {
workflowTestFolderPathResolved: workflowTestFolderPath ? 'true' : 'false',
});
const { mockClassContent, foundActionMocks, foundTriggerMocks } = await getOperationMockClassContent(
operationInfo,
outputParameters,
workflowNode.fsPath,
workflowName,
logicAppName
);
if (!foundTriggerMocks || Object.keys(foundTriggerMocks).length === 0) {
throw new Error(localize('noTriggersFound', 'No trigger found in the workflow. Unit tests must include a mocked trigger.'));
}
// Log telemetry before proceeding
logTelemetry(context, { workflowName, unitTestName });
// Save the unit test
await callWithTelemetryAndErrorHandling('logicApp.saveBlankUnitTest', async (telemetryContext: IActionContext) => {
Object.assign(telemetryContext, context);
await generateBlankCodefulUnitTest(
context,
projectPath,
workflowName,
unitTestName,
mockClassContent,
foundActionMocks,
foundTriggerMocks
);
});
logTelemetry(context, {
unitTestSaveStatus: 'Success',
unitTestProcessingTimeMs: (Date.now() - startTime).toString(),
});
try {
const csprojFilePath = path.join(logicAppTestFolderPath, `${logicAppName}.csproj`);
ext.outputChannel.appendLog(`Updating solution in tests folder: ${unitTestFolderPath}`);
await updateTestsSln(testsDirectory, csprojFilePath);
} catch (solutionError) {
ext.outputChannel.appendLog(`Failed to update solution: ${solutionError}`);
}
} catch (error) {
// Handle errors using the helper function
logTelemetry(context, {
unitTestGenerationStatus: 'Failed',
errorMessage: parseError(error).message,
});
handleError(context, error, 'saveBlankUnitTest');
}
}