Skip to content

Commit df47cbf

Browse files
committed
Rename BeforeLoadScriptFailed to BeforeLoadScriptError, refactor.
1 parent 9cd9ebc commit df47cbf

File tree

5 files changed

+23
-26
lines changed

5 files changed

+23
-26
lines changed

doc/api.rst

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ Exceptions
125125

126126
.. autoexception:: tmuxp.exc.ConfigError
127127

128-
.. autoexception:: tmuxp.exc.BeforeLoadScriptFailed
128+
.. autoexception:: tmuxp.exc.BeforeLoadScriptError
129129

130130
.. autoexception:: tmuxp.exc.BeforeLoadScriptNotExists
131131

@@ -136,10 +136,6 @@ Test tools
136136

137137
.. automethod:: tmuxp.testsuite.helpers.get_test_window_name
138138

139-
.. automethod:: tmuxp.testsuite.helpers.get_test_pane_name
140-
141139
.. automethod:: tmuxp.testsuite.helpers.temp_session
142140

143141
.. automethod:: tmuxp.testsuite.helpers.temp_window
144-
145-
.. automethod:: tmuxp.testsuite.helpers.temp_pane

tmuxp/exc.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,22 @@ def __init__(self, *args, **kwargs):
5454

5555

5656
@implements_to_string
57-
class BeforeLoadScriptFailed(subprocess.CalledProcessError):
57+
class BeforeLoadScriptError(Exception):
5858

59-
"""Exception extending :py:class:`subprocess.CalledProcessError` for
59+
"""Exception replacing :py:class:`subprocess.CalledProcessError` for
6060
:meth:`util.run_before_script`.
6161
"""
6262

63-
def __init__(self, *args, **kwargs):
64-
super(BeforeLoadScriptFailed, self).__init__(*args, **kwargs)
63+
def __init__(self, returncode, cmd, output=None):
64+
self.returncode = returncode
65+
self.cmd = cmd
66+
self.output = output
67+
self.message = (
68+
'before_script failed with returncode {returncode}.\n'
69+
'command: {cmd}\n'
70+
'Error output:\n'
71+
'{output}'
72+
).format(returncode=self.returncode, cmd=self.cmd, output=self.output)
6573

6674
def __str__(self):
67-
return "before_script failed (%s): %s." \
68-
"\nError Output: \n%s" % (
69-
self.returncode, self.cmd, self.output
70-
)
75+
return self.message

tmuxp/testsuite/util.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
from .. import exc
1919
from ..util import has_required_tmux_version, run_before_script
20-
from ..exc import BeforeLoadScriptNotExists, BeforeLoadScriptFailed
20+
from ..exc import BeforeLoadScriptNotExists, BeforeLoadScriptError
2121

2222

2323
from .helpers import TmuxTestCase, TestCase
@@ -72,10 +72,10 @@ def test_raise_BeforeLoadScriptNotExists_if_not_exists(self):
7272
with self.assertRaises(OSError):
7373
run_before_script(script_file)
7474

75-
def test_raise_BeforeLoadScriptFailed_if_retcode(self):
75+
def test_raise_BeforeLoadScriptError_if_retcode(self):
7676
script_file = os.path.join(fixtures_dir, 'script_failed.sh')
7777

78-
with self.assertRaises(BeforeLoadScriptFailed):
78+
with self.assertRaises(BeforeLoadScriptError):
7979
run_before_script(script_file)
8080

8181
def test_return_stdout_if_ok(self):
@@ -84,24 +84,24 @@ def test_return_stdout_if_ok(self):
8484
run_before_script(script_file)
8585

8686

87-
class BeforeLoadScriptFailedTestCase(TestCase):
87+
class BeforeLoadScriptErrorTestCase(TestCase):
8888

8989
def test_returncode(self):
9090
script_file = os.path.join(fixtures_dir, 'script_failed.sh')
9191

92-
with self.assertRaisesRegexp(subprocess.CalledProcessError, "113"):
92+
with self.assertRaisesRegexp(exc.BeforeLoadScriptError, "113"):
9393
run_before_script(script_file)
9494

9595
def test_returns_stderr_messages(self):
9696
script_file = os.path.join(fixtures_dir, 'script_failed.sh')
9797

98-
with self.assertRaisesRegexp(subprocess.CalledProcessError, "An error has occured"):
98+
with self.assertRaisesRegexp(exc.BeforeLoadScriptError, "failed with returncode"):
9999
run_before_script(script_file)
100100

101101

102102
def suite():
103103
suite = unittest.TestSuite()
104-
suite.addTest(unittest.makeSuite(BeforeLoadScriptFailedTestCase))
104+
suite.addTest(unittest.makeSuite(BeforeLoadScriptErrorTestCase))
105105
suite.addTest(unittest.makeSuite(RunBeforeScript))
106106
suite.addTest(unittest.makeSuite(TmuxVersionTest))
107107
return suite

tmuxp/testsuite/workspacebuilder.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import sys
1414
import logging
1515
import unittest
16-
import subprocess
1716
import time
1817

1918
import kaptan
@@ -602,7 +601,7 @@ def test_throw_error_if_retcode_error(self):
602601
with self.temp_session() as sess:
603602
session_name = sess.get('session_name')
604603

605-
with self.assertRaises((exc.BeforeLoadScriptFailed, subprocess.CalledProcessError)):
604+
with self.assertRaises(exc.BeforeLoadScriptError):
606605
builder.build(session=sess)
607606

608607
result = self.server.has_session(session_name)

tmuxp/util.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,8 @@ def run_before_script(script_file):
4343
stderr = console_to_str(stderr).split('\n')
4444
stderr = '\n'.join(list(filter(None, stderr))) # filter empty values
4545

46-
# python 2.6 doesn't have a third "output" argument.
47-
error = exc.BeforeLoadScriptFailed(proc.returncode, script_file)
48-
error.output = stderr
46+
raise exc.BeforeLoadScriptError(proc.returncode, script_file, stderr)
4947

50-
raise(error)
5148
return proc.returncode
5249
except OSError as e:
5350
if e.errno == 2:

0 commit comments

Comments
 (0)