Skip to content

Commit

Permalink
Merge pull request #25 from gtagency/refactor-file-resolution
Browse files Browse the repository at this point in the history
Refactor file resolution
  • Loading branch information
joshuamorton authored Aug 20, 2017
2 parents 1bb4b67 + d5a83a4 commit 6a4f2eb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 19 deletions.
33 changes: 15 additions & 18 deletions pyrostest/ros_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ class NoMessage(Exception):
"""
pass

def _resolve_location(binary_name):
"""Find the location of a mock node to run.
"""
location = pkg_resources.resource_filename(__name__, binary_name)
if not os.path.isfile(location):
this_dir = os.path.dirname(os.path.abspath(__file__))
data_dir = os.path.join(this_dir, '..', 'data')
location = os.path.join(data_dir, binary_name)
if not os.path.isfile(location):
raise FileNotFoundError('{} cannot be located'.format(location))
return location



class MockPublisher(object):
"""Mock of a node object for testing.
Expand All @@ -40,16 +53,7 @@ def __init__(self, topic, msg_type, queue_size):
self.topic = topic
self.msg_type = msg_type
pub_data = pickle.dumps((topic, msg_type, queue_size))
# dynamically looks up the location of the publisher.py file in
# relation to this file (they should be in the same dir)
location = pkg_resources.resource_filename(__name__, "publisher.py")
if not os.path.isfile(location):
this_dir = os.path.dirname(os.path.abspath(__file__))
data_dir = os.path.join(this_dir, '..', 'data')
location = os.path.join(data_dir, 'publisher.py')
if not os.path.isfile(location):
raise FileNotFoundError('{} cannot be located'.format(location))

location = _resolve_location('publisher.py')
self.proc = subprocess.Popen(['python', location, pub_data],
stdin=subprocess.PIPE)

Expand All @@ -76,14 +80,7 @@ def __init__(self, topic, msg_type, timeout):
self.msg_type = msg_type
self.killed = False

location = pkg_resources.resource_filename(__name__, "subscriber.py")
if not os.path.isfile(location):
this_dir = os.path.dirname(os.path.abspath(__file__))
data_dir = os.path.join(this_dir, '..', 'data')
location = os.path.join(data_dir, 'publisher.py')
location = os.path.join(data_dir, 'subscriber.py')
if not os.path.isfile(location):
raise FileNotFoundError('{} cannot be located'.format(location))
location = _resolve_location('subscriber.py')
self.proc = subprocess.Popen(['python', location,
pickle.dumps((topic, msg_type))], stdout=subprocess.PIPE)
self._message = None
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

setup(
name='pyrostest',
version='0.1.9',
version='0.1.10',
description='The most lit ros testing framework',
long_description=long_description,
packages=['pyrostest'],
Expand Down

0 comments on commit 6a4f2eb

Please sign in to comment.