@@ -36,6 +36,14 @@ def get_test_session_name(server, prefix='tmuxp_'):
3636 return session_name
3737
3838
39+ def get_test_window_name (session , prefix = 'tmuxp_' ):
40+ while True :
41+ window_name = prefix + str (randint (0 , 9999999 ))
42+ if not session .findWhere (window_name = window_name ):
43+ break
44+ return window_name
45+
46+
3947@contextlib .contextmanager
4048def temp_session (server , session_name = None ):
4149 if not session_name :
@@ -50,6 +58,81 @@ def temp_session(server, session_name=None):
5058 return
5159
5260
61+ @contextlib .contextmanager
62+ def temp_session (server , * args , ** kwargs ):
63+ """Return a context manager with a temporary session.
64+
65+ e.g.::
66+
67+ with temp_session(server) as session:
68+ session.new_window(window_name='my window')
69+
70+ The session will destroy itself upon closing with :meth:`Session.
71+ kill_session()`.
72+
73+ If no ``session_name`` is entered, :func:`get_test_session_name` will make
74+ an unused session name.
75+
76+ :args: Same arguments as :meth:`Server.new_session`
77+ :yields: Temporary session
78+ :rtype: :class:`Session`
79+ """
80+
81+ if 'session_name' in kwargs :
82+ session_name = kwargs .pop ('session_name' )
83+ else :
84+ session_name = get_test_session_name (server )
85+
86+ print (kwargs )
87+
88+ session = server .new_session (session_name , * args , ** kwargs )
89+
90+ try :
91+ yield session
92+ finally :
93+ if server .has_session (session_name ):
94+ session .kill_session ()
95+ return
96+
97+
98+ @contextlib .contextmanager
99+ def temp_window (session , * args , ** kwargs ):
100+ """Return a context manager with a temporary window.
101+
102+ e.g.::
103+
104+ with temp_window(session) as window:
105+ my_pane = window.split_window()
106+
107+ The window will destroy itself upon closing with :meth:`window.
108+ kill_window()`.
109+
110+ If no ``window_name`` is entered, :func:`get_test_window_name` will make
111+ an unused window name.
112+
113+ :args: Same arguments as :meth:`Session.new_window`
114+ :yields: Temporary window
115+ :rtype: :class:`Window`
116+ """
117+
118+ if 'window_name' not in kwargs :
119+ window_name = get_test_window_name (session )
120+ else :
121+ window_name = kwargs .pop ('window_name' )
122+
123+ window = session .new_window (window_name , * args , ** kwargs )
124+
125+ # Get ``window_id`` before returning it, it may be killed within context.
126+ window_id = window .get ('window_id' )
127+
128+ try :
129+ yield session
130+ finally :
131+ if session .findWhere (window_id = window_id ):
132+ window .kill_window ()
133+ return
134+
135+
53136class TestCase (unittest .TestCase ):
54137
55138 """Base TestClass so we don't have to try: unittest2 every module. """
@@ -68,6 +151,9 @@ class TmuxTestCase(TestCase):
68151 #: Session name for the TestCase.
69152 TEST_SESSION_NAME = None
70153
154+ def temp_session (self , session_name = None ):
155+ return temp_session (self .server , session_name )
156+
71157 def setUp (self ):
72158 """Run bootstrap if :attr:`~.session` is not set."""
73159
0 commit comments