~ximion/listaller/master

1202 by Matthias Klumpp
tests: Port to new testing infrastructure
1
/* testcommon.vala -- Basic testing infrastructure
2
 *
1341 by Matthias Klumpp
Happy new year!
3
 * Copyright (C) 2012-2014 Matthias Klumpp <matthias@tenstral.net>
1202 by Matthias Klumpp
tests: Port to new testing infrastructure
4
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 *
18
 * Author:
19
 * 	Matthias Klumpp <matthias@tenstral.net>
20
 */
21
22
using GLib;
23
24
/**
25
 * A generic Listaller testing environment
26
 */
27
private class TestEnvironment {
28
	private string test_env_name;
29
	private string test_tmpdir;
30
31
	private string tmp_home_dir;
32
	private string tmp_home_data_dir;
33
	private string tmp_home_config_dir;
34
	private string tmp_home_cache_dir;
35
36
	public TestEnvironment (string test_name) {
37
		test_env_name = test_name;
38
39
		// build tmp directory name
40
		string base_tmp_path = Path.build_filename ("/var", "tmp", "listaller", "unit-tests", null);
41
		Listaller.Utils.create_dir_structure (base_tmp_path);
42
		string template = Path.build_filename (base_tmp_path, "%s-XXXXXX".printf (test_env_name), null);
43
44
		string res = DirUtils.mkdtemp (template);
45
		if (res == null) {
46
			error ("Unable to create temporary test dir! Error: %s", strerror (errno));
47
		}
48
		test_tmpdir = res;
49
50
		// create fake dirs
51
		tmp_home_dir = Path.build_filename (test_tmpdir, "home", null);
52
		tmp_home_data_dir = Path.build_filename (tmp_home_dir, "data", null);
53
		tmp_home_config_dir = Path.build_filename (tmp_home_dir, "config", null);
54
		tmp_home_cache_dir = Path.build_filename (tmp_home_dir, "cache", null);
55
	}
56
1222 by Matthias Klumpp
tests: Greatly improve updater test and testing framework in general
57
	private void clear_environment_variables () {
58
		string[] vars = Environment.list_variables ();
59
		foreach (string s in vars)
60
			Environment.unset_variable (s);
61
	}
62
1229 by Matthias Klumpp
tests: Fix FooBar building test
63
	public void create_environment (bool clear_standard_env = true) {
64
		if (clear_standard_env)
65
			clear_environment_variables ();
1222 by Matthias Klumpp
tests: Greatly improve updater test and testing framework in general
66
1202 by Matthias Klumpp
tests: Port to new testing infrastructure
67
		// make sure directories exist
68
		Listaller.Utils.create_dir_structure (tmp_home_dir);
69
		Listaller.Utils.create_dir_structure (tmp_home_data_dir);
70
		Listaller.Utils.create_dir_structure (tmp_home_config_dir);
71
		Listaller.Utils.create_dir_structure (tmp_home_cache_dir);
72
73
		// set environment variables
74
		Environment.set_variable ("HOME", tmp_home_dir, true);
75
		Environment.set_variable ("XDG_HOME_DIR", tmp_home_dir, true);
76
		Environment.set_variable ("XDG_DATA_HOME", tmp_home_data_dir, true);
77
		Environment.set_variable ("XDG_CONFIG_HOME", tmp_home_config_dir, true);
78
		Environment.set_variable ("XDG_CACHE_HOME", tmp_home_cache_dir, true);
79
80
		// PackageKit limits the use of GIO, and we don't want (and can't) to use it's advanced
81
		// features in Listaller too.
82
		Environment.set_variable ("GIO_USE_VFS", "local", true);
83
	}
84
85
	public void listaller_set_unittestmode (bool enabled) {
86
		Listaller.Utils.__unittestmode = enabled;
87
	}
88
89
	public void enforce_full_verbosity () {
90
		Environment.set_variable ("G_MESSAGES_DEBUG", "all", true);
91
	}
92
93
	public void init ([CCode (array_length_pos = 0.9)] ref unowned string[] args) {
94
		Test.init (ref args);
95
96
		// set Listaller generic verbosity settings
97
		Listaller.set_console_mode (true);
98
		Listaller.set_verbose_mode (true);
99
		Listaller.add_log_domain ("LiTest");
100
101
		// switch Listaller internals to unittestmode
102
		listaller_set_unittestmode (true);
103
	}
104
105
	public void run () {
106
		Test.run ();
107
	}
108
1327 by Matthias Klumpp
tests: Compile with global valac flags
109
	public void add_func (string testpath, TestFunc test_funcvoid) {
1202 by Matthias Klumpp
tests: Port to new testing infrastructure
110
		Test.add_func ("/%s/%s".printf (test_env_name, testpath), test_funcvoid);
111
	}
1222 by Matthias Klumpp
tests: Greatly improve updater test and testing framework in general
112
113
	public void print_environment_vars () {
114
		string[] vars = Environment.list_variables ();
115
		string vars_str = "";
116
		foreach (string s in vars)
117
			vars_str += "%s = %s\n".printf (s, Environment.get_variable (s));
118
119
		stdout.printf ("Environment: %s", vars_str);
120
	}
1202 by Matthias Klumpp
tests: Port to new testing infrastructure
121
}