Skip to content

Commit 7a00fdd

Browse files
committed
overhaul option commands and errorbacks, tests
1 parent cd6122f commit 7a00fdd

File tree

5 files changed

+61
-53
lines changed

5 files changed

+61
-53
lines changed

libtmux/common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
import logging
1010
import os
1111
import re
12-
import sys
1312
import subprocess
13+
import sys
1414
from distutils.version import LooseVersion
1515

1616
from . import exc

libtmux/session.py

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
from . import exc, formats
1414
from .common import EnvironmentMixin, TmuxMappingObject, \
15-
TmuxRelationalObject, session_check_name
15+
TmuxRelationalObject, session_check_name, handle_option_error
1616
from .window import Window
1717

1818
logger = logging.getLogger(__name__)
@@ -132,17 +132,10 @@ def new_window(self,
132132
window_shell=None):
133133
"""Return :class:`Window` from ``$ tmux new-window``.
134134
135-
.. note::
136-
137-
By default, this will make the window active. For the new window
138-
to be created and not set to current, pass in ``attach=False``.
135+
By default, this will make the window active. For the new window
136+
to be created and not set to current, pass in ``attach=False``.
139137
140138
:param window_name: window name.
141-
142-
.. code-block:: bash
143-
144-
$ tmux new-window -n <window_name> -c <start_directory>
145-
146139
:type window_name: string
147140
:param start_directory: specifies the working directory in which the
148141
new window is created.
@@ -335,6 +328,8 @@ def set_option(self, option, value, g=False):
335328
:type value: bool
336329
:param global: check for option globally across all servers (-g)
337330
:type global: bool
331+
:raises: :exc:`exc.OptionError`, :exc:`exc.UnknownOption`,
332+
:exc:`exc.InvalidOption`, :exc:`exc.AmbiguousOption`
338333
339334
"""
340335

@@ -355,13 +350,7 @@ def set_option(self, option, value, g=False):
355350
)
356351

357352
if isinstance(proc.stderr, list) and len(proc.stderr):
358-
error = proc.stderr[0]
359-
if 'unknown option' in error:
360-
raise exc.UnknownOption(error)
361-
elif 'invalid option' in error:
362-
raise exc.InvalidOption(error)
363-
364-
raise ValueError('tmux set-option stderr: %s' % error)
353+
handle_option_error(proc.stderr[0])
365354

366355
def show_options(self, option=None, g=False):
367356
"""Return a dict of options for the window.
@@ -425,11 +414,7 @@ def show_option(self, option, g=False):
425414
)
426415

427416
if isinstance(cmd.stderr, list) and len(cmd.stderr):
428-
error = cmd.stderr[0]
429-
if 'unknown option' in error:
430-
raise exc.UnknownOption(error)
431-
else:
432-
raise exc.LibTmuxException(error)
417+
handle_option_error(cmd.stderr[0])
433418

434419
if not len(cmd.stdout):
435420
return None

libtmux/window.py

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
import os
1212

1313
from . import exc, formats
14-
from .common import TmuxMappingObject, TmuxRelationalObject
14+
from .common import (
15+
TmuxMappingObject, TmuxRelationalObject, handle_option_error
16+
)
1517
from .pane import Pane
1618

1719
logger = logging.getLogger(__name__)
@@ -149,22 +151,7 @@ def set_window_option(self, option, value):
149151
)
150152

151153
if isinstance(cmd.stderr, list) and len(cmd.stderr):
152-
error = cmd.stderr[0]
153-
if any(
154-
x in error
155-
for x in ['unknown option', 'invalid option']
156-
):
157-
raise exc.UnknownOption(error)
158-
159-
raise ValueError(
160-
'tmux set-window-option -t%s:%s %s %s\n%s' % (
161-
self.get('session_id'),
162-
self.index,
163-
option,
164-
value,
165-
error
166-
)
167-
)
154+
handle_option_error(cmd.stderr[0])
168155

169156
def show_window_options(self, option=None, g=False):
170157
"""Return a dict of options for the window.
@@ -228,10 +215,7 @@ def show_window_option(self, option, g=False):
228215
)
229216

