Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 47f5ddb

Browse files
authoredFeb 2, 2025
Merge pull request #2390 from farid-zare/CI
Ci
2 parents 2f5c6ab + 989f362 commit 47f5ddb

File tree

3 files changed

+113
-29
lines changed

3 files changed

+113
-29
lines changed
 

‎.github/workflows/codecov.yml

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,50 @@
1-
name: Code Coverage
1+
name: MATLAB Tests and Code Coverage
2+
23
on:
3-
push:
4-
branches: [develop]
54
pull_request:
6-
branches: [develop]
5+
branches:
6+
- '**'
7+
78
jobs:
8-
test:
9-
runs-on: ubuntu-latest
9+
build:
10+
runs-on: self-hosted
1011
steps:
11-
- uses: actions/checkout@v3
12-
13-
- name: Set up MATLAB
14-
uses: matlab-actions/setup-matlab@v1
15-
with:
16-
release: R2022a
17-
18-
- name: Install MOcov
12+
13+
- name: Checkout PR Code
14+
uses: actions/checkout@v4
15+
16+
- name: Merge PR with Develop
17+
run: |
18+
git fetch origin develop
19+
git checkout -b test-branch
20+
git merge origin/develop --no-edit
21+
22+
- name: Clone COBRA Toolbox
1923
run: |
20-
git clone https://github.com/MOxUnit/MOcov.git /opt/MOcov
21-
sudo chmod -R 755 /opt/MOcov
22-
23-
- name: Verify MATLAB installation
24-
run: matlab -batch "version"
25-
26-
- name: Run MATLAB tests
24+
git clone https://github.com/opencobra/cobratoolbox.git cobratoolbox
25+
26+
- name: Run MATLAB Tests
2727
run: |
28-
matlab -batch "addpath(genpath(pwd)); run_coverage_tests"
29-
30-
- name: Upload coverage to Codecov
28+
matlab -batch "run('cobratoolbox/initCobraToolbox.m'); diary('test_results.txt'); run('cobratoolbox/testAll.m'); diary off;"
29+
30+
- name: Upload Test Report
31+
uses: actions/upload-artifact@v4
32+
with:
33+
name: test-report
34+
path: test_results.txt
35+
36+
- name: Upload Coverage to Codecov
37+
if: always() # Ensure it runs even if tests fail
3138
uses: codecov/codecov-action@v3
3239
with:
33-
files: ./coverage.xml
34-
flags: matlab
40+
files: CodeCovTestResults.xml
3541
token: ${{ secrets.CODECOV_TOKEN }}
3642
fail_ci_if_error: true
43+
flags: matlab
3744
comment: true
45+
46+
- name: Upload Test Results to Codecov
47+
if: always()
48+
uses: codecov/test-results-action@v1
49+
with:
50+
token: ${{ secrets.CODECOV_TOKEN }}

‎.github/workflows/testall.yml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
name: MATLAB Tests
2+
23
on:
34
pull_request:
45
branches:
@@ -8,13 +9,21 @@ jobs:
89
build:
910
runs-on: self-hosted
1011
steps:
11-
- uses: actions/checkout@v2
12+
13+
- name: Checkout PR Code
14+
uses: actions/checkout@v4
15+
16+
- name: Merge PR with Develop
17+
run: |
18+
git fetch origin develop
19+
git checkout -b test-branch
20+
git merge origin/develop --no-edit
1221
1322
- name: Clone COBRA Toolbox
1423
run: |
1524
git clone https://github.com/opencobra/cobratoolbox.git cobratoolbox
1625
17-
- name: Run MATLAB tests
26+
- name: Run MATLAB Tests
1827
run: |
1928
matlab -batch "run('cobratoolbox/initCobraToolbox.m'); diary('test_results.txt'); run('cobratoolbox/runTestsAndGenerateReport.m'); diary off;"
2029

‎test/testAll.m

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
% request explicitly from the user to launch test suite locally
1717
% if contains(getenv('HOME'), 'vmhadmin') || contains(getenv('HOME'), 'jenkins')
18-
if contains(getenv('HOME'), 'cobratoolbox')
18+
if contains(getenv('HOME'), 'saleh')
1919
% Running in CI environment
2020
% fprintf('Running test in Jenkins/CI environment\n');
2121
fprintf('Running test in cobratoolbox/CI environment\n');
@@ -182,6 +182,55 @@
182182
sumFailed = sum(resultTable.Failed);
183183

184184
fprintf(['\n > ', num2str(sumFailed), ' tests failed. ', num2str(sumSkipped), ' tests were skipped due to missing requirements.\n\n']);
185+
%% NEW: Generate JUnit XML report for Codecov
186+
xmlFileName = 'CodeCovTestResults.xml';
187+
fid = fopen(xmlFileName, 'w');
188+
if fid == -1
189+
error('Could not open file for writing: %s', xmlFileName);
190+
end
191+
192+
fprintf(fid, '<?xml version="1.0" encoding="UTF-8"?>\n');
193+
194+
numTests = height(resultTable);
195+
numFailures = sum(resultTable.Failed);
196+
numSkipped = sum(resultTable.Skipped);
197+
totalTime = 0;
198+
for i = 1:numTests
199+
if isnan(resultTable.Time(i))
200+
tVal = 0;
201+
else
202+
tVal = resultTable.Time(i);
203+
end
204+
totalTime = totalTime + tVal;
205+
end
206+
207+
fprintf(fid, '<testsuite name="COBRA Toolbox Test Suite" tests="%d" failures="%d" skipped="%d" time="%.3f">\n', ...
208+
numTests, numFailures, numSkipped, totalTime);
209+
210+
for i = 1:numTests
211+
testName = resultTable.TestName{i};
212+
if isnan(resultTable.Time(i))
213+
tVal = 0;
214+
else
215+
tVal = resultTable.Time(i);
216+
end
217+
fprintf(fid, ' <testcase classname="COBRA Toolbox" name="%s" time="%.3f"', testName, tVal);
218+
if resultTable.Passed(i)
219+
fprintf(fid, '/>\n');
220+
else
221+
fprintf(fid, '>\n');
222+
if resultTable.Skipped(i)
223+
fprintf(fid, ' <skipped message="%s"/>\n', escapeXML(resultTable.Details{i}));
224+
else
225+
fprintf(fid, ' <failure message="%s"/>\n', escapeXML(resultTable.Details{i}));
226+
end
227+
fprintf(fid, ' </testcase>\n');
228+
end
229+
end
230+
231+
fprintf(fid, '</testsuite>\n');
232+
fclose(fid);
233+
%% End of XML generation
185234

186235
% count the number of covered lines of code
187236
if COVERAGE
@@ -291,3 +340,16 @@
291340
% explicit 'exit' required for R2018b in non-interactive mode to avoid SEGV near end of test
292341
exit
293342
end
343+
344+
%% Local helper function to escape XML special characters.
345+
function out = escapeXML(in)
346+
if isempty(in)
347+
out = '';
348+
return;
349+
end
350+
out = strrep(in, '&', '&amp;');
351+
out = strrep(out, '<', '&lt;');
352+
out = strrep(out, '>', '&gt;');
353+
out = strrep(out, '"', '&quot;');
354+
out = strrep(out, '''', '&apos;');
355+
ens

0 commit comments

Comments
 (0)