Skip to content

Commit be906dd

Browse files
committed
add more tests + CI/CD pipeline begin
1 parent 386efa5 commit be906dd

13 files changed

Lines changed: 751 additions & 11 deletions

‎.github/workflows/ci-cd-matlab.yml‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
name: MATLAB CI/CD
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
8+
permissions:
9+
contents: write
10+
11+
jobs:
12+
matlab-test-and-report:
13+
name: Unit + Integration + E2E + Report
14+
runs-on: ubuntu-latest
15+
16+
steps:
17+
- name: Checkout repository
18+
uses: actions/checkout@v4
19+
20+
- name: Setup MATLAB
21+
uses: matlab-actions/setup-matlab@v2
22+
23+
- name: Prepare diversified external/synthetic data
24+
uses: matlab-actions/run-command@v2
25+
with:
26+
command: |
27+
addpath(fullfile(pwd, 'tests', 'ci'));
28+
prepare_ci_data(fullfile('temp', 'ci_data'));
29+
env:
30+
MONALISA_TEST_DATA_URLS: ${{ vars.MONALISA_TEST_DATA_URLS }}
31+
32+
- name: Run MATLAB test suites and generate reports
33+
uses: matlab-actions/run-command@v2
34+
with:
35+
command: |
36+
addpath(fullfile(pwd, 'tests', 'ci'));
37+
run_ci_test_suite(fullfile('temp', 'ci_data'), fullfile('temp', 'ci_reports'));
38+
env:
39+
MONALISA_TEST_DATA_URLS: ${{ vars.MONALISA_TEST_DATA_URLS }}
40+
41+
- name: Upload MATLAB report artifacts
42+
uses: actions/upload-artifact@v4
43+
with:
44+
name: matlab-ci-reports
45+
path: |
46+
temp/ci_reports/matlab_test_summary.md
47+
temp/ci_reports/matlab_test_summary.json
48+
temp/ci_reports/junit.xml
49+
if-no-files-found: error
50+
51+
- name: Publish summary in GitHub job summary
52+
shell: bash
53+
run: |
54+
cat temp/ci_reports/matlab_test_summary.md >> "$GITHUB_STEP_SUMMARY"
55+
56+
publish-test-report:
57+
name: Publish report to gh-pages (CD)
58+
needs: matlab-test-and-report
59+
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
60+
runs-on: ubuntu-latest
61+
62+
steps:
63+
- name: Checkout repository
64+
uses: actions/checkout@v4
65+
66+
- name: Download report artifacts
67+
uses: actions/download-artifact@v4
68+
with:
69+
name: matlab-ci-reports
70+
path: temp/ci_reports
71+
72+
- name: Publish reports under ci-reports/<sha>
73+
uses: peaceiris/actions-gh-pages@v3
74+
with:
75+
github_token: ${{ secrets.GITHUB_TOKEN }}
76+
publish_dir: temp/ci_reports
77+
destination_dir: ci-reports/${{ github.sha }}
78+
keep_files: true

‎.github/workflows/matlab-unit-tests.yml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ on:
44
push:
55
branches:
66
- 'ci-*' # note: use quotes for wildcard, this is only for testing ci in a specific branch
7-
pull_request:
7+
workflow_dispatch:
88

99
jobs:
1010
test:
@@ -21,4 +21,4 @@ jobs:
2121
uses: matlab-actions/run-tests@v2 # In theory this triggers all tests in the /tests folder
2222
with:
2323
source-folder: src
24-
select-by-folder: tests
24+
select-by-folder: tests

‎.github/workflows/pages.yml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# During a push on the main branch, the code is tested and the file describing the results of the matlab code is written
2+
# the tests are performed in docker containers with example data. The results of these tests are then published on the project's github page
3+
# The workflow is triggered by a push on the main branch. It uses the sphinx-notes/pages action to build the Sphinx documentation from the RST files located in the ./docs/rst directory, installing the dependencies listed in ./docs/requirements.txt. The result of the build is then published on GitHub Pages using the peaceiris/actions-gh-pages action.
4+
5+
6+
17
name: Deploy Sphinx documentation to Pages
28

