Skip to content

Commit 8938b50

Browse files
authored
Move job summary opt-out check to plugin service classes (#29)
* move check to services * fix test * update env var name for consistency with input field name
1 parent 9188145 commit 8938b50

8 files changed

Lines changed: 59 additions & 71 deletions

File tree

plugins/+buildframework/BuildSummaryPlugin.m

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,13 @@
1010
function runTaskGraph(plugin, pluginData)
1111
runTaskGraph@matlab.buildtool.plugins.BuildRunnerPlugin(plugin, pluginData);
1212

13-
if strcmpi(getenv("MW_GENERATE_JOB_SUMMARY"), "true")
14-
[fID, msg] = fopen(fullfile(getenv("RUNNER_TEMP"), "buildSummary" + getenv("GITHUB_ACTION") + "_" + string(datetime('now', 'Format', 'yyyyMMdd_HHmmss_SSS')) + ".json"), "w");
15-
if fID == -1
16-
warning("buildframework:BuildSummaryPlugin:UnableToOpenFile","Unable to open a file required to create the MATLAB build summary table: %s", msg);
17-
else
18-
closeFile = onCleanup(@()fclose(fID));
19-
s = jsonencode(plugin.TaskDetails);
20-
fprintf(fID, "%s",s);
21-
end
13+
[fID, msg] = fopen(fullfile(getenv("RUNNER_TEMP"), "buildSummary" + getenv("GITHUB_ACTION") + "_" + string(datetime('now', 'Format', 'yyyyMMdd_HHmmss_SSS')) + ".json"), "w");
14+
if fID == -1
15+
warning("buildframework:BuildSummaryPlugin:UnableToOpenFile","Unable to open a file required to create the MATLAB build summary table: %s", msg);
16+
else
17+
closeFile = onCleanup(@()fclose(fID));
18+
s = jsonencode(plugin.TaskDetails);
19+
fprintf(fID, "%s",s);
2220
end
2321
end
2422

plugins/+buildframework/ParallelizableBuildSummaryPlugin.m

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,14 @@ function runBuild(plugin, pluginData)
3434
end
3535

3636
% Write to file
37-
if strcmpi(getenv("MW_GENERATE_JOB_SUMMARY"), "true")
38-
folder = fileparts(plugin.TempFolder);
39-
[fID, msg] = fopen(fullfile(folder, "buildSummary" + getenv("GITHUB_ACTION") + "_" + string(datetime('now', 'Format', 'yyyyMMdd_HHmmss_SSS')) + ".json"), "w");
40-
if fID == -1
41-
warning("buildframework:BuildSummaryPlugin:UnableToOpenFile","Unable to open a file required to create the MATLAB build summary table: %s", msg);
42-
else
43-
closeFile = onCleanup(@()fclose(fID));
44-
s = jsonencode(taskDetails);
45-
fprintf(fID, "%s", s);
46-
end
37+
folder = fileparts(plugin.TempFolder);
38+
[fID, msg] = fopen(fullfile(folder, "buildSummary" + getenv("GITHUB_ACTION") + "_" + string(datetime('now', 'Format', 'yyyyMMdd_HHmmss_SSS')) + ".json"), "w");
39+
if fID == -1
40+
warning("buildframework:BuildSummaryPlugin:UnableToOpenFile","Unable to open a file required to create the MATLAB build summary table: %s", msg);
41+
else
42+
closeFile = onCleanup(@()fclose(fID));
43+
s = jsonencode(taskDetails);
44+
fprintf(fID, "%s", s);
4745
end
4846
end
4947

plugins/+buildframework/getDefaultPlugins.m

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@
66
pluginProviderData (1,1) struct = struct();
77
end
88

9-
if isMATLABReleaseOlderThan("R2026a")
10-
reportPlugin = buildframework.BuildSummaryPlugin();
11-
else
12-
reportPlugin = buildframework.ParallelizableBuildSummaryPlugin();
13-
end
14-
159
plugins = [ ...
1610
matlab.buildtool.internal.getFactoryDefaultPlugins(pluginProviderData) ...
1711
buildframework.GitHubLogPlugin() ...
18-
reportPlugin ...
1912
];
13+
14+
if strcmpi(getenv("MW_GENERATE_SUMMARY"), "true")
15+
if isMATLABReleaseOlderThan("R2026a")
16+
reportPlugin = buildframework.BuildSummaryPlugin();
17+
else
18+
reportPlugin = buildframework.ParallelizableBuildSummaryPlugin();
19+
end
20+
plugins = [plugins reportPlugin];
21+
end
2022
end

plugins/+matlab/+unittest/+internal/+services/+plugins/TestResultsSummaryPluginService.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
methods
55
function plugins = providePlugins(~, ~)
66
% Check if MATLAB Test license is available
7-
if license('test', 'matlab_test')
7+
if strcmpi(getenv("MW_GENERATE_SUMMARY"), "true") && license('test', 'matlab_test')
88
plugins = testframework.TestResultsSummaryPlugin();
99
else
1010
plugins = matlab.unittest.plugins.TestRunnerPlugin.empty(1,0);

plugins/+testframework/TestResultsSummaryPlugin.m

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,37 @@
33

44
methods (Access=protected)
55
function reportFinalizedSuite(plugin, pluginData)
6-
if strcmpi(getenv("MW_GENERATE_JOB_SUMMARY"), "true")
7-
% Checkout MATLAB Test license
8-
license('checkout', 'matlab_test');
6+
% Checkout MATLAB Test license
7+
license('checkout', 'matlab_test');
98

10-
testDetails = struct([]);
11-
for idx = 1:numel(pluginData.TestResult)
12-
testDetails(idx).TestResult.Duration = pluginData.TestResult(idx).Duration;
13-
if isfield(pluginData.TestResult(idx).Details, "DiagnosticRecord") && ~isempty(pluginData.TestResult(idx).Details.DiagnosticRecord)
14-
testDetails(idx).TestResult.Details.DiagnosticRecord.Event = pluginData.TestResult(idx).Details.DiagnosticRecord.Event;
15-
testDetails(idx).TestResult.Details.DiagnosticRecord.Report = pluginData.TestResult(idx).Details.DiagnosticRecord.Report;
16-
else
17-
testDetails(idx).TestResult.Details = struct();
18-
end
19-
testDetails(idx).TestResult.Name = pluginData.TestResult(idx).Name;
20-
testDetails(idx).TestResult.Passed = pluginData.TestResult(idx).Passed;
21-
testDetails(idx).TestResult.Failed = pluginData.TestResult(idx).Failed;
22-
testDetails(idx).TestResult.Incomplete = pluginData.TestResult(idx).Incomplete;
23-
testDetails(idx).BaseFolder = pluginData.TestSuite(idx).BaseFolder;
9+
testDetails = struct([]);
10+
for idx = 1:numel(pluginData.TestResult)
11+
testDetails(idx).TestResult.Duration = pluginData.TestResult(idx).Duration;
12+
if isfield(pluginData.TestResult(idx).Details, "DiagnosticRecord") && ~isempty(pluginData.TestResult(idx).Details.DiagnosticRecord)
13+
testDetails(idx).TestResult.Details.DiagnosticRecord.Event = pluginData.TestResult(idx).Details.DiagnosticRecord.Event;
14+
testDetails(idx).TestResult.Details.DiagnosticRecord.Report = pluginData.TestResult(idx).Details.DiagnosticRecord.Report;
15+
else
16+
testDetails(idx).TestResult.Details = struct();
2417
end
18+
testDetails(idx).TestResult.Name = pluginData.TestResult(idx).Name;
19+
testDetails(idx).TestResult.Passed = pluginData.TestResult(idx).Passed;
20+
testDetails(idx).TestResult.Failed = pluginData.TestResult(idx).Failed;
21+
testDetails(idx).TestResult.Incomplete = pluginData.TestResult(idx).Incomplete;
22+
testDetails(idx).BaseFolder = pluginData.TestSuite(idx).BaseFolder;
23+
end
2524

26-
try
27-
jsonTestResults = jsonencode(testDetails, "PrettyPrint", true);
28-
testArtifactFile = fullfile(getenv("RUNNER_TEMP"), "matlabTestResults" + getenv("GITHUB_ACTION") + "_" + string(datetime('now', 'Format', 'yyyyMMdd_HHmmss_SSS')) + ".json");
29-
[fID, msg] = fopen(testArtifactFile, "w");
30-
if fID == -1
31-
warning("testframework:TestResultsSummaryPlugin:UnableToOpenFile","Unable to open a file required to create the table of test results. (Cause: %s)", msg);
32-
else
33-
closeFile = onCleanup(@()fclose(fID));
34-
fprintf(fID, '%s', jsonTestResults);
35-
end
36-
catch e
37-
warning("testframework:TestResultsSummaryPlugin:UnableToJsonEncode","Unable to jsonencode test results data. (Cause: %s)", e.message);
25+
try
26+
jsonTestResults = jsonencode(testDetails, "PrettyPrint", true);
27+
testArtifactFile = fullfile(getenv("RUNNER_TEMP"), "matlabTestResults" + getenv("GITHUB_ACTION") + "_" + string(datetime('now', 'Format', 'yyyyMMdd_HHmmss_SSS')) + ".json");
28+
[fID, msg] = fopen(testArtifactFile, "w");
29+
if fID == -1
30+
warning("testframework:TestResultsSummaryPlugin:UnableToOpenFile","Unable to open a file required to create the table of test results. (Cause: %s)", msg);
31+
else
32+
closeFile = onCleanup(@()fclose(fID));
33+
fprintf(fID, '%s', jsonTestResults);
3834
end
35+
catch e
36+
warning("testframework:TestResultsSummaryPlugin:UnableToJsonEncode","Unable to jsonencode test results data. (Cause: %s)", e.message);
3937
end
4038

4139
% Invoke the superclass method

src/testResultsSummary.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ export function getTestResults(
8686
actionName: string,
8787
workspace: string,
8888
): TestResultsData | null {
89-
let testResultsData = null;
9089
const filePrefix = `matlabTestResults${actionName}_`;
9190
const fileSuffix = `.json`;
9291

@@ -118,12 +117,6 @@ export function getTestResults(
118117
Duration: 0,
119118
};
120119

121-
testResultsData = {
122-
TestSessions: testSessions,
123-
OverallStats: overallStats,
124-
};
125-
126-
// Process each test result file
127120
for (const fileName of testResultFiles) {
128121
const resultsPath = path.join(runnerTemp, fileName);
129122

@@ -140,21 +133,18 @@ export function getTestResults(
140133
};
141134

142135
const map = new Map<string, MatlabTestFile>();
143-
144136
const testCases = Array.isArray(testArtifact) ? testArtifact : [testArtifact];
145137

146138
for (const jsonTestCase of testCases) {
147139
processTestCase(sessionResults, jsonTestCase, map, sessionStats, workspace);
148140
}
149141

150-
// Add this session to the list
151142
testSessions.push({
152143
FileName: fileName,
153144
TestResults: sessionResults,
154145
Stats: sessionStats,
155146
});
156147

157-
// Update overall stats
158148
overallStats.Total += sessionStats.Total;
159149
overallStats.Passed += sessionStats.Passed;
160150
overallStats.Failed += sessionStats.Failed;
@@ -178,7 +168,11 @@ export function getTestResults(
178168
}
179169
}
180170

181-
return testResultsData;
171+
if (testSessions.length === 0) {
172+
return null;
173+
}
174+
175+
return { TestSessions: testSessions, OverallStats: overallStats };
182176
}
183177
export function addSummary(
184178
testResultsData: TestResultsData | null,

src/testResultsSummary.unit.test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -653,9 +653,7 @@ describe("Error Handling Tests", () => {
653653

654654
try {
655655
const result = testResultsSummary.getTestResults(runnerTemp, "", "");
656-
expect(result).not.toBeNull();
657-
expect(result!.TestSessions.length).toBe(0);
658-
expect(result!.OverallStats.Total).toBe(0);
656+
expect(result).toBeNull();
659657

660658
expect(consoleSpy).toHaveBeenCalledWith(
661659
expect.stringContaining(

tests/tParallelizableBuildSummaryPlugin.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function createPlugin(testCase)
2424

2525
testCase.applyFixture(WorkingFolderFixture);
2626
testCase.TempFolder = pwd();
27-
testCase.applyFixture(EnvironmentVariableFixture("MW_GENERATE_JOB_SUMMARY", "true"));
27+
testCase.applyFixture(EnvironmentVariableFixture("MW_GENERATE_SUMMARY", "true"));
2828
testCase.applyFixture(EnvironmentVariableFixture("GITHUB_ACTION", "my-action"));
2929

3030
testCase.Plugin = ParallelizableBuildSummaryPlugin(TempFolder=testCase.TempFolder);

0 commit comments

Comments
 (0)