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
|
/*
* Copyright (c) 2019 Alecaddd (http://alecaddd.com)
*
* This file is part of Akira.
*
* Akira is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* Akira 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 General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with Akira. If not, see <https://www.gnu.org/licenses/>.
*
* Authored by: Alessandro "Alecaddd" Castellani <castellani.ale@gmail.com>
*/
public delegate void Akira.TestCaseFunc ();
public GLib.SettingsSchemaSource schema_source;
private class Akira.TestSuiteAdaptor {
public string name;
private Akira.TestCaseFunc func;
private Akira.TestSuite test_suite;
public TestSuiteAdaptor (string name, owned Akira.TestCaseFunc test_case_func,
Akira.TestSuite test_suite) {
this.name = name;
this.func = (owned) test_case_func;
this.test_suite = test_suite;
}
public void setup (void* fixture) {
this.test_suite.setup ();
}
public void run (void* fixture) {
this.func ();
}
public void teardown (void* fixture) {
this.test_suite.teardown ();
}
public GLib.TestCase get_g_test_case () {
return new GLib.TestCase (this.name,
this.setup,
this.run,
this.teardown);
}
}
public class Akira.TestSuite : GLib.Object {
private GLib.TestSuite g_test_suite;
private TestSuiteAdaptor[] adaptors = new TestSuiteAdaptor[0];
public Akira.Application app;
public TestSuite () {
var name = this.get_name ();
this.g_test_suite = new GLib.TestSuite (name);
app = new Akira.Application ();
}
public string get_name () {
return this.get_type ().name ();
}
public GLib.TestSuite get_g_test_suite () {
return this.g_test_suite;
}
public void add_test (string name, owned Akira.TestCaseFunc func) {
var adaptor = new TestSuiteAdaptor (name, (owned) func, this);
this.adaptors += adaptor;
this.g_test_suite.add (adaptor.get_g_test_case ());
}
public virtual void setup () {}
public virtual void teardown () {}
}
public class Akira.TestRunner : GLib.Object {
private GLib.TestSuite root_suite;
private GLib.File tmp_dir;
private const string SCHEMA_FILE_NAME = "com.github.akiraux.akira.gschema.xml.in";
public TestRunner (GLib.TestSuite? root_suite = null) {
if (root_suite == null) {
this.root_suite = GLib.TestSuite.get_root ();
} else {
this.root_suite = root_suite;
}
}
public void add (Akira.TestSuite test_suite) {
this.root_suite.add_suite (test_suite.get_g_test_suite ());
}
private void setup_settings () {
Environment.set_variable ("GSETTINGS_BACKEND", "memory", true);
Environment.set_variable ("GSETTINGS_SCHEMA_DIR", this.tmp_dir.get_path (), true);
/* prepare temporary settings */
var target_schema_path = this.tmp_dir.get_path ();
try {
var top_builddir = TestRunner.get_top_builddir ();
var source_schema_file = GLib.File.new_for_path (
Path.build_filename (top_builddir, "data/schemas", SCHEMA_FILE_NAME));
var target_schema_file = GLib.File.new_for_path (
Path.build_filename (target_schema_path, SCHEMA_FILE_NAME));
source_schema_file.copy (target_schema_file,
GLib.FileCopyFlags.OVERWRITE);
} catch (GLib.Error error) {
GLib.error ("Error copying schema file: %s", error.message);
}
var compile_schemas_result = 0;
try {
GLib.Process.spawn_command_line_sync (
"glib-compile-schemas %s".printf (target_schema_path),
null,
null,
out compile_schemas_result);
} catch (GLib.SpawnError error) {
GLib.error (error.message);
}
if (compile_schemas_result != 0) {
GLib.error ("Could not compile schemas '%s'.", target_schema_path);
}
}
public virtual void global_setup () {
Environment.set_variable ("LANGUAGE", "C", true);
try {
this.tmp_dir = GLib.File.new_for_path (GLib.DirUtils.make_tmp ("gnome-Akira-test-XXXXXX"));
} catch (GLib.Error error) {
GLib.error ("Error creating temporary directory for test files: %s".printf (error.message));
}
this.setup_settings ();
}
public virtual void global_teardown () {
if (this.tmp_dir != null) {
var tmp_dir_path = this.tmp_dir.get_path ();
var delete_tmp_result = 0;
try {
GLib.Process.spawn_command_line_sync (
"rm -rf %s".printf (tmp_dir_path),
null,
null,
out delete_tmp_result);
} catch (GLib.SpawnError error) {
GLib.warning (error.message);
}
if (delete_tmp_result != 0) {
GLib.warning ("Could not delete temporary directory '%s'", tmp_dir_path);
}
}
}
public int run () {
/* TODO: spawn a child process to tun tests, if it fails than we
will be able to exit cleanly */
this.global_setup ();
var exit_status = GLib.Test.run ();
this.global_teardown ();
return exit_status;
}
private static string get_top_builddir () {
var builddir = Environment.get_variable ("top_builddir");
if (builddir == null) {
var dir = GLib.File.new_for_path (Environment.get_current_dir ());
while (dir != null) {
var schema_path = GLib.Path.build_filename (dir.get_path (),
"data/schemas",
SCHEMA_FILE_NAME);
if (GLib.FileUtils.test (schema_path, GLib.FileTest.IS_REGULAR)) {
builddir = dir.get_path ();
break;
}
dir = dir.get_parent ();
}
}
if (builddir == null) {
/* fallback to parent dir, test should be ran from 'tests' dir */
builddir = "..";
}
return builddir;
}
}
|