Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing long class name #20739

Merged
merged 5 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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