From bfb8dfd58fb42583c5c070f4057be661050afeb4 Mon Sep 17 00:00:00 2001 From: "Lemmer, Jan" <jan.lemmer@fst.tu-darmstadt.de> Date: Thu, 24 Feb 2022 16:21:01 +0100 Subject: [PATCH] implement CI tests --- +PlotID/@config/config.m | 2 +- +PlotID/Publish.m | 2 +- +PlotID/copyUserFCN.m | 4 +- .gitlab-ci.yml | 24 ++++++++-- CI_files/default_test.m | 2 - CI_files/example_test.m | 88 +++++++++++++++++++++++++++++++++++ CI_files/run_example_test.sh | 15 ++++++ Examples/PlotID_QRcode.m | 4 +- Examples/PlotID_advanced.m | 2 +- Examples/PlotID_centralized.m | 2 +- Examples/PlotID_variables.m | 4 +- Examples/clearEnv.m | 9 ++++ PlotID_example.m | 2 +- 13 files changed, 144 insertions(+), 16 deletions(-) create mode 100644 CI_files/example_test.m create mode 100644 CI_files/run_example_test.sh create mode 100644 Examples/clearEnv.m diff --git a/+PlotID/@config/config.m b/+PlotID/@config/config.m index b9b4779..c29d213 100644 --- a/+PlotID/@config/config.m +++ b/+PlotID/@config/config.m @@ -22,7 +22,7 @@ classdef config < handle % wizard is started by the catch block obj.configFileName = configFileName; try %Validity Check - tmp = what("PlotID"); + tmp = what("+PlotID"); tmp = strrep(tmp.path,'+PlotID',''); defaultConfigPath = fullfile(tmp,obj.configFileName); if isfile(defaultConfigPath) diff --git a/+PlotID/Publish.m b/+PlotID/Publish.m index 909dc03..85a2b3d 100644 --- a/+PlotID/Publish.m +++ b/+PlotID/Publish.m @@ -57,7 +57,7 @@ end % find scriptpath if ~isfile(scriptPath) - scriptPath= which(['/',scriptPath]); + scriptPath= which(scriptPath); if ~isfile(scriptPath) msg = ['Script path: ',scriptPath, ' not found on matlab path', newline, ... '- check spelling or us a path']; diff --git a/+PlotID/copyUserFCN.m b/+PlotID/copyUserFCN.m index cabf490..eaa291d 100644 --- a/+PlotID/copyUserFCN.m +++ b/+PlotID/copyUserFCN.m @@ -36,8 +36,8 @@ function [fListClean] = removePltIdFiles(fList) PltID_classlist = packageContent.classes; % Class Methods need to be listed in an additional function - Class_flist = cell(1,numel(packageContent.classes)); %preallocate - for i=1:numel(packageContent.classes) + Class_flist = cell(1,numel(PltID_classlist)); %preallocate + for i=1:numel(PltID_classlist) temp = what(['PlotID',filesep,'@',PltID_classlist{i}]); Class_flist{i} = temp.m; end diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 938d8bc..45f0a37 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,8 +1,8 @@ #Based on STFS Workshop stages: - - Run -Test Code: - stage: Run + - Testing +Default Test: + stage: Testing tags: - matlab - bash @@ -18,3 +18,21 @@ Test Code: when: always expire_in: 1 week + +Example Test: + stage: Testing + tags: + - matlab + - bash + script: + - cd ./CI_files + - chmod +x ./run_example_test.sh + - ./run_example_test.sh + - cat ./log.txt + artifacts: + paths: + - ./CI_files/log.txt + - ./CI_files/matlab_log.txt + + when: always + expire_in: 1 week diff --git a/CI_files/default_test.m b/CI_files/default_test.m index ae9826f..cee8abc 100644 --- a/CI_files/default_test.m +++ b/CI_files/default_test.m @@ -2,8 +2,6 @@ function [result] = default_test() %default_test() This is a simple test if Plot ID works for the default settings % Detailed explanation goes here -clear; clc; close all; - % set path % starting path of gitlab runner is in CI_files if contains(pwd,'CI_files') diff --git a/CI_files/example_test.m b/CI_files/example_test.m new file mode 100644 index 0000000..1a07617 --- /dev/null +++ b/CI_files/example_test.m @@ -0,0 +1,88 @@ +function [result] = example_test() +% example This is a test if all PlotID example works run through + +% prevent function to be affected by clear commands in examples +ciTest = true; + +% set path +% starting path of gitlab runner is in CI_files +if contains(pwd,'CI_files') + cd .. % move one directory up +end + +addpath('CI_files','Examples'); + +% clean up, if previous run failed +try + delete(['CI_files' filesep 'PlotID_export' filesep '*']); + delete(['CI_files' filesep '*.mat']); + delete(['CI_files' filesep '*.h5']); + rmdir(['CI_files' filesep 'PlotID_export'],'s'); +end + + +exampleList = {'PlotID_example','PlotID_advanced',... + 'PlotID_QRcode', 'PlotID_variables' }; + + +% initialise +numberOfTests = numel(exampleList); +testResults = zeros(numberOfTests,1); + +%start log +fid = fopen(fullfile('CI_files','log.txt'),'w'); +txt = ['default test started ' newline]; +fprintf(fid,txt); + +% create Config for CI-Tests +fid1 = fopen(fullfile('CI_files','config.json'),'w'); +configData.Author = 'CI-Test'; configData.ProjectID = 'CI-001'; +txt = jsonencode(configData,'PrettyPrint',true); +%fprintf does not write paths correctly !!! +fwrite(fid1,txt); +fclose(fid1); + +%% Testing Loop +for i=1:numberOfTests + clearvars -except i fid exampleList testResults + try + runExample(exampleList{i}); + msg = ['example_test succeed example ', exampleList{i}]; + catch + testResults(1) = 1; %test fails + msg = ['example_test failed at example', exampleList{i}]; + warning(msg); + end + + fprintf(fid,[msg, newline]); +end + + +%% Test result +if any(testResults) + result = 1; %fail + warning('test failed'); +else + result = 0; % pass + disp('test succeed'); +end + +fclose(fid); + +% final clean up +try + delete(['CI_files' filesep 'PlotID_export' filesep '*']); + delete(['CI_files' filesep '*.mat']); + delete(['CI_files' filesep '*.h5']); + rmdir(['CI_files' filesep 'PlotID_export'],'s'); + delete(fullfile('CI_files','CI_config.json')); +end + +end + +function runExample(scriptName) +% runExample prevents scripts to infulence the CI script + run(scriptName); + +end + diff --git a/CI_files/run_example_test.sh b/CI_files/run_example_test.sh new file mode 100644 index 0000000..73d0907 --- /dev/null +++ b/CI_files/run_example_test.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +#matlab -r "disp(['Current folder: ' pwd])" +matlab $@ -nodisplay -nodesktop -nosplash -logfile matlab_log.txt -r "example_test;exit(ans);" + +exitstatus=$? +if [[ $exitstatus -eq '0' ]] +then + echo "matlab succeed. Exitstatus: $exitstatus" + exit $exitstatus +else + echo "matlab failed. Exitstatus: $exitstatus" + exit $exitstatus + +fi diff --git a/Examples/PlotID_QRcode.m b/Examples/PlotID_QRcode.m index c330898..41bc591 100644 --- a/Examples/PlotID_QRcode.m +++ b/Examples/PlotID_QRcode.m @@ -1,10 +1,10 @@ -clear; close all; clc; +clearEnv; %% Tag the plot with a QR code (experimental) % here the work flow for tagging the plot with a QR code is shown % the content of the qr code is the ID % plots and data -fig(1) = figure; +fig = figure; [x1, y1, datapath1] = createExampleData('matlab'); plot(x1,y1,'-b'); box off; hold on; set(gca, 'TickDir', 'out', 'YLim', [0,4]); diff --git a/Examples/PlotID_advanced.m b/Examples/PlotID_advanced.m index 310e428..d6fead3 100644 --- a/Examples/PlotID_advanced.m +++ b/Examples/PlotID_advanced.m @@ -1,4 +1,4 @@ -clear; close all; clc; +clearEnv; %% Tag and export multiple plots % here the work flow for tagging and exporting multiple plots is shown diff --git a/Examples/PlotID_centralized.m b/Examples/PlotID_centralized.m index 921917e..025dbbf 100644 --- a/Examples/PlotID_centralized.m +++ b/Examples/PlotID_centralized.m @@ -1,4 +1,4 @@ -clear; close all; clc; +clearEnv; %% multiple plots from the same data-set (centralized method) % A central data folder is used for saving the research data files, the % subfolders contain linked hdf5-files (if hdf5 is used) otherwise the data diff --git a/Examples/PlotID_variables.m b/Examples/PlotID_variables.m index 1d34b74..28d2505 100644 --- a/Examples/PlotID_variables.m +++ b/Examples/PlotID_variables.m @@ -1,7 +1,7 @@ -clear; clc; close all; +clearEnv; %% Example Script - How to pass Variables % This script how to pass variables instead or additional to the DataPaths - +addpath('Examples'); %% Data (only necessary for this example) [x, y, datapath] = createExampleData('matlab'); scriptPath = mfilename('fullpath'); diff --git a/Examples/clearEnv.m b/Examples/clearEnv.m new file mode 100644 index 0000000..65c5ea2 --- /dev/null +++ b/Examples/clearEnv.m @@ -0,0 +1,9 @@ +function [] = clearEnv() +%CLEARENV clears Environment with exception for CI testing. + +if ~exist('ciTest', 'var') + clear; close all; clc; +end + +end + diff --git a/PlotID_example.m b/PlotID_example.m index 33deaf1..feecb29 100644 --- a/PlotID_example.m +++ b/PlotID_example.m @@ -3,7 +3,7 @@ % please run Initilisation.m before first time use %% Clear and set environment -clear; clc; close all; +clearEnv; % contains clear clc, close all addpath('Examples'); %% Data (only necessary for this example) -- GitLab