parsing file of irregular activity, requesting feedback on approach
Hi I'd like to get some ideas and feedback as to how you would tackle the task of parsing this file. I basically need the name and status of each subtests between "Starting Test" and "Finished Test" The issue is that sometimes there is no "Finished" and it goes straight to start due to a test crash.
I started my script going line by line trying to tackle all the scenarios but the logic started to get complex and convoluted so I couldn't help but think there must be a smarter approach to this. Any ideas, code, pseudocode would be greatly appreciated.
myfile.log
****Starting test of test1
Message: Total Tests: 5
Log 23w4arg
Log 3958300
Message: PASS : test1SubTest1(): Pass
Log 23w4arg
Log efasf0
Message: PASS : test1SubTest2(): Pass
Log eafsarg
Log 3asf0
Message: FAIL: test1SubTest3(): Pass
Log 23asffg
Log 3fsaf00
Message: PASS : test1SubTest4(): Pass
Log 23fsafg
Log 3ag300
Message: PASS : test1SubTest5(): Pass
Message: Totals: 4 passed, 1 failed, 0 skipped
****Finished testing test1
Log 3ag3aw0
Log 3ag340
****Starting test of test2
Message: Total Tests: 3
Log 23w4arg
Log 3958300
Message: PASS : test2SubTest1(): Pass
Log 23w4arg
Log efasf0
Message: PASS : test2SubTest2(): Pass
Log 3ag340
****Starting test of test3
Log 234rg
Log 394w300
Message: PASS : test3SubTest1(): Pass
Log 23w4gdg
Log esdsf开发者_高级运维0
Message: PASS : test3SubTest2(): Pass
Log eafjhg
Log 3hj0
Message: FAIL: test3SubTest3(): Pass
Message: Totals: 2 passed, 0 failed, 0 skipped
****Finished testing test3
<?php
$file = "log.txt";
$fh = fopen($file, "r");
$contents = fread($fh, filesize($file));
$lines = explode("\n", $contents);
foreach($lines as $l) {
$startStr = "****Starting test of ";
$l = trim($l);
if(stristr($l, $startStr)) {
$curTest = str_replace($startStr, "", $l);
echo "$curTest info:\n";
}
if(substr($l, 0, 7)=='Message') {
if(stristr($l, "Total Tests")) {
echo "$l\n";
}
else {
// Test result
$infoArr = explode(":", $l);
echo "$infoArr[2] completed with result $infoArr[1]\n";
}
}
}
?>
Produces:
test1 info:
Message: Total Tests: 5
test1SubTest1() completed with result PASS
test1SubTest2() completed with result PASS
test1SubTest3() completed with result FAIL
test1SubTest4() completed with result PASS
test1SubTest5() completed with result PASS
4 passed, 1 failed, 0 skipped completed with result Totals
test2 info:
Message: Total Tests: 3
test2SubTest1() completed with result PASS
test2SubTest2() completed with result PASS
test3 info:
test3SubTest1() completed with result PASS
test3SubTest2() completed with result PASS
test3SubTest3() completed with result FAIL
2 passed, 0 failed, 0 skipped completed with result Totals
I would add in some checking for failed tests and stuff, but this should be enough to get you started.
精彩评论