File: test-cal-cache-utils.c

package info (click to toggle)
evolution-data-server 3.56.2-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 47,108 kB
  • sloc: ansic: 365,415; xml: 578; cpp: 482; perl: 297; sh: 62; makefile: 60; python: 35; javascript: 29
file content (233 lines) | stat: -rw-r--r-- 6,715 bytes parent folder | download | duplicates (2)
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
 * Copyright (C) 2017 Red Hat, Inc. (www.redhat.com)
 *
 * This library is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library. If not, see <http://www.gnu.org/licenses/>.
 */

#include "evolution-data-server-config.h"

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

#ifndef G_OS_WIN32
#include <sys/wait.h>
#endif

#include "test-cal-cache-utils.h"

static const gchar *args_data_dir = NULL;

void
tcu_read_args (gint argc,
	       gchar **argv)
{
	gint ii;

	for (ii = 0; ii < argc; ii++) {
		if (g_strcmp0 (argv[ii], "--data-dir") == 0) {
			if (ii + 1 < argc)
				args_data_dir = argv[ii + 1];
			break;
		}
	}
}

static void
delete_work_directory (const gchar *filename)
{
	/* XXX Instead of complex error checking here, we should ideally use
	 * a recursive GDir / g_unlink() function.
	 *
	 * We cannot use GFile and the recursive delete function without
	 * corrupting our contained D-Bus environment with service files
	 * from the OS.
	 */
	const gchar *argv[] = { "/bin/rm", "-rf", filename, NULL };
	gboolean spawn_succeeded;
	gint exit_status;

	spawn_succeeded = g_spawn_sync (
		NULL, (gchar **) argv, NULL, 0, NULL, NULL,
					NULL, NULL, &exit_status, NULL);

	g_assert_true (spawn_succeeded);
	#ifndef G_OS_WIN32
	g_assert_true (WIFEXITED (exit_status));
	g_assert_cmpint (WEXITSTATUS (exit_status), ==, 0);
	#else
	g_assert_cmpint (exit_status, ==, 0);
	#endif
}

void
tcu_fixture_setup (TCUFixture *fixture,
		   gconstpointer user_data)
{
	const TCUClosure *closure = user_data;
	gchar *filename, *directory;
	GError *error = NULL;

	/* Cleanup from last test */
	directory = g_build_filename (g_get_tmp_dir (), "test-cal-cache", NULL);
	delete_work_directory (directory);
	g_free (directory);
	filename = g_build_filename (g_get_tmp_dir (), "test-cal-cache", "cache.db", NULL);

	fixture->cal_cache = e_cal_cache_new (filename, NULL, &error);

	if (!fixture->cal_cache)
		g_error ("Failed to create the ECalCache: %s", error->message);

	g_free (filename);

	if (closure) {
		if (closure->load_set == TCU_LOAD_COMPONENT_SET_EVENTS || closure->load_set == TCU_LOAD_COMPONENT_SET_EVENTS_WITH_0) {
			tcu_add_component_from_test_case (fixture, "event-1", NULL);
			tcu_add_component_from_test_case (fixture, "event-2", NULL);
			tcu_add_component_from_test_case (fixture, "event-3", NULL);
			tcu_add_component_from_test_case (fixture, "event-4", NULL);
			tcu_add_component_from_test_case (fixture, "event-5", NULL);
			tcu_add_component_from_test_case (fixture, "event-6", NULL);
			tcu_add_component_from_test_case (fixture, "event-6-a", NULL);
			tcu_add_component_from_test_case (fixture, "event-7", NULL);
			tcu_add_component_from_test_case (fixture, "event-8", NULL);
			tcu_add_component_from_test_case (fixture, "event-9", NULL);

			if (closure->load_set == TCU_LOAD_COMPONENT_SET_EVENTS_WITH_0)
				tcu_add_component_from_test_case (fixture, "event-0", NULL);
		} else if (closure->load_set == TCU_LOAD_COMPONENT_SET_TASKS) {
			tcu_add_component_from_test_case (fixture, "task-1", NULL);
			tcu_add_component_from_test_case (fixture, "task-2", NULL);
			tcu_add_component_from_test_case (fixture, "task-3", NULL);
			tcu_add_component_from_test_case (fixture, "task-4", NULL);
			tcu_add_component_from_test_case (fixture, "task-5", NULL);
			tcu_add_component_from_test_case (fixture, "task-6", NULL);
			tcu_add_component_from_test_case (fixture, "task-7", NULL);
			tcu_add_component_from_test_case (fixture, "task-8", NULL);
			tcu_add_component_from_test_case (fixture, "task-9", NULL);
		}
	}
}

void
tcu_fixture_teardown (TCUFixture *fixture,
		      gconstpointer user_data)
{
	g_object_unref (fixture->cal_cache);
}

gchar *
tcu_get_test_case_filename (const gchar *case_name)
{
	gchar *filename;
	gchar *case_filename;

	case_filename = g_strdup_printf ("%s.ics", case_name);

	/* In the case of installed tests, they run in ${pkglibexecdir}/installed-tests
	 * and the components are installed in ${pkglibexecdir}/installed-tests/components
	 */
	if (g_getenv ("TEST_INSTALLED_SERVICES") != NULL) {
		filename = g_build_filename (INSTALLED_TEST_DIR, "components", case_filename, NULL);
	} else {
		if (!args_data_dir) {
			g_warning ("Data directory not set, pass it with `--data-dir PATH`");
			exit(1);
		}

		filename = g_build_filename (args_data_dir, case_filename, NULL);
	}

	g_free (case_filename);

	return filename;
}

gchar *
tcu_new_icalstring_from_test_case (const gchar *case_name)
{
	gchar *filename;
	GFile * file;
	GError *error = NULL;
	gchar *icalstring = NULL, *uripart;

	filename = tcu_get_test_case_filename (case_name);

	file = g_file_new_for_path (filename);
	if (!g_file_load_contents (file, NULL, &icalstring, NULL, NULL, &error))
		g_error (
			"Failed to read test iCal string file '%s': %s",
			filename, error->message);

	g_free (filename);
	g_object_unref (file);

	uripart = strstr (icalstring, "$EVENT1URI$");
	if (uripart) {
		gchar *uri;
		GString *str;

		filename = tcu_get_test_case_filename ("event-1");
		uri = g_filename_to_uri (filename, NULL, NULL);
		g_free (filename);

		str = g_string_new_len (icalstring, uripart - icalstring);
		g_string_append (str, uri);
		g_string_append (str, uripart + 11);
		g_free (icalstring);
		g_free (uri);

		icalstring = g_string_free (str, FALSE);
	}

	return icalstring;
}

ECalComponent *
tcu_new_component_from_test_case (const gchar *case_name)
{
	gchar *icalstring;
	ECalComponent *component = NULL;

	icalstring = tcu_new_icalstring_from_test_case (case_name);
	if (icalstring)
		component = e_cal_component_new_from_string (icalstring);
	g_free (icalstring);

	if (!component)
		g_error (
			"Failed to construct component from test case '%s'",
			case_name);

	return component;
}

void
tcu_add_component_from_test_case (TCUFixture *fixture,
				  const gchar *case_name,
				  ECalComponent **out_component)
{
	ECalComponent *component;
	GError *error = NULL;

	component = tcu_new_component_from_test_case (case_name);

	if (!e_cal_cache_put_component (fixture->cal_cache, component, case_name, 0, E_CACHE_IS_ONLINE, NULL, &error))
		g_error ("Failed to add component: %s", error->message);

	if (out_component)
		*out_component = g_object_ref (component);

	g_clear_object (&component);
}