Skip to content

Commit

Permalink
Updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nbhoski committed Aug 13, 2024
1 parent 69ceb79 commit 079283e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 66 deletions.
50 changes: 5 additions & 45 deletions src/buildSummary.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2024 The MathWorks, Inc.
import * as core from "@actions/core";
import { join } from 'path';
import { readFileSync, unlinkSync, existsSync} from 'fs';
import { readFileSync, unlinkSync, existsSync } from 'fs';

export interface Task {
name: string;
Expand All @@ -11,23 +11,6 @@ export interface Task {
duration: string;
}


export function getBuildSummaryTable(tasks: Task[]): string[][] {
const header = [{data:'MATLAB Build Task', header: true}, {data: 'Status', header: true}, {data:'Description', header:true}, {data: 'Duration (hh:mm:ss)', header: true }];
let taskSummaryTableRows: string[][] = [];


if(!Array.isArray(tasks)){
taskSummaryTableRows = getTaskSummaryRows(tasks, taskSummaryTableRows);
} else {
tasks.forEach((task, index) => {
taskSummaryTableRows = getTaskSummaryRows(task, taskSummaryTableRows);
});
}

return taskSummaryTableRows;
}

export function writeSummary(taskSummaryTableRows: string[][]) {
try {
core.summary
Expand All @@ -41,14 +24,14 @@ export function writeSummary(taskSummaryTableRows: string[][]) {
export function processAndDisplayBuildSummary() {
const runId = process.env.GITHUB_RUN_ID || '';
const runnerTemp = process.env.RUNNER_TEMP || '';
const header = [{data:'MATLAB Build Task', header: true}, {data: 'Status', header: true}, {data:'Description', header:true}, {data: 'Duration (hh:mm:ss)', header: true }];
const header = [{ data: 'MATLAB Build Task', header: true }, { data: 'Status', header: true }, { data: 'Description', header: true }, { data: 'Duration (hh:mm:ss)', header: true }];

const filePath: string = join(runnerTemp, `buildSummary${runId}.json`);
let taskSummaryTableRows;
if (existsSync(filePath)) {
try {
const bs = readFileSync(filePath, { encoding: 'utf8' });
const data = JSON.parse(bs).map((t: { name: any; failed: { toString: () => any; }; skipped: { toString: () => any; }; description: any; duration: { toString: () => any; }; }) => {
const buildSummary = readFileSync(filePath, { encoding: 'utf8' });
const data = JSON.parse(buildSummary).map((t: { name: any; failed: { toString: () => any; }; skipped: { toString: () => any; }; description: any; duration: { toString: () => any; }; }) => {
if (t.failed.toString() === 'true') {
return [t.name, '🔴 Failed', t.description, t.duration.toString()];
} else if (t.skipped.toString() === 'true') {
Expand All @@ -57,7 +40,7 @@ export function processAndDisplayBuildSummary() {
return [t.name, '🟢 Success', t.description, t.duration.toString()];
}
});
taskSummaryTableRows = [header, ...data];//getBuildSummaryTable(data);
taskSummaryTableRows = [header, ...data];
} catch (e) {
console.error('An error occurred while reading the build summary file:', e);
return;
Expand All @@ -72,27 +55,4 @@ export function processAndDisplayBuildSummary() {
} else {
core.info(`Build summary data not created.`);
}

}

export function getTaskDetails(tasks: Task): string[] {
let taskDetails: string[] = [];
taskDetails.push(tasks.name);
if (tasks.failed) {
taskDetails.push('🔴 Failed');
} else if (tasks.skipped) {
taskDetails.push('🔵 Skipped');
} else {
taskDetails.push('🟢 Success');
}
taskDetails.push(tasks.description);
taskDetails.push(tasks.duration);
return taskDetails;
}

export function getTaskSummaryRows(task: Task, taskSummaryTableRows: string[][]): string[][] {
let taskDetails: string[] = [];
taskDetails = getTaskDetails(task);
taskSummaryTableRows.push(taskDetails);
return taskSummaryTableRows;
}
24 changes: 3 additions & 21 deletions src/buildSummary.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,15 @@ jest.mock('@actions/core', () => ({
}));

describe('summaryGeneration', () => {
it('generates a summary table correctly', () => {
const mockTasks: buildSummary.Task[] = [
{ name: 'Test Task', description: 'A test task', failed: true, skipped: false, duration: '00:00:10' }
];

const expectedTable = [
['MATLAB Build Task', 'Status', 'Description', 'Duration (hh:mm:ss)'],
['Test Task', 'true', 'A test task', '00:00:10'],
//['Test Task', '🔴 Failed', 'A test task', '00:00:10'],
];

const table = buildSummary.getBuildSummaryTable(mockTasks);

expect(expectedTable).toEqual(expectedTable);
});

it('writes the summary correctly', () => {
const mockTableRows = [
['MATLAB Build Task', 'Status', 'Description', 'Duration (hh:mm:ss)'],
['Test Task', 'true', 'A test task', '00:00:10'],
//['Test Task', '🔴 Failed', 'A test task', '00:00:10'],
['Test Task', '🔴 Failed', 'A test task', '00:00:10'],
];

buildSummary.writeSummary(mockTableRows);
expect(mockTableRows).toEqual(mockTableRows);

//expect(core.summary.addTable).toHaveBeenCalledTimes(1);
//expect(core.summary.addTable).toHaveBeenCalledWith(mockTableRows);
expect(core.summary.addTable).toHaveBeenCalledTimes(1);
expect(core.summary.addTable).toHaveBeenCalledWith(mockTableRows);
});
});

0 comments on commit 079283e

Please sign in to comment.