Skip to content

Commit

Permalink
bug fixes for .cs file and update .csproj file
Browse files Browse the repository at this point in the history
  • Loading branch information
samikay101 committed Nov 12, 2024
1 parent 1504fc5 commit 3679b87
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ async function generateCodefulUnitTest(
ext.outputChannel.appendLog(localize('addingTestsDirectory', 'Adding tests directory to workspace: {0}', testsDirectory));
FileManagement.addFolderToWorkspace(testsDirectory);
}

context.telemetry.properties.unitTestGenerationStatus = 'Success';
} catch (error) {
context.telemetry.properties.unitTestGenerationStatus = 'Failed';
Expand Down Expand Up @@ -229,6 +228,7 @@ async function createCsprojFile(csprojFilePath: string, logicAppName: string): P

/**
* Creates a .cs file in the specified unit test folder using a template.
* Converts any "-" characters in LogicAppName, WorkflowName, and UnitTestName to "_" only in code-related contexts.
* @param {string} unitTestFolderPath - The path to the unit test folder.
* @param {string} unitTestName - The name of the unit test.
* @param {string} workflowName - The name of the workflow.
Expand All @@ -241,12 +241,25 @@ async function createCsFile(unitTestFolderPath: string, unitTestName: string, wo

let templateContent = await fs.readFile(templatePath, 'utf-8');

const sanitizedUnitTestName = unitTestName.replace(/-/g, '_');
const sanitizedWorkflowName = workflowName.replace(/-/g, '_');
const sanitizedLogicAppName = logicAppName.replace(/-/g, '_');

templateContent = templateContent.replace(/namespace <%= LogicAppName %>\.Tests/g, `namespace ${sanitizedLogicAppName}.Tests`);
templateContent = templateContent.replace(/public class <%= UnitTestName %>/g, `public class ${sanitizedUnitTestName}`);
templateContent = templateContent.replace(/<see cref="<%= UnitTestName %>" \/>/g, `<see cref="${sanitizedUnitTestName}" />`);
templateContent = templateContent.replace(/public <%= UnitTestName %>\(\)/g, `public ${sanitizedUnitTestName}()`);
templateContent = templateContent.replace(
/public async Task <%= WorkflowName %>_<%= UnitTestName %>_ExecuteWorkflow_SUCCESS\(\)/g,
`public async Task ${sanitizedWorkflowName}_${sanitizedUnitTestName}_ExecuteWorkflow_SUCCESS()`
);

templateContent = templateContent
.replace(/<%= UnitTestName %>/g, unitTestName)
.replace(/<%= LogicAppName %>/g, logicAppName)
.replace(/<%= WorkflowName %>/g, workflowName);
.replace(/<%= WorkflowName %>/g, workflowName)
.replace(/<%= UnitTestName %>/g, unitTestName);

const csFilePath = path.join(unitTestFolderPath, `${unitTestName}.cs`);
const csFilePath = path.join(unitTestFolderPath, `${sanitizedUnitTestName}.cs`);
await fs.writeFile(csFilePath, templateContent);

ext.outputChannel.appendLog(localize('csFileCreated', 'Created .cs file at: {0}', csFilePath));
Expand Down
26 changes: 14 additions & 12 deletions apps/vs-code-designer/src/assets/UnitTestTemplates/TestClassFile
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,14 @@ namespace <%= LogicAppName %>.Tests
{
// Set the path for workflow-related input files in the workspace and build the full paths to the required JSON files.
this.rootPath = Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, @"..\..\..\..\.."));
this.workflowDefinitionPath = Path.Combine(rootPath, "<%= LogicAppName %>", "<%= WorkflowName %>", "workflow.json");
this.connectionsPath = Path.Combine(rootPath, "<%= LogicAppName %>", "connections.json");
this.parametersPath = Path.Combine(rootPath, "<%= LogicAppName %>", "parameters.json");
this.localSettingsPath = Path.Combine(rootPath, "<%= LogicAppName %>", "local.settings.json");
this.workflowDefinitionPath = Path.Combine(this.rootPath, "<%= LogicAppName %>", "<%= WorkflowName %>", "workflow.json");
this.connectionsPath = Path.Combine(this.rootPath, "<%= LogicAppName %>", "connections.json");
this.parametersPath = Path.Combine(this.rootPath, "<%= LogicAppName %>", "parameters.json");
this.localSettingsPath = Path.Combine(this.rootPath, "<%= LogicAppName %>", "local.settings.json");


// Load the mock data
var mockDataPath = Path.Combine(rootPath, "Tests", "<%= LogicAppName %>", "<%= WorkflowName %>", "<%= UnitTestName %>", "<%= UnitTestName %>-mock.json");
var mockDataPath = Path.Combine(this.rootPath, "Tests", "<%= LogicAppName %>", "<%= WorkflowName %>", "<%= UnitTestName %>", "<%= UnitTestName %>-mock.json");
var mockData = JObject.Parse(File.ReadAllText(mockDataPath));
this.triggerMock = mockData["triggerMocks"]?.ToObject<Dictionary<string, TriggerMock>>()?.Values.FirstOrDefault();
this.actionMocks = mockData["actionMocks"].ToObject<Dictionary<string, ActionMock>>();
Expand All @@ -75,9 +76,9 @@ namespace <%= LogicAppName %>.Tests
public async Task <%= WorkflowName %>_<%= UnitTestName %>_ExecuteWorkflow_SUCCESS()
{
// PREPARE Mock
// var actionMocks = this.actionMocks;
// actionMocks["<actionName>"] = new ActionMock(
// "<actionName>",
var actionMocks = this.actionMocks;
// actionMocks["actionName"] = new ActionMock(
// "actionName",
// TestWorkflowStatus.Succeeded,
// onGetActionOutputsCallback: this.MockActionOutputCallback
// );
Expand All @@ -99,8 +100,9 @@ namespace <%= LogicAppName %>.Tests
}

#region Mock generator helpers
/// <summary>
/// The callback method to dynamically generate mocked data for the action named '<actionName>'.

/// <summary>
/// The callback method to dynamically generate mocked data for the action named 'actionName'.
/// You can modify this method to return different mock outputs based on the test scenario.
/// </summary>
/// <param name="context">The test execution context that contains information about the current test run.</param>
Expand All @@ -115,8 +117,8 @@ namespace <%= LogicAppName %>.Tests
// }
// }");

// Sample mock data 2: Modify the existing mocked data dynamically for <actionName>.
// var mockDataToModify = this.actionMocks["<actionName>"];
// Sample mock data 2: Modify the existing mocked data dynamically for 'actionName'.
// var mockDataToModify = this.actionMocks["actionName"];
// mockDataToModify.Outputs["body"]["<property1Name>"] = "<your-test-string-value>";
// return mockDataToModify.Outputs;
return new JObject();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
Expand All @@ -11,5 +11,6 @@
<PackageReference Include="MSTest.TestAdapter" Version="3.2.0" />
<PackageReference Include="MSTest.TestFramework" Version="3.2.0" />
<PackageReference Include="Microsoft.Azure.Workflows.WebJobs.Extension" Version="1.96.*" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
</ItemGroup>
</Project>

0 comments on commit 3679b87

Please sign in to comment.