Skip to content

Commit

Permalink
Add GitHub CI tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rweinsteMW committed Nov 10, 2023
1 parent 29ff234 commit cb33fed
Show file tree
Hide file tree
Showing 12 changed files with 211 additions and 102 deletions.
69 changes: 69 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
name: MATLAB Build

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

jobs:
test:
strategy:
fail-fast: false
matrix:
MATLABVersion: [R2023a,R2023b]
runs-on: ubuntu-latest
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v3

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

# Run SmokeTests
- name: Run SmokeTests
uses: matlab-actions/run-command@v1
with:
command: openProject(pwd); results = runtests(fullfile("SoftwareTests","SmokeTests.m")); assertSuccess(results);

# Run FunctionTests
- name: Run FunctionTests
uses: matlab-actions/run-command@v1
with:
command: openProject(pwd); results = runtests(fullfile("SoftwareTests","FunctionTests.m")); assertSuccess(results);

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

# 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); results = runtests(fullfile("SoftwareTests","CreateBadge.m"));

# 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
git commit Images/TestedWith.json -m "Update CI badges ${{ github.ref_name }}"
git push
28 changes: 22 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@
*.slx.r*
*.mdl.r*

# Derived content-obscured files
*.p
# MATLAB Drive
*.MATLABDriveTag

# Compiled MEX files
# Compiled files
*.mex*
*.p

# Compressed files
*.zip

# Packaged app and toolbox files
*.mlappinstall
Expand All @@ -23,13 +27,25 @@
# Generated helpsearch folders
helpsearch*/

# Code generation folders
# Defined Simulink cache folder
Utilities/SimulinkCache/*

# Standard code generation folders
slprj/
sccprj/
codegen/

# Code generation file
*.eep
*.elf
*.hex
*.bin

# Cache files
*.slxc

# Cloud based storage dotfile
.MATLABDriveTag
# Project settings
Utilities/ProjectSettings.mat

# Test results
SoftwareTests/TestResults_*
85 changes: 0 additions & 85 deletions .gitlab-ci.yml

This file was deleted.

82 changes: 82 additions & 0 deletions SoftwareTests/CreateBadge.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
% Run these tests with runMyTests
% All tests so far are on code expected to run without errors
% If/when we end up with a version that _should_ error,
% please add it to this set of examples
classdef CreateBadge < matlab.unittest.TestCase

properties
rootProject
results
end


methods (TestClassSetup)

function setUpPath(testCase)

try
project = currentProject;
testCase.rootProject = project.RootFolder;
cd(testCase.rootProject)
catch
error("Load project prior to run tests")
end

testCase.log("Running in " + version)

end % function setUpPath

function readResults(testCase)
Release = string([]);
Passed = [];
testCase.results = table(Release,Passed);

ResultFiles = dir("SoftwareTests"+filesep+"TestResults_*");
for kFiles = 1:size(ResultFiles)
Results = readtable(fullfile(ResultFiles(kFiles).folder,ResultFiles(kFiles).name),...
Delimiter=",",TextType="string");
Release = Results.Version(1);
Passed = all(Results.Status == "passed");
testCase.results(end+1,:) = table(Release,Passed);
end
end

end % methods (TestClassSetup)

methods(Test)

function writeBadge(testCase)

% Create JSON
badgeInfo = struct;
badgeInfo.schemaVersion = 1;
badgeInfo.label = "tested with";
badgeInfo.message = "";

% Check that results exist:
if size(testCase.results,1) == 0
badgeInfo.message = "None";
badgeInfo.color = "failed";
else
for i = 1:size(testCase.results,1)
if testCase.results.Passed(i)
if badgeInfo.message ~= ""
badgeInfo.message = badgeInfo.message + " | ";
end
badgeInfo.message = badgeInfo.message + string(testCase.results.Release(i));
end
end
badgeInfo.color = "success";
end

% Write JSON file out
badgeJSON = jsonencode(badgeInfo);
fid = fopen(fullfile("Images","TestedWith.json"),"w");
fwrite(fid,badgeJSON);
fclose(fid);

end

end

end
29 changes: 18 additions & 11 deletions SoftwareTests/SmokeTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,29 @@ function setUpResults(testCase)

function smokeTest(testCase)
myFiles = testCase.results.Name;
for k = 1:length(myFiles)
if myFiles(k) ~= "TreasureHuntAdvanced.mlx"
fid = fopen(fullfile("SoftwareTests","TestResults_"+release_version+".txt"),"w");
fprintf(fid,"Version,File,Status,ElapsedTime\n");
for kTest = 1:length(myFiles)
if myFiles(kTest) ~= "TreasureHuntAdvanced.mlx"
try
disp("Running " + myFiles(k))
disp("Running " + myFiles(kTest))
tic
run(myFiles(k))
testCase.results.Time(k) = toc;
disp("Finished " + myFiles(k))
testCase.results.Passed(k) = true;
run(myFiles(kTest))
testCase.results.Time(kTest) = toc;
disp("Finished " + myFiles(kTest))
testCase.results.Passed(kTest) = true;
fprintf(fid,"%s,%s,%s,%s\n",release_version,myFiles(kTest),"passed",testCase.results.Time(kTest));
catch ME
testCase.results.Time(k) = toc;
disp("Failed " + myFiles(k) + " because " + ...
testCase.results.Time(kTest) = toc;
disp("Failed " + myFiles(kTest) + " because " + ...
newline + ME.message)
testCase.results.Message(k) = ME.message;
testCase.results.Message(kTest) = ME.message;
fprintf(fid,"%s,%s,%s,%s\n",release_version,myFiles(kTest),"failed",testCase.results.Time(kTest));
end
end
clearvars -except kTest testCase myFiles fid
end
fclose(fid);
struct2table(testCase.results)
end

Expand All @@ -70,7 +76,8 @@ function smokeTest(testCase)
methods (TestClassTeardown)

function closeAllFigure(testCase)
close all force
close all force % Close figure windows
bdclose all % Close Simulink models
end

end % methods (TestClassTeardown)
Expand Down
Binary file modified Utilities/ProjectSettings.mat
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version='1.0' encoding='UTF-8'?>
<Info>
<Category UUID="FileClassCategory">
<Label UUID="test"/>
</Category>
</Info>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version='1.0' encoding='UTF-8'?>
<Info location="CreateBadge.m" type="File"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version='1.0' encoding='UTF-8'?>
<Info>
<Category UUID="FileClassCategory">
<Label UUID="test"/>
</Category>
</Info>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version='1.0' encoding='UTF-8'?>
<Info location="FunctionTests.m" type="File"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version='1.0' encoding='UTF-8'?>
<Info/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version='1.0' encoding='UTF-8'?>
<Info location="CONTRIBUTING.md" type="File"/>

0 comments on commit cb33fed

Please sign in to comment.