Skip to content

Commit

Permalink
Check if args are passed to MatchedClient
Browse files Browse the repository at this point in the history
fixes #176
  • Loading branch information
BrianJKoopman committed Jul 2, 2021
1 parent e1a813e commit e954753
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
3 changes: 3 additions & 0 deletions ocs/matched_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ def __init__(self, instance_id, **kwargs):
For additional kwargs see site_config.get_control_client.
"""
if kwargs.get('args') is None:
kwargs['args'] = []

self._client = site_config.get_control_client(instance_id, **kwargs)
self.instance_id = instance_id

Expand Down
28 changes: 28 additions & 0 deletions tests/test_matched_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import os
from unittest.mock import MagicMock, patch
from ocs.matched_client import MatchedClient

mocked_client = MagicMock()
mock_from_yaml = MagicMock()


@patch('ocs.client_http.ControlClient', mocked_client)
@patch('ocs.site_config.SiteConfig.from_yaml', mock_from_yaml)
@patch('sys.argv', ['example_client.py', 'test'])
def test_extra_argv():
"""If there are extra arguments in sys.argv and args=[] is not set when
instantiating a MatchedClient, then internally
site_config.get_control_client() will inspect sys.argv[1:], which causes
issues down the line when run through the site_config parser.
Here we patch in a mocked ControlClient to avoid needing to talk to a
crossbar server. We also patch in a mocked from_yaml() method, to avoid
needing to read a real site file. Lastly, and most importantly, we patch in
sys.argv with an actual additional argument. This will cause an
"unrecognized arguments" error when argparse inspects sys.argv within the
site_config parser.
"""
# Set for get_config to pick up on
os.environ["OCS_CONFIG_DIR"] = '/tmp/'
MatchedClient("test")

0 comments on commit e954753

Please sign in to comment.