diff --git a/src/app/utils.cpp b/src/app/utils.cpp index 478c37ae6a0dc42abe9ce68683e64d8aa2785c03..535a9943ee049be2db2c4d5fcc49f951888205d2 100644 --- a/src/app/utils.cpp +++ b/src/app/utils.cpp @@ -51,6 +51,35 @@ #include <windows.h> #endif +// Removes the given argument from the command line arguments, and invokes the callback +// function with the removed argument if it was found. +// This is required for custom args as quick_test_main_with_setup() will +// fail if given an invalid command-line argument. +void +Utils::remove_argument(char** argv, + int& argc, + const std::string& arg_to_remove, + std::function<void()> callback) +{ + // Remove arg_to_remove from argv, as quick_test_main_with_setup() will + // fail if given an invalid command-line argument. + auto new_end = std::remove_if(argv + 1, argv + argc, [&](char* arg_ptr) { + if (std::string(arg_ptr).compare(arg_to_remove) == 0) { + // Invoke the callback function with the removed argument. + callback(); + return true; + } else { + return false; + } + }); + + // If any occurrences were removed... + if (new_end != argv + argc) { + // Adjust the argument count. + argc = std::distance(argv, new_end); + } +} + void Utils::testVulkanSupport() { diff --git a/src/app/utils.h b/src/app/utils.h index 25b0d4cfa8aae8fd7e29b136930ad612449652a0..0084a9b7d7b2ed2ddd24520c2e9da247a7bff63d 100644 --- a/src/app/utils.h +++ b/src/app/utils.h @@ -56,6 +56,12 @@ class LRCInstance; namespace Utils { +// Helper used to remove app arguments. +void remove_argument(char** argv, + int& argc, + const std::string& arg_to_remove, + std::function<void()> callback); + // Throws if Vulkan cannot be instantiated. void testVulkanSupport(); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 3b9373c3a4eae916a662e688c5eb90fa42210149..ab83ec618553d781e9b35a678645fe4528d4ef2c 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -108,7 +108,7 @@ function(setup_test TEST_NAME TEST_SOURCES TEST_INPUT) set_target_properties(${TEST_BINARY_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_${BUILD_TYPE} ${OUTPUT_DIRECTORY}) endif() - add_test(NAME ${TEST_NAME} COMMAND ${TEST_BINARY_NAME} -input ${TEST_INPUT} -mutejamid) + add_test(NAME ${TEST_NAME} COMMAND ${TEST_BINARY_NAME} -input ${TEST_INPUT} --mutejamid) endfunction() # QML tests diff --git a/tests/qml/main.cpp b/tests/qml/main.cpp index 0f974e64e27d3f77a430ce8d07c8ab55cc15d56a..a8ab7be4dd09dcb1621d45e06d309822dadf1c14 100644 --- a/tests/qml/main.cpp +++ b/tests/qml/main.cpp @@ -145,18 +145,12 @@ main(int argc, char** argv) bool muteDring {false}; - // Remove "-mutejamid" from argv, as quick_test_main_with_setup() will - // fail if given an invalid command-line argument. - auto end = std::remove_if(argv + 1, argv + argc, [](char* argv) { - return (strcmp(argv, "-mutejamid") == 0); - }); + // We likely want to mute the daemon for log clarity. + Utils::remove_argument(argv, argc, "--mutejamid", [&]() { muteDring = true; }); - if (end != argv + argc) { - muteDring = true; + // Allow the user to enable fatal warnings for certain tests. + Utils::remove_argument(argv, argc, "--failonwarn", [&]() { qputenv("QT_FATAL_WARNINGS", "1"); }); - // Adjust the argument count. - argc = std::distance(argv, end); - } #ifdef WITH_WEBENGINE QtWebEngineQuick::initialize(); #endif diff --git a/tests/unittests/main_unittest.cpp b/tests/unittests/main_unittest.cpp index 1ccf20efd70708083b555aed11dc1051aef926e0..36f54d918a86b5594a587b7e45b6054aae0b030e 100644 --- a/tests/unittests/main_unittest.cpp +++ b/tests/unittests/main_unittest.cpp @@ -39,18 +39,11 @@ main(int argc, char* argv[]) if (!envSet) return 1; - // Remove "-mutejamid" from argv, as quick_test_main_with_setup() will - // fail if given an invalid command-line argument. - auto end = std::remove_if(argv + 1, argv + argc, [](char* argv) { - return (strcmp(argv, "-mutejamid") == 0); - }); + // We likely want to mute the daemon for log clarity. + Utils::remove_argument(argv, argc, "--mutejamid", [&]() { globalEnv.muteDring = true; }); - if (end != argv + argc) { - globalEnv.muteDring = true; - - // Adjust the argument count. - argc = std::distance(argv, end); - } + // Allow the user to enable fatal warnings for certain tests. + Utils::remove_argument(argv, argc, "--failonwarn", [&]() { qputenv("QT_FATAL_WARNINGS", "1"); }); QApplication a(argc, argv); a.processEvents();