39
on:

‎.gitignore‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,17 @@
5454
/temp/*.txt
5555
/temp/*.*
5656
/temp/*
57+
/temp/ci_data/
58+
/temp/ci_reports/
5759

5860
# large data
59-
*.dat
61+
*.dat
62+
63+
# test data
64+
/test_data/
65+
/test_data/*.dat
66+
/test_data/*.txt
67+
68+
69+
# env file
70+
.env

‎tests/ci/prepare_ci_data.m‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
function prepare_ci_data(dataDir)
2+
% Prepare diversified CI test data outside the repository-tracked files.
3+
% Data source policy:
4+
% 1) Optional download from URL list in MONALISA_TEST_DATA_URLS (semicolon separated)
5+
% 2) Always generate deterministic synthetic datasets
6+
7+
if nargin < 1 || isempty(dataDir)
8+
dataDir = fullfile('temp', 'ci_data');
9+
end
10+
11+
if ~exist(dataDir, 'dir')
12+
mkdir(dataDir);
13+
end
14+
15+
% Try downloading external datasets if provided.
16+
urlList = getenv('MONALISA_TEST_DATA_URLS');
17+
if ~isempty(urlList)
18+
urls = split(string(urlList), ';');
19+
for i = 1:numel(urls)
20+
u = strtrim(urls(i));
21+
if strlength(u) == 0
22+
continue;
23+
end
24+
outFile = fullfile(dataDir, sprintf('external_%02d.mat', i));
25+
try
26+
websave(outFile, char(u));
27+
fprintf('Downloaded external test data: %s\n', outFile);
28+
catch ME
29+
warning('Could not download "%s": %s', u, ME.message);
30+
end
31+
end
32+
end
33+
34+
% Deterministic synthetic datasets (diversified types/shapes).
35+
rng(42);
36+
cases = struct([]);
37+
38+
for k = 1:12
39+
nCh = randi([1, 4]);
40+
nPt = randi([4, 50]);
41+
raw = randn(nCh, nPt);
42+
if mod(k, 2) == 0
43+
raw = raw + 1i * randn(size(raw));
44+
end
45+
46+
weight = rand(1, nCh * nPt);
47+
weight = reshape(weight, size(raw));
48+
scalarWeight = 0.5 + rand();
49+
50+
cellX = {raw, raw * 2};
51+
cellY = {raw * 0.5, raw * 3};
52+
53+
cases(k).id = k;
54+
cases(k).raw = raw; %#ok<*AGROW>
55+
cases(k).nCh = nCh;
56+
cases(k).weight = weight;
57+
cases(k).scalarWeight = scalarWeight;
58+
cases(k).cellX = cellX;
59+
cases(k).cellY = cellY;
60+
end
61+
62+
save(fullfile(dataDir, 'synthetic_cases.mat'), 'cases', '-v7.3');
63+
fprintf('Synthetic CI test data written to %s\n', fullfile(dataDir, 'synthetic_cases.mat'));
64+
65+
end

‎tests/ci/run_ci_test_suite.m‎

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
function run_ci_test_suite(dataDir, reportDir)
2+
% Run unit/integration/e2e MATLAB tests and write machine/human reports.
3+
4+
if nargin < 1 || isempty(dataDir)
5+
dataDir = fullfile('temp', 'ci_data');
6+
end
7+
if nargin < 2 || isempty(reportDir)
8+
reportDir = fullfile('temp', 'ci_reports');
9+
end
10+
11+
if ~exist(reportDir, 'dir')
12+
mkdir(reportDir);
13+
end
14+
15+
setenv('MONALISA_CI_DATA_DIR', dataDir);
16+
setup_test_path();
17+
18+
import matlab.unittest.TestSuite
19+
import matlab.unittest.TestRunner
20+
import matlab.unittest.plugins.DiagnosticsOutputPlugin
21+
import matlab.unittest.plugins.XMLPlugin
22+
23+
runner = TestRunner.withTextOutput('OutputDetail', matlab.unittest.Verbosity.Detailed);
24+
runner.addPlugin(DiagnosticsOutputPlugin);
25+
runner.addPlugin(XMLPlugin.producingJUnitFormat(fullfile(reportDir, 'junit.xml')));
26+
27+
suiteUnit = TestSuite.fromFolder(fullfile('tests', 'unit'), 'IncludingSubfolders', true);
28+
suiteIntegration = TestSuite.fromFolder(fullfile('tests', 'integration'), 'IncludingSubfolders', true);
29+
suiteE2E = TestSuite.fromFolder(fullfile('tests', 'e2e'), 'IncludingSubfolders', true);
30+
31+
resultsUnit = runner.run(suiteUnit);
32+
resultsIntegration = runner.run(suiteIntegration);
33+
resultsE2E = runner.run(suiteE2E);
34+
allResults = [resultsUnit, resultsIntegration, resultsE2E];
35+
36+
summary.unit = summarizeResults(resultsUnit);
37+
summary.integration = summarizeResults(resultsIntegration);
38+
summary.e2e = summarizeResults(resultsE2E);
39+
summary.global = summarizeResults(allResults);
40+
summary.generatedAtUtc = char(datetime('now', 'TimeZone', 'UTC', 'Format', 'yyyy-MM-dd''T''HH:mm:ss''Z'''));
41+
summary.dataDir = dataDir;
42+
summary.reportDir = reportDir;
43+
44+
jsonText = jsonencode(summary, 'PrettyPrint', true);
45+
fid = fopen(fullfile(reportDir, 'matlab_test_summary.json'), 'w');
46+
fwrite(fid, jsonText, 'char');
47+
fclose(fid);
48+
49+
md = buildMarkdownReport(summary);
50+
fid = fopen(fullfile(reportDir, 'matlab_test_summary.md'), 'w');
51+
fwrite(fid, md, 'char');
52+
fclose(fid);
53+
54+
disp('=== MATLAB CI TEST SUMMARY ===');
55+
disp(md);
56+
57+
assertNoFailures(resultsUnit, 'Unit tests failed');
58+
assertNoFailures(resultsIntegration, 'Integration tests failed');
59+
assertNoFailures(resultsE2E, 'End-to-end tests failed');
60+
61+
end
62+
63+
function s = summarizeResults(results)
64+
if isempty(results)
65+
s.total = 0;
66+
s.passed = 0;
67+
s.failed = 0;
68+
s.incomplete = 0;
69+
s.durationSeconds = 0;
70+
s.passRate = 0;
71+
return;
72+
end
73+
74+
s.total = numel(results);
75+
s.passed = sum([results.Passed]);
76+
s.failed = sum([results.Failed]);
77+
s.incomplete = sum([results.Incomplete]);
78+
s.durationSeconds = sum([results.Duration]);
79+
s.passRate = s.passed / max(1, s.total);
80+
end
81+
82+
function txt = buildMarkdownReport(summary)
83+
txt = sprintf([ ...
84+
'# MATLAB CI Report\n\n' ...
85+
'- Generated (UTC): `%s`\n' ...
86+
'- Data directory: `%s`\n' ...
87+
'- Report directory: `%s`\n\n' ...
88+
'## Global\n' ...
89+
'- Total: **%d**\n' ...
90+
'- Passed: **%d**\n' ...
91+
'- Failed: **%d**\n' ...
92+
'- Incomplete: **%d**\n' ...
93+
'- Duration (s): **%.3f**\n' ...
94+
'- Pass rate: **%.2f%%**\n\n' ...
95+
'## Unit\n' ...
96+
'- Total: %d, Passed: %d, Failed: %d, Incomplete: %d, Duration: %.3fs\n' ...
97+
'## Integration\n' ...
98+
'- Total: %d, Passed: %d, Failed: %d, Incomplete: %d, Duration: %.3fs\n' ...
99+
'## End-to-end\n' ...
100+
'- Total: %d, Passed: %d, Failed: %d, Incomplete: %d, Duration: %.3fs\n'], ...
101+
summary.generatedAtUtc, summary.dataDir, summary.reportDir, ...
102+
summary.global.total, summary.global.passed, summary.global.failed, ...
103+
summary.global.incomplete, summary.global.durationSeconds, ...
104+
100 * summary.global.passRate, ...
105+
summary.unit.total, summary.unit.passed, summary.unit.failed, ...
106+
summary.unit.incomplete, summary.unit.durationSeconds, ...
107+
summary.integration.total, summary.integration.passed, summary.integration.failed, ...
108+
summary.integration.incomplete, summary.integration.durationSeconds, ...
109+
summary.e2e.total, summary.e2e.passed, summary.e2e.failed, ...
110+
summary.e2e.incomplete, summary.e2e.durationSeconds);
111+
end
112+
113+
function assertNoFailures(results, message)
114+
if isempty(results)
115+
return;
116+
end
117+
if any([results.Failed]) || any([results.Incomplete])
118+
error(message);
119+
end
120+
end

‎tests/ci/setup_test_path.m‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function setup_test_path()
2+
% Ensure src is on MATLAB path for CI and local test runs.
3+
4+
thisFile = mfilename('fullpath');
5+
testsDir = fileparts(fileparts(thisFile)); % .../tests
6+
repoRoot = fileparts(testsDir);
7+
srcDir = fullfile(repoRoot, 'src');
8+
9+
addpath(genpath(srcDir));
10+
setenv('MONALISA_REPO_ROOT', repoRoot);
11+
12+
end

‎tests/e2e/TestSyntheticDataE2E.m‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
classdef TestSyntheticDataE2E < matlab.unittest.TestCase
2+
% End-to-end test over diversified synthetic datasets.
3+
% This validates a complete mini-processing flow over multiple cases.
4+
5+
properties
6+
Cases
7+
end
8+
9+
methods (TestClassSetup)
10+
function addProjectPath(~)
11+
if exist('setup_test_path', 'file') ~= 2
12+
thisFile = mfilename('fullpath');
13+
testsDir = fileparts(fileparts(thisFile)); % .../tests
14+
addpath(fullfile(testsDir, 'ci'));
15+
end
16+
setup_test_path();
17+
end
18+
19+
function loadCases(testCase)
20+
dataDir = getenv('MONALISA_CI_DATA_DIR');
21+
if isempty(dataDir)
22+
repoRoot = getenv('MONALISA_REPO_ROOT');
23+
if isempty(repoRoot)
24+
thisFile = mfilename('fullpath');
25+
testsDir = fileparts(fileparts(thisFile)); % .../tests
26+
repoRoot = fileparts(testsDir);
27+
end
28+
dataDir = fullfile(repoRoot, 'temp', 'ci_data');
29+
end
30+
p = fullfile(dataDir, 'synthetic_cases.mat');
31+
testCase.assumeTrue(exist(p, 'file') == 2, ...
32+
sprintf('Missing synthetic dataset: %s', p));
33+
s = load(p, 'cases');
34+
testCase.Cases = s.cases;
35+
end
36+
end
37+
38+
methods (Test)
39+
function testMiniPipelineOverAllCases(testCase)
40+
cases = testCase.Cases;
41+
testCase.verifyGreaterThan(numel(cases), 5);
42+
43+
metrics = zeros(1, numel(cases));
44+
for i = 1:numel(cases)
45+
c = cases(i);
46+
x = bmPointReshape(c.raw, c.nCh);
47+
x2 = bmMult(x, conj(x));
48+
e = bmSquaredNorm(x2, c.scalarWeight);
49+
metrics(i) = e;
50+
end
51+
52+
testCase.verifyTrue(all(isfinite(metrics)));
53+
testCase.verifyTrue(all(metrics >= 0));
54+
testCase.verifyGreaterThan(std(metrics), 0);
55+
end
56+
end
57+
end

0 commit comments

Comments
 (0)