Skip to content

Commit 5df90e0

Browse files
committed
Add context manager to Local
1 parent 27203ff commit 5df90e0

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

browserstack/local.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
from browserstack.bserrors import BrowserStackLocalError
44

55
class Local:
6-
def __init__(self, key=None, binary_path=None):
6+
def __init__(self, key=None, binary_path=None, **kwargs):
77
self.key = os.environ['BROWSERSTACK_ACCESS_KEY'] if 'BROWSERSTACK_ACCESS_KEY' in os.environ else key
8-
self.options = None
8+
self.options = kwargs
99
self.local_logfile_path = os.path.join(os.getcwd(), 'local.log')
1010

1111
def __xstr(self, key, value):
@@ -29,7 +29,8 @@ def _generate_stop_cmd(self):
2929
return cmd
3030

3131
def start(self, **kwargs):
32-
self.options = kwargs
32+
for k, v in kwargs.items():
33+
self.options[k] = v
3334

3435
if 'key' in self.options:
3536
self.key = self.options['key']
@@ -74,3 +75,10 @@ def stop(self):
7475
(out, err) = proc.communicate()
7576
except Exception as e:
7677
return
78+
79+
def __enter__(self):
80+
self.start(**self.options)
81+
return self
82+
83+
def __exit__(self, *args):
84+
self.stop()

tests/test_local.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,7 @@ def test_local_identifier(self):
7979
self.local.start(localIdentifier='mytunnel', onlyCommand=True)
8080
self.assertIn('-localIdentifier', self.local._generate_cmd())
8181
self.assertIn('mytunnel', self.local._generate_cmd())
82+
83+
def test_context_manager(self):
84+
with Local('BROWSERSTACK_ACCESS_KEY') as local:
85+
self.assertNotEqual(local.proc.pid, 0)

0 commit comments

Comments
 (0)