[gnome-autoar/wip/oholy/meson-test: 5/5] tests: Adapt test suite so it can be used from build dir




commit 8ed545d28c5fa717550fcd93a7060d1a263811f9
Author: Ondrej Holy <oholy redhat com>
Date:   Thu Aug 5 14:10:27 2021 +0200

    tests: Adapt test suite so it can be used from build dir
    
    The `test-extract-unit` suite expects that the source directory is the
    same as the build directory. Consequently, it fails over `ninja test`
    as it doesn't see the test files. Let's adapt the codes to use the
    `G_TEST_SRCDIR`  so it can be used from the build dir.

 tests/meson.build         |  2 +-
 tests/test-extract-unit.c | 54 ++++++++++-------------------------------------
 2 files changed, 12 insertions(+), 44 deletions(-)
---
diff --git a/tests/meson.build b/tests/meson.build
index 5da28d2..b50f221 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -23,7 +23,7 @@ foreach test_unit: test_units
     test(
       test_unit[0],
       exe,
-      should_fail: true,
+      env : [f'G_TEST_SRCDIR=@source_root@']
     )
   endif
 endforeach
diff --git a/tests/test-extract-unit.c b/tests/test-extract-unit.c
index 1aa8574..615ba22 100644
--- a/tests/test-extract-unit.c
+++ b/tests/test-extract-unit.c
@@ -312,45 +312,6 @@ extract_test_data_free (ExtractTestData *data)
   g_free (data);
 }
 
-static gboolean
-setup_extract_tests_directory (const char *executable_path)
-{
-  g_autoptr (GFile) tests_dir = NULL;
-  gboolean found;
-
-  tests_dir = g_file_new_for_commandline_arg (executable_path);
-
-  found = FALSE;
-  while (!found && g_file_has_parent (tests_dir, NULL)) {
-    GFile *parent;
-    g_autofree gchar *parent_basename;
-
-    parent = g_file_get_parent (tests_dir);
-    parent_basename = g_file_get_basename (parent);
-
-    if (g_strcmp0 (parent_basename, TESTS_DIR_NAME) == 0) {
-      found = TRUE;
-    }
-
-    g_object_unref (tests_dir);
-    tests_dir = parent;
-  }
-
-  if (!found) {
-    g_printerr ("Tests directory not in executable path\n");
-    return FALSE;
-  }
-
-  extract_tests_dir = g_file_get_child (tests_dir, EXTRACT_TESTS_DIR_NAME);
-
-  if (!g_file_query_exists (extract_tests_dir, NULL)) {
-    g_printerr ("Extract tests directory does not exist in tests directory\n");
-    return FALSE;
-  }
-
-  return TRUE;
-}
-
 /* Asserts that all files in @included are also in @including */
 static void
 scan_directory (GFile *directory,
@@ -1374,14 +1335,21 @@ main (int argc,
       char *argv[])
 {
   int tests_result;
-
-  if (!setup_extract_tests_directory (argv[0])) {
-    return -1;
-  }
+  g_autofree gchar *extract_tests_dir_path = NULL;
 
   g_test_init (&argc, &argv, NULL);
   g_test_set_nonfatal_assertions ();
 
+  extract_tests_dir_path = g_test_build_filename (G_TEST_DIST,
+                                                  TESTS_DIR_NAME,
+                                                  EXTRACT_TESTS_DIR_NAME,
+                                                  NULL);
+  extract_tests_dir = g_file_new_for_path (extract_tests_dir_path);
+  if (!g_file_query_exists (extract_tests_dir, NULL)) {
+    g_printerr ("Extract tests directory not found\n");
+    return -1;
+  }
+
   setup_test_suite ();
 
   tests_result = g_test_run ();


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]