Skip to content

Commit 82f24aa

Browse files
committed
Use pytest's monkeypatch to set env variables
1 parent 162dc67 commit 82f24aa

File tree

3 files changed

+22
-21
lines changed

3 files changed

+22
-21
lines changed

osbrain/tests/test_agent.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -276,15 +276,15 @@ def test_bind_tcp_addr_specific_port(nsproxy):
276276
(0, 1, False),
277277
(-1, 1, True),
278278
])
279-
def test_linger(nsproxy, linger, sleep_time, should_receive):
279+
def test_linger(monkeypatch, nsproxy, linger, sleep_time, should_receive):
280280
'''
281281
Test linger works when closing the sockets of an agent.
282282
'''
283283
class AgentTest(Agent):
284284
def on_init(self):
285285
self.received = []
286286

287-
osbrain.config['LINGER'] = linger
287+
monkeypatch.setitem(osbrain.config, 'LINGER', linger)
288288

289289
puller = run_agent('puller', base=AgentTest)
290290
pusher = run_agent('pusher', base=AgentTest)
@@ -684,14 +684,14 @@ def spawn_process(self):
684684
assert agent.spawn_process()
685685

686686

687-
def test_agent_execute_as_function(nsproxy):
687+
def test_agent_execute_as_function(monkeypatch, nsproxy):
688688
"""
689689
Test `execute_as_function` method, which should execute a given function
690690
in the remote agent.
691691
"""
692692
class EnvironmentAgent(Agent):
693693
def set_environment(self, value):
694-
os.environ['__OSBRAIN_TEST'] = value
694+
monkeypatch.setitem(os.environ, '__OSBRAIN_TEST', value)
695695

696696
def name(prefix, suffix='suffix'):
697697
return prefix + os.environ.get('__OSBRAIN_TEST', '') + suffix

osbrain/tests/test_agent_transport.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def test_agent_bind_transport_platform_default(nsproxy):
3232

3333

3434
@skip_windows_spawn
35-
def test_agent_bind_transport_global(nsproxy):
35+
def test_agent_bind_transport_global(monkeypatch, nsproxy):
3636
"""
3737
Test global default transport change.
3838
"""
@@ -42,7 +42,7 @@ def test_agent_bind_transport_global(nsproxy):
4242
assert address.transport != 'inproc'
4343

4444
# Changing default global transport to `inproc`
45-
osbrain.config['TRANSPORT'] = 'inproc'
45+
monkeypatch.setitem(osbrain.config, 'TRANSPORT', 'inproc')
4646
agent = run_agent('a1')
4747
address = agent.bind('PUSH')
4848
assert address.transport == 'inproc'

osbrain/tests/test_proxy.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import pytest
88
from threading import Timer
99

10-
1110
import osbrain
1211
from osbrain import run_agent
1312
from osbrain import Agent
@@ -179,20 +178,20 @@ def test_agent_proxy_nameserver_address(nsproxy):
179178
assert agent.nsaddr() == nsproxy.addr()
180179

181180

182-
def test_agent_proxy_safe_and_unsafe_property(nsproxy):
181+
def test_agent_proxy_safe_and_unsafe_property(monkeypatch, nsproxy):
183182
"""
184183
Using the safe/unsafe property from a proxy should allow us to
185184
override the environment global configuration.
186185
"""
187186
run_agent('foo')
188187
# Safe environment
189-
osbrain.config['SAFE'] = True
188+
monkeypatch.setitem(osbrain.config, 'SAFE', True)
190189
proxy = Proxy('foo')
191190
assert proxy._safe
192191
assert proxy.safe._safe
193192
assert not proxy.unsafe._safe
194193
# Unsafe environment
195-
osbrain.config['SAFE'] = False
194+
monkeypatch.setitem(osbrain.config, 'SAFE', False)
196195
proxy = Proxy('foo')
197196
assert not proxy._safe
198197
assert proxy.safe._safe
@@ -210,33 +209,33 @@ def test_agent_run_agent_safe_and_unsafe(nsproxy):
210209
assert not unsafe._safe
211210

212211

