diff --git a/+PlotID/runner_linux.sh b/+PlotID/runner_linux.sh new file mode 100644 index 0000000000000000000000000000000000000000..e5d3f9a9d8ec12bb1eb2155d5250c42d1fd4599e --- /dev/null +++ b/+PlotID/runner_linux.sh @@ -0,0 +1,14 @@ +#!/bin/bash + +matlab -nodisplay -nodesktop -r "CI_files/failrunner;exit(ans)" + +exitstatus=$? +if [[$exitstatus -eq '0']] +then + echo "matlab succeed. Exitstatus: $exitstatus" + exit $exitstatus +else + echo "matlab failed. Exitstatus: $exitstatus" + exit $exitstatus + +fi \ No newline at end of file diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 7892033b84d6d432c1c7f6e18ff3450a2e17998b..938d8bc80d5bdeff95757803ef2d80a778269d1e 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -5,5 +5,16 @@ Test Code: stage: Run tags: - matlab + - bash script: - - ./CI_files/runtest.ps1 + - cd ./CI_files + - chmod +x ./runner_linux.sh + - ./runner_linux.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 bb4949820ef43aedf2f4e9c67919c8a9a67f62f0..6acb781c186427727d9fe41457a7f3ef0aff6416 100644 --- a/CI_files/default_test.m +++ b/CI_files/default_test.m @@ -4,28 +4,45 @@ function [result] = default_test() clear; clc; close all; +% set path +% starting path of gitlab runner is in CI_files +if contains(pwd,'CI_files') + cd .. % move one directory up +end + % clean up, if previous run failed try - delete CI_files/export/* CI_files/*.mat CI_files/*.h5 - rmdir('CI_files/export','s'); + delete(['CI_files' filesep 'export' filesep '*']); + delete(['CI_files' filesep '*.mat']); + delete(['CI_files' filesep '*.h5']); + rmdir(['CI_files' filesep 'export'],'s'); end +% initialise +numberOfTests = 2; +testResults = zeros(numberOfTests,1); + +%start log +fid = fopen(fullfile('CI_files','log.txt'),'w'); +txt = ['default test started ' newline]; +fprintf(fid,txt); + + try ProjectID = 'Test01'; %% Data % some random data x = linspace(0,7); y = rand(1,100)+2; - dataset1 = 'test_data.mat'; - pwd - save('CI_files/test_data.mat','x','y'); + dataset1 = fullfile('CI_files','test_data.mat'); + save(dataset1,'x','y'); % some data as .h5 x1 = linspace(0,2*pi); y1 = sin(x1)+2; % define file path & name - fpath = "CI_files/testdata2.h5"; - dataset2 = 'CI_files/testdata2.h5'; + fpath = fullfile("CI_files","testdata2.h5"); + dataset2 = fullfile('CI_files','testdata2.h5'); % create hdf5 file and dataset > write data to hdf5 file / dataset h5create(fpath, "/x1", size(x1), "Datatype", class(x1)) @@ -39,19 +56,22 @@ try plot(x,y,'-k'); hold on plot(x1,y1,'-r'); + msg = 'simple_test succeed stage 1'; catch - result = false; - warning('simple_test failed in Stage 1'); - exit; + testResults(1) = 1; %test fails + msg = 'simple_test failed in Stage 1'; + warning(msg); end +fprintf(fid,[msg newline]); + %% Tag the plot try [figs, ID] = PlotID.TagPlot(fig,'ProjectID', ProjectID); %% call a dummy function - a=1; - a = example_fcn(a); + %a=1; + %a = example_fcn(a); %% publishing @@ -63,19 +83,33 @@ try rdata = {dataset1,dataset2} ; % don't forget the extension PlotID.Publish(rdata,script, figs, 'Location', 'CI-Test') - result = true; - - % clean up - delete CI_files/export/* CI_files/*.mat CI_files/*.h5 - rmdir('CI_files/export','s'); - - clc; + msg = 'simple_test succeed Stage 2'; catch - result = false; - warning('simple_test failed in Stage 2'); + testResults(2) = 1; %fail + msg = 'simple_test failed in Stage 2'; + warning(msg); +end +fprintf(fid,[msg newline]); %log + +%% 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 'export' filesep '*']); + delete(['CI_files' filesep '*.mat']); + delete(['CI_files' filesep '*.h5']); + rmdir(['CI_files' filesep 'export'],'s'); +end end diff --git a/CI_files/fail_runner.m b/CI_files/fail_runner.m new file mode 100644 index 0000000000000000000000000000000000000000..e9071dd65411802d16b925aca9ca35c5df933ea4 --- /dev/null +++ b/CI_files/fail_runner.m @@ -0,0 +1,8 @@ +function [retVal] = fail_runner() +%RUNNER_TESTING testing function to test, if the runner is set up properly + retVal = 1; % test should fail ! + fid = fopen(fullfile('log.txt'),'w'); + txt = ['This is a test, Errorstate: ', num2str(retVal)]; + fprintf(fid,txt); + fclose(fid); +end diff --git a/CI_files/pass_runner.m b/CI_files/pass_runner.m new file mode 100644 index 0000000000000000000000000000000000000000..957268576f9bd1f4bdc89fb2cbcb69428d8cd229 --- /dev/null +++ b/CI_files/pass_runner.m @@ -0,0 +1,8 @@ +function [retVal] = pass_runner() +%pass_runner testing function to test, if the runner is set up properly + retVal = 0; % test should succeed ! + fid = fopen(fullfile('log.txt'),'w'); + txt = ['This is a test, Errorstate: ', num2str(retVal)]; + fprintf(fid,txt); + fclose(fid); +end diff --git a/CI_files/runner_linux.sh b/CI_files/runner_linux.sh new file mode 100644 index 0000000000000000000000000000000000000000..f64784cf471314482b52f05ff6f80a39067a367a --- /dev/null +++ b/CI_files/runner_linux.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +#matlab -r "disp(['Current folder: ' pwd])" +matlab $@ -nodisplay -nodesktop -nosplash -logfile matlab_log.txt -r "default_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/CI_files/runner_test.m b/CI_files/runner_test.m deleted file mode 100644 index ea47e815fd9601b34e20516b760296dbbfc8312a..0000000000000000000000000000000000000000 --- a/CI_files/runner_test.m +++ /dev/null @@ -1,14 +0,0 @@ -%Test 1 -try - resultt = runner_testing -catch - warning("Error in Test1"); -end -exit(resultt) - -function [result] = runner_testing() -%RUNNER_TESTING testing function to test, if the runner is set up properly - result = true; - disp(pwd); - result = version; disp(result); result = isempty(result); disp(result); result = int8(result); disp(result); pause(5); -end diff --git a/CI_files/runtest.ps1 b/CI_files/runtest.ps1 index aacf5b692ba1656b89e0e33535df8273c05e8df3..15d17374764ae708af434eabcfa372828f527be0 100644 --- a/CI_files/runtest.ps1 +++ b/CI_files/runtest.ps1 @@ -5,7 +5,7 @@ $wd = pwd; $LOGFILE = $("$wd" + "\" + "$LOGFILE") $CIfolder = "$wd" + "\" + "CI_files\" # $MFILE='"C:\git\NFDI4ing\plot_ID_matlab\CI_files\runner_test.m"' -$arguments = "-nodesktop", "-nosplash","-minimize","-wait","-sd", "$wd", "-logfile", "$LOGFILE","-batch","CI_files\default_test" +$arguments = "-nodesktop", "-nosplash","-minimize","-wait","-sd", "$wd", "-logfile", "$LOGFILE","-batch","CI_files\test_runner_STFS.m" $Returnvalue = & 'C:\Program Files\MATLAB\R2021b\bin\matlab.exe' $arguments #$CODE = $?