Skip to content
Draft
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
208 changes: 131 additions & 77 deletions packages/adt-aunit/src/commands/aunit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,55 @@ function buildRunConfiguration(targetUris: string[]): RunConfigurationBody {
};
}

/**
* Convert a single test method alert to an AunitAlert, updating the status.
*/
function convertAlerts(
rawAlerts: Array<{
kind?: string;
severity?: string;
title?: string;
details?: { detail?: Array<{ text?: string }> };
stack?: {
stackEntry?: Array<{
uri?: string;
type?: string;
name?: string;
description?: string;
}>;
};
}>,
): { alerts: AunitAlert[]; status: AunitTestMethod['status'] } {
const alerts: AunitAlert[] = [];
let status: AunitTestMethod['status'] = 'pass';

for (const alert of rawAlerts) {
const details = (alert.details?.detail ?? []).map((d) => d.text || '');
const stack = (alert.stack?.stackEntry ?? []).map((s) => ({
uri: s.uri,
type: s.type,
name: s.name,
description: s.description,
}));

alerts.push({
kind: alert.kind || 'unknown',
severity: alert.severity || 'unknown',
title: alert.title || '',
details,
stack,
});

if (alert.severity === 'critical' || alert.kind === 'failedAssertion') {
status = 'fail';
} else if (alert.severity === 'fatal' || alert.kind === 'error') {
status = 'error';
}
}

return { alerts, status };
}

