Skip to content

Commit

Permalink
Added tested badge for github release
Browse files Browse the repository at this point in the history
  • Loading branch information
callieEDU committed Apr 13, 2024
1 parent 6066f74 commit a810170
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 0 deletions.
82 changes: 82 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
name: Module Test

# Controls when the action will run.
on:
push:
branches: [ release ]
pull_request:
branches: [ release ]

jobs:
RunTests:
strategy:
fail-fast: false
matrix:
MATLABVersion: [R2024a]
runs-on: ubuntu-latest
steps:
# Checks-out your repository
- uses: actions/checkout@v3

# Sets up MATLAB
- name: Setup MATLAB
uses: matlab-actions/setup-matlab@v1
with:
release: ${{ matrix.MATLABVersion }}

# Run all the tests
- name: Run SmokeTests
uses: matlab-actions/run-command@v1
with:
command: openProject(pwd); RunAllTests;

# Upload the test results as artifact
- name: Upload TestResults
if: always()
uses: actions/[email protected]
with:
name: TestResults
path: ./SoftwareTests/TestResults_${{ matrix.MATLABVersion }}.txt


CreateBadge:
if: ${{ always() }}
needs: [RunTests]
strategy:
fail-fast: false
runs-on: ubuntu-latest
steps:

# Checks-out your repository
- uses: actions/checkout@v3

# Sets up R2023b
- name: Setup MATLAB
uses: matlab-actions/setup-matlab@v1
with:
release: R2023b

# Download the test results from artifact
- name: Download TestResults
uses: actions/[email protected]
with:
name: TestResults
path: ./SoftwareTests/

# Create the test results badge
- name: Run CreateBadge
uses: matlab-actions/run-command@v1
with:
command: openProject(pwd); CreateBadge;

# Commit the JSON for the MATLAB releases badge
- name: Commit changed files
continue-on-error: true
run: |
git config user.name "${{ github.workflow }} by ${{ github.actor }}"
git config user.email "<>"
git pull
git add Images/TestedWith.json
git commit Images/TestedWith.json -m "Update CI badges ${{ github.ref_name }}"
git fetch
git push
1 change: 1 addition & 0 deletions Images/TestedWith.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"schemaVersion":1,"label":"Tested with","color":"success","message":"R2024a"}
47 changes: 47 additions & 0 deletions SoftwareTests/CheckTestResults.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
classdef CheckTestResults < matlab.unittest.TestCase

properties (SetAccess = protected)
end

properties (ClassSetupParameter)
Project = {''};
end

properties (TestParameter)
Version
end


methods (TestParameterDefinition,Static)

function Version = GetResults(Project)
RootFolder = currentProject().RootFolder;
Version = dir(fullfile(RootFolder,"SoftwareTests","TestResults*.txt"));
Version = extractBetween([Version.name],"TestResults_",".txt");
end

end

methods (TestClassSetup)

function SetUpSmokeTest(testCase,Project)
try
currentProject;
catch
error("Project is not loaded.")
end
end

end

methods(Test)

function CheckResults(testCase,Version)
File = fullfile("SoftwareTests","TestResults_"+Version+".txt");
Results = readtable(File,TextType="string");
testCase.verifyTrue(all(Results.Passed));
end

end

end
28 changes: 28 additions & 0 deletions SoftwareTests/CreateBadge.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
% Create the test suite with SmokeTest and Function test if they exist
Suite = testsuite("CheckTestResults");

% Create a runner with no plugins
Runner = matlab.unittest.TestRunner.withNoPlugins;

% Run the test suite
Results = Runner.run(Suite);

% Format the results in a table and save them
Results = table(Results');
Results = Results(Results.Passed,:);
Version = extractBetween(string(Results.Name),"Version=",")");


% Format the JSON file
Badge = struct;
Badge.schemaVersion = 1;
Badge.label = "Tested with";
if size(Results,1) >= 1
Badge.color = "success"
Badge.message = join(Version," | ");
else
Badge.color = "failure";
Badge.message = "Pipeline fails";
end
Badge = jsonencode(Badge);
writelines(Badge,fullfile("Images","TestedWith.json"));
48 changes: 48 additions & 0 deletions SoftwareTests/RunAllTests.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
function RunAllTest(EnableReport,ReportFolder)
arguments
EnableReport (1,1) logical = false;
ReportFolder (1,1) string = "public";
end

import matlab.unittest.plugins.TestReportPlugin;

% Create a runner
Runner = matlab.unittest.TestRunner.withTextOutput;
if EnableReport
Folder = fullfile(currentProject().RootFolder,ReportFolder);
if ~isfolder(Folder)
mkdir(Folder)
else
rmdir(Folder,'s')
mkdir(Folder)
end
Plugin = TestReportPlugin.producingHTML(Folder,...
"IncludingPassingDiagnostics",true,...
"IncludingCommandWindowText",true,...
"LoggingLevel",matlab.automation.Verbosity(1));
Runner.addPlugin(Plugin);
end

% Create the test suite with SmokeTest and Function test if they exist
Suite = testsuite("SmokeTests");
Suite = [Suite testsuite("FunctionTests")];

% Run the test suite
Results = Runner.run(Suite);

if EnableReport
web(fullfile(Folder,"index.html"))
else
T = table(Results);
disp(newline + "Test summary:")
disp(T)
end

% Format the results in a table and save them
ResultsTable = table(Results')
writetable(ResultsTable,fullfile("SoftwareTests","TestResults_"+release_version+".txt"));

% Assert success of test
assertSuccess(Results);

end

0 comments on commit a810170

Please sign in to comment.