From 16c9928d133a86ac049dfcd81a12e59535d80f99 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andr=C3=A9=20Offringa?= <offringa@gmail.com>
Date: Wed, 15 Mar 2023 08:57:12 +0000
Subject: [PATCH] Fix aoqplot -save crash due to uninitialized pango

---
 .gitlab-ci.yml                |  4 ++--
 CMakeLists.txt                |  2 +-
 applications/aoqplot.cpp      |  4 ++++
 applications/rfigui.cpp       |  5 ++++-
 rfigui/settings.cpp           | 16 +++++++++++-----
 scripts/test/testconfig.py.in |  2 ++
 test/integration/tBase.py     | 18 ++++++++++++++++++
 7 files changed, 42 insertions(+), 9 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 94e4760e..20321855 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -71,7 +71,7 @@ ubuntu20-tests:
     - cmake -DENABLE_TESTS=ON -DCMAKE_CXX_FLAGS="-coverage" -DCMAKE_EXE_LINKER_FLAGS="-coverage" ../
     # Do not install AOFlagger in this job. The tests should still run.
     - make -j`nproc` all runtests
-    - ctest
+    - ctest --output-on-failure
   artifacts:
     paths:
       - build/*.xml
@@ -94,7 +94,7 @@ ubuntu22-tests:
     - cmake -DENABLE_TESTS=ON -GNinja ../
     - ninja all runtests install
     - ninja check
-    - ctest
+    - ctest --output-on-failure
     - cd python
     - echo "import aoflagger"|python3
   artifacts:
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 988863c6..5af785ce 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -521,7 +521,7 @@ add_custom_target(
 
 # TODO(RAP-368) Make a proper integration test target.
 option(ENABLE_TESTS
-       "Enable the integration tests, this requires downloading test data" OFF)
+       "Enable the integration tests. This requires downloading test data." OFF)
 if(ENABLE_TESTS)
 
   find_package(Boost 1.56.0 REQUIRED COMPONENTS unit_test_framework)
diff --git a/applications/aoqplot.cpp b/applications/aoqplot.cpp
index 390411f6..7a06f725 100644
--- a/applications/aoqplot.cpp
+++ b/applications/aoqplot.cpp
@@ -5,6 +5,8 @@
 #include <gtkmm/main.h>
 #include <gtkmm/filechooserdialog.h>
 
+#include <pangomm/init.h>
+
 #include <version.h>
 
 #include "../util/logger.h"
@@ -122,6 +124,8 @@ int main(int argc, char* argv[]) {
     window->Open(files);
     app->run(*window);
   } else {
+    Pango::init();
+
     if (files.empty()) {
       Logger::Error << "No observation specified.\n";
       return 1;
diff --git a/applications/rfigui.cpp b/applications/rfigui.cpp
index 0f97a396..b8f7d674 100644
--- a/applications/rfigui.cpp
+++ b/applications/rfigui.cpp
@@ -12,6 +12,8 @@
 
 #include <gtkmm/application.h>
 
+#include <pangomm/init.h>
+
 #include <glibmm/error.h>
 #include <glibmm/wrap.h>
 
@@ -123,7 +125,7 @@ static void run(int argc, char* argv[]) {
   if (interactive) {
     app = Gtk::Application::create(altArgc, argv, "",
                                    Gio::APPLICATION_HANDLES_OPEN);
-    window.reset(new RFIGuiWindow(&controller, argv[0]));
+    window = std::make_unique<RFIGuiWindow>(&controller, argv[0]);
     window->present();
   }
 
@@ -132,6 +134,7 @@ static void run(int argc, char* argv[]) {
       if (interactive)
         window->OpenPaths(filenames);
       else {
+        Pango::init();
         MSOptions options;
         options.ioMode = DirectReadMode;
         options.dataColumnName = dataColumnName;
diff --git a/rfigui/settings.cpp b/rfigui/settings.cpp
index 5286036b..3c3f66d3 100644
--- a/rfigui/settings.cpp
+++ b/rfigui/settings.cpp
@@ -8,6 +8,7 @@
 #include <fstream>
 
 #include "../lua/default-strategy.h"
+#include "../util/logger.h"
 
 Settings::Settings() {
   initStrArray("recent-files", std::vector<std::string>(10));
@@ -25,15 +26,21 @@ std::string Settings::GetStrategyFilename() const {
 std::string Settings::GetConfigDir() {
   std::filesystem::path configPath =
       std::filesystem::path(Glib::get_user_config_dir()) / "aoflagger";
-  if (!std::filesystem::is_directory(configPath))
-    std::filesystem::create_directory(configPath);
+  if (!std::filesystem::is_directory(configPath)) {
+    // We don't want to crash if the dir can't be created; we will just report
+    // an error to the cmd line
+    try {
+      std::filesystem::create_directories(configPath);
+    } catch (std::exception& exception) {
+      Logger::Error << "Failed to create config directory: " << exception.what()
+                    << '\n';
+    }
+  }
   return configPath.string();
 }
 
 void Settings::InitializeWorkStrategy() {
   std::string filename = GetStrategyFilename();
-  // if(!std::filesystem::exists(filename))
-  //{
   std::ofstream str(filename);
   str.write(reinterpret_cast<const char*>(data_strategies_generic_default_lua),
             data_strategies_generic_default_lua_len);
@@ -41,7 +48,6 @@ void Settings::InitializeWorkStrategy() {
     throw std::runtime_error(
         "Failed to write working file for Lua strategy: " + filename +
         ", size " + std::to_string(data_strategies_generic_default_lua_len));
-  //}
 }
 
 void Settings::Load() {
diff --git a/scripts/test/testconfig.py.in b/scripts/test/testconfig.py.in
index 6c2a1507..86395387 100644
--- a/scripts/test/testconfig.py.in
+++ b/scripts/test/testconfig.py.in
@@ -2,5 +2,7 @@ SRC_DIR = "@CMAKE_SOURCE_DIR@/test/integration"
 BIN_DIR = "@CMAKE_BINARY_DIR@"
 RESOURCE_DIR = "@CMAKE_BINARY_DIR@/test_data"
 aoflagger = "@CMAKE_BINARY_DIR@/aoflagger"
+aoqplot = "@CMAKE_BINARY_DIR@/aoqplot"
 aoquality = "@CMAKE_BINARY_DIR@/aoquality"
+rfigui = "@CMAKE_BINARY_DIR@/rfigui"
 taql = "@TAQL_EXECUTABLE@"
diff --git a/test/integration/tBase.py b/test/integration/tBase.py
index fa7c95aa..879d7e3c 100644
--- a/test/integration/tBase.py
+++ b/test/integration/tBase.py
@@ -44,3 +44,21 @@ def test():
     )
     # Did we write one history record?
     assert_taql(f"select from {MS}/HISTORY", 1)
+
+    # Test rfigui save baseline functionality (parameters: filename, a1, a2, band, sequence)
+    # The output isn't checked -- this is just to see if the command succeeds.
+    check_call(
+        [
+            config.rfigui,
+            MS,
+            "-save-baseline",
+            "baseline-test.pdf",
+            "0",
+            "1",
+            "0",
+            "0",
+        ]
+    )
+
+    # Test if the statistic plots (pdf files) can be saved.
+    check_call([config.aoqplot, MS, "-save", "test", "StandardDeviation"])
-- 
GitLab