|
7 | 7 | """ |
8 | 8 | import os |
9 | 9 | import sys |
| 10 | +import tempfile |
10 | 11 |
|
11 | 12 | import mock |
12 | 13 | import pytest |
@@ -504,25 +505,63 @@ def test_save_invalid_path(base_app, capsys): |
504 | 505 |
|
505 | 506 |
|
506 | 507 | def test_output_redirection(base_app): |
507 | | - # TODO: Use a temporary directory/file for this file |
508 | | - filename = 'out.txt' |
509 | | - |
510 | | - # Verify that writing to a file works |
511 | | - run_cmd(base_app, 'help > {}'.format(filename)) |
512 | | - expected = normalize(BASE_HELP) |
513 | | - with open(filename) as f: |
514 | | - content = normalize(f.read()) |
515 | | - assert content == expected |
516 | | - |
517 | | - # Verify that appending to a file also works |
518 | | - run_cmd(base_app, 'help history >> {}'.format(filename)) |
519 | | - expected = normalize(BASE_HELP + '\n' + HELP_HISTORY) |
520 | | - with open(filename) as f: |
521 | | - content = normalize(f.read()) |
522 | | - assert content == expected |
523 | | - |
524 | | - # Delete file that was created |
525 | | - os.remove(filename) |
| 508 | + fd, filename = tempfile.mkstemp(prefix='cmd2_test', suffix='.txt') |
| 509 | + os.close(fd) |
| 510 | + |
| 511 | + try: |
| 512 | + # Verify that writing to a file works |
| 513 | + run_cmd(base_app, 'help > {}'.format(filename)) |
| 514 | + expected = normalize(BASE_HELP) |
| 515 | + with open(filename) as f: |
| 516 | + content = normalize(f.read()) |
| 517 | + assert content == expected |
| 518 | + |
| 519 | + # Verify that appending to a file also works |
| 520 | + run_cmd(base_app, 'help history >> {}'.format(filename)) |
| 521 | + expected = normalize(BASE_HELP + '\n' + HELP_HISTORY) |
| 522 | + with open(filename) as f: |
| 523 | + content = normalize(f.read()) |
| 524 | + assert content == expected |
| 525 | + except: |
| 526 | + raise |
| 527 | + finally: |
| 528 | + os.remove(filename) |
| 529 | + |
| 530 | + |
| 531 | +def test_feedback_to_output_true(base_app): |
| 532 | + base_app.feedback_to_output = True |
| 533 | + base_app.timing = True |
| 534 | + f, filename = tempfile.mkstemp(prefix='cmd2_test', suffix='.txt') |
| 535 | + os.close(f) |
| 536 | + |
| 537 | + try: |
| 538 | + run_cmd(base_app, 'help > {}'.format(filename)) |
| 539 | + with open(filename) as f: |
| 540 | + content = f.readlines() |
| 541 | + assert content[-1].startswith('Elapsed: ') |
| 542 | + except: |
| 543 | + raise |
| 544 | + finally: |
| 545 | + os.remove(filename) |
| 546 | + |
| 547 | + |
| 548 | +def test_feedback_to_output_false(base_app, capsys): |
| 549 | + base_app.feedback_to_output = False |
| 550 | + base_app.timing = True |
| 551 | + f, filename = tempfile.mkstemp(prefix='feedback_to_output', suffix='.txt') |
| 552 | + os.close(f) |
| 553 | + |
| 554 | + try: |
| 555 | + run_cmd(base_app, 'help > {}'.format(filename)) |
| 556 | + out, err = capsys.readouterr() |
| 557 | + with open(filename) as f: |
| 558 | + content = f.readlines() |
| 559 | + assert not content[-1].startswith('Elapsed: ') |
| 560 | + assert err.startswith('Elapsed') |
| 561 | + except: |
| 562 | + raise |
| 563 | + finally: |
| 564 | + os.remove(filename) |
526 | 565 |
|
527 | 566 |
|
528 | 567 | def test_allow_redirection(base_app): |
@@ -619,9 +658,9 @@ def test_base_timing(base_app, capsys): |
619 | 658 | assert out == expected |
620 | 659 | out, err = capsys.readouterr() |
621 | 660 | if sys.platform == 'win32': |
622 | | - assert out.startswith('Elapsed: 0:00:00') |
| 661 | + assert err.startswith('Elapsed: 0:00:00') |
623 | 662 | else: |
624 | | - assert out.startswith('Elapsed: 0:00:00.0') |
| 663 | + assert err.startswith('Elapsed: 0:00:00.0') |
625 | 664 |
|
626 | 665 |
|
627 | 666 | def test_base_debug(base_app, capsys): |
|
0 commit comments