/
csv2testrail.php
392 lines (334 loc) · 8.65 KB
/
csv2testrail.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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#!/usr/bin/php
<?php
/**
* This is a simple PHP command line script for migrating CSV/Excel
* files to TestRail's import/export files.
*
* Copyright 2010 Gurock Software GmbH. All rights reserved.
* http://www.gurock.com - contact@gurock.com
*
**********************************************************************
*
* Learn more about this migration script on its project website:
*
* http://code.gurock.com/p/testrail-migrate/
*
*/
error_reporting(E_ALL);
require_once(dirname(__FILE__) . '/lib/csv/Dialect.php');
require_once(dirname(__FILE__) . '/lib/csv/Reader/Abstract.php');
require_once(dirname(__FILE__) . '/lib/csv/Reader.php');
define('MODE_EXPORT', 1);
define('MODE_CSV', 2);
define('MODE_CASES', 3);
define('MODE_TREE', 4);
$section_count = 0;
$case_count = 0;
/*********************************************************************/
function xml_escape($str)
{
return str_replace(
array(
'&',
'"',
'\'',
'<',
'>'
),
array(
'&',
'"',
''',
'<',
'>'
),
$str);
}
/*********************************************************************/
function xml_write_tag($handle, $tag, $value)
{
// fprintf($handle, "<%s>%s</%s>\n", $tag, xml_escape($value), $tag);
fprintf($handle, "<%s>%s</%s>\n", $tag, iconv('UTF-8', 'UTF-8//IGNORE', xml_escape($value)), $tag);
}
/*********************************************************************/
function xml_write_html_tag($handle, $tag, $value)
{
xml_write_tag($handle, $tag, html_to_markdown($value));
}
/*********************************************************************/
function xml_write_opening_tag($handle, $tag)
{
fprintf($handle, "<%s>\n", $tag);
}
/*********************************************************************/
function xml_write_closing_tag($handle, $tag)
{
fprintf($handle, "</%s>\n", $tag);
}
/*********************************************************************/
function write_output($handle, $sections)
{
fprintf($handle, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
write_sections($handle, $sections);
}
/*********************************************************************/
function write_sections($handle, $sections)
{
xml_write_opening_tag($handle, 'sections');
foreach ($sections as $section)
{
write_section($handle, $section);
}
xml_write_closing_tag($handle, 'sections');
}
/*********************************************************************/
function write_cases($handle, $cases)
{
xml_write_opening_tag($handle, 'cases');
foreach ($cases as $case)
{
write_case($handle, $case);
}
xml_write_closing_tag($handle, 'cases');
}
/*********************************************************************/
function write_case_tags($handle, $tags, $force_tag = false)
{
foreach ($tags as $name => $value)
{
if (is_array($value))
{
if (preg_match('/collection\:(.+)/', $name, $matches))
{
write_case_tags($handle, $value, $matches[1]);
}
else
{
xml_write_opening_tag($handle, $force_tag ?
$force_tag : $name);
write_case_tags($handle, $value);
xml_write_closing_tag($handle, $force_tag ?
$force_tag : $name);
}
}
else
{
xml_write_tag($handle, $name, $value);
}
}
}
/*********************************************************************/
function write_case($handle, $case)
{
if (!(isset($case['title']) || $case['title']))
{
return; // Cases without title/name are ignored
}
global $case_count;
$case_count++;
xml_write_opening_tag($handle, 'case');
unset($case['section']);
write_case_tags($handle, $case);
xml_write_closing_tag($handle, 'case');
}
/*********************************************************************/
function write_section($handle, $section)
{
if (!$section->name)
{
return; // Sections without name are ignored
}
global $section_count;
$section_count++;
xml_write_opening_tag($handle, 'section');
xml_write_tag($handle, 'name', $section->name);
if ($section->sections)
{
write_sections($handle, $section->sections);
}
if ($section->cases)
{
write_cases($handle, $section->cases);
}
xml_write_closing_tag($handle, 'section');
}
/*********************************************************************/
function print_line($line)
{
print "$line\n";
}
/*********************************************************************/
function print_error_and_exit($error)
{
print("$error\n");
exit(1);
}
/*********************************************************************/
function print_usage_and_exit($error = false)
{
if ($error)
{
$error .= "\n";
}
else
{
$error = '';
}
global $argv;
$usage = sprintf("Usage: %s <filter-script> <input-file> <output-file> [mode] [delimiter]\n",
$argv[0]) .
"Copyright 2010 Gurock Software GmbH. All rights reserved.
<filter-script> a PHP script to extract the CSV data for conversion.
See the project website for more details.
<input-file> should be the filename of a CSV file with test cases
you want to convert (for example, an exported Excel file).
<output-file> specifies the filename of the resulting TestRail
import/export file.
[mode] An optional mode. The following modes are available:
--export The default behavior; exports the data to the XML file.
--csv For debugging: prints the CSV data as seen by the script
--cases For debugging: prints the cases after the filter script
was called
--tree For debugging: prints the section/case tree after analyzing
the cases and sections
[delimiter] Allows you to override the default comma delimiter.";
print_error_and_exit($error . $usage);
}
/*********************************************************************/
function read_input($filename, $delimiter)
{
$dialect = new Csv_Dialect();
$dialect->delimiter = $delimiter;
try
{
$reader = new Csv_Reader($filename, $dialect);
}
catch (Exception $e)
{
print_error_and_exit('Could not open the specified input file');
}
$rows = $reader->toArray();
return $rows;
}
/*********************************************************************/
function filter_csv_input($csv)
{
$result = custom_filter($csv);
if (!$result)
{
print_error_and_exit("The 'custom_filter' function didn't " .
"return any data");
}
return $result;
}
/*********************************************************************/
function build_tree($cases)
{
$top_sections = array();
$count = 0;
foreach($cases as $case)
{
// Find the sections for this case
if (isset($case['section']) && $case['section'])
{
$section_names = preg_split('/\>/', $case['section']);
$section_names = array_map('trim', $section_names);
if (count($section_names) == 0)
{
print_error_and_exit("Case $count has no sections defined.");
}
}
else
{
$section_names = array('Unnamed');
}
// Find/build the sections and attach the case
$current_sections =& $top_sections;
foreach ($section_names as $name)
{
$key = strtolower($name);
if (!isset($current_sections[$key]))
{
$current_sections[$key] = new stdClass();
$current_sections[$key]->sections = array();
$current_sections[$key]->cases = array();
$current_sections[$key]->name = $name;
}
$current_cases =& $current_sections[$key]->cases;
$current_sections =& $current_sections[$key]->sections;
}
$current_cases[] = $case;
$count++;
}
return $top_sections;
}
/*********************************************************************/
if (count($argv) < 4)
{
print_usage_and_exit();
}
$filter = $argv[1];
$in = $argv[2];
$out = $argv[3];
$mode = isset($argv[4]) ? strtolower($argv[4]) : '--export';
switch ($mode)
{
case '--export':
$mode = MODE_EXPORT;
break;
case '--csv':
$mode = MODE_CSV;
break;
case '--cases':
$mode = MODE_CASES;
break;
case '--tree':
$mode = MODE_TREE;
break;
default:
print_usage_and_exit('Unknow mode specified');
}
// Process the input file
if (!file_exists($in))
{
print_usage_and_exit("File $in not found");
}
$csv = read_input($in, isset($argv[5]) ? $argv[5] : ',');
if ($mode == MODE_CSV)
{
print_r($csv);
exit(1);
}
// Filter the CSV data for conversion
if (!@include($filter))
{
print_usage_and_exit("Could not include filter script $in");
}
if (!function_exists('custom_filter'))
{
print_error_and_exit("Filter function 'custom_filter' not " .
"defined in filter script");
}
$cases = filter_csv_input($csv);
if ($mode == MODE_CASES)
{
print_r($cases);
exit(1);
}
// Generate a section tree
$sections = build_tree($cases);
if ($mode == MODE_TREE)
{
print_r($sections);
exit(1);
}
// Write the output
$handle = fopen($out, 'w');
if (!$handle)
{
print_error_and_exit('Could not create output file');
}
write_output($handle, $sections);
fclose($handle);
print("Successfully converted $section_count sections and $case_count cases\n");
?>