test_utils.c revision 65f03d4f644ce73618e5f4f50dd694b26f55ae12
1/*
2 * Copyright (C) 2010 Arno Renevier
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB.  If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20#include "test_utils.h"
21
22#include <glib.h>
23#include <glib/gstdio.h>
24
25int testutils_relative_chdir(const gchar* target_filename, const gchar* executable_path)
26{
27    if (g_path_is_absolute(executable_path)) {
28        if (g_chdir(g_path_get_dirname(executable_path))) {
29            return -1;
30        }
31    }
32
33    while (!g_file_test(target_filename, G_FILE_TEST_EXISTS)) {
34        gchar *path_name;
35        if (g_chdir("..")) {
36            return -1;
37        }
38        g_assert(!g_str_equal((path_name = g_get_current_dir()), "/"));
39        g_free(path_name);
40    }
41
42    gchar* dirname = g_path_get_dirname(target_filename);
43    if (g_chdir(dirname)) {
44        g_free(dirname);
45        return -1;
46    }
47
48    g_free(dirname);
49    return 0;
50}
51