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 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
#[macro_use] extern crate nom; use std::str; use nom::{line_ending, digit, space}; named!( rest_of_line<&str>, do_parse!( content: map_res!( nom::not_line_ending, str::from_utf8 ) >> line_ending >> (content) ) ); named!( compiling<Vec<()> >, many0!( do_parse!( ws!(tag!("Compiling")) >> rest_of_line >> () ) ) ); named!( finished<()>, do_parse!( ws!(tag!("Finished")) >> rest_of_line >> () ) ); named!( suite_line<&str>, do_parse!( ws!( alt!(tag!("Running") | tag!("Doc-tests")) ) >> name: rest_of_line >> (name) ) ); named!( suite_count<()>, do_parse!( ws!(tag!("running")) >> rest_of_line >> () ) ); named!( ok<&str>, map!(tag!("ok"), |_| "pass") ); named!( failed<&str>, map!(tag!("FAILED"), |_| "fail") ); named!( ok_or_failed<&str>, alt!(ok | failed) ); #[derive(Debug, PartialEq)] pub struct Test<'a, 'b, 'c> { pub name: &'a str, pub status: &'b str, pub error: Option<&'c str>, } named!( test_result<Test>, do_parse!( tag!("test") >> space >> name: map_res!( take_until_s!(" ..."), str::from_utf8 ) >> tag!(" ...") >> status: ws!(ok_or_failed) >> (Test { name: name, status: status, error: None }) ) ); named!( test_results<Vec<Test> >, many0!( test_result ) ); named!( digits<i64>, map_res!( map_res!( ws!(digit), str::from_utf8 ), str::FromStr::from_str ) ); #[derive(Debug, PartialEq)] pub struct SuiteResult<'a> { pub state: &'a str, pub passed: i64, pub failed: i64, pub ignored: i64, pub total: i64, pub measured: i64, } named!( suite_result<SuiteResult>, do_parse!( ws!(tag!("test result: ")) >> state: ok_or_failed >> char!('.') >> passed: digits >> tag!("passed;") >> failed: digits >> tag!("failed;") >> ignored: digits >> tag!("ignored;") >> measured: digits >> ws!(tag!("measured")) >> (SuiteResult { state:state, passed:passed, failed:failed, ignored:ignored, total: passed + failed + ignored, measured:measured }) ) ); named!( fail_line<&str>, do_parse!( ws!(tag!("----")) >> name: map_res!( take_until!(" "), str::from_utf8 ) >> ws!(tag!("stdout")) >> ws!(tag!("----")) >> (name) ) ); #[derive(Debug, PartialEq)] pub struct Failure<'a, 'b> { pub name: &'a str, pub error: &'b str, } named!( failure<Failure>, do_parse!( name: fail_line >> error: rest_of_line >> opt!( tag!("note: Run with `RUST_BACKTRACE=1` for a backtrace.") ) >> line_ending >> line_ending >> (Failure { name:name, error:error }) ) ); named!(failures<Vec<Failure> >, many1!(failure)); named!(fail_opt<Option<Vec<Failure> > >, opt!( do_parse!( ws!( tag!("failures:") ) >> f: failures >> take_until!( "test result: " ) >> (f) ) ) ); #[derive(Debug, PartialEq)] pub struct Suite<'a, 'b, 'c, 'd, 'e> { pub name: &'a str, pub state: &'b str, pub passed: i64, pub failed: i64, pub ignored: i64, pub measured: i64, pub total: i64, pub tests: Vec<Test<'c, 'd, 'e>>, } fn find_message_by_name<'a, 'b>(name: &str, failures: &Vec<Failure<'a, 'b>>) -> Option<&'b str> { failures.iter() .find(|x| x.name == name) .map(|x| x.error) } fn handle_parsed_suite<'a, 'b, 'c, 'd, 'e>(name: &'a str, tests: Vec<Test<'c, 'd, 'e>>, failures: Option<Vec<Failure<'e, 'e>>>, result: SuiteResult<'b>) -> Suite<'a, 'b, 'c, 'd, 'e> { let tests_with_failures = match failures { Some(xs) => { tests.iter() .map(|t| { Test { error: find_message_by_name(t.name, &xs), name: t.name, status: t.status, } }) .collect() } None => tests, }; Suite { name: name, tests: tests_with_failures, state: result.state, total: result.total, passed: result.passed, failed: result.failed, ignored: result.ignored, measured: result.measured, } } named!( suite_parser<Suite>, do_parse!( name: suite_line >> suite_count >> tests: test_results >> failures: fail_opt >> result: suite_result >> (handle_parsed_suite(name, tests, failures, result)) ) ); named!( suites_parser<Vec<Suite > >, many1!(suite_parser) ); named!( pub cargo_test_result_parser<Vec<Suite > >, do_parse!( compiling >> finished >> suites: suites_parser >> (suites) ) ); #[cfg(test)] mod parser_tests { use nom::IResult; use std::fmt::Debug; use super::{compiling, finished, suite_line, suite_count, ok_or_failed, Test, test_result, test_results, digits, suite_result, SuiteResult, cargo_test_result_parser, Suite, fail_line, failure, Failure, failures}; fn assert_done<R: PartialEq + Debug>(l: IResult<&[u8], R>, r: R) { assert_eq!( l, IResult::Done(&b""[..], r) ) } #[test] fn it_should_match_a_compiler_line() { let output = &b" Compiling docker-command v0.1.0 (file:///Users/joegrund/projects/docker-command-rs) "[..]; assert_done(compiling(output), vec![()]); } #[test] fn it_should_parse_finish_line() { let result = finished(&b" Finished debug [unoptimized + debuginfo] target(s) in 0.0 secs "[..]); assert_done(result, ()); } #[test] fn it_should_parse_suite_line() { let result = suite_line(&b"Running target/debug/deps/docker_command-be014e20fbd07382 "[..]); assert_done(result, "target/debug/deps/docker_command-be014e20fbd07382"); } #[test] fn it_should_parse_suite_count() { let result = suite_count(&b"running 0 tests "[..]); assert_done(result, ()); } #[test] fn it_should_match_ok() { assert_done(ok_or_failed(&b"ok"[..]), "pass"); } #[test] fn it_should_match_failed() { assert_done(ok_or_failed(&b"FAILED"[..]), "fail"); } #[test] fn it_should_parse_test_result() { let result = test_result(&b"test it_runs_a_command ... ok"[..]); assert_done(result, Test { name: "it_runs_a_command", status: "pass", error: None, }); } #[test] fn it_should_parse_test_results() { let result = test_results(&b"test tests::it_should_parse_first_line ... ok test tests::it_should_parse_a_status_line ... ok test tests::it_should_parse_test_output ... ok test tests::it_should_parse_suite_line ... FAILED "[..]); assert_done(result, vec![ Test { name: "tests::it_should_parse_first_line", status: "pass", error: None }, Test { name: "tests::it_should_parse_a_status_line", status: "pass", error: None }, Test { name: "tests::it_should_parse_test_output", status: "pass", error: None }, Test { name: "tests::it_should_parse_suite_line", status: "fail", error: None } ]); } #[test] fn it_should_capture_digits() { assert_done(digits(b"10"), 10); } #[test] fn it_should_parse_a_suite_result() { let result = suite_result(&b"test result: FAILED. 3 passed; 1 failed; 0 ignored; 0 measured"[..]); assert_done(result, SuiteResult { state: "fail", passed: 3, failed: 1, ignored: 0, total: 4, measured: 0, }); } #[test] fn it_should_parse_successful_test_output() { let output = &b" Finished debug [unoptimized + debuginfo] target(s) in 0.0 secs Running target/debug/cargo_test_junit-83252957c74e106d running 2 tests test tests::it_should_match_failed ... ok test tests::it_should_parse_first_line ... ok test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured "[..]; let result = cargo_test_result_parser(output); assert_done(result, vec![Suite { name: "target/debug/cargo_test_junit-83252957c74e106d", state: "pass", tests: vec![ Test { name: "tests::it_should_match_failed", status: "pass", error: None }, Test { name: "tests::it_should_parse_first_line", status: "pass", error: None } ], passed: 2, failed: 0, ignored: 0, measured: 0, total: 2 }]); } #[test] fn test_fail_line() { let output = b"---- fail stdout ----"; assert_done(fail_line(output), "fail"); } #[test] fn test_failure() { let output = b"---- fail stdout ---- thread 'fail' panicked at 'assertion failed: `(left == right)` (left: `1`, right: `2`)', tests/integration_test.rs:16 note: Run with `RUST_BACKTRACE=1` for a backtrace. "; assert_done(failure(output), Failure { name: "fail", error: "thread 'fail' panicked at 'assertion failed: `(left == right)` \ (left: `1`, right: `2`)', tests/integration_test.rs:16", }); } #[test] fn test_failures() { let output = b"---- fail stdout ---- thread 'fail' panicked at 'assertion failed: `(left == right)` (left: `1`, right: `2`)', tests/integration_test.rs:16 note: Run with `RUST_BACKTRACE=1` for a backtrace. ---- fail2 stdout ---- thread 'fail2' panicked at 'assertion failed: `(left == right)` (left: `3`, right: `2`)', tests/integration_test.rs:22 "; assert_done( failures(output), vec![ Failure { name: "fail", error: "thread 'fail' panicked at 'assertion failed: `(left == right)` (left: `1`, right: `2`)', tests/integration_test.rs:16" }, Failure { name: "fail2", error: "thread 'fail2' panicked at 'assertion failed: `(left == right)` (left: `3`, right: `2`)', tests/integration_test.rs:22" } ] ); } #[test] fn test_fail_run() { let output = b" Compiling blah v0.1.0 (file:blah) Finished debug [unoptimized + debuginfo] target(s) in 0.32 secs Running target/debug/deps/docker_command-be014e20fbd07382 running 0 tests test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured Running target/debug/integration_test-d4fc68dd5824cbb9 running 3 tests test fail ... FAILED test fail2 ... FAILED test it_runs_a_command ... ok failures: ---- fail stdout ---- thread 'fail' panicked at 'assertion failed: `(left == right)` (left: `1`, right: `2`)', tests/integration_test.rs:16 note: Run with `RUST_BACKTRACE=1` for a backtrace. ---- fail2 stdout ---- thread 'fail2' panicked at 'assertion failed: `(left == right)` (left: `3`, right: `2`)', tests/integration_test.rs:22 failures: fail fail2 test result: FAILED. 1 passed; 2 failed; 0 ignored; 0 measured error: test failed"; let x = match cargo_test_result_parser(output) { IResult::Done(_, x) => x, _ => panic!("BOOM!"), }; assert_eq!( x, vec![ Suite { name: "target/debug/deps/docker_command-be014e20fbd07382", state: "pass", passed: 0, failed: 0, ignored: 0, measured: 0, total: 0, tests: vec![] }, Suite { name: "target/debug/integration_test-d4fc68dd5824cbb9", state: "fail", passed: 1, failed: 2, ignored: 0, measured: 0, total: 3, tests: vec![ Test { name: "fail", status: "fail", error: Some("thread \'fail\' panicked at \'assertion failed: `(left == right)` (left: `1`, right: `2`)\', tests/integration_test.rs:16") }, Test { name: "fail2", status: "fail", error: Some("thread \'fail2\' panicked at \'assertion failed: `(left == right)` (left: `3`, right: `2`)\', tests/integration_test.rs:22") }, Test { name: "it_runs_a_command", status: "pass", error: None } ] } ] ); } #[test] fn test_success_run() { let output = b" Compiling rustc-serialize v0.3.22 Compiling toml v0.2.1 Compiling pre-commit v0.5.2 Compiling foo v0.1.0 (file:///foo) Finished debug [unoptimized + debuginfo] target(s) in 12.11 secs Running target/debug/deps/foo-5a7be5d1b9c8e0f6 running 0 tests test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured Running target/debug/integration_test-283604d1063344ba running 1 test test it_runs_a_command ... ok test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured Doc-tests foo running 0 tests test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured"; assert_done(cargo_test_result_parser(output), vec![ Suite { name: "target/debug/deps/foo-5a7be5d1b9c8e0f6", state: "pass", passed: 0, failed: 0, ignored: 0, measured: 0, total: 0, tests: vec![] }, Suite { name: "target/debug/integration_test-283604d1063344ba", state: "pass", passed: 1, failed: 0, ignored: 0, measured: 0, total: 1, tests: vec![ Test { name: "it_runs_a_command", status: "pass", error: None } ] }, Suite { name: "foo", state: "pass", passed: 0, failed: 0, ignored: 0, measured: 0, total: 0, tests: vec![] } ]); } }