From f8d9ed5defa04da7525acfad771a12caed61374f Mon Sep 17 00:00:00 2001 From: Hugo Oliveira Date: Wed, 22 Jan 2020 11:57:46 +1100 Subject: [PATCH 1/5] feat(tests): add batchTesting and scripts This commit support running all the tests in "batch" mode. This involved creating a function to manually setup the paths (setToolboxPaths), a batchTesting function to output the result of tests and shell/cmd scripts to run all tests. --- Util/Path/setToolboxPaths.m | 71 +++++++++++++++++++++ batchTesting.m | 121 ++++++++++++++++++++++++++++++++++++ runalltests.bat | 5 ++ runalltests.sh | 2 + 4 files changed, 199 insertions(+) create mode 100644 Util/Path/setToolboxPaths.m create mode 100644 batchTesting.m create mode 100755 runalltests.bat create mode 100755 runalltests.sh diff --git a/Util/Path/setToolboxPaths.m b/Util/Path/setToolboxPaths.m new file mode 100644 index 000000000..a03213d6a --- /dev/null +++ b/Util/Path/setToolboxPaths.m @@ -0,0 +1,71 @@ +function setToolboxPaths(toolbox_path) +% function setToolboxPaths(toolbox_path) +% +% Add all the folders of the toolbox to the search +% path. +% +% Inputs: +% +% toolbox_path - the root path of the IMOS toolbox +% +% Outputs: +% +% Example: +% +% setToolboxPaths('/home/joe/imos-toolbox') +% assert(exist('detectType.m','file')) +% +% author: hugo.oliveira@utas.edu.au +% + +% Copyright (C) 2020, Australian Ocean Data Network (AODN) and Integrated +% Marine Observing System (IMOS). +% +% This program is free software: you can redistribute it and/or modify +% it under the terms of the GNU General Public License as published by +% the Free Software Foundation version 3 of the License. +% +% This program is distributed in the hope that it will be useful, +% but WITHOUT ANY WARRANTY; without even the implied warranty of +% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +% GNU General Public License for more details. +% +% You should have received a copy of the GNU General Public License +% along with this program. +% If not, see . +% + +% This will add all required paths for the toolbox to run +folders = { +'AutomaticQC', ... +'Seawater/TEOS10', ... +'Seawater/TEOS10/library', ... +'Seawater/EOS80', ... +'Util', ... +'Util/CellUtils', ... +'Util/StructUtils', ... +'Util/Path', ... +'Util/Schema', ... +'Util/units', ... +'IMOS', ... +'NetCDF', ... +'Parser', ... +'Parser/GenericParser', ... +'Parser/GenericParser/InstrumentRules', ... +'Parser/GenericParser/InstrumentParsers', ... +'test', ... +'test/Parser', ... +'test/Preprocessing', ... +'test/Util', ... +'test/Util/Schema', ... +'test/Util/CellUtils', ... +'DDB', ... +'Preprocessing'}; + +addpath(toolbox_path) + +for k = 1:length(folders) + addpath(fullfile(toolbox_path,folders{k})); +end + +end diff --git a/batchTesting.m b/batchTesting.m new file mode 100644 index 000000000..44de3beac --- /dev/null +++ b/batchTesting.m @@ -0,0 +1,121 @@ +function batchTesting(parallel) +% function batchTesting(parallel) +% +% Execute all the xunit Test functions of the toolbox +% in batch mode. +% +% Inputs: +% +% parallel - true for parallel execution +% +% Example: +% +% batchTesting(1) +% +% author: hugo.oliveira@utas.edu.au +% + +% Copyright (C) 2020, Australian Ocean Data Network (AODN) and Integrated +% Marine Observing System (IMOS). +% +% This program is free software: you can redistribute it and/or modify +% it under the terms of the GNU General Public License as published by +% the Free Software Foundation version 3 of the License. +% +% This program is distributed in the hope that it will be useful, +% but WITHOUT ANY WARRANTY; without even the implied warranty of +% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +% GNU General Public License for more details. +% +% You should have received a copy of the GNU General Public License +% along with this program. +% If not, see . +% + +root_folder = pwd; +addpath([root_folder filesep 'Util' filesep 'Path']) +setToolboxPaths(root_folder) + +try + results = runAllTests(parallel) %do not supress report output +catch + error('Tests failed') +end + +failed = {' Tests failed:\n'}; +c = 2; + +for k = 1:length(results) + ktest = results(k); + + if ~ktest.Passed + fparam = extract_param(ktest.Name); + fnames = fieldnames(fparam); + fnames = fnames(2:end); + fstr = [repmat(' ',1,length(failed{1})) fparam.testname ':\n']; + lfstr = length(fstr) + for kk = 1:length(fnames) + fstr = [fstr repmat(' ',1,lfstr) fnames{kk} '->' fparam.(fnames{kk}) '\n']; + end + fstr = [fstr '\n']; + failed{c} = fstr; + c = c + 1; + end + +end + +if length(failed) > 1 + error(sprintf(strjoin(failed))) +end + +end + +function params = extract_param(namestr) +% function extract_param(namestr) +% +% Extract the name parameters used in the test +% based on the name of the test. +% +% Inputs: +% +% namestr - the name of the test in the TestResult array. +% +% Example: +% +% testname='mytest(myparam='abc',myparam2='cba',folder='p__home_user_S'); +% params=extract_param(namestr); +% assert(strcmpi(params.testname,'mytest')) +% assert(strcmpi(params.myparam,'abc')) +% assert(strcmpi(params.myparam2,'cba')) +% assert(strcmpi(params.folder='/home/user')) +% +% author: hugo.oliveira@utas.edu.au + +params = struct(); + +tmp = strsplit(namestr, '('); +tmp2 = strsplit(tmp{1}, '/'); +params.testname = tmp2{end}; + +pcell = strsplit(namestr, ','); + +tmp = strsplit(pcell{1}, '('); +tmp2 = strsplit(tmp{end}, '='); +pname = tmp2{1}; +pval = tmp2{2}; +params.(pname) = pval; + +for k = 2:length(pcell) - 1 + tmp = strsplit(pcell{k}, '='); + pname = tmp{1}; + pval = tmp{2}; + params.(pname) = pval; +end + +tmp = strsplit(pcell{end}, ')'); +tmp2 = strsplit(tmp{1}, '='); +pname = tmp2{1}; +pval = tmp2{2}; +params.(pname) = pval; + +end diff --git a/runalltests.bat b/runalltests.bat new file mode 100755 index 000000000..ec27381eb --- /dev/null +++ b/runalltests.bat @@ -0,0 +1,5 @@ +@echo off +echo "Loading the IMOS-Toolbox Tests..." +matlab -nosplash -nodesktop -r "batchTesting(0)" +echo "Please check the tests reports in the command line window..." +@pause diff --git a/runalltests.sh b/runalltests.sh new file mode 100755 index 000000000..89a2e7e80 --- /dev/null +++ b/runalltests.sh @@ -0,0 +1,2 @@ +#!/usr/bin/env sh +matlab -nosplash -nodesktop -r "batchTesting(0);exit" From f11d91d8ab06f8fb6947a797a9dff889c213c30d Mon Sep 17 00:00:00 2001 From: Hugo Oliveira Date: Wed, 22 Jan 2020 12:49:45 +1100 Subject: [PATCH 2/5] feat(tests): abort batchTesting if missing files --- runalltests.bat | 5 ++++- runalltests.sh | 7 ++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/runalltests.bat b/runalltests.bat index ec27381eb..4244ab31e 100755 --- a/runalltests.bat +++ b/runalltests.bat @@ -1,5 +1,8 @@ @echo off +if exist data ( echo "Loading the IMOS-Toolbox Tests..." matlab -nosplash -nodesktop -r "batchTesting(0)" -echo "Please check the tests reports in the command line window..." +echo "Please check the tests reports in the command line window...") else ( +echo "Cannot run tests - please obtain all test files first with the get_testfiles.py script." +) @pause diff --git a/runalltests.sh b/runalltests.sh index 89a2e7e80..b886d2585 100755 --- a/runalltests.sh +++ b/runalltests.sh @@ -1,2 +1,7 @@ #!/usr/bin/env sh -matlab -nosplash -nodesktop -r "batchTesting(0);exit" +if [ -d "data" ]; then + matlab -nosplash -nodesktop -r "batchTesting(0);exit" +else + echo "Cannot run tests - please obtain all test files first with the get_testfiles.py script." + exit 1 +fi From acced2e3872fd8d278208c35c8a0e4dbf0bcb10e Mon Sep 17 00:00:00 2001 From: Hugo Oliveira Date: Tue, 28 Jan 2020 17:38:04 +1100 Subject: [PATCH 3/5] feat(review): update docstring --- test/runAllTests.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/runAllTests.m b/test/runAllTests.m index 3d165d951..6dbd858eb 100644 --- a/test/runAllTests.m +++ b/test/runAllTests.m @@ -15,7 +15,7 @@ % % % Example: -% runAllTests(4); +% runAllTests(1); % % author: hugo.oliveira@utas.edu.au % From ef48dc2803b0816259178540be73fdc3e1e5a57c Mon Sep 17 00:00:00 2001 From: Hugo Oliveira Date: Tue, 28 Jan 2020 17:43:17 +1100 Subject: [PATCH 4/5] feat(review): improve linting and add comments in the for loop --- batchTesting.m | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/batchTesting.m b/batchTesting.m index 44de3beac..eace90fba 100644 --- a/batchTesting.m +++ b/batchTesting.m @@ -37,7 +37,7 @@ function batchTesting(parallel) setToolboxPaths(root_folder) try - results = runAllTests(parallel) %do not supress report output + results = runAllTests(parallel)%do not supress report output catch error('Tests failed') end @@ -45,20 +45,25 @@ function batchTesting(parallel) failed = {' Tests failed:\n'}; c = 2; -for k = 1:length(results) - ktest = results(k); +%pretty print the results of failed tests. +for k = 1:length(results)% loop through all results + ktest = results(k); %get k-results if ~ktest.Passed - fparam = extract_param(ktest.Name); - fnames = fieldnames(fparam); - fnames = fnames(2:end); - fstr = [repmat(' ',1,length(failed{1})) fparam.testname ':\n']; + fparam = extract_param(ktest.Name); % extract all test parameters + fnames = fieldnames(fparam); %get parameter names + fnames = fnames(2:end); % remove the testname from the list + + %now create a pretty string + fstr = [repmat(' ', 1, length(failed{1})) fparam.testname ':\n']; lfstr = length(fstr) + for kk = 1:length(fnames) - fstr = [fstr repmat(' ',1,lfstr) fnames{kk} '->' fparam.(fnames{kk}) '\n']; + fstr = [fstr repmat(' ', 1, lfstr) fnames{kk} '->' fparam.(fnames{kk}) '\n']; end + fstr = [fstr '\n']; - failed{c} = fstr; + failed{c} = fstr; % store the string for subsequent failed tests if any c = c + 1; end @@ -82,8 +87,8 @@ function batchTesting(parallel) % % Example: % -% testname='mytest(myparam='abc',myparam2='cba',folder='p__home_user_S'); -% params=extract_param(namestr); +% testname_str='mytest(myparam='abc',myparam2='cba',folder='p__home_user_S'); +% params=extract_param(testname_str); % assert(strcmpi(params.testname,'mytest')) % assert(strcmpi(params.myparam,'abc')) % assert(strcmpi(params.myparam2,'cba')) @@ -94,7 +99,7 @@ function batchTesting(parallel) params = struct(); tmp = strsplit(namestr, '('); -tmp2 = strsplit(tmp{1}, '/'); +tmp2 = strsplit(tmp{1}, fileSep); params.testname = tmp2{end}; pcell = strsplit(namestr, ','); From 966bbdd8471fed395ca0887877f21b5d05d4668b Mon Sep 17 00:00:00 2001 From: Hugo Oliveira Date: Tue, 28 Jan 2020 17:59:46 +1100 Subject: [PATCH 5/5] feat(review): remove pwd --- batchTesting.m | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/batchTesting.m b/batchTesting.m index eace90fba..ea7c37938 100644 --- a/batchTesting.m +++ b/batchTesting.m @@ -32,7 +32,8 @@ function batchTesting(parallel) % If not, see . % -root_folder = pwd; +csplit = strsplit(mfilename('fullpath'),[ filesep 'batchTesting']); +root_folder = csplit{1}; addpath([root_folder filesep 'Util' filesep 'Path']) setToolboxPaths(root_folder)