forked from leafo/lessphp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.php
More file actions
139 lines (118 loc) · 3.09 KB
/
Copy pathtest.php
File metadata and controls
139 lines (118 loc) · 3.09 KB
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
<?php
error_reporting(E_ALL);
/**
* Go through all files matching pattern in input directory
* and compile them, then compare them to paired file in
* output directory.
*/
$difftool = 'meld';
$input = array(
'dir' => 'inputs',
'glob' => '*.less',
);
$output = array(
'dir' => 'outputs',
'filename' => '%s.css',
);
$prefix = realpath(dirname(__FILE__));
require $prefix.'/../lessc.inc.php';
$compiler = new lessc();
$compiler->importDir = $input['dir'].'/test-imports';
$fa = 'Fatal Error: ';
if (php_sapi_name() != 'cli') {
exit($fa.$argv[0].' must be run in the command line.');
}
$exe = array_shift($argv); // remove filename
function flag($f) {
if (func_num_args() > 1) {
foreach (func_get_args() as $f) if (flag($f)) return true;
return false;
}
global $argv;
$pre = strlen($f) > 1 ? '--' : '-';
foreach ($argv as $a) {
if (preg_match('/^'.$pre.$f.'($|\s)/', $a)) return true;
}
return false;
}
if (flag('h', 'help')) {
exit('help me');
}
$input['dir'] = $prefix.'/'.$input['dir'];
$output['dir'] = $prefix.'/'.$output['dir'];
if (!is_dir($input['dir']) || !is_dir($output['dir']))
exit($fa." both input and output directories must exist\n");
// get the first non flag as search string
$searchString = null;
foreach ($argv as $a) {
if (strlen($a) > 0 && $a{0} != '-') {
$searchString = $a;
break;
}
}
$tests = array();
$matches = glob($input['dir'].'/'.(!is_null($searchString) ? '*'.$searchString : '' ).$input['glob']);
if ($matches) {
foreach ($matches as $fname) {
extract(pathinfo($fname)); // for $filename, from php 5.2
$tests[] = array(
'in' => $fname,
'out' => $output['dir'].'/'.sprintf($output['filename'], $filename),
);
}
}
$count = count($tests);
$compiling = flag('C');
$showDiff = flag('d', 'diff');
echo ($compiling ? "Compiling" : "Running")." $count test".($count == 1 ? '' : 's').":\n";
function dump($msgs, $depth = 1, $prefix=" ") {
if (!is_array($msgs)) $msgs = array($msgs);
foreach ($msgs as $m) {
echo str_repeat($prefix, $depth).' - '.$m."\n";
}
}
$fail_prefix = " ** ";
$i = 1;
foreach ($tests as $test) {
printf(" [Test %04d/%04d] %s -> %s\n", $i, $count, basename($test['in']), basename($test['out']));
try {
ob_start();
$parsed = trim($compiler->parse(file_get_contents($test['in'])));
ob_end_clean();
} catch (exception $e) {
dump(array(
"Failed to compile input, reason:",
$e->getMessage(),
"Aborting"
), 1, $fail_prefix);
break;
}
if ($compiling) {
file_put_contents($test['out'], $parsed);
} else {
if (!is_file($test['out'])) {
dump(array(
"Failed to find output file: $test[out]",
"Maybe you forgot to compile tests?",
"Aborting"
), 1, $fail_prefix);
break;
}
$expected = trim(file_get_contents($test['out']));
if ($expected != $parsed) {
if ($showDiff) {
dump("Failed:", 1, $fail_prefix);
$tmp = $test['out'].".tmp";
file_put_contents($tmp, $parsed);
system($difftool.' '.$test['out'].' '.$tmp);
unlink($tmp);
dump("Aborting");
break;
} else dump("Failed, run with -d flag to view diff", 1, $fail_prefix);
} else {
dump("Passed");
}
}
$i++;
}
?>