[gnome-autoar/wip/razvan/extract-refactor2] Add tests for the extract operation



commit b10e5d9508c3ca4920f346074a785a281c7d27f6
Author: Razvan Chitu <razvan ch95 gmail com>
Date:   Tue Jun 7 11:53:01 2016 +0300

    Add tests for the extract operation

 .gitignore                                         |    1 +
 Makefile.am                                        |    5 +
 .../in/arextract.zip                               |  Bin 0 -> 930 bytes
 .../out/arextract/arextract.txt                    |    1 +
 .../out/arextract/arextract/arextract.txt          |    1 +
 .../arextract/arextract_nested/arextract.txt       |    1 +
 .../ref/arextract/arextract.txt                    |    1 +
 .../ref/arextract/arextract/arextract.txt          |    1 +
 .../arextract/arextract_nested/arextract.txt       |    1 +
 .../test-multiple-files-same-name/in/arextract.zip |  Bin 0 -> 762 bytes
 .../out/arextract/arextract.txt                    |    1 +
 .../out/arextract/arextract_nested/arextract.txt   |    1 +
 .../ref/arextract/arextract.txt                    |    1 +
 .../ref/arextract/arextract_nested/arextract.txt   |    1 +
 .../test-one-file-different-name/in/arextract.zip  |  Bin 0 -> 208 bytes
 .../out/arextract/arextractdifferent.txt           |    1 +
 .../ref/arextract/arextractdifferent.txt           |    1 +
 .../test-one-file-same-name/in/arextract.zip       |  Bin 0 -> 190 bytes
 .../test-one-file-same-name/out/arextract.txt      |    1 +
 .../test-one-file-same-name/ref/arextract.txt      |    1 +
 tests/test-extract-unit.c                          |  634 ++++++++++++++++++++
 21 files changed, 654 insertions(+), 0 deletions(-)
---
diff --git a/.gitignore b/.gitignore
index 899beeb..db5f7d4 100644
--- a/.gitignore
+++ b/.gitignore
@@ -45,6 +45,7 @@ gnome-autoar/gnome-autoar.pc
 gnome-autoar/gnome-autoar-gtk.pc
 
 tests/test-extract
+tests/test-extract-unit
 tests/test-extract-memory
 tests/test-create
 tests/test-pref
diff --git a/Makefile.am b/Makefile.am
index e75aef8..1d278b2 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -185,6 +185,7 @@ EXTRA_DIST += $(gsettings_SCHEMAS)
 # Directory: tests
 noinst_PROGRAMS = \
        tests/test-extract      \
+       tests/test-extract-unit
        tests/test-pref         \
        tests/test-create       \
        $(NULL)
@@ -204,6 +205,10 @@ tests_test_extract_SOURCES = tests/test-extract.c
 tests_test_extract_CFLAGS = $(test_cflags)
 tests_test_extract_LDADD = $(test_libs)
 
+tests_test_extract_unit_SOURCES = tests/test-extract-unit.c
+tests_test_extract_unit_CFLAGS = $(test_cflags)
+tests_test_extract_unit_LDADD = $(test_libs)
+
 tests_test_pref_SOURCES = tests/test-pref.c
 tests_test_pref_CFLAGS = $(test_cflags)
 tests_test_pref_LDADD = $(test_libs)
diff --git a/tests/files/extract/test-multiple-files-different-name/in/arextract.zip 
b/tests/files/extract/test-multiple-files-different-name/in/arextract.zip
new file mode 100644
index 0000000..4f5f42a
Binary files /dev/null and b/tests/files/extract/test-multiple-files-different-name/in/arextract.zip differ
diff --git a/tests/files/extract/test-multiple-files-different-name/out/arextract/arextract.txt 
b/tests/files/extract/test-multiple-files-different-name/out/arextract/arextract.txt
new file mode 100644
index 0000000..12466e2
--- /dev/null
+++ b/tests/files/extract/test-multiple-files-different-name/out/arextract/arextract.txt
@@ -0,0 +1 @@
+AutoarExtract
diff --git a/tests/files/extract/test-multiple-files-different-name/out/arextract/arextract/arextract.txt 
b/tests/files/extract/test-multiple-files-different-name/out/arextract/arextract/arextract.txt
new file mode 100644
index 0000000..12466e2
--- /dev/null
+++ b/tests/files/extract/test-multiple-files-different-name/out/arextract/arextract/arextract.txt
@@ -0,0 +1 @@
+AutoarExtract
diff --git 
a/tests/files/extract/test-multiple-files-different-name/out/arextract/arextract/arextract_nested/arextract.txt
 
b/tests/files/extract/test-multiple-files-different-name/out/arextract/arextract/arextract_nested/arextract.txt
new file mode 100644
index 0000000..12466e2
--- /dev/null
+++ 
b/tests/files/extract/test-multiple-files-different-name/out/arextract/arextract/arextract_nested/arextract.txt
@@ -0,0 +1 @@
+AutoarExtract
diff --git a/tests/files/extract/test-multiple-files-different-name/ref/arextract/arextract.txt 
b/tests/files/extract/test-multiple-files-different-name/ref/arextract/arextract.txt
new file mode 100644
index 0000000..12466e2
--- /dev/null
+++ b/tests/files/extract/test-multiple-files-different-name/ref/arextract/arextract.txt
@@ -0,0 +1 @@
+AutoarExtract
diff --git a/tests/files/extract/test-multiple-files-different-name/ref/arextract/arextract/arextract.txt 
b/tests/files/extract/test-multiple-files-different-name/ref/arextract/arextract/arextract.txt
new file mode 100644
index 0000000..12466e2
--- /dev/null
+++ b/tests/files/extract/test-multiple-files-different-name/ref/arextract/arextract/arextract.txt
@@ -0,0 +1 @@
+AutoarExtract
diff --git 
a/tests/files/extract/test-multiple-files-different-name/ref/arextract/arextract/arextract_nested/arextract.txt
 
b/tests/files/extract/test-multiple-files-different-name/ref/arextract/arextract/arextract_nested/arextract.txt
new file mode 100644
index 0000000..12466e2
--- /dev/null
+++ 
b/tests/files/extract/test-multiple-files-different-name/ref/arextract/arextract/arextract_nested/arextract.txt
@@ -0,0 +1 @@
+AutoarExtract
diff --git a/tests/files/extract/test-multiple-files-same-name/in/arextract.zip 
b/tests/files/extract/test-multiple-files-same-name/in/arextract.zip
new file mode 100644
index 0000000..83d30b3
Binary files /dev/null and b/tests/files/extract/test-multiple-files-same-name/in/arextract.zip differ
diff --git a/tests/files/extract/test-multiple-files-same-name/out/arextract/arextract.txt 
b/tests/files/extract/test-multiple-files-same-name/out/arextract/arextract.txt
new file mode 100644
index 0000000..12466e2
--- /dev/null
+++ b/tests/files/extract/test-multiple-files-same-name/out/arextract/arextract.txt
@@ -0,0 +1 @@
+AutoarExtract
diff --git a/tests/files/extract/test-multiple-files-same-name/out/arextract/arextract_nested/arextract.txt 
b/tests/files/extract/test-multiple-files-same-name/out/arextract/arextract_nested/arextract.txt
new file mode 100644
index 0000000..12466e2
--- /dev/null
+++ b/tests/files/extract/test-multiple-files-same-name/out/arextract/arextract_nested/arextract.txt
@@ -0,0 +1 @@
+AutoarExtract
diff --git a/tests/files/extract/test-multiple-files-same-name/ref/arextract/arextract.txt 
b/tests/files/extract/test-multiple-files-same-name/ref/arextract/arextract.txt
new file mode 100644
index 0000000..12466e2
--- /dev/null
+++ b/tests/files/extract/test-multiple-files-same-name/ref/arextract/arextract.txt
@@ -0,0 +1 @@
+AutoarExtract
diff --git a/tests/files/extract/test-multiple-files-same-name/ref/arextract/arextract_nested/arextract.txt 
b/tests/files/extract/test-multiple-files-same-name/ref/arextract/arextract_nested/arextract.txt
new file mode 100644
index 0000000..12466e2
--- /dev/null
+++ b/tests/files/extract/test-multiple-files-same-name/ref/arextract/arextract_nested/arextract.txt
@@ -0,0 +1 @@
+AutoarExtract
diff --git a/tests/files/extract/test-one-file-different-name/in/arextract.zip 
b/tests/files/extract/test-one-file-different-name/in/arextract.zip
new file mode 100644
index 0000000..1c24bdc
Binary files /dev/null and b/tests/files/extract/test-one-file-different-name/in/arextract.zip differ
diff --git a/tests/files/extract/test-one-file-different-name/out/arextract/arextractdifferent.txt 
b/tests/files/extract/test-one-file-different-name/out/arextract/arextractdifferent.txt
new file mode 100644
index 0000000..12466e2
--- /dev/null
+++ b/tests/files/extract/test-one-file-different-name/out/arextract/arextractdifferent.txt
@@ -0,0 +1 @@
+AutoarExtract
diff --git a/tests/files/extract/test-one-file-different-name/ref/arextract/arextractdifferent.txt 
b/tests/files/extract/test-one-file-different-name/ref/arextract/arextractdifferent.txt
new file mode 100644
index 0000000..12466e2
--- /dev/null
+++ b/tests/files/extract/test-one-file-different-name/ref/arextract/arextractdifferent.txt
@@ -0,0 +1 @@
+AutoarExtract
diff --git a/tests/files/extract/test-one-file-same-name/in/arextract.zip 
b/tests/files/extract/test-one-file-same-name/in/arextract.zip
new file mode 100644
index 0000000..8c2eeb6
Binary files /dev/null and b/tests/files/extract/test-one-file-same-name/in/arextract.zip differ
diff --git a/tests/files/extract/test-one-file-same-name/out/arextract.txt 
b/tests/files/extract/test-one-file-same-name/out/arextract.txt
new file mode 100644
index 0000000..12466e2
--- /dev/null
+++ b/tests/files/extract/test-one-file-same-name/out/arextract.txt
@@ -0,0 +1 @@
+AutoarExtract
diff --git a/tests/files/extract/test-one-file-same-name/ref/arextract.txt 
b/tests/files/extract/test-one-file-same-name/ref/arextract.txt
new file mode 100644
index 0000000..12466e2
--- /dev/null
+++ b/tests/files/extract/test-one-file-same-name/ref/arextract.txt
@@ -0,0 +1 @@
+AutoarExtract
diff --git a/tests/test-extract-unit.c b/tests/test-extract-unit.c
new file mode 100644
index 0000000..a67e176
--- /dev/null
+++ b/tests/test-extract-unit.c
@@ -0,0 +1,634 @@
+#include <gnome-autoar/autoar.h>
+#include <gio/gio.h>
+#include <glib/gstdio.h>
+#include <errno.h>
+#include <string.h>
+
+#define TESTS_DIR_NAME "tests"
+#define EXTRACT_TESTS_DIR_NAME "files/extract"
+
+
+GFile *extract_tests_dir;
+
+AutoarPref *arpref;
+
+
+typedef struct _ExtractTest ExtractTest;
+typedef struct _ExtractTestData ExtractTestData;
+
+struct _ExtractTest {
+  GFile *in;
+  GFile *out;
+  GFile *ref;
+};
+
+struct _ExtractTestData {
+  GCancellable *cancellable;
+
+  guint number_of_files;
+
+  GFile *suggested_destination;
+  GFile *destination_to_suggest;
+
+  guint64 completed_size;
+  guint completed_files;
+
+  gboolean completed;
+
+  GError *error;
+
+  gboolean cancelled;
+};
+
+static gboolean
+remove_dir (GFile *dir)
+{
+  gboolean success = TRUE;
+  GFileEnumerator *enumerator;
+  GError *error = NULL;
+
+  enumerator = g_file_enumerate_children (dir,
+                                          G_FILE_ATTRIBUTE_STANDARD_NAME,
+                                          G_FILE_QUERY_INFO_NONE,
+                                          NULL, NULL);
+
+  if (enumerator) {
+    GFileInfo *info;
+    GFile *child;
+
+    while ((info = g_file_enumerator_next_file (enumerator, NULL, NULL)) != NULL) {
+      child = g_file_get_child (dir, g_file_info_get_name (info));
+
+      if (!g_file_delete (child, NULL, &error)) {
+        if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_EMPTY)) {
+          success &= remove_dir (child);
+        } else {
+          success = FALSE;
+        }
+
+        g_error_free (error);
+        error = NULL;
+      }
+
+      g_object_unref (info);
+      g_object_unref (child);
+    }
+
+    g_object_unref (enumerator);
+  }
+
+  g_file_delete (dir, NULL, &error);
+
+  if (error) {
+    success = FALSE;
+    g_error_free (error);
+  }
+
+  return success;
+}
+
+static ExtractTest*
+extract_test_new (const char *test_name)
+{
+  ExtractTest *extract_test;
+  GFile *workdir;
+  GFile *in;
+  GFile *out;
+  GFile *ref;
+
+  workdir = g_file_get_child (extract_tests_dir, test_name);
+  if (g_file_query_file_type (workdir, G_FILE_QUERY_INFO_NONE, NULL) != G_FILE_TYPE_DIRECTORY) {
+    g_printerr ("%s: workdir does not exist", test_name);
+
+    g_object_unref (workdir);
+
+    return NULL;
+  }
+
+  in = g_file_get_child (workdir, "in");
+  ref = g_file_get_child (workdir, "ref");
+
+  if (g_file_query_file_type (in, G_FILE_QUERY_INFO_NONE, NULL) != G_FILE_TYPE_DIRECTORY ||
+      g_file_query_file_type (ref, G_FILE_QUERY_INFO_NONE, NULL) != G_FILE_TYPE_DIRECTORY) {
+    g_printerr ("%s: input or output directory does not exist\n", test_name);
+
+    g_object_unref (workdir);
+    g_object_unref (in);
+
+    return NULL;
+  }
+
+  out = g_file_get_child (workdir, "out");
+
+  remove_dir (out);
+
+  g_file_make_directory_with_parents (out, NULL, NULL);
+
+  extract_test = g_new0 (ExtractTest, 1);
+
+  extract_test->in = in;
+  extract_test->ref = ref;
+  extract_test->out = out;
+
+  return extract_test;
+}
+
+static void
+extract_test_free (ExtractTest *extract_test)
+{
+  g_object_unref (extract_test->in);
+  g_object_unref (extract_test->ref);
+  g_object_unref (extract_test->out);
+  g_free (extract_test);
+}
+
+static void
+scanned_handler (AutoarExtract *arextract,
+                 guint files,
+                 gpointer user_data)
+{
+  ExtractTestData *data = user_data;
+
+  data->number_of_files = files;
+}
+
+static GFile*
+confirm_dest_handler (AutoarExtract *arextract,
+                      GFile *dest,
+                      GList *files,
+                      gpointer user_data)
+{
+  ExtractTestData *data = user_data;
+
+  data->suggested_destination = g_object_ref (dest);
+
+  return data->destination_to_suggest;
+}
+
+static void
+progress_handler (AutoarExtract *arextract,
+                  guint64 completed_size,
+                  guint completed_files,
+                  gpointer user_data)
+{
+  ExtractTestData *data = user_data;
+
+  data->completed_size = completed_size;
+  data->completed_files = completed_files;
+}
+
+static GFile*
+conflict_handler (AutoarExtract *arextract,
+                  GFile *file,
+                  gpointer user_data)
+{
+  ExtractTestData *data = user_data;
+
+  data = data;
+
+  return NULL;
+}
+
+static void
+error_handler (AutoarExtract *arextract,
+               GError *error,
+               gpointer user_data)
+{
+  ExtractTestData *data = user_data;
+
+  data->error = g_error_copy (error);
+}
+
+static void
+completed_handler (AutoarExtract *arextract,
+                   gpointer user_data)
+{
+  ExtractTestData *data = user_data;
+
+  data->completed = TRUE;
+}
+
+static void
+cancelled_handler (AutoarExtract *arextract,
+                   gpointer user_data)
+{
+  ExtractTestData *data = user_data;
+
+  data->cancelled = TRUE;
+}
+
+static ExtractTestData*
+extract_test_data_new_for_extract (AutoarExtract *arextract)
+{
+  ExtractTestData *data;
+
+  data = g_new0 (ExtractTestData, 1);
+
+  data->cancellable = g_cancellable_new ();
+
+  g_signal_connect (arextract, "scanned",
+                    G_CALLBACK (scanned_handler), data);
+  g_signal_connect (arextract, "confirm-dest",
+                    G_CALLBACK (confirm_dest_handler), data);
+  g_signal_connect (arextract, "progress",
+                    G_CALLBACK (progress_handler), data);
+  g_signal_connect (arextract, "conflict",
+                    G_CALLBACK (conflict_handler), data);
+  g_signal_connect (arextract, "completed",
+                    G_CALLBACK (completed_handler), data);
+  g_signal_connect (arextract, "error",
+                    G_CALLBACK (error_handler), data);
+  g_signal_connect (arextract, "cancelled",
+                    G_CALLBACK (cancelled_handler), data);
+
+  return data;
+}
+
+static void
+extract_test_data_free (ExtractTestData *data)
+{
+  g_object_unref (data->cancellable);
+
+  if (data->suggested_destination) {
+    g_object_unref (data->suggested_destination);
+  }
+
+  if (data->destination_to_suggest) {
+    g_object_unref (data->destination_to_suggest);
+  }
+
+  if (data->error) {
+    g_error_free (data->error);
+  }
+
+  g_free (data);
+}
+
+static gboolean
+setup_extract_tests_dir (const char *executable_path)
+{
+  GFile *tests_dir;
+  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;
+    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);
+    g_free (parent_basename);
+
+    tests_dir = parent;
+  }
+
+  if (!found) {
+    g_printerr ("Tests directory not in executable path\n");
+    g_object_unref (tests_dir);
+    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");
+    g_object_unref (tests_dir);
+    return FALSE;
+  }
+
+  g_object_unref (tests_dir);
+
+  return TRUE;
+}
+
+/* Asserts that all files in @included are also in @including */
+static void
+assert_directory_inclusion (GFile *included, GFile *including)
+{
+  GQueue *files;
+  GQueue *file_infos;
+  GFileEnumerator *enumerator;
+
+  files = g_queue_new ();
+  file_infos = g_queue_new ();
+
+  g_queue_push_tail (files, g_object_ref (included));
+  g_queue_push_tail (file_infos,
+                     g_file_query_info (included,
+                                        G_FILE_ATTRIBUTE_STANDARD_NAME","
+                                        G_FILE_ATTRIBUTE_STANDARD_TYPE,
+                                        G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
+                                        NULL, NULL));
+
+  while (!g_queue_is_empty (files)) {
+    GFile *file;
+    GFileInfo *file_info;
+    GFile *corresponding_file;
+    GFileInfo *corresponding_file_info;
+    g_autofree char *relative_path;
+
+    file = g_queue_pop_tail (files);
+    file_info = g_queue_pop_tail (file_infos);
+
+    relative_path = file == included ? g_strdup ("") : g_file_get_relative_path (included, file);
+    corresponding_file = g_file_resolve_relative_path (including, relative_path);
+    corresponding_file_info = g_file_query_info (corresponding_file,
+                                                 G_FILE_ATTRIBUTE_STANDARD_NAME","
+                                                 G_FILE_ATTRIBUTE_STANDARD_TYPE,
+                                                 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
+                                                 NULL, NULL);
+
+    g_assert_nonnull (corresponding_file_info);
+    if (corresponding_file_info != NULL) {
+      g_assert_cmpuint (g_file_info_get_file_type (file_info),
+                        ==,
+                        g_file_info_get_file_type (corresponding_file_info));
+    }
+
+
+    if (g_file_info_get_file_type (file_info) == G_FILE_TYPE_DIRECTORY) {
+      enumerator = g_file_enumerate_children (file,
+                                              G_FILE_ATTRIBUTE_STANDARD_NAME","
+                                              G_FILE_ATTRIBUTE_STANDARD_TYPE,
+                                              G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
+                                              NULL, NULL);
+
+      if (enumerator) {
+        GFile *child;
+        GFileInfo *child_info;
+      
+        while ((child_info = g_file_enumerator_next_file (enumerator, NULL, NULL)) != NULL) {
+          child = g_file_get_child (file, g_file_info_get_name (child_info));
+
+          g_queue_push_tail (files, child);
+          g_queue_push_tail (file_infos, child_info);
+        }
+
+        g_object_unref (enumerator);
+      }
+    }
+
+    g_clear_object (&corresponding_file);
+    g_clear_object (&corresponding_file_info);
+  }
+
+  g_queue_free_full (files, g_object_unref);
+  g_queue_free_full (file_infos, g_object_unref);
+}
+
+/* Asserts that all files in @first are included in @second and the other way
+ * round
+ */
+static void
+assert_directory_equality (GFile *first, GFile *second)
+{
+  assert_directory_inclusion (first, second);
+  assert_directory_inclusion (second, first);
+}
+
+static void
+test_one_file_same_name (void)
+{
+  /* arextract.zip
+   * └── arextract.txt
+   * 
+   * 0 directories, 1 file
+   * 
+   * 
+   * ref
+   * └── arextract.txt
+   * 
+   * 0 directories, 1 file
+   */
+
+  ExtractTest *extract_test;
+  ExtractTestData *data;
+  GFile *archive;
+  AutoarExtract *arextract;
+
+  extract_test = extract_test_new ("test-one-file-same-name");
+
+  if (!extract_test) {
+    g_assert_true (FALSE);
+    return;
+  }
+
+  archive = g_file_get_child (extract_test->in, "arextract.zip");
+
+  arextract = autoar_extract_new_file (archive, extract_test->out, arpref);
+
+  data = extract_test_data_new_for_extract (arextract);
+
+  autoar_extract_start (arextract, data->cancellable);
+
+  g_assert_cmpuint (data->number_of_files, ==, 1);
+  g_assert_no_error (data->error);
+  assert_directory_equality (extract_test->out, extract_test->ref);
+
+  extract_test_free (extract_test);
+  extract_test_data_free (data);
+  g_object_unref (archive);
+  g_object_unref (arextract);
+}
+
+static void
+test_one_file_different_name (void)
+{
+  /* arextract.zip
+   * └── arextractdifferent.txt
+   * 
+   * 0 directories, 1 file
+   * 
+   * 
+   * ref
+   * └── arextract
+   *     └── arextractdifferent.txt
+   * 
+   * 1 directory, 1 file
+   */
+
+  ExtractTest *extract_test;
+  ExtractTestData *data;
+  GFile *archive;
+  AutoarExtract *arextract;
+
+  extract_test = extract_test_new ("test-one-file-different-name");
+
+  if (!extract_test) {
+    g_assert_true (FALSE);
+    return;
+  }
+
+  archive = g_file_get_child (extract_test->in, "arextract.zip");
+
+  arextract = autoar_extract_new_file (archive, extract_test->out, arpref);
+
+  data = extract_test_data_new_for_extract (arextract);
+
+  autoar_extract_start (arextract, data->cancellable);
+
+  g_assert_cmpuint (data->number_of_files, ==, 1);
+  g_assert_no_error (data->error);
+  assert_directory_equality (extract_test->out, extract_test->ref);
+
+  extract_test_free (extract_test);
+  extract_test_data_free (data);
+  g_object_unref (archive);
+  g_object_unref (arextract);
+}
+
+static void
+test_multiple_files_same_name (void)
+{
+  /* arextract.zip
+   * └── arextract
+   *     ├── arextract_nested
+   *     │   └── arextract.txt
+   *     └── arextract.txt
+   * 
+   * 2 directories, 2 files
+   * 
+   * 
+   * ref
+   * └── arextract
+   *     ├── arextract_nested
+   *     │   └── arextract.txt
+   *     └── arextract.txt
+   * 
+   * 2 directories, 2 files
+   */
+
+  ExtractTest *extract_test;
+  ExtractTestData *data;
+  GFile *archive;
+  AutoarExtract *arextract;
+
+  extract_test = extract_test_new ("test-multiple-files-same-name");
+
+  if (!extract_test) {
+    g_assert_true (FALSE);
+    return;
+  }
+
+  archive = g_file_get_child (extract_test->in, "arextract.zip");
+
+  arextract = autoar_extract_new_file (archive, extract_test->out, arpref);
+
+  data = extract_test_data_new_for_extract (arextract);
+
+  autoar_extract_start (arextract, data->cancellable);
+
+  g_assert_cmpuint (data->number_of_files, ==, 4);
+  g_assert_no_error (data->error);
+  assert_directory_equality (extract_test->out, extract_test->ref);
+
+  extract_test_free (extract_test);
+  extract_test_data_free (data);
+  g_object_unref (archive);
+  g_object_unref (arextract);
+
+}
+
+static void
+test_multiple_files_different_name (void)
+{
+  /* arextract.zip
+   * ├── arextract
+   * │   ├── arextract_nested
+   * │   │   └── arextract.txt
+   * │   └── arextract.txt
+   * └── arextract.txt
+   * 
+   * 2 directories, 3 files
+   * 
+   * 
+   * ref
+   * └── arextract
+   *     ├── arextract
+   *     │   ├── arextract_nested
+   *     │   │   └── arextract.txt
+   *     │   └── arextract.txt
+   *     └── arextract.txt
+   * 
+   * 3 directories, 3 files
+   */
+
+  ExtractTest *extract_test;
+  ExtractTestData *data;
+  GFile *archive;
+  AutoarExtract *arextract;
+
+  extract_test = extract_test_new ("test-multiple-files-different-name");
+
+  if (!extract_test) {
+    g_assert_true (FALSE);
+    return;
+  }
+
+  archive = g_file_get_child (extract_test->in, "arextract.zip");
+
+  arextract = autoar_extract_new_file (archive, extract_test->out, arpref);
+
+  data = extract_test_data_new_for_extract (arextract);
+
+  autoar_extract_start (arextract, data->cancellable);
+
+  g_assert_cmpuint (data->number_of_files, ==, 5);
+  g_assert_no_error (data->error);
+  assert_directory_equality (extract_test->out, extract_test->ref);
+
+  extract_test_free (extract_test);
+  extract_test_data_free (data);
+  g_object_unref (archive);
+  g_object_unref (arextract);
+
+}
+
+static void
+setup_test_suite (void)
+{
+  g_test_add_func ("/autoar-extract/test-one-file-same-name",
+                   test_one_file_same_name);
+  g_test_add_func ("/autoar-extract/test-one-file-different-name",
+                   test_one_file_different_name);
+  g_test_add_func ("/autoar-extract/test-multiple-files-same-name",
+                   test_multiple_files_same_name);
+  g_test_add_func ("/autoar-extract/test-multiple-files-different-name",
+                   test_multiple_files_different_name);
+}
+
+int
+main (int argc,
+      char *argv[])
+{
+  GSettings *settings;
+
+  if (!setup_extract_tests_dir (argv[0])) {
+    return -1;
+  }
+
+  settings = g_settings_new (AUTOAR_PREF_DEFAULT_GSCHEMA_ID);
+  arpref = autoar_pref_new_with_gsettings (settings);
+  autoar_pref_set_delete_if_succeed (arpref, FALSE);
+
+  g_test_init (&argc, &argv, NULL);
+  g_test_set_nonfatal_assertions ();
+
+  setup_test_suite ();
+
+  g_test_run ();
+
+  g_object_unref (settings);
+  g_object_unref (arpref);
+  g_object_unref (extract_tests_dir);
+
+  return 0;
+}


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