Skip to content

Commit 99f02ef

Browse files
committed
update test
1 parent f93916d commit 99f02ef

1 file changed

Lines changed: 136 additions & 143 deletions

File tree

src/testResultsSummary.unit.test.ts

Lines changed: 136 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,55 @@ const fs = await import("fs");
3737
const testResultsSummary = await import("./testResultsSummary.js");
3838
const { MatlabTestStatus } = testResultsSummary;
3939

40+
function getOSInfo() {
41+
const platform = os.platform().toLowerCase();
42+
if (platform.includes("win") && !platform.includes("darwin"))
43+
return { osName: "windows", workspaceParent: "C:\\" };
44+
if (platform.includes("linux") || platform.includes("unix") || platform.includes("aix"))
45+
return { osName: "linux", workspaceParent: "/home/user/" };
46+
if (platform.includes("darwin")) return { osName: "mac", workspaceParent: "/Users/username/" };
47+
throw new Error(`Unsupported OS: ${platform}`);
48+
}
49+
50+
function safeDelete(filePath: string) {
51+
try {
52+
nodeFs.unlinkSync(filePath);
53+
} catch (e) {
54+
/* ignore */
55+
}
56+
}
57+
58+
const runnerTemp = path.join(import.meta.dirname, "..");
59+
const osInfo = getOSInfo();
60+
const workspace = path.join(osInfo.workspaceParent, "workspace");
61+
62+
function getTestDataSource(testFolder: string, fileName: string) {
63+
return path.join(
64+
import.meta.dirname,
65+
"test-data",
66+
"testResultsArtifacts",
67+
testFolder,
68+
osInfo.osName,
69+
fileName,
70+
);
71+
}
72+
73+
function copyTestDataAndRun(
74+
testFolder: string,
75+
sourceFileName: string,
76+
destFileName: string,
77+
actionName = "",
78+
) {
79+
const sourceFilePath = getTestDataSource(testFolder, sourceFileName);
80+
const destPath = path.join(runnerTemp, destFileName);
81+
fs.copyFileSync(sourceFilePath, destPath);
82+
83+
const result = testResultsSummary.getTestResults(runnerTemp, actionName, workspace);
84+
85+
safeDelete(destPath);
86+
return result;
87+
}
88+
4089
describe("Artifact Processing Tests", () => {
4190
// Shared test data
4291
let testResultsData: TestResultsData | null;
@@ -45,60 +94,28 @@ describe("Artifact Processing Tests", () => {
4594
let stats: TestStatistics;
4695

4796
beforeAll(() => {
48-
const runnerTemp = path.join(import.meta.dirname, "..");
49-
const osInfo = getOSInfo();
50-
const workspace = path.join(osInfo.workspaceParent, "workspace");
97+
const sourceFilePath = getTestDataSource(
98+
"t1",
99+
"matlabTestResults_20250101_100000_001.json",
100+
);
101+
const destPath = path.join(runnerTemp, "matlabTestResults_20250101_100000_001.json");
51102

52-
copyTestDataFile(osInfo.osName, runnerTemp);
103+
try {
104+
fs.copyFileSync(sourceFilePath, destPath);
105+
} catch (err) {
106+
console.error("Error copying test-data:", err);
107+
}
53108

54-
testResultsData = testResultsSummary.getTestResults(runnerTemp, workspace);
109+
testResultsData = testResultsSummary.getTestResults(runnerTemp, "", workspace);
55110
if (testResultsData) {
56111
testSession = testResultsData.TestSessions[0];
57112
testResults = testSession.TestResults;
58113
stats = testResultsData.OverallStats;
59114
}
60115

61-
// Clean up test file since unlinkSync is mocked
62-
const testFile = path.join(runnerTemp, "matlabTestResults_20250101_100000_001.json");
63-
try {
64-
nodeFs.unlinkSync(testFile);
65-
} catch (e) {
66-
/* ignore */
67-
}
116+
safeDelete(destPath);
68117
});
69118

70-
function getOSInfo() {
71-
const platform = os.platform().toLowerCase();
72-
if (platform.includes("win") && !platform.includes("darwin"))
73-
return { osName: "windows", workspaceParent: "C:\\" };
74-
if (platform.includes("linux") || platform.includes("unix") || platform.includes("aix"))
75-
return { osName: "linux", workspaceParent: "/home/user/" };
76-
if (platform.includes("darwin"))
77-
return { osName: "mac", workspaceParent: "/Users/username/" };
78-
throw new Error(`Unsupported OS: ${platform}`);
79-
}
80-
81-
function copyTestDataFile(osName: string, runnerTemp: string) {
82-
const sourceFilePath = path.join(
83-
import.meta.dirname,
84-
"test-data",
85-
"testResultsArtifacts",
86-
"t1",
87-
osName,
88-
"matlabTestResults_20250101_100000_001.json",
89-
);
90-
const destinationFilePath = path.join(
91-
runnerTemp,
92-
"matlabTestResults_20250101_100000_001.json",
93-
);
94-
95-
try {
96-
fs.copyFileSync(sourceFilePath, destinationFilePath);
97-
} catch (err) {
98-
console.error("Error copying test-data:", err);
99-
}
100-
}
101-
102119
it("should return correct test results data for valid JSON", () => {
103120
expect(testResultsData).toBeDefined();
104121
expect(testResultsData!.TestSessions.length).toBe(1);
@@ -389,28 +406,9 @@ describe("HTML Structure Tests", () => {
389406
describe("Multiple Sessions Tests", () => {
390407
let testResultsData: TestResultsData | null;
391408

392-
function getOSInfo() {
393-
const platform = os.platform().toLowerCase();
394-
if (platform.includes("win") && !platform.includes("darwin"))
395-
return { osName: "windows", workspaceParent: "C:\\" };
396-
if (platform.includes("linux") || platform.includes("unix") || platform.includes("aix"))
397-
return { osName: "linux", workspaceParent: "/home/user/" };
398-
if (platform.includes("darwin"))
399-
return { osName: "mac", workspaceParent: "/Users/username/" };
400-
throw new Error(`Unsupported OS: ${platform}`);
401-
}
402-
403409
beforeAll(() => {
404-
const runnerTemp = path.join(import.meta.dirname, "..");
405-
const osInfo = getOSInfo();
406-
const workspace = path.join(osInfo.workspaceParent, "workspace");
407-
408-
const sourceFilePath = path.join(
409-
import.meta.dirname,
410-
"test-data",
411-
"testResultsArtifacts",
410+
const sourceFilePath = getTestDataSource(
412411
"t1",
413-
osInfo.osName,
414412
"matlabTestResults_20250101_100000_001.json",
415413
);
416414

@@ -425,19 +423,10 @@ describe("Multiple Sessions Tests", () => {
425423
console.error("Error copying test-data:", err);
426424
}
427425

428-
testResultsData = testResultsSummary.getTestResults(runnerTemp, workspace);
426+
testResultsData = testResultsSummary.getTestResults(runnerTemp, "", workspace);
429427

430-
// Clean up test files since unlinkSync is mocked
431-
try {
432-
nodeFs.unlinkSync(dest1);
433-
} catch (e) {
434-
/* ignore */
435-
}
436-
try {
437-
nodeFs.unlinkSync(dest2);
438-
} catch (e) {
439-
/* ignore */
440-
}
428+
safeDelete(dest1);
429+
safeDelete(dest2);
441430
});
442431

443432
it("should return multiple test sessions", () => {
@@ -534,13 +523,17 @@ describe("Multiple Sessions Tests", () => {
534523
describe("No Results Tests", () => {
535524
it("should return null when no matching files exist", () => {
536525
const emptyDir = path.join(import.meta.dirname, "test-data");
537-
const result = testResultsSummary.getTestResults(emptyDir, "");
526+
const result = testResultsSummary.getTestResults(emptyDir, "", workspace);
538527
expect(result).toBeNull();
539528
});
540529

541530
it("should return null when directory does not exist", () => {
542531
const consoleSpy = jest.spyOn(console, "error").mockImplementation(() => {});
543-
const result = testResultsSummary.getTestResults("/nonexistent/directory/path", "");
532+
const result = testResultsSummary.getTestResults(
533+
"/nonexistent/directory/path",
534+
"",
535+
workspace,
536+
);
544537
expect(result).toBeNull();
545538
expect(consoleSpy).toHaveBeenCalled();
546539
expect(consoleSpy.mock.calls[0][0] as string).toContain(
@@ -550,6 +543,66 @@ describe("No Results Tests", () => {
550543
});
551544
});
552545

546+
describe("actionName Filtering Tests", () => {
547+
it("should only pick up files matching the given actionName", () => {
548+
const sourceFilePath = getTestDataSource(
549+
"t1",
550+
"matlabTestResults_20250101_100000_001.json",
551+
);
552+
553+
const matchingFile = path.join(
554+
runnerTemp,
555+
"matlabTestResultsmy-action_20250101_100000_001.json",
556+
);
557+
const nonMatchingFile = path.join(
558+
runnerTemp,
559+
"matlabTestResultsother-action_20250101_100000_001.json",
560+
);
561+
562+
try {
563+
fs.copyFileSync(sourceFilePath, matchingFile);
564+
fs.copyFileSync(sourceFilePath, nonMatchingFile);
565+
566+
const result = testResultsSummary.getTestResults(runnerTemp, "my-action", workspace);
567+
568+
expect(result).not.toBeNull();
569+
expect(result!.TestSessions.length).toBe(1);
570+
expect(result!.TestSessions[0].FileName).toBe(
571+
"matlabTestResultsmy-action_20250101_100000_001.json",
572+
);
573+
} finally {
574+
safeDelete(matchingFile);
575+
safeDelete(nonMatchingFile);
576+
}
577+
});
578+
579+
it("should return null when no files match the actionName", () => {
580+
const sourceFilePath = getTestDataSource(
581+
"t1",
582+
"matlabTestResults_20250101_100000_001.json",
583+
);
584+
585+
const nonMatchingFile = path.join(
586+
runnerTemp,
587+
"matlabTestResultsother-action_20250101_100000_001.json",
588+
);
589+
590+
try {
591+
fs.copyFileSync(sourceFilePath, nonMatchingFile);
592+
593+
const result = testResultsSummary.getTestResults(
594+
runnerTemp,
595+
"nonexistent-action",
596+
workspace,
597+
);
598+
599+
expect(result).toBeNull();
600+
} finally {
601+
safeDelete(nonMatchingFile);
602+
}
603+
});
604+
});
605+
553606
describe("Error Handling Tests", () => {
554607
it("should handle errors gracefully in addSummary", () => {
555608
const consoleSpy = jest.spyOn(console, "error").mockImplementation(() => {});
@@ -595,77 +648,56 @@ describe("Error Handling Tests", () => {
595648
it("should handle JSON parsing errors gracefully", () => {
596649
const consoleSpy = jest.spyOn(console, "error").mockImplementation(() => {});
597650

598-
const runnerTemp = path.join(import.meta.dirname, "..");
599-
600-
// Create a file with invalid JSON matching the new naming pattern
601651
const invalidJsonPath = path.join(runnerTemp, "matlabTestResults_20250101_100000_097.json");
602652
fs.writeFileSync(invalidJsonPath, "{ invalid json content");
603653

604654
try {
605-
const result = testResultsSummary.getTestResults(runnerTemp, "");
655+
const result = testResultsSummary.getTestResults(runnerTemp, "", "");
606656
expect(result).not.toBeNull();
607657
expect(result!.TestSessions.length).toBe(0);
608658
expect(result!.OverallStats.Total).toBe(0);
609659

610-
// Verify error was logged
611660
expect(consoleSpy).toHaveBeenCalledWith(
612661
expect.stringContaining(
613662
"An error occurred while reading the test results summary file",
614663
),
615664
expect.any(Error),
616665
);
617666
} finally {
618-
// Clean up
619-
if (nodeFs.existsSync(invalidJsonPath)) {
620-
nodeFs.unlinkSync(invalidJsonPath);
621-
}
667+
safeDelete(invalidJsonPath);
622668
consoleSpy.mockRestore();
623669
}
624670
});
625671

626672
it("should handle file deletion errors gracefully", () => {
627673
const consoleSpy = jest.spyOn(console, "error").mockImplementation(() => {});
628674

629-
// Set up the mock to throw an error for this test
630675
mockUnlinkSync.mockImplementationOnce(() => {
631676
throw new Error("Permission denied - cannot delete file");
632677
});
633678

634-
const runnerTemp = path.join(import.meta.dirname, "..");
635-
636-
// Create a valid JSON file matching the new naming pattern
637679
const validJsonPath = path.join(runnerTemp, "matlabTestResults_20250101_100000_096.json");
638-
fs.writeFileSync(validJsonPath, "[]"); // Empty array - valid JSON
680+
fs.writeFileSync(validJsonPath, "[]");
639681

640682
try {
641-
const result = testResultsSummary.getTestResults(runnerTemp, "");
683+
const result = testResultsSummary.getTestResults(runnerTemp, "", "");
642684

643-
// Should still return results even if deletion fails
644685
expect(result).not.toBeNull();
645686
expect(result!.TestSessions.length).toBe(1);
646687
expect(result!.TestSessions[0].TestResults).toEqual([]);
647688

648-
// Verify deletion error was logged
649689
expect(consoleSpy).toHaveBeenCalledWith(
650690
expect.stringContaining(
651691
"An error occurred while trying to delete the test results summary file",
652692
),
653693
expect.any(Error),
654694
);
655695

656-
// Verify unlinkSync was called
657696
expect(mockUnlinkSync).toHaveBeenCalledWith(validJsonPath);
658697
} finally {
659-
// Clean up
660698
mockUnlinkSync.mockReset();
661699
consoleSpy.mockRestore();
662-
663-
// Clean up the test file using real fs
664-
try {
665-
nodeFs.unlinkSync(validJsonPath);
666-
} catch (e) {
667-
// Ignore cleanup errors
668-
}
700+
safeDelete(validJsonPath);
669701
}
670702
});
671703
});
@@ -678,45 +710,6 @@ describe("Data Processing Edge Cases", () => {
678710
: never
679711
: never;
680712

681-
function getOSInfoHelper() {
682-
const platform = os.platform().toLowerCase();
683-
if (platform.includes("win") && !platform.includes("darwin"))
684-
return { osName: "windows", workspaceParent: "C:\\" };
685-
if (platform.includes("linux") || platform.includes("unix") || platform.includes("aix"))
686-
return { osName: "linux", workspaceParent: "/home/user/" };
687-
if (platform.includes("darwin"))
688-
return { osName: "mac", workspaceParent: "/Users/username/" };
689-
throw new Error(`Unsupported OS: ${platform}`);
690-
}
691-
692-
function copyTestDataAndRun(testFolder: string, sourceFileName: string, destFileName: string) {
693-
const runnerTemp = path.join(import.meta.dirname, "..");
694-
const osInfo = getOSInfoHelper();
695-
const workspace = path.join(osInfo.workspaceParent, "workspace");
696-
697-
const sourceFilePath = path.join(
698-
import.meta.dirname,
699-
"test-data",
700-
"testResultsArtifacts",
701-
testFolder,
702-
osInfo.osName,
703-
sourceFileName,
704-
);
705-
const destPath = path.join(runnerTemp, destFileName);
706-
fs.copyFileSync(sourceFilePath, destPath);
707-
708-
const result = testResultsSummary.getTestResults(runnerTemp, workspace);
709-
710-
// Clean up since unlinkSync is mocked
711-
try {
712-
nodeFs.unlinkSync(destPath);
713-
} catch (e) {
714-
/* ignore */
715-
}
716-
717-
return result;
718-
}
719-
720713
it("should handle single object JSON (non-array test artifact)", () => {
721714
const result = copyTestDataAndRun(
722715
"t2",

0 commit comments

Comments
 (0)