From 079283e27b32b3016d7c19fa3691dc3c7f4a962a Mon Sep 17 00:00:00 2001 From: Nikhil Bhoski <47204011+nbhoski@users.noreply.github.com> Date: Tue, 13 Aug 2024 13:24:38 +0530 Subject: [PATCH] Updated tests --- src/buildSummary.ts | 50 ++++------------------------------- src/buildSummary.unit.test.ts | 24 +++-------------- 2 files changed, 8 insertions(+), 66 deletions(-) diff --git a/src/buildSummary.ts b/src/buildSummary.ts index f819971..3caf574 100644 --- a/src/buildSummary.ts +++ b/src/buildSummary.ts @@ -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; @@ -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 @@ -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') { @@ -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; @@ -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; } diff --git a/src/buildSummary.unit.test.ts b/src/buildSummary.unit.test.ts index c46867e..44906d4 100644 --- a/src/buildSummary.unit.test.ts +++ b/src/buildSummary.unit.test.ts @@ -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); }); });