Statistics
| Revision:

root / trunk / tests / common.c @ 1843

History | View | Annotate | Download (3.1 KB)

1
/* 
2
    Copyright (C) 2005-2011  Erik van Pienbroek
3

                
4
    This program is free software; you can redistribute it and/or modify
5
    it under the terms of the GNU General Public License as published by
6
    the Free Software Foundation; either version 2 of the License, or
7
    (at your option) any later version.
8

                
9
    This program 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
12
    GNU General Public License for more details.
13

                
14
    You should have received a copy of the GNU General Public License
15
    along with this program; if not, write to the Free Software
16
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
*/
18

                
19
#include 
20
#include 
21
#include 
22
#include 
23

                
24
#ifdef WIN32
25
#include 
26
#else
27
#include 
28
#include 
29
#endif
30

                
31
static char *
32
generate_tmpdir(void)
33
{
34
    char *tmpdir;
35
    pid_t pid;
36
    char pid_str[16];
37

                
38
    memset(&pid_str, 0, sizeof(pid_str));
39
    pid = getpid();
40
    snprintf(pid_str, sizeof(pid_str) - 1, "%i", (int) pid);
41
    tmpdir = g_build_filename(g_get_tmp_dir(), "nntpgrab_tests", pid_str, NULL);
42

                
43
    return tmpdir;
44
}
45

                
46
char *
47
get_location_of_download_queue(void)
48
{
49
    char *tmpdir = generate_tmpdir();
50
    char *ret;
51

                
52
    ret = g_build_filename(tmpdir, "NNTPGrab", "download_queue.db", NULL);
53

                
54
    g_free(tmpdir);
55

                
56
    return ret;
57
}
58

                
59
void
60
initialize_tests(void)
61
{
62
    char *tmpdir;
63
    char *errmsg = NULL;
64

                
65
    /* Create a temporary folder to perform the tests */
66
    tmpdir = generate_tmpdir();
67
    if (g_mkdir_with_parents(tmpdir, 0700) == -1) {
68
        g_error("Unable to create temporary folder named '%s'. Unable to continue tests: %s\n", tmpdir, errmsg);
69
    }
70
    g_setenv("NNTPGRAB_CONFIG_DIR", tmpdir, TRUE);
71

                
72
    g_free(tmpdir);
73
}
74

                
75
void
76
cleanup_tests(void)
77
{
78
    char *path;
79
    char *tmpdir = generate_tmpdir();
80

                
81
    /* Remove: /tmp/nntpgrab_tests/$PID/NNTPGrab/download_queue.db */
82
    path = g_build_filename(tmpdir, "NNTPGrab", "download_queue.db", NULL);
83
    if (g_unlink(path) == -1) {
84
        g_error("Unable to remove file '%s': %s\n", path, strerror(errno));
85
    }
86
    g_free(path);
87

                
88
    path = g_build_filename(tmpdir, "NNTPGrab", "nntpgrab.conf", NULL);
89
    if (g_unlink(path) == -1) {
90
        g_error("Unable to remove file '%s': %s\n", path, strerror(errno));
91
    }
92
    g_free(path);
93

                
94
    /* Remove: /tmp/nntpgrab_tests/$PID/NNTPGrab */
95
    path = g_build_filename(tmpdir, "NNTPGrab", NULL);
96
    if (g_rmdir(path) == -1) {
97
        g_error("Unable to remove folder '%s': %s\n", path, strerror(errno));
98
    }
99
    g_free(path);
100

                
101
    /* Remove: /tmp/nntpgrab_tests/$PID */
102
    if (g_rmdir(tmpdir) == -1) {
103
        g_error("Unable to remove folder '%s': %s\n", tmpdir, strerror(errno));
104
    }
105

                
106
    /* Remove: /tmp/nntpgrab_tests */
107
    path = g_path_get_dirname(tmpdir);
108
    if (g_rmdir(path) == -1) {
109
        /* No need to give an error here as other tests may still be running */
110
        //g_error("Unable to remove folder '%s': %s\n", path, strerror(errno));
111
    }
112
    g_free(path);
113

                
114
    g_free(tmpdir);
115
}