|
13 | 13 | import sys |
14 | 14 | import logging |
15 | 15 | import unittest |
| 16 | +import subprocess |
16 | 17 | import time |
17 | 18 |
|
18 | 19 | import kaptan |
19 | 20 |
|
20 | 21 | from .. import Window, config, exc |
21 | 22 | from .._compat import text_type |
22 | 23 | from ..workspacebuilder import WorkspaceBuilder |
23 | | -from .helpers import TmuxTestCase |
| 24 | +from .helpers import TestCase, TmuxTestCase, temp_session |
24 | 25 |
|
25 | 26 | logger = logging.getLogger(__name__) |
26 | 27 |
|
27 | 28 | current_dir = os.path.abspath(os.path.dirname(__file__)) |
28 | 29 | example_dir = os.path.abspath(os.path.join(current_dir, '..', '..', 'examples')) |
| 30 | +fixtures_dir = os.path.abspath(os.path.join(current_dir, 'fixtures')) |
29 | 31 |
|
30 | 32 |
|
31 | 33 | class TwoPaneTest(TmuxTestCase): |
@@ -339,7 +341,7 @@ def test_blank_pane_count(self): |
339 | 341 | test_config = kaptan.Kaptan().import_config(self.yaml_config_file).get() |
340 | 342 | test_config = config.expand(test_config) |
341 | 343 | # for window in test_config['windows']: |
342 | | - # window['layout'] = 'tiled' |
| 344 | + # window['layout'] = 'tiled' |
343 | 345 | builder = WorkspaceBuilder(sconf=test_config) |
344 | 346 | builder.build(session=self.session) |
345 | 347 |
|
@@ -527,8 +529,133 @@ def test_window_index(self): |
527 | 529 | self.assertEqual(int(window['window_index']), expected_index) |
528 | 530 |
|
529 | 531 |
|
| 532 | +class BeforeLoadScript(TmuxTestCase): |
| 533 | + |
| 534 | + config_script_not_exists = """ |
| 535 | + session_name: sampleconfig |
| 536 | + before_script: {fixtures_dir}/script_not_exists.sh |
| 537 | + windows: |
| 538 | + - panes: |
| 539 | + - pane |
| 540 | + """ |
| 541 | + |
| 542 | + config_script_fails = """ |
| 543 | + session_name: sampleconfig |
| 544 | + before_script: {fixtures_dir}/script_failed.sh |
| 545 | + windows: |
| 546 | + - panes: |
| 547 | + - pane |
| 548 | + """ |
| 549 | + |
| 550 | + config_script_completes = """ |
| 551 | + session_name: sampleconfig |
| 552 | + before_script: {fixtures_dir}/script_complete.sh |
| 553 | + windows: |
| 554 | + - panes: |
| 555 | + - pane |
| 556 | + """ |
| 557 | + |
| 558 | + def test_throw_error_if_retcode_false(self): |
| 559 | + |
| 560 | + sconfig = kaptan.Kaptan(handler='yaml') |
| 561 | + yaml = self.config_script_fails.format( |
| 562 | + fixtures_dir=fixtures_dir |
| 563 | + ) |
| 564 | + sconfig = sconfig.import_config(yaml).get() |
| 565 | + sconfig = config.expand(sconfig) |
| 566 | + sconfig = config.trickle(sconfig) |
| 567 | + |
| 568 | + builder = WorkspaceBuilder(sconf=sconfig) |
| 569 | + |
| 570 | + with temp_session(self.server) as sess: |
| 571 | + session_name = sess.get('session_name') |
| 572 | + |
| 573 | + with self.assertRaises(subprocess.CalledProcessError): |
| 574 | + builder.build(session=sess) |
| 575 | + |
| 576 | + result = self.server.has_session(session_name) |
| 577 | + self.assertFalse( |
| 578 | + result, |
| 579 | + msg="Kills session if before_script exits with errcode" |
| 580 | + ) |
| 581 | + |
| 582 | + def test_throw_error_if_file_not_exists(self): |
| 583 | + |
| 584 | + sconfig = kaptan.Kaptan(handler='yaml') |
| 585 | + yaml = self.config_script_not_exists.format( |
| 586 | + fixtures_dir=fixtures_dir |
| 587 | + ) |
| 588 | + sconfig = sconfig.import_config(yaml).get() |
| 589 | + sconfig = config.expand(sconfig) |
| 590 | + sconfig = config.trickle(sconfig) |
| 591 | + |
| 592 | + builder = WorkspaceBuilder(sconf=sconfig) |
| 593 | + |
| 594 | + with temp_session(self.server) as sess: |
| 595 | + session_name = sess.get('session_name') |
| 596 | + temp_session_exists = self.server.has_session(sess.get('session_name')) |
| 597 | + self.assertTrue(temp_session_exists) |
| 598 | + with self.assertRaisesRegexp( |
| 599 | + (BeforeLoadScriptNotExists, OSError), |
| 600 | + 'No such file or directory' |
| 601 | + ): |
| 602 | + builder.build(session=sess) |
| 603 | + result = self.server.has_session(session_name) |
| 604 | + self.assertFalse( |
| 605 | + result, |
| 606 | + msg="Kills session if before_script doesn't exist" |
| 607 | + ) |
| 608 | + |
| 609 | + def test_true_if_test_passes(self): |
| 610 | + |
| 611 | + sconfig = kaptan.Kaptan(handler='yaml') |
| 612 | + yaml = self.config_script_completes.format( |
| 613 | + fixtures_dir=fixtures_dir |
| 614 | + ) |
| 615 | + sconfig = sconfig.import_config(yaml).get() |
| 616 | + sconfig = config.expand(sconfig) |
| 617 | + sconfig = config.trickle(sconfig) |
| 618 | + |
| 619 | + builder = WorkspaceBuilder(sconf=sconfig) |
| 620 | + |
| 621 | + with temp_session(self.session.server) as session: |
| 622 | + builder.build(session=self.session) |
| 623 | + |
| 624 | + |
| 625 | +from ..workspacebuilder import run_before_script, BeforeLoadScriptNotExists, \ |
| 626 | + BeforeLoadScriptFailed |
| 627 | + |
| 628 | + |
| 629 | +class RunBeforeScript(TestCase): |
| 630 | + |
| 631 | + def test_raise_BeforeLoadScriptNotExists_if_not_exists(self): |
| 632 | + script_file = os.path.join(fixtures_dir, 'script_noexists.sh') |
| 633 | + |
| 634 | + with self.assertRaises(BeforeLoadScriptNotExists): |
| 635 | + run_before_script(script_file) |
| 636 | + |
| 637 | + with self.assertRaises(OSError): |
| 638 | + run_before_script(script_file) |
| 639 | + |
| 640 | + def test_raise_BeforeLoadScriptFailed_if_retcode(self): |
| 641 | + script_file = os.path.join(fixtures_dir, 'script_failed.sh') |
| 642 | + |
| 643 | + with self.assertRaises(BeforeLoadScriptFailed): |
| 644 | + run_before_script(script_file) |
| 645 | + |
| 646 | + with self.assertRaises(subprocess.CalledProcessError): |
| 647 | + run_before_script(script_file) |
| 648 | + |
| 649 | + def test_return_stdout_if_exits_zero(self): |
| 650 | + script_file = os.path.join(fixtures_dir, 'script_complete.sh') |
| 651 | + |
| 652 | + run_before_script(script_file) |
| 653 | + |
| 654 | + |
530 | 655 | def suite(): |
531 | 656 | suite = unittest.TestSuite() |
| 657 | + suite.addTest(unittest.makeSuite(BeforeLoadScript)) |
| 658 | + suite.addTest(unittest.makeSuite(RunBeforeScript)) |
532 | 659 | suite.addTest(unittest.makeSuite(BlankPaneTest)) |
533 | 660 | suite.addTest(unittest.makeSuite(FocusAndPaneIndexTest)) |
534 | 661 | suite.addTest(unittest.makeSuite(PaneOrderingTest)) |
|
0 commit comments