230217
if len(cmd.stderr):
231-
if 'unknown option' in cmd.stderr:
232-
raise exc.UnknownOption(cmd.stderr[0])
233-
elif 'invalid option' in cmd.stderr:
234-
raise exc.InvalidOption(cmd.stderr[0])
218+
handle_option_error(cmd.stderr[0])
235219

236220
if not len(cmd.stdout):
237221
return None

tests/test_session.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import pytest
88

99
from libtmux import Pane, Session, Window, exc
10+
from libtmux.common import has_gte_version
1011
from libtmux.test import TEST_SESSION_PREFIX, namer
1112

1213
logger = logging.getLogger(__name__)
@@ -133,15 +134,31 @@ def test_empty_session_option_returns_None(session):
133134

134135

135136
def test_show_option_unknown(session):
136-
"""Session.show_option raises UnknownOption for bad option key."""
137+
"""Session.show_option raises UnknownOption for invalid option."""
137138
with pytest.raises(exc.UnknownOption):
138139
session.show_option('moooz')
139140

140141

141-
def test_set_option_bad(session):
142-
"""Session.set_option raises UnknownOption for bad option key."""
143-
with pytest.raises(exc.UnknownOption):
144-
session.set_option('afewewfew', 43)
142+
def test_show_option_ambiguous(session):
143+
"""Session.show_option raises AmbiguousOption for ambiguous option."""
144+
with pytest.raises(exc.AmbiguousOption):
145+
session.show_option('bell-')
146+
147+
148+
def test_set_option_ambigous(session):
149+
"""Session.set_option raises AmbiguousOption for invalid option."""
150+
with pytest.raises(exc.AmbiguousOption):
151+
session.set_option('bell-', 43)
152+
153+
154+
def test_set_option_invalid(session):
155+
"""Session.set_option raises UnknownOption for invalid option."""
156+
if has_gte_version('2.4'):
157+
with pytest.raises(exc.InvalidOption):
158+
session.set_option('afewewfew', 43)
159+
else:
160+
with pytest.raises(exc.UnknownOption):
161+
session.set_option('afewewfew', 43)
145162

146163

147164
def test_show_environment(session):

tests/test_window.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import pytest
77

88
from libtmux import Pane, Server, Window, exc
9+
from libtmux.common import has_gte_version
910

1011

1112
logger = logging.getLogger(__name__)
@@ -198,17 +199,38 @@ def test_show_window_option(session):
198199
def test_show_window_option_unknown(session):
199200
"""Window.show_window_option raises UnknownOption for bad option key."""
200201
window = session.new_window(window_name='test_window')
202+
201203
with pytest.raises(exc.UnknownOption):
202204
window.show_window_option('moooz')
203205

204206

205-
def test_set_window_option_bad(session):
206-
"""Window.set_window_option raises ValueError for bad option key."""
207+
def test_show_window_option_ambiguous(session):
208+
"""show_window_option raises AmbiguousOption for ambiguous option."""
209+
window = session.new_window(window_name='test_window')
210+
211+
with pytest.raises(exc.AmbiguousOption):
212+
window.show_window_option('clock-mode')
207213

214+
215+
def test_set_window_option_ambiguous(session):
216+
"""set_window_option raises AmbiguousOption for ambiguous option."""
208217
window = session.new_window(window_name='test_window')
209218

210-
with pytest.raises(exc.UnknownOption):
211-
window.set_window_option('afewewfew', 43)
219+
with pytest.raises(exc.AmbiguousOption):
220+
window.set_window_option('clock-mode', 12)
221+
222+
223+
def test_set_window_option_invalid(session):
224+
"""Window.set_window_option raises ValueError for invalid option key."""
225+
226+
window = session.new_window(window_name='test_window')
227+
228+
if has_gte_version('2.4'):
229+
with pytest.raises(exc.InvalidOption):
230+
window.set_window_option('afewewfew', 43)
231+
else:
232+
with pytest.raises(exc.UnknownOption):
233+
window.set_window_option('afewewfew', 43)
212234

213235

214236
def test_move_window(session):

0 commit comments

Comments
 (0)