$search
00001 #!/usr/bin/env python 00002 # Software License Agreement (BSD License) 00003 # 00004 # Copyright (c) 2008, Willow Garage, Inc. 00005 # All rights reserved. 00006 # 00007 # Redistribution and use in source and binary forms, with or without 00008 # modification, are permitted provided that the following conditions 00009 # are met: 00010 # 00011 # * Redistributions of source code must retain the above copyright 00012 # notice, this list of conditions and the following disclaimer. 00013 # * Redistributions in binary form must reproduce the above 00014 # copyright notice, this list of conditions and the following 00015 # disclaimer in the documentation and/or other materials provided 00016 # with the distribution. 00017 # * Neither the name of Willow Garage, Inc. nor the names of its 00018 # contributors may be used to endorse or promote products derived 00019 # from this software without specific prior written permission. 00020 # 00021 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00024 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00025 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00026 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00027 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00028 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00029 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00030 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00031 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00032 # POSSIBILITY OF SUCH DAMAGE. 00033 # 00034 # Revision $Id$ 00035 00036 from __future__ import with_statement 00037 00038 NAME = 'test_rosmsg_command_line' 00039 import roslib; roslib.load_manifest('test_rosmsg') 00040 00041 import os 00042 import sys 00043 import unittest 00044 import cStringIO 00045 import time 00046 00047 import rostest 00048 00049 from subprocess import Popen, PIPE, check_call, call 00050 00051 class TestRosmsg(unittest.TestCase): 00052 00053 def setUp(self): 00054 pass 00055 00056 ## test that the rosmsg command works 00057 def test_cmd_help(self): 00058 sub = ['show', 'md5', 'package', 'packages', 'users'] 00059 00060 for cmd in ['rosmsg', 'rossrv']: 00061 output = Popen([cmd], stdout=PIPE).communicate()[0] 00062 self.assert_('Commands' in output) 00063 output = Popen([cmd, '-h'], stdout=PIPE).communicate()[0] 00064 self.assert_('Commands' in output) 00065 self.assert_('Traceback' not in output) 00066 for c in sub: 00067 self.assert_("%s %s"%(cmd, c) in output, output) 00068 00069 for c in sub: 00070 output = Popen([cmd, c, '-h'], stdout=PIPE).communicate()[0] 00071 self.assert_('Usage' in output) 00072 self.assert_("%s %s"%(cmd, c) in output, output) 00073 00074 def xtest_cmd_packages(self): 00075 # - single line 00076 output1 = Popen(['rosmsg', 'packages', '-s'], stdout=PIPE).communicate()[0] 00077 # - multi-line 00078 output2 = Popen(['rosmsg', 'packages'], stdout=PIPE).communicate()[0] 00079 l1 = [x for x in output1.split() if x] 00080 l2 = [x.strip() for x in output2.split('\n') if x.strip()] 00081 self.assertEquals(l1, l2) 00082 for p in ['roslib', 'test_rosmsg', 'std_msgs', 'test_ros']: 00083 self.assert_(p in l1) 00084 for p in ['std_srvs', 'rospy']: 00085 self.assert_(p not in l1) 00086 00087 output1 = Popen(['rossrv', 'packages', '-s'], stdout=PIPE).communicate()[0] 00088 output2 = Popen(['rossrv', 'packages'], stdout=PIPE).communicate()[0] 00089 l1 = [x for x in output1.split() if x] 00090 l2 = [x.strip() for x in output2.split('\n') if x.strip()] 00091 self.assertEquals(l1, l2) 00092 for p in ['test_rosmsg', 'std_srvs', 'test_ros']: 00093 self.assert_(p in l1) 00094 for p in ['std_msgs', 'rospy']: 00095 self.assert_(p not in l1) 00096 00097 def xtest_cmd_package(self): 00098 # this test is obviously very brittle, but should stabilize as the tests stabilize 00099 # - single line output 00100 output1 = Popen(['rosmsg', 'package', '-s', 'test_rosmsg'], stdout=PIPE).communicate()[0] 00101 # - multi-line output 00102 output2 = Popen(['rosmsg', 'package', 'test_rosmsg'], stdout=PIPE).communicate()[0] 00103 l = set([x for x in output1.split() if x]) 00104 l2 = set([x.strip() for x in output2.split('\n') if x.strip()]) 00105 self.assertEquals(l, l2) 00106 00107 self.assertEquals(set(['test_rosmsg/RosmsgA', 00108 'test_rosmsg/RosmsgB', 00109 'test_rosmsg/RosmsgC', 00110 ]), l) 00111 00112 output = Popen(['rossrv', 'package', '-s', 'test_rosmsg'], stdout=PIPE).communicate()[0] 00113 output2 = Popen(['rossrv', 'package','test_rosmsg'], stdout=PIPE).communicate()[0] 00114 l = set([x for x in output.split() if x]) 00115 l2 = set([x.strip() for x in output2.split('\n') if x.strip()]) 00116 self.assertEquals(l, l2) 00117 00118 self.assertEquals(set(['test_rosmsg/RossrvA', 'test_rosmsg/RossrvB']), l) 00119 00120 ## test that the rosmsg/rossrv show command works 00121 def xtest_cmd_show(self): 00122 output = Popen(['rosmsg', 'show', 'std_msgs/String'], stdout=PIPE).communicate()[0] 00123 self.assertEquals('string data', output.strip()) 00124 00125 output = Popen(['rossrv', 'show', 'std_srvs/Empty'], stdout=PIPE).communicate()[0] 00126 self.assertEquals('---', output.strip()) 00127 output = Popen(['rossrv', 'show', 'std_srvs/Empty'], stdout=PIPE).communicate()[0] 00128 self.assertEquals('---', output.strip()) 00129 output = Popen(['rossrv', 'show', 'test_ros/AddTwoInts'], stdout=PIPE).communicate()[0] 00130 self.assertEquals('int64 a\nint64 b\n---\nint64 sum', output.strip()) 00131 00132 # test against test_rosmsg package 00133 d = roslib.packages.get_pkg_dir('test_rosmsg') 00134 msg_d = os.path.join(d, 'msg') 00135 # - test with non-recursive types, which should have identical raw/non-raw 00136 for t in ['RosmsgA', 'RosmsgB']: 00137 with open(os.path.join(msg_d, '%s.msg'%t), 'r') as f: 00138 text = f.read() 00139 text = text+'\n' # running command adds one new line 00140 type_ ='test_rosmsg/'+t 00141 output = Popen(['rosmsg', 'show', type_], stdout=PIPE).communicate()[0] 00142 self.assertEquals(text, output) 00143 output = Popen(['rosmsg', 'show', '-r',type_], stdout=PIPE, stderr=PIPE).communicate() 00144 self.assertEquals(text, output[0], "Failed: %s"%(str(output))) 00145 output = Popen(['rosmsg', 'show', '--raw', type_], stdout=PIPE).communicate()[0] 00146 self.assertEquals(text, output) 00147 00148 # test as search 00149 type_ = t 00150 text = "[test_rosmsg/%s]:\n%s"%(t, text) 00151 output = Popen(['rosmsg', 'show', type_], stdout=PIPE).communicate()[0] 00152 self.assertEquals(text, output) 00153 output = Popen(['rosmsg', 'show', '-r',type_], stdout=PIPE, stderr=PIPE).communicate() 00154 self.assertEquals(text, output[0], "Failed: %s"%(str(output))) 00155 output = Popen(['rosmsg', 'show', '--raw', type_], stdout=PIPE).communicate()[0] 00156 self.assertEquals(text, output) 00157 00158 if __name__ == '__main__': 00159 rostest.unitrun('test_rosmsg', NAME, TestRosmsg, sys.argv, coverage_packages=[])