Skip to content

Commit

Permalink
Fixing long class name (#20739)
Browse files Browse the repository at this point in the history
Updating the discovery phase of python language flow and updated typescript version consumed in the task
  • Loading branch information
adityashahms authored Dec 18, 2024
1 parent 3224341 commit 7ae5e4e
Show file tree
Hide file tree
Showing 16 changed files with 1,683 additions and 1,441 deletions.
89 changes: 42 additions & 47 deletions Tasks/AzureTestPlanV0/Invokers/pythoninvoker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,25 @@ export async function executePythonTests(testsToBeExecuted: string[]):Promise<nu

// Extract discovered tests from stdout
const discoveredTests: string[] = extractDiscoveredTests(discoveryResult.stdout ?? '');
testsToBeExecuted = testsToBeExecuted.map(transformTestStrings);
var testStringtoFQNMap: Map<string, string> = new Map<string, string>();

for(let test of discoveredTests){
testStringtoFQNMap.set(transformTestStrings(test), test);
}

var testsToRun: string[] = [];

for(let test of testsToBeExecuted){
if(!testStringtoFQNMap.has(test)){
tl.debug(`Test ${test} not found in discovered tests`);
}
else{
testsToRun.push(testStringtoFQNMap.get(test));
}
}

// Find common tests between testsToBeExecuted and discovered tests
const testsToRun: string[] = testsToBeExecuted.filter(test => discoveredTests.indexOf(test) !== -1);
//const testsToRun: string[] = testsToBeExecuted.filter(test => discoveredTests.indexOf(test) !== -1);

// Variables for debug console logs
const testsToBeExecutedString: string = testsToBeExecuted.join(", ");
Expand Down Expand Up @@ -65,41 +80,9 @@ async function runPytestCommand(args: string[]): Promise<SpawnResult> {
}
}

function extractOldDiscoveredTests(output) {
const testNames = [];
let currentPackage = '';
let currentModule = '';
let currentClass = '';

const lines = output.split('\n');

for (let i = 0; i < lines.length; i++) {
const line = lines[i].trim();

if (line.startsWith('<Package')) {
currentPackage = line.match(/<Package (.*?)>/)[1];
} else if (line.startsWith('<Module')) {
currentModule = line.match(/<Module (.*?)>/)[1];
} else if (line.startsWith('<UnitTestCase')) {
currentClass = line.match(/<UnitTestCase (.*?)>/)[1];
} else if (line.startsWith('<TestCaseFunction')) {
const functionName = line.match(/<TestCaseFunction (.*?)>/)[1];
let fullyQualifiedName = '';
if (currentPackage !== '') {
fullyQualifiedName += currentPackage + '/';
}
fullyQualifiedName += `${currentModule}::${currentClass}::${functionName}`;
testNames.push(fullyQualifiedName);
}
}
tl.debug("Discovered tests : " + testNames);
return testNames;
}

function extractDiscoveredTests(output: string) {
const testNames = [];

const lines = output.split('\n');
function extractDiscoveredTests(output: string): string[] {
var testNames: string[] = [];
var lines: string[] = output.split('\n');

for (let i = 0; i < lines.length; i++) {
const line = lines[i].trim();
Expand All @@ -111,17 +94,29 @@ function extractDiscoveredTests(output: string) {
return testNames;
}

function transformTestStrings(test: string): string {
// Input is like Folder/SubFolder/TestClass.py::TestSubClass::TestSubSubClass::test_method_name
// Output is lke Folder.SubFolder.TestClass.TestSubClass.TestSubSubClass.test_method_name
function transformTestStrings(automatedTestName: string): string {
// Remove any leading or trailing whitespace
test = test.trim();
automatedTestName = automatedTestName.trim();
let updatedAutomatedTestName: string = automatedTestName;

const index = automatedTestName.indexOf("::");
if(index !== -1) {
let testFilePath = automatedTestName.substring(0, index);
let testMethod = automatedTestName.substring(index + 2);

//Check if testfilePath is a python file
if(testFilePath.endsWith(".py")) {
testFilePath = testFilePath.slice(0, -3).replace(/\//g, '.');

// Replace '.' with '/' for the directory structure
// Replace the last part of the string with '.py'
test = test.replace(/\./g, '/').replace(/\.([^/]+)$/, '.py');
//Do the same replace with :: to . in testMethod
testMethod = testMethod.replace(/::/g, '.');

//Finally generate updatedAutomatedTestName
updatedAutomatedTestName = testFilePath + "." + testMethod;
}
}

// Add the `::` before the test function name
const parts = test.split('/');
const functionName = parts.pop(); // Remove the function name
const testFile = parts.join('/'); // Join back the file path
return `${testFile}.py::${functionName}`; // Format as required
return updatedAutomatedTestName;
}
Loading

0 comments on commit 7ae5e4e

Please sign in to comment.