213-
def test_agent_proxy_safe_and_unsafe_parameter(nsproxy):
212+
def test_agent_proxy_safe_and_unsafe_parameter(monkeypatch, nsproxy):
214213
"""
215214
Using the safe/unsafe parameter when initializating a proxy should allow
216215
us to override the environment global configuration.
217216
"""
218217
run_agent('foo')
219218
# Safe environment
220-
osbrain.config['SAFE'] = True
219+
monkeypatch.setitem(osbrain.config, 'SAFE', True)
221220
proxy = Proxy('foo')
222221
assert proxy._safe
223222
proxy = Proxy('foo', safe=False)
224223
assert not proxy._safe
225224
# Unsafe environment
226-
osbrain.config['SAFE'] = False
225+
monkeypatch.setitem(osbrain.config, 'SAFE', False)
227226
proxy = Proxy('foo')
228227
assert not proxy._safe
229228
proxy = Proxy('foo', safe=True)
230229
assert proxy._safe
231230

232231

233-
def test_agent_proxy_safe_and_unsafe_calls_property_safe(nsproxy):
232+
def test_agent_proxy_safe_and_unsafe_calls_property_safe(monkeypatch, nsproxy):
234233
"""
235234
An agent can be accessed through a proxy in both safe and unsafe ways.
236235
When using the `safe` property, calls are expected to wait until the main
237236
thread is able to process them to avoid concurrency.
238237
"""
239-
osbrain.config['SAFE'] = False
238+
monkeypatch.setitem(osbrain.config, 'SAFE', False)
240239
worker = setup_bussy_worker(nsproxy)
241240
assert not worker._safe
242241
t0 = time.time()
@@ -247,13 +246,14 @@ def test_agent_proxy_safe_and_unsafe_calls_property_safe(nsproxy):
247246
assert not worker._safe
248247

249248

250-
def test_agent_proxy_safe_and_unsafe_calls_property_unsafe(nsproxy):
249+
def test_agent_proxy_safe_and_unsafe_calls_property_unsafe(
250+
monkeypatch, nsproxy):
251251
"""
252252
An agent can be accessed through a proxy in both safe and unsafe ways.
253253
When using the `unsafe` property, calls are not expected to wait until
254254
the main thread is able to process them (concurrency is allowed).
255255
"""
256-
osbrain.config['SAFE'] = True
256+
monkeypatch.setitem(osbrain.config, 'SAFE', True)
257257
worker = setup_bussy_worker(nsproxy)
258258
assert worker._safe
259259
t0 = time.time()
@@ -266,27 +266,28 @@ def test_agent_proxy_safe_and_unsafe_calls_property_unsafe(nsproxy):
266266
assert worker._safe
267267

268268

269-
def test_agent_proxy_safe_and_unsafe_calls_environ_safe(nsproxy):
269+
def test_agent_proxy_safe_and_unsafe_calls_environ_safe(monkeypatch, nsproxy):
270270
"""
271271
An agent can be accessed through a proxy in both safe and unsafe ways.
272272
When using the `safe` property, calls are expected to wait until the main
273273
thread is able to process them to avoid concurrency.
274274
"""
275-
osbrain.config['SAFE'] = True
275+
monkeypatch.setitem(osbrain.config, 'SAFE', True)
276276
worker = setup_bussy_worker(nsproxy)
277277
t0 = time.time()
278278
assert worker.listen() == 'OK'
279279
assert since(t0, passed=2., tolerance=0.1)
280280
assert not worker.get_attr('bussy')
281281

282282

283-
def test_agent_proxy_safe_and_unsafe_calls_environ_unsafe(nsproxy):
283+
def test_agent_proxy_safe_and_unsafe_calls_environ_unsafe(
284+
monkeypatch, nsproxy):
284285
"""
285286
An agent can be accessed through a proxy in both safe and unsafe ways.
286287
When using the `unsafe` property, calls are not expected to wait until
287288
the main thread is able to process them (concurrency is allowed).
288289
"""
289-
osbrain.config['SAFE'] = False
290+
monkeypatch.setitem(osbrain.config, 'SAFE', False)
290291
worker = setup_bussy_worker(nsproxy)
291292
t0 = time.time()
292293
assert worker.listen() == 'OK'

0 commit comments

Comments
 (0)