Statistics
| Revision:

root / trunk / tests / common.c @ 1847

History | View | Annotate | Download (3.2 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
#include "common.h"
32

                
33
Configuration *config = NULL;
34

                
35
static char *
36
generate_tmpdir(void)
37
{
38
    char *tmpdir;
39
    pid_t pid;
40
    char pid_str[16];
41

                
42
    memset(&pid_str, 0, sizeof(pid_str));
43
    pid = getpid();
44
    snprintf(pid_str, sizeof(pid_str) - 1, "%i", (int) pid);
45
    tmpdir = g_build_filename(g_get_tmp_dir(), "nntpgrab_tests", pid_str, NULL);
46

                
47
    return tmpdir;
48
}
49

                
50
char *
51
get_location_of_download_queue(void)
52
{
53
    char *tmpdir = generate_tmpdir();
54
    char *ret;
55

                
56
    ret = g_build_filename(tmpdir, "NNTPGrab", "download_queue.db", NULL);
57

                
58
    g_free(tmpdir);
59

                
60
    return ret;
61
}
62

                
63
void
64
initialize_tests(void)
65
{
66
    char *tmpdir;
67
    char *errmsg = NULL;
68

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

                
76
    g_free(tmpdir);
77
}
78

                
79
void
80
cleanup_tests(void)
81
{
82
    char *path;
83
    char *tmpdir = generate_tmpdir();
84

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

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

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

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

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

                
118
    g_free(tmpdir);
119
}