/**
* Convert SAP AUnit response to our normalized AunitResult
*/
Expand All @@ -209,41 +258,10 @@ function convertResponse(response: RunResultResponse): AunitResult {
const methods: AunitTestMethod[] = [];

for (const tm of tc.testMethods?.testMethod || []) {
const execTime = parseFloat(tm.executionTime || '0');
const execTime = Number.parseFloat(tm.executionTime || '0');
totalTime += execTime;

const alerts: AunitAlert[] = [];
let status: AunitTestMethod['status'] = 'pass';

for (const alert of tm.alerts?.alert || []) {
const details = (alert.details?.detail || []).map(
(d) => d.text || '',
);
const stack = (alert.stack?.stackEntry || []).map((s) => ({
uri: s.uri,
type: s.type,
name: s.name,
description: s.description,
}));

alerts.push({
kind: alert.kind || 'unknown',
severity: alert.severity || 'unknown',
title: alert.title || '',
details,
stack,
});

// Determine status from alert severity/kind
if (
alert.severity === 'critical' ||
alert.kind === 'failedAssertion'
) {
status = 'fail';
} else if (alert.severity === 'fatal' || alert.kind === 'error') {
status = 'error';
}
}
const { alerts, status } = convertAlerts(tm.alerts?.alert || []);

totalTests++;
if (status === 'pass') passCount++;
Expand Down Expand Up @@ -311,17 +329,27 @@ function adtLink(name: string, uri: string, systemName?: string): string {
}

/**
* Display AUnit results in console
* Display a single failed/errored test method in console
*/
function displayResults(result: AunitResult, systemName?: string): void {
if (result.totalTests === 0) {
console.log(`\n⚠️ No tests found`);
return;
function displayFailedMethod(method: AunitTestMethod): void {
const icon = method.status === 'fail' ? ansi.red('✗') : ansi.red('⚠');
console.log(` ${icon} ${method.name} (${method.executionTime}s)`);
for (const alert of method.alerts) {
console.log(` ${ansi.dim(alert.title)}`);
for (const detail of alert.details) {
const dimDetail = ansi.dim(` ${detail}`);
console.log(` ${dimDetail}`);
}
}
}

/**
* Display AUnit results summary in console
*/
function displaySummary(result: AunitResult): void {
const allPassed = result.failCount === 0 && result.errorCount === 0;

console.log(`\n${allPassed ? '✅' : '❌'} ABAP Unit Test Results:`);
const statusIcon = allPassed ? '✅' : '❌';
console.log(`\n${statusIcon} ABAP Unit Test Results:`);
console.log(
` 📋 Total: ${result.totalTests} tests in ${result.totalTime.toFixed(3)}s`,
);
Expand All @@ -333,6 +361,18 @@ function displayResults(result: AunitResult, systemName?: string): void {
console.log(` ${ansi.red(`⚠ ${result.errorCount} errors`)}`);
if (result.skipCount > 0)
console.log(` ${ansi.yellow(`○ ${result.skipCount} skipped`)}`);
}

/**
* Display AUnit results in console
*/
function displayResults(result: AunitResult, systemName?: string): void {
if (result.totalTests === 0) {
console.log(`\n⚠️ No tests found`);
return;
}

displaySummary(result);

// Show failed tests
for (const prog of result.programs) {
Expand All @@ -350,19 +390,57 @@ function displayResults(result: AunitResult, systemName?: string): void {
console.log(`\n ${classLink}`);

for (const method of failedMethods) {
const icon = method.status === 'fail' ? ansi.red('✗') : ansi.red('⚠');
console.log(` ${icon} ${method.name} (${method.executionTime}s)`);
for (const alert of method.alerts) {
console.log(` ${ansi.dim(alert.title)}`);
for (const detail of alert.details) {
console.log(` ${ansi.dim(` ${detail}`)}`);
}
}
displayFailedMethod(method);
}
}
}
}

/**
* Resolve target URIs and name from command options
*/
async function resolveTargets(options: {
fromFile?: string;
transport?: string;
class?: string;
package?: string;
object?: string;
}): Promise<{ targetUris: string[]; targetName: string }> {
if (options.fromFile) {
const { readFileSync } = await import('node:fs');
const content = readFileSync(options.fromFile, 'utf-8');
const targetUris = content
.split('\n')
.map((line) => line.trim())
.filter((line) => line && !line.startsWith('#'));
return {
targetUris,
targetName: `${targetUris.length} objects from ${options.fromFile}`,
};
}
if (options.transport) {
return {
targetUris: [
`/sap/bc/adt/cts/transportrequests/${options.transport.toUpperCase()}`,
],
targetName: `Transport ${options.transport.toUpperCase()}`,
};
}
if (options.class) {
return {
targetUris: [`/sap/bc/adt/oo/classes/${options.class.toLowerCase()}`],
targetName: `Class ${options.class.toUpperCase()}`,
};
}
if (options.package) {
return {
targetUris: [`/sap/bc/adt/packages/${options.package.toUpperCase()}`],
targetName: `Package ${options.package.toUpperCase()}`,
};
}
return { targetUris: [options.object!], targetName: options.object! };
}

/**
* AUnit Command Plugin
*/
Expand Down Expand Up @@ -453,35 +531,11 @@ export const aunitCommand: CliCommandPlugin = {
const client = (await ctx.getAdtClient()) as AdtClient;

// Determine targets
let targetUris: string[];
let targetName: string;

if (options.fromFile) {
const { readFileSync } = await import('fs');
const content = readFileSync(options.fromFile, 'utf-8');
targetUris = content
.split('\n')
.map((line) => line.trim())
.filter((line) => line && !line.startsWith('#'));
if (targetUris.length === 0) {
ctx.logger.error(`❌ No objects found in ${options.fromFile}`);
process.exit(1);
}
targetName = `${targetUris.length} objects from ${options.fromFile}`;
} else if (options.transport) {
targetUris = [
`/sap/bc/adt/cts/transportrequests/${options.transport.toUpperCase()}`,
];
targetName = `Transport ${options.transport.toUpperCase()}`;
} else if (options.class) {
targetUris = [`/sap/bc/adt/oo/classes/${options.class.toLowerCase()}`];
targetName = `Class ${options.class.toUpperCase()}`;
} else if (options.package) {
targetUris = [`/sap/bc/adt/packages/${options.package.toUpperCase()}`];
targetName = `Package ${options.package.toUpperCase()}`;
} else {
targetUris = [options.object!];
targetName = options.object!;
const { targetUris, targetName } = await resolveTargets(options);

if (options.fromFile && targetUris.length === 0) {
ctx.logger.error(`❌ No objects found in ${options.fromFile}`);
process.exit(1);
}

ctx.logger.info(`🧪 Running ABAP Unit tests on ${targetName}...`);
Expand Down
Loading