Skip to content

Commit 73e43c8

Browse files
committed
Fix: #27 return None if findWhere returns nothing.
1 parent 3fa69dc commit 73e43c8

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

tmuxp/testsuite/test_tmuxobject.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import random
1414
from .. import Pane, Window, Session
1515
from . import t
16-
from .helpers import TmuxTestCase
16+
from .helpers import TmuxTestCase, TEST_SESSION_PREFIX
1717

1818
import logging
1919

@@ -58,6 +58,21 @@ def test_findWhere(self):
5858
self.assertIsInstance(window.findWhere(
5959
{'pane_id': pane_id}), Pane)
6060

61+
def test_findWhere_None(self):
62+
""".findWhere returns None if no results found."""
63+
64+
while True:
65+
nonexistant_session = TEST_SESSION_PREFIX + str(
66+
random.randint(0, 9999)
67+
)
68+
69+
if not t.has_session(nonexistant_session):
70+
break
71+
72+
self.assertIsNone(t.findWhere({
73+
'session_name': nonexistant_session
74+
}))
75+
6176
def test_findWhere_multiple_attrs(self):
6277
""".findWhere returns objects with multiple attributes."""
6378

tmuxp/util.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,10 @@ def findWhere(self, attrs):
156156
.. _underscore.js: http://underscorejs.org/
157157
158158
"""
159-
return self.where(attrs)[0] or None
159+
try:
160+
return self.where(attrs)[0]
161+
except IndexError:
162+
return None
160163

161164
def where(self, attrs, first=False):
162165
"""Return objects matching child objects properties.

0 commit comments

Comments
 (0)