From d97be9ae15beaf288929bfba7b3b6507a9f84562 Mon Sep 17 00:00:00 2001 From: Brian Gerkey Date: Tue, 3 Jan 2012 02:08:30 +0000 Subject: [PATCH] Added the ability to specify the name of the package that contains the test file. This is needed when the test file is a compiled program (e.g., gtest) that lives in the build tree, where rospkg's package-name-inference strategy doesn't behave as expected. The new stuff is optional; when not given, the old behavior persists. --- tools/rosunit/src/rosunit/baretest.py | 9 +++++++-- tools/rosunit/src/rosunit/rosunit_main.py | 13 +++++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/tools/rosunit/src/rosunit/baretest.py b/tools/rosunit/src/rosunit/baretest.py index e9ec1789..d6daf351 100644 --- a/tools/rosunit/src/rosunit/baretest.py +++ b/tools/rosunit/src/rosunit/baretest.py @@ -62,7 +62,7 @@ class TestTimeoutException(Exception): pass class BareTestCase(unittest.TestCase): - def __init__(self, exe, args, retry=0, time_limit=None, test_name=None, text_mode=False): + def __init__(self, exe, args, retry=0, time_limit=None, test_name=None, text_mode=False, package_name=None): """ @param exe: path to executable to run @type exe: str @@ -73,10 +73,15 @@ class BareTestCase(unittest.TestCase): @type time_limit: float @param test_name: (optional) override automatically generated test name @type test_name: str + @param package_name: (optional) override automatically inferred package name + @type package_name: str """ super(BareTestCase, self).__init__() self.text_mode = text_mode - self.package = rospkg.get_package_name(exe) + if package_name: + self.package = package_name + else: + self.package = rospkg.get_package_name(exe) self.exe = os.path.abspath(exe) if test_name is None: self.test_name = os.path.basename(exe) diff --git a/tools/rosunit/src/rosunit/rosunit_main.py b/tools/rosunit/src/rosunit/rosunit_main.py index 871c29b6..7dc525b8 100644 --- a/tools/rosunit/src/rosunit/rosunit_main.py +++ b/tools/rosunit/src/rosunit/rosunit_main.py @@ -63,6 +63,9 @@ def rosunitmain(): parser.add_option("--name", metavar="TEST_NAME", dest="test_name", default=None, help="Test name") + parser.add_option("--package", metavar="PACKAGE_NAME", + dest="pkg", default=None, + help="Package name (optional)") (options, args) = parser.parse_args() if len(args) < 1: @@ -78,8 +81,14 @@ def rosunitmain(): test_name = test_name[:test_name.rfind('.')] time_limit = float(options.time_limit) if options.time_limit else None + # If the caller didn't tell us the package name, we'll try to infer it. # compute some common names we'll be using to generate test names and files - pkg = rospkg.get_package_name(test_file) + pkg = options.pkg + if not pkg: + pkg = rospkg.get_package_name(test_file) + if not pkg: + print "Error: failed to determine package name for file '%s'; maybe you should supply the --package argument to rosunit?"%(test_file) + sys.exit(1) try: runner_result = None @@ -87,7 +96,7 @@ def rosunitmain(): test_case = BareTestCase(test_file, args[1:], \ retry=0, time_limit=time_limit, \ - test_name=test_name, text_mode=options.text_mode) + test_name=test_name, text_mode=options.text_mode, package_name=pkg) suite = unittest.TestSuite() suite.addTest(test_case)