From 270fe6c1674c56423280b7d5079013e5745b6480 Mon Sep 17 00:00:00 2001
From: Andreas Traczyk <andreas.traczyk@savoirfairelinux.com>
Date: Wed, 29 Mar 2023 13:07:51 -0400
Subject: [PATCH] tests: add an option to force fail on warning for certain
 tests

Several subsequent patches will be required so this can be used for our tests.

Gitlab: #899
Change-Id: I3939770ed34fe40aafa2ab93e2bab3cd77a20955
---
 src/app/utils.cpp                 | 29 +++++++++++++++++++++++++++++
 src/app/utils.h                   |  6 ++++++
 tests/CMakeLists.txt              |  2 +-
 tests/qml/main.cpp                | 14 ++++----------
 tests/unittests/main_unittest.cpp | 15 ++++-----------
 5 files changed, 44 insertions(+), 22 deletions(-)

diff --git a/src/app/utils.cpp b/src/app/utils.cpp
index 478c37ae6..535a9943e 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 25b0d4cfa..0084a9b7d 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 3b9373c3a..ab83ec618 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 0f974e64e..a8ab7be4d 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 1ccf20efd..36f54d918 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();
-- 
GitLab