Commit 1ca7c8a3 authored by Emmanuel Promayon's avatar Emmanuel Promayon
Browse files

FIXED clean code + more tests

Testing lots of bad arguments handling
parent dfd4fa17
......@@ -7,9 +7,49 @@ camitk_application(DEFAULT
DESCRIPTION "Create CEP and file skeletons from XML files (no GUI)"
)
#---------------------------------
# Testing command-line robustness
#---------------------------------
set(TEST_BASENAME ${APPLICATION_TARGET_NAME})
camitk_init_test(${TEST_BASENAME})
# should pass because invoking cepgenerator without arguments or with help arg shows usage and exit success
camitk_add_test(PROJECT_NAME ${TEST_BASENAME} TEST_SUFFIX "-")
camitk_add_test(EXECUTABLE_ARGS "--help" PROJECT_NAME ${TEST_BASENAME} TEST_SUFFIX "-")
camitk_add_test(EXECUTABLE_ARGS "-h" PROJECT_NAME ${TEST_BASENAME} TEST_SUFFIX "-")
# should pass because invoking cepgenerator with a faulty arguments results in printing
# an "Argument error" message (and exit failure)
camitk_add_test(EXECUTABLE_ARGS "-badarg"
PASS_REGULAR_EXPRESSION "unknown option '-badarg'"
PROJECT_NAME ${TEST_BASENAME} TEST_SUFFIX "-")
camitk_add_test(EXECUTABLE_ARGS "-f missingdir"
PASS_REGULAR_EXPRESSION "Argument error: please provide an output directory."
PROJECT_NAME ${TEST_BASENAME} TEST_SUFFIX "-")
camitk_add_test(EXECUTABLE_ARGS "-d missingfile"
PASS_REGULAR_EXPRESSION "Argument error: please provide an input XML file."
PROJECT_NAME ${TEST_BASENAME} TEST_SUFFIX "-")
camitk_add_test(EXECUTABLE_ARGS "-f badfile -d baddir"
PASS_REGULAR_EXPRESSION "File badfile does not exist or is not a file"
PROJECT_NAME ${TEST_BASENAME} TEST_SUFFIX "-")
camitk_add_test(EXECUTABLE_ARGS "-f badfile -d ${CMAKE_CURRENT_BINARY_DIR}"
PASS_REGULAR_EXPRESSION "File badfile does not exist or is not a file"
PROJECT_NAME ${TEST_BASENAME} TEST_SUFFIX "-")
camitk_add_test(EXECUTABLE_ARGS "-f ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp -d ${CMAKE_CURRENT_BINARY_DIR}"
PASS_REGULAR_EXPRESSION "instance document parsing failed."
PROJECT_NAME ${TEST_BASENAME} TEST_SUFFIX "-")
camitk_add_test(EXECUTABLE_ARGS "-f ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp -d baddir"
PASS_REGULAR_EXPRESSION "Path baddir/ is not a directory"
PROJECT_NAME ${TEST_BASENAME} TEST_SUFFIX "-")
# Add config test
find_program (BASH_PROGRAM bash)
if (BASH_PROGRAM)
# use "ctest -VV -R cepgenerator-test" to run just this test
add_test(cepgenerator-test ${BASH_PROGRAM} ${CMAKE_CURRENT_SOURCE_DIR}/resources/cepgenerator-test.sh -inbuild ${PROJECT_BINARY_DIR} ${PROJECT_SOURCE_DIR})
add_test(cepgenerator-test-shell ${BASH_PROGRAM} ${CMAKE_CURRENT_SOURCE_DIR}/resources/cepgenerator-test.sh -inbuild ${PROJECT_BINARY_DIR} ${PROJECT_SOURCE_DIR})
endif (BASH_PROGRAM)
......@@ -39,17 +39,17 @@ const char* description = "Please visit http://camitk.imag.fr for more informati
"camitk-cepgenerator aims at creating CamiTK Extension Project skeleton from\n"
"a description given in a XML file (valid CamiTK cepcoreschema language).";
// usage for this application
void usage(char* appName) {
std::cerr << appName << std::endl;
std::cerr << std::endl;
std::cerr << "Usage: " << appName << " [options]" << std::endl;
std::cerr << "Built using " << camitk::Core::version << std::endl;
void usage(std::string msg = "") {
if (msg != "") {
std::cout << msg << std::endl;
}
std::cerr << "Usage: camitk-cepgenerator [options]" << std::endl;
std::cerr << "Build using " << camitk::Core::version << std::endl;
std::cerr << std::endl;
std::cout << description << std::endl;
std::cout << std::endl;
std::cerr << "Options:" << endl;
options::print_usage(cerr);
std::cerr << "Options:" << std::endl;
options::print_usage(std::cerr);
}
/**
......@@ -60,31 +60,30 @@ void usage(char* appName) {
*
*/
int main(int argc, char* argv[]) {
std::exception_ptr otherException;
try {
int end; // End of options.
options o(argc, argv, end);
// if specific help or no options provided
if (o.help()) {
usage(argv[0]);
if (o.help() || (o.file().empty() && o.directory().empty())) {
usage();
return EXIT_SUCCESS;
}
else {
// print the CamiTK version
if (o.version()) {
std::cout << argv[0] << " build using " << camitk::Core::version << std::endl;
std::cout << "camitk-cepgenerator build using " << camitk::Core::version << std::endl;
return EXIT_SUCCESS;
}
else {
// file and directory are mandatory options!
if (o.file().empty()) {
std::cout << "Argument error: Please provide an input XML file" << std::endl << std::endl;
usage(argv[0]);
usage("Argument error: please provide an input XML file.");
return EXIT_FAILURE;
}
else if (o.directory().empty()) {
std::cout << "Argument error: Please provide an output directory" << std::endl << std::endl;
usage(argv[0]);
usage("Argument error: please provide an output directory.");
return EXIT_FAILURE;
}
else {
......@@ -97,16 +96,28 @@ int main(int argc, char* argv[]) {
}
}
catch (const cli::exception& e) {
std::cerr << e << std::endl;
usage(argv[0]);
std::cerr << "camitk-cepgenerator aborted due to invalid arguments: " << e.what() << "." << std::endl;
e.print(std::cerr);
std::cerr << std::endl;
usage();
return EXIT_FAILURE;
}
catch (const std::exception& e) {
std::cout << argv[0] << " aborted by std exception: " << e.what() << std::endl;
std::cerr << "camitk-cepgenerator aborted by std exception: " << e.what() << "." << std::endl;
return EXIT_FAILURE;
}
catch (...) {
std::cout << argv[0] << " aborted by unknown exception" << std::endl;
std::cerr << "camitk-cepgenerator aborted by unknown exception" << std::endl;
otherException = std::current_exception();
try {
if (otherException) {
std::rethrow_exception(otherException);
}
}
catch (const std::exception& e) {
std::cerr << ": " << e.what() << ".";
}
std::cerr << std::endl;
return EXIT_FAILURE;
}
......
......@@ -764,7 +764,7 @@ testcepfile() {
# --------------------------------------------------------------------------
testcepfile cep.xml nrOfExcpectedNewExtensions
#testcepfile cep.xml nrOfExcpectedNewExtensions
testcepfile complete-test-1.xml 3 testlib
testcepfile exampleComponents.xml 2
testcepfile actionsExamplesLicence.xml 2
......
......@@ -16,5 +16,5 @@ set(SHIBOKEN_CAMITK_SDK_PATH ${SHIBOKEN_CAMITK_SDK_PATH}:${CMAKE_CURRENT_SOURCE_
# Add config test
find_program (BASH_PROGRAM bash)
if (BASH_PROGRAM)
add_test(config-test ${BASH_PROGRAM} ${CMAKE_CURRENT_SOURCE_DIR}/resources/config-test.sh -inbuild ${PROJECT_BINARY_DIR})
add_test(config-test-shell ${BASH_PROGRAM} ${CMAKE_CURRENT_SOURCE_DIR}/resources/config-test.sh -inbuild ${PROJECT_BINARY_DIR})
endif (BASH_PROGRAM)
......@@ -21,9 +21,11 @@ set( APPLICATION_TEST_COMPONENT ${APPLICATION_TARGET_NAME} CACHE INTERNAL "")# C
# Testing command-line robustness
#---------------------------------
set(TEST_BASENAME ${APPLICATION_TARGET_NAME})
camitk_init_test(${APPLICATION_TARGET_NAME})
# should pass because invoking testcomponents without arguments shows usage and exit success
camitk_init_test(${TEST_BASENAME})
# should pass because invoking testcomponents without arguments or with help arg shows usage and exit success
camitk_add_test(PROJECT_NAME ${TEST_BASENAME} TEST_SUFFIX "-")
camitk_add_test(EXECUTABLE_ARGS "--help" PROJECT_NAME ${TEST_BASENAME} TEST_SUFFIX "-")
camitk_add_test(EXECUTABLE_ARGS "-h" PROJECT_NAME ${TEST_BASENAME} TEST_SUFFIX "-")
# should pass because invoking testcomponents with a faulty arguments results in printing
# an "Argument error" message (and exit failure)
......
......@@ -46,10 +46,10 @@ void usage(std::string msg = "") {
if (msg != "") {
std::cout << msg << std::endl;
}
std::cerr << "Usage: testcomponents [options]" << std::endl;
std::cerr << "Usage: camitk-testcomponents [options]" << std::endl;
std::cerr << "Build using " << Core::version << std::endl;
std::cerr << std::endl;
std::cerr << "Options:" << endl;
std::cerr << "Options:" << std::endl;
options::print_usage(std::cerr);
}
......@@ -119,7 +119,7 @@ int main(int argc, char* argv[]) {
int level = atoi(o.level().c_str());
std::cout << "testcomponents run with arguments:" << std::endl;
std::cout << "camitk-testcomponents run with arguments:" << std::endl;
std::cout << "- component library file: \"" << o.component() << "\"" << std::endl;
std::cout << "- input test file: \"" << o.input() << "\"" << std::endl;
std::cout << "- level of test: \"" << level << "\"" << std::endl;
......@@ -191,21 +191,22 @@ int main(int argc, char* argv[]) {
}
}
catch (const cli::exception& e) {
std::cerr << "testcomponents aborted due to invalid arguments: " << e.what() << "." << std::endl;
std::cerr << "camitk-testcomponents aborted due to invalid arguments: " << e.what() << "." << std::endl;
e.print(std::cerr);
std::cerr << std::endl;
usage();
return EXIT_FAILURE;
}
catch (const camitk::AbortException& e) {
std::cerr << "testcomponents aborted by CamiTK abort exception: " << e.what() << "." << std::endl;
std::cerr << "camitk-testcomponents aborted by CamiTK abort exception: " << e.what() << "." << std::endl;
return EXIT_FAILURE;
}
catch (const std::exception& e) {
std::cerr << "testcomponents aborted by std exception: " << e.what() << "." << std::endl;
std::cerr << "camitk-testcomponents aborted by std exception: " << e.what() << "." << std::endl;
return EXIT_FAILURE;
}
catch (...) {
std::cerr << "testcomponents aborted by unknown exception" << std::endl;
std::cerr << "camitk-testcomponents aborted by unknown exception" << std::endl;
otherException = std::current_exception();
try {
if (otherException) {
......@@ -213,8 +214,9 @@ int main(int argc, char* argv[]) {
}
}
catch (const std::exception& e) {
std::cerr << ": " << e.what() << "." << std::endl;
std::cerr << ": " << e.what() << ".";
}
std::cerr << std::endl;
return EXIT_FAILURE;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment