|
8 | 8 | import pytest |
9 | 9 |
|
10 | 10 | from tmuxp import Pane, Session, Window |
11 | | -from .helpers import TEST_SESSION_PREFIX, TmuxTestCase, namer |
| 11 | + |
| 12 | +from .helpers import TEST_SESSION_PREFIX, namer |
12 | 13 |
|
13 | 14 | logger = logging.getLogger(__name__) |
14 | 15 |
|
15 | 16 |
|
16 | | -class SessionTest(TmuxTestCase): |
| 17 | +def test_has_session(server, session): |
| 18 | + """Server.has_session returns True if has session_name exists.""" |
| 19 | + TEST_SESSION_NAME = session.get('session_name') |
| 20 | + assert server.has_session(TEST_SESSION_NAME) |
| 21 | + assert not server.has_session('asdf2314324321') |
| 22 | + |
| 23 | + |
| 24 | +def test_select_window(session): |
| 25 | + """Session.select_window moves window.""" |
| 26 | + # get the current window_base_index, since different user tmux config |
| 27 | + # may start at 0 or 1, or whatever they want. |
| 28 | + window_base_index = int( |
| 29 | + session.attached_window().get('window_index') |
| 30 | + ) |
| 31 | + |
| 32 | + session.new_window(window_name='test_window') |
| 33 | + window_count = len(session._windows) |
| 34 | + |
| 35 | + assert window_count >= 2 # 2 or more windows |
| 36 | + |
| 37 | + assert len(session._windows) == window_count |
| 38 | + |
| 39 | + # tmux selects a window, moves to it, shows it as attached_window |
| 40 | + selected_window1 = session.select_window(window_base_index) |
| 41 | + assert isinstance(selected_window1, Window) |
| 42 | + attached_window1 = session.attached_window() |
| 43 | + |
| 44 | + assert selected_window1 == attached_window1 |
| 45 | + assert selected_window1.__dict__ == attached_window1.__dict__ |
| 46 | + |
| 47 | + # again: tmux selects a window, moves to it, shows it as |
| 48 | + # attached_window |
| 49 | + selected_window2 = session.select_window(window_base_index + 1) |
| 50 | + assert isinstance(selected_window2, Window) |
| 51 | + attached_window2 = session.attached_window() |
17 | 52 |
|
18 | | - def test_has_session(self): |
19 | | - """Server.has_session returns True if has session_name exists.""" |
20 | | - assert self.t.has_session(self.TEST_SESSION_NAME) |
21 | | - assert not self.t.has_session('asdf2314324321') |
| 53 | + assert selected_window2 == attached_window2 |
| 54 | + assert selected_window2.__dict__ == attached_window2.__dict__ |
22 | 55 |
|
23 | | - def test_select_window(self): |
24 | | - """Session.select_window moves window.""" |
25 | | - # get the current window_base_index, since different user tmux config |
26 | | - # may start at 0 or 1, or whatever they want. |
27 | | - window_base_index = int( |
28 | | - self.session.attached_window().get('window_index') |
29 | | - ) |
| 56 | + # assure these windows were really different |
| 57 | + assert selected_window1 != selected_window2 |
| 58 | + assert selected_window1.__dict__ != selected_window2.__dict__ |
30 | 59 |
|
31 | | - self.session.new_window(window_name='test_window') |
32 | | - window_count = len(self.session._windows) |
33 | 60 |
|
34 | | - assert window_count >= 2 # 2 or more windows |
| 61 | +def test_select_window_returns_Window(session): |
| 62 | + """Session.select_window returns Window object.""" |
35 | 63 |
|
36 | | - assert len(self.session._windows) == window_count |
| 64 | + window_count = len(session._windows) |
| 65 | + assert len(session._windows) == window_count |
| 66 | + window_base_index = int( |
| 67 | + session.attached_window().get('window_index')) |
37 | 68 |
|
38 | | - # tmux selects a window, moves to it, shows it as attached_window |
39 | | - selected_window1 = self.session.select_window(window_base_index) |
40 | | - assert isinstance(selected_window1, Window) |
41 | | - attached_window1 = self.session.attached_window() |
| 69 | + assert isinstance( |
| 70 | + session.select_window(window_base_index), Window |
| 71 | + ) |
42 | 72 |
|
43 | | - assert selected_window1 == attached_window1 |
44 | | - assert selected_window1.__dict__ == attached_window1.__dict__ |
45 | 73 |
|
46 | | - # again: tmux selects a window, moves to it, shows it as |
47 | | - # attached_window |
48 | | - selected_window2 = self.session.select_window(window_base_index + 1) |
49 | | - assert isinstance(selected_window2, Window) |
50 | | - attached_window2 = self.session.attached_window() |
| 74 | +def test_attached_window(session): |
| 75 | + """Session.attached_window() returns Window.""" |
| 76 | + assert isinstance(session.attached_window(), Window) |
51 | 77 |
|
52 | | - assert selected_window2 == attached_window2 |
53 | | - assert selected_window2.__dict__ == attached_window2.__dict__ |
54 | 78 |
|
55 | | - # assure these windows were really different |
56 | | - assert selected_window1 != selected_window2 |
57 | | - assert selected_window1.__dict__ != selected_window2.__dict__ |
| 79 | +def test_attached_pane(session): |
| 80 | + """Session.attached_pane() returns Pane.""" |
| 81 | + assert isinstance(session.attached_pane(), Pane) |
58 | 82 |
|
59 | | - def test_select_window_returns_Window(self): |
60 | | - """Session.select_window returns Window object.""" |
61 | 83 |
|
62 | | - window_count = len(self.session._windows) |
63 | | - assert len(self.session._windows) == window_count |
64 | | - window_base_index = int( |
65 | | - self.session.attached_window().get('window_index')) |
| 84 | +def test_session_rename(session): |
| 85 | + """Session.rename_session renames session.""" |
| 86 | + TEST_SESSION_NAME = session.get('session_name') |
| 87 | + test_name = 'testingdis_sessname' |
| 88 | + session.rename_session(test_name) |
| 89 | + assert session.get('session_name') == test_name |
| 90 | + session.rename_session(TEST_SESSION_NAME) |
| 91 | + assert session.get('session_name') == TEST_SESSION_NAME |
66 | 92 |
|
67 | | - assert isinstance( |
68 | | - self.session.select_window(window_base_index), Window |
69 | | - ) |
70 | 93 |
|
71 | | - def test_attached_window(self): |
72 | | - """Session.attached_window() returns Window.""" |
73 | | - assert isinstance(self.session.attached_window(), Window) |
| 94 | +def test_new_session(server): |
| 95 | + """Server.new_session creates new session.""" |
| 96 | + new_session_name = TEST_SESSION_PREFIX + next(namer) |
| 97 | + new_session = server.new_session( |
| 98 | + session_name=new_session_name, detach=True) |
74 | 99 |
|
75 | | - def test_attached_pane(self): |
76 | | - """Session.attached_pane() returns Pane.""" |
77 | | - assert isinstance(self.session.attached_pane(), Pane) |
| 100 | + assert isinstance(new_session, Session) |
| 101 | + assert new_session.get('session_name') == new_session_name |
78 | 102 |
|
79 | | - def test_session_rename(self): |
80 | | - """Session.rename_session renames session.""" |
81 | | - test_name = 'testingdis_sessname' |
82 | | - self.session.rename_session(test_name) |
83 | | - assert self.session.get('session_name') == test_name |
84 | | - self.session.rename_session(self.TEST_SESSION_NAME) |
85 | | - assert self.session.get('session_name') == self.TEST_SESSION_NAME |
86 | 103 |
|
| 104 | +def test_show_options(session): |
| 105 | + """Session.show_options() returns dict.""" |
87 | 106 |
|
88 | | -class SessionNewTest(TmuxTestCase): |
| 107 | + options = session.show_options() |
| 108 | + assert isinstance(options, dict) |
89 | 109 |
|
90 | | - def test_new_session(self): |
91 | | - """Server.new_session creates new session.""" |
92 | | - new_session_name = TEST_SESSION_PREFIX + next(namer) |
93 | | - new_session = self.t.new_session( |
94 | | - session_name=new_session_name, detach=True) |
95 | 110 |
|
96 | | - assert isinstance(new_session, Session) |
97 | | - assert new_session.get('session_name') == new_session_name |
| 111 | +def test_set_show_options_single(session): |
| 112 | + """Set option then Session.show_options(key).""" |
98 | 113 |
|
| 114 | + session.set_option('history-limit', 20) |
| 115 | + assert session.show_options('history-limit') == 20 |
99 | 116 |
|
100 | | -class Options(TmuxTestCase): |
| 117 | + session.set_option('history-limit', 40) |
| 118 | + assert session.show_options('history-limit') == 40 |
101 | 119 |
|
102 | | - def test_show_options(self): |
103 | | - """Session.show_options() returns dict.""" |
| 120 | + assert session.show_options()['history-limit'] == 40 |
104 | 121 |
|
105 | | - options = self.session.show_options() |
106 | | - assert isinstance(options, dict) |
107 | 122 |
|
108 | | - def test_set_show_options_single(self): |
109 | | - """Set option then Session.show_options(key).""" |
| 123 | +def test_set_show_option(session): |
| 124 | + """Set option then Session.show_option(key).""" |
| 125 | + session.set_option('history-limit', 20) |
| 126 | + assert session.show_option('history-limit') == 20 |
110 | 127 |
|
111 | | - self.session.set_option('history-limit', 20) |
112 | | - assert self.session.show_options('history-limit') == 20 |
| 128 | + session.set_option('history-limit', 40) |
113 | 129 |
|
114 | | - self.session.set_option('history-limit', 40) |
115 | | - assert self.session.show_options('history-limit') == 40 |
| 130 | + assert session.show_option('history-limit') == 40 |
116 | 131 |
|
117 | | - assert self.session.show_options()['history-limit'] == 40 |
118 | 132 |
|
119 | | - def test_set_show_option(self): |
120 | | - """Set option then Session.show_option(key).""" |
121 | | - self.session.set_option('history-limit', 20) |
122 | | - assert self.session.show_option('history-limit') == 20 |
| 133 | +def test_set_option_bad(session): |
| 134 | + """Session.set_option raises ValueError for bad option key.""" |
| 135 | + with pytest.raises(ValueError): |
| 136 | + session.set_option('afewewfew', 43) |
123 | 137 |
|
124 | | - self.session.set_option('history-limit', 40) |
125 | 138 |
|
126 | | - assert self.session.show_option('history-limit') == 40 |
| 139 | +def test_show_environment(session): |
| 140 | + """Session.show_environment() returns dict.""" |
127 | 141 |
|
128 | | - def test_set_option_bad(self): |
129 | | - """Session.set_option raises ValueError for bad option key.""" |
130 | | - with pytest.raises(ValueError): |
131 | | - self.session.set_option('afewewfew', 43) |
| 142 | + _vars = session.show_environment() |
| 143 | + assert isinstance(_vars, dict) |
132 | 144 |
|
133 | 145 |
|
134 | | -class Environment(TmuxTestCase): |
| 146 | +def test_set_show_environment_single(session): |
| 147 | + """Set environment then Session.show_environment(key).""" |
135 | 148 |
|
136 | | - def test_show_environment(self): |
137 | | - """Session.show_environment() returns dict.""" |
| 149 | + session.set_environment('FOO', 'BAR') |
| 150 | + assert session.show_environment('FOO') == 'BAR' |
138 | 151 |
|
139 | | - _vars = self.session.show_environment() |
140 | | - assert isinstance(_vars, dict) |
| 152 | + session.set_environment('FOO', 'DAR') |
| 153 | + assert session.show_environment('FOO') == 'DAR' |
141 | 154 |
|
142 | | - def test_set_show_environment_single(self): |
143 | | - """Set environment then Session.show_environment(key).""" |
| 155 | + assert session.show_environment()['FOO'] == 'DAR' |
144 | 156 |
|
145 | | - self.session.set_environment('FOO', 'BAR') |
146 | | - assert self.session.show_environment('FOO') == 'BAR' |
147 | 157 |
|
148 | | - self.session.set_environment('FOO', 'DAR') |
149 | | - assert self.session.show_environment('FOO') == 'DAR' |
| 158 | +def test_show_environment_not_set(session): |
| 159 | + """Not set environment variable returns None.""" |
| 160 | + assert session.show_environment('BAR') is None |
150 | 161 |
|
151 | | - assert self.session.show_environment()['FOO'] == 'DAR' |
152 | 162 |
|
153 | | - def test_show_environment_not_set(self): |
154 | | - """Not set environment variable returns None.""" |
155 | | - assert self.session.show_environment('BAR') is None |
| 163 | +def test_remove_environment(session): |
| 164 | + """Remove environment variable.""" |
| 165 | + assert session.show_environment('BAM') is None |
| 166 | + session.set_environment('BAM', 'OK') |
| 167 | + assert session.show_environment('BAM') == 'OK' |
| 168 | + session.remove_environment('BAM') |
| 169 | + assert session.show_environment('BAM') is None |
156 | 170 |
|
157 | | - def test_remove_environment(self): |
158 | | - """Remove environment variable.""" |
159 | | - assert self.session.show_environment('BAM') is None |
160 | | - self.session.set_environment('BAM', 'OK') |
161 | | - assert self.session.show_environment('BAM') == 'OK' |
162 | | - self.session.remove_environment('BAM') |
163 | | - assert self.session.show_environment('BAM') is None |
164 | 171 |
|
165 | | - def test_unset_environment(self): |
166 | | - """Unset environment variable.""" |
167 | | - assert self.session.show_environment('BAM') is None |
168 | | - self.session.set_environment('BAM', 'OK') |
169 | | - assert self.session.show_environment('BAM') == 'OK' |
170 | | - self.session.unset_environment('BAM') |
171 | | - assert self.session.show_environment('BAM') is None |
| 172 | +def test_unset_environment(session): |
| 173 | + """Unset environment variable.""" |
| 174 | + assert session.show_environment('BAM') is None |
| 175 | + session.set_environment('BAM', 'OK') |
| 176 | + assert session.show_environment('BAM') == 'OK' |
| 177 | + session.unset_environment('BAM') |
| 178 | + assert session.show_environment('BAM') is None |
0 commit comments