-
Notifications
You must be signed in to change notification settings - Fork 2
/
Pakefile.php
180 lines (139 loc) · 5.2 KB
/
Pakefile.php
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
<?php
if (!function_exists('pake_require_version'))
throw new pakeException('Your version of Pake is too old. Upgrade');
ini_set('display_errors', 'On');
define('_SRC_DIR_', dirname(__FILE__));
pake_require_version('1.6.1');
// "public" tasks
pake_desc('Init tests');
pake_task('init_tests', 'clean_tests');
pake_desc('Init test-database');
pake_task('init_test_db', 'init_tests', 'clean_test_db', 'enable_midgard');
// "private" tasks
pake_task('clean_tests');
pake_task('clean_test_db', 'enable_midgard');
pake_task('enable_midgard');
// Adding standard extension-building tasks
pake_import('phpExtension', false);
// -> injecting dependencies
pakePhpExtensionTask::$tasks['test'][1][] = 'init_tests';
pakePhpExtensionTask::$tasks['test'][1][] = 'init_test_db';
pakePhpExtensionTask::$tasks['clean'][1][] = 'clean_test_db';
pakePhpExtensionTask::$tasks['clean'][1][] = 'clean_tests';
// -> registering tasks
pakePhpExtensionTask::import_default_tasks();
// DONE
function run_init_test_db()
{
$cfg = _get_midgard_config();
pake_echo_comment('Creating empty database for tests… (be patient, it takes time)');
putenv('MIDGARD_ENV_GLOBAL_SHAREDIR='._SRC_DIR_.'/tests/share'.'');
putenv('PAKE_MIDGARD_CFG='._SRC_DIR_.'/tests/test.cfg');
pake_sh(_get_php_executable().' -c '.escapeshellarg(_SRC_DIR_.'/tests/test.ini').' '.escapeshellarg(_SRC_DIR_.'/pake/create_database.php'), false);
}
function run_init_tests($task, $args, $long_args)
{
pake_echo_comment('Creating tests from templates');
$path = _SRC_DIR_.'/tests';
$src_path = _SRC_DIR_.'/tests_templates';
$tokens = array(
'PATH' => $path,
'SHARE_PATH' => $path.'/share',
'BLOB_PATH' => $path.'/blobs',
'CFG_FILE' => $path.'/test.cfg'
);
if (isset($long_args['mysql'])) {
$tokens['DB_TYPE'] = 'MySQL';
$tokens['DB_NAME'] = pake_input('Database name?', 'midgard');
$tokens['DB_USER'] = pake_input('Database user?', 'midgard');
$tokens['DB_PASS'] = pake_input('Database password?', 'midgard');
$tokens['DB_HOST'] = pake_input('Database host?', 'localhost');
} else {
$tokens['DB_TYPE'] = 'SQLite';
$tokens['DB_NAME'] = 'test';
$tokens['DB_USER'] = '';
$tokens['DB_PASS'] = '';
$tokens['DB_HOST'] = '';
}
$files = pakeFinder::type('file')->ignore_version_control()->relative()->in($src_path);
pake_replace_tokens_to_dir($files, $src_path, $path, '[[', ']]', $tokens);
}
function run_clean_tests()
{
$path = _SRC_DIR_.'/tests';
$src_path = _SRC_DIR_.'/tests_templates';
// clean runtime-generated files
$finder = pakeFinder::type('file')->ignore_version_control()->name('*.php', '*.exp', '*.out', '*.log', '*.diff', '*.sh', '*.mem');
pake_remove($finder, $path);
// clean "generated" tests
$_files = pakeFinder::type('file')->ignore_version_control()->relative()->in($src_path);
$finder = pakeFinder::type('file')->ignore_version_control();
call_user_func_array(array($finder, 'name'), $_files);
pake_remove($finder, $path);
pake_remove(pakeFinder::type('file')->name('tmp-php.ini'), _SRC_DIR_);
}
function run_clean_test_db($task, $args, $long_args)
{
if (isset($long_args['mysql'])) {
$db_name = pake_input('Database name?', 'midgard');
$db_user = pake_input('Database user?', 'midgard');
$db_pass = pake_input('Database password?', 'midgard');
$db_host = pake_input('Database host?', 'localhost');
$db = new pakeMySQL($db_user, $db_pass, $db_host);
$db->dropDatabase($db_name);
$db->createDatabase($db_name);
} else {
$file = _SRC_DIR_.'/tests/'._get_midgard_config()->database.'.db';
if (file_exists($file))
pake_remove($file, '');
}
}
function run_enable_midgard()
{
pake_echo_comment('Enabling midgard2 extension');
if (!extension_loaded('midgard2')) {
throw new LogicException('Please load midgard2-extension in php.ini');
}
if (ini_get('enable_dl') != 1) {
throw new LogicException('Please enable "enable_dl" setting in php.ini. it is required for proper work of tests');
}
ini_set('midgard.http', 'Off');
//dl('midgard2.so');
}
// Support tools
function _get_midgard_config()
{
static $cfg = null;
if (null === $cfg) {
$cfg = new midgard_config();
$cfg->read_file_at_path(_SRC_DIR_.'/tests/test.cfg');
}
return $cfg;
}
function _get_php_executable()
{
static $php_exec = null;
if (null === $php_exec) {
if (!file_exists(_SRC_DIR_.'/Makefile')) {
throw new LogicException("Makefile is missing. You have to build extension before testing it!");
}
$makefile = file(_SRC_DIR_.'/Makefile');
foreach ($makefile as $row) {
if (strpos($row, 'PHP_EXECUTABLE = ') !== 0)
continue;
$row = rtrim($row);
$parts = explode(' = ', $row);
if (!isset($parts[1]) or strlen($parts[1]) == 0)
continue;
$php_exec = $parts[1];
break;
}
unset($makefile);
}
if (!$php_exec)
{
// Fallback
$php_exec = 'php';
}
return $php_exec;
}