summaryrefslogtreecommitdiff
path: root/tests/test_trash.c
blob: 5138bd6262b5f6a4d472ba1ff7147e2dfc176e0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*:*
 * ggaze — trash unit test
 *
 * Exercises the ./Trash bin: bin (lazy create + collision suffix), restore
 * (undo), permanent delete, and can_undo. Uses temp dirs; no display.
 *
 * Copyright (c) 2026 ggaze contributors
 * SPDX-License-Identifier: GPL-3.0-or-later
 *:*/

#include "trash.h"

#include <gio/gio.h>
#include <glib.h>

static char *
make_temp_dir(void) {
   GError *p_err = NULL;
   char   *c_dir = g_dir_make_tmp("ggaze-trash-XXXXXX", &p_err);
   g_assert_no_error(p_err);
   return (c_dir);
}

static GFile *
write_file(const char *c_dir, const char *c_name) {
   char   *c_path = g_build_filename(c_dir, c_name, NULL);
   GFile  *p_file = g_file_new_for_path(c_path);
   GError *p_err  = NULL;
   g_file_replace_contents(p_file, "x", 1, NULL, FALSE,
                           G_FILE_CREATE_REPLACE_DESTINATION, NULL, NULL,
                           &p_err);
   g_assert_no_error(p_err);
   g_free(c_path);
   return (p_file);
}

static void
cleanup_temp_dir(char *c_dir) {
   GFile           *p_dir = g_file_new_for_path(c_dir);
   GFileEnumerator *p_e =
      g_file_enumerate_children(p_dir, "standard::name,standard::type",
                                G_FILE_QUERY_INFO_NONE, NULL, NULL);
   if (p_e != NULL) {
      GFileInfo *p_info;
      while ((p_info = g_file_enumerator_next_file(p_e, NULL, NULL)) != NULL) {
         GFile *p_child = g_file_get_child(p_dir, g_file_info_get_name(p_info));
         if (g_file_info_get_file_type(p_info) == G_FILE_TYPE_DIRECTORY) {
            /* recursively clean a subdir (.Trash) */
            GFileEnumerator *p_e2 = g_file_enumerate_children(
               p_child, "standard::name", G_FILE_QUERY_INFO_NONE, NULL, NULL);
            if (p_e2 != NULL) {
               GFileInfo *p_i2;
               while ((p_i2 = g_file_enumerator_next_file(p_e2, NULL, NULL)) !=
                      NULL) {
                  GFile *p_c2 =
                     g_file_get_child(p_child, g_file_info_get_name(p_i2));
                  g_file_delete(p_c2, NULL, NULL);
                  g_object_unref(p_c2);
                  g_object_unref(p_i2);
               }
               g_object_unref(p_e2);
            }
         }
         g_file_delete(p_child, NULL, NULL);
         g_object_unref(p_child);
         g_object_unref(p_info);
      }
      g_object_unref(p_e);
   }
   g_file_delete(p_dir, NULL, NULL);
   g_object_unref(p_dir);
   g_free(c_dir);
}

static void
test_bin_and_restore(void) {
   char  *c_dir  = make_temp_dir();
   GFile *p_a    = write_file(c_dir, "a.jpg");
   GFile *p_b    = write_file(c_dir, "b.jpg");
   GFile *p_dirf = g_file_new_for_path(c_dir);

   Trash  *p_t   = trash_new(p_dirf);
   GError *p_err = NULL;

   /* Bin a.jpg → moves to .Trash/a.jpg. */
   g_assert_true(trash_bin(p_t, p_a, &p_err));
   g_assert_no_error(p_err);
   g_assert_false(g_file_query_exists(p_a, NULL)); /* gone from dir */
   g_assert_true(trash_can_undo(p_t));

   /* Restore → a.jpg back at original. */
   g_assert_true(trash_restore_last(p_t, &p_err));
   g_assert_no_error(p_err);
   g_assert_true(g_file_query_exists(p_a, NULL));
   g_assert_false(trash_can_undo(p_t));

   trash_delete(p_t);
   g_object_unref(p_a);
   g_object_unref(p_b);
   g_object_unref(p_dirf);
   cleanup_temp_dir(c_dir);
}

static void
test_collision_suffix(void) {
   char  *c_dir  = make_temp_dir();
   GFile *p_a1   = write_file(c_dir, "img.jpg");
   GFile *p_a2   = write_file(c_dir, "img-1.jpg");
   GFile *p_dirf = g_file_new_for_path(c_dir);

   Trash  *p_t   = trash_new(p_dirf);
   GError *p_err = NULL;

   /* Bin img.jpg → .Trash/img.jpg. */
   g_assert_true(trash_bin(p_t, p_a1, &p_err));
   g_assert_no_error(p_err);

   /* Bin img-1.jpg → .Trash/img-1.jpg (no collision yet). */
   g_assert_true(trash_bin(p_t, p_a2, &p_err));
   g_assert_no_error(p_err);

   /* Re-create img.jpg and bin it → .Trash/img-1.jpg would collide? No,
    * img.jpg was already binned. Create a new img.jpg and bin → collision
    * → .Trash/img-1.jpg (suffix). */
   GFile *p_a3 = write_file(c_dir, "img.jpg");
   g_assert_true(trash_bin(p_t, p_a3, &p_err));
   g_assert_no_error(p_err);

   /* Verify .Trash has 3 files. */
   GFile           *p_td = g_file_get_child(p_dirf, ".Trash");
   GFileEnumerator *p_e  = g_file_enumerate_children(
      p_td, "standard::name", G_FILE_QUERY_INFO_NONE, NULL, NULL);
   guint u_count = 0;
   if (p_e != NULL) {
      GFileInfo *p_info;
      while ((p_info = g_file_enumerator_next_file(p_e, NULL, NULL)) != NULL) {
         u_count++;
         g_object_unref(p_info);
      }
      g_object_unref(p_e);
   }
   g_assert_cmpint(u_count, ==, 3);
   g_object_unref(p_td);
   g_object_unref(p_a3);

   trash_delete(p_t);
   g_object_unref(p_a1);
   g_object_unref(p_a2);
   g_object_unref(p_dirf);
   cleanup_temp_dir(c_dir);
}

static void
test_permanent_delete(void) {
   char  *c_dir  = make_temp_dir();
   GFile *p_a    = write_file(c_dir, "a.jpg");
   GFile *p_dirf = g_file_new_for_path(c_dir);

   Trash  *p_t   = trash_new(p_dirf);
   GError *p_err = NULL;

   g_assert_true(trash_permanently_delete(p_t, p_a, &p_err));
   g_assert_no_error(p_err);
   g_assert_false(g_file_query_exists(p_a, NULL));
   g_assert_false(trash_can_undo(p_t)); /* permanent delete is not undoable */

   trash_delete(p_t);
   g_object_unref(p_a);
   g_object_unref(p_dirf);
   cleanup_temp_dir(c_dir);
}

int
main(int i_argc, char **c_argv) {
   g_test_init(&i_argc, &c_argv, NULL);
   g_test_add_func("/trash/bin_and_restore", test_bin_and_restore);
   g_test_add_func("/trash/collision_suffix", test_collision_suffix);
   g_test_add_func("/trash/permanent_delete", test_permanent_delete);
   return (g_test_run());
}