check_rosparam.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # Software License Agreement (BSD License)
3 #
4 # Copyright (c) 2009, Willow Garage, Inc.
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
10 #
11 # * Redistributions of source code must retain the above copyright
12 # notice, this list of conditions and the following disclaimer.
13 # * Redistributions in binary form must reproduce the above
14 # copyright notice, this list of conditions and the following
15 # disclaimer in the documentation and/or other materials provided
16 # with the distribution.
17 # * Neither the name of Willow Garage, Inc. nor the names of its
18 # contributors may be used to endorse or promote products derived
19 # from this software without specific prior written permission.
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 # POSSIBILITY OF SUCH DAMAGE.
33 
34 import os
35 import sys
36 import unittest
37 import rostest
38 
39 try:
40  from cStringIO import StringIO
41 except ImportError:
42  from io import StringIO
43 from subprocess import Popen, PIPE, check_call, call
44 
45 import rosgraph
46 import rosparam
47 
49  return rosgraph.Master('/test_rosparam')
50 
51 from contextlib import contextmanager
52 @contextmanager
53 def fakestdout():
54  realstdout = sys.stdout
55  fakestdout = StringIO()
56  sys.stdout = fakestdout
57  yield fakestdout
58  sys.stdout = realstdout
59 
60 @contextmanager
61 def fakestdin(input_str):
62  realstdin = sys.stdin
63  fakestdin = StringIO(input_str)
64  sys.stdin = fakestdin
65  yield fakestdin
66  sys.stdin = realstdin
67 
68 def tolist(b):
69  return [x.strip() for x in b.getvalue().split('\n') if x.strip()]
70 
72  return os.path.abspath(os.path.join(os.path.dirname(__file__)))
73 
74 class TestRosparam(unittest.TestCase):
75 
76  def setUp(self):
77  pass
78 
79  def _check(self, expected, actual):
80  """
81  Make sure all elements of expected are present in actual
82  """
83  for t in expected:
84  self.assert_(t in actual)
85  def _notcheck(self, not_expected, actual):
86  """
87  Make sure all elements of not_expected are not present in actual
88  """
89  for t in not_expected:
90  self.failIf(t in actual)
91 
92  def test_rosparam_list(self):
93  cmd = 'rosparam'
94 
95  params = ['/string', '/int', '/float',
96  '/g1/string', '/g1/int', '/g1/float',
97  '/g2/string', '/g2/int', '/g2/float',
98  ]
99  l = rosparam.list_params('')
100  for t in params:
101  self.assert_(t in l)
102 
103  with fakestdout() as b:
104  rosparam.yamlmain([cmd, 'list'])
105  self._check(params, tolist(b))
106  with fakestdout() as b:
107  rosparam.yamlmain([cmd, 'list', '/'])
108  self._check(params, tolist(b))
109 
110  # test with namespace
111  g1p = [p for p in params if p.startswith('/g1/')]
112  not_g1p = [p for p in params if not p.startswith('/g1/')]
113  with fakestdout() as b:
114  rosparam.yamlmain([cmd, 'list', '/g1'])
115  self._check(g1p, tolist(b))
116  self._notcheck(not_g1p, tolist(b))
117  with fakestdout() as b:
118  rosparam.yamlmain([cmd, 'list', '/g1/'])
119  self._check(g1p, tolist(b))
120  self._notcheck(not_g1p, tolist(b))
121  # test with no match
122  with fakestdout() as b:
123  rosparam.yamlmain([cmd, 'list', '/not/a/namespace/'])
124  self.assertEquals([], tolist(b))
125 
127  f = os.path.join(get_test_path(), 'test.yaml')
128  f_ns = os.path.join(get_test_path(), 'test_ns.yaml')
129 
130  cmd = 'rosparam'
131  try:
132  rosparam.yamlmain([cmd, 'load'])
133  self.fail("command-line arg should have failed")
134  except SystemExit as e:
135  self.assertNotEquals(0, e.code)
136  try:
137  rosparam.yamlmain([cmd, 'load', 'fake-file.yaml'])
138  self.fail("command-line arg should have failed")
139  except SystemExit as e:
140  self.assertNotEquals(0, e.code)
141 
142  ps = get_param_server()
143 
144  # load into top-level
145  rosparam.yamlmain([cmd, 'load', f])
146  self.assertEquals('bar', ps.getParam('/foo'))
147  # - make sure it did an overlay, not erase
148  self.assertEquals('foo-value', ps.getParam('/string'))
149 
150  rosparam.yamlmain([cmd, 'load', '-v', f])
151  self.assertEquals('bar', ps.getParam('/foo'))
152 
153  # load into top-level from stdin
154  with fakestdin('stdin_string: stdin_foo\nstdin_string2: stdin_bar'):
155  rosparam.yamlmain([cmd, 'load', '-'])
156  self.assertEquals('stdin_foo', ps.getParam('/stdin_string'))
157  self.assertEquals('stdin_bar', ps.getParam('/stdin_string2'))
158 
159  # load into namespace
160  rosparam.yamlmain([cmd, 'load', f, '/rosparam_load/test'])
161  self.assertEquals('bar', ps.getParam('/rosparam_load/test/foo'))
162  rosparam.yamlmain([cmd, 'load', '-v', f, '/rosparam_load/test'])
163  self.assertEquals('bar', ps.getParam('/rosparam_load/test/foo'))
164 
165  # load file with namespace spec in it
166  # - load into top-level
167  rosparam.yamlmain([cmd, 'load', f_ns])
168  self.assertEquals('baz', ps.getParam('/a/b/foo'))
169  self.assertEquals('bar', ps.getParam('/foo'))
170  rosparam.yamlmain([cmd, 'load', '-v', f_ns])
171  self.assertEquals('baz', ps.getParam('/a/b/foo'))
172 
173  # load into namespace
174  rosparam.yamlmain([cmd, 'load', f_ns, '/rosparam_load/test2'])
175  self.assertEquals('baz', ps.getParam('/rosparam_load/test2/a/b/foo'))
176  rosparam.yamlmain([cmd, 'load', '-v', f_ns, '/rosparam_load/test2'])
177  self.assertEquals('baz', ps.getParam('/rosparam_load/test2/a/b/foo'))
178 
179  def test_rosparam_get(self):
180  import rosparam
181  cmd = 'rosparam'
182  try:
183  rosparam.yamlmain([cmd, 'get'])
184  self.fail("command-line arg should have failed")
185  except SystemExit as e:
186  self.assertNotEquals(0, e.code)
187 
188  with fakestdout() as b:
189  rosparam.yamlmain([cmd, 'get', "string"])
190  self.assertEquals('foo-value', b.getvalue().strip())
191  with fakestdout() as b:
192  rosparam.yamlmain([cmd, 'get', '-p', "string"])
193  self.assertEquals('foo-value', b.getvalue().strip())
194  with fakestdout() as b:
195  rosparam.yamlmain([cmd, 'get', "/string"])
196  self.assertEquals('foo-value', b.getvalue().strip())
197  with fakestdout() as b:
198  rosparam.yamlmain([cmd, 'get', "/g1/string"])
199  self.assertEquals('g1-foo-value', b.getvalue().strip())
200  with fakestdout() as b:
201  rosparam.yamlmain([cmd, 'get', "g1/string"])
202  self.assertEquals('g1-foo-value', b.getvalue().strip())
203  with fakestdout() as b:
204  rosparam.yamlmain([cmd, 'get', "int"])
205  self.assertEquals('1', b.getvalue().strip())
206  with fakestdout() as b:
207  rosparam.yamlmain([cmd, 'get', "/int"])
208  self.assertEquals('1', b.getvalue().strip())
209  with fakestdout() as b:
210  rosparam.yamlmain([cmd, 'get', '-p', "int"])
211  self.assertEquals('1', b.getvalue().strip())
212  with fakestdout() as b:
213  rosparam.yamlmain([cmd, 'get', "/g1/int"])
214  self.assertEquals('10', b.getvalue().strip())
215  with fakestdout() as b:
216  rosparam.yamlmain([cmd, 'get', "g1/int"])
217  self.assertEquals('10', b.getvalue().strip())
218  with fakestdout() as b:
219  rosparam.yamlmain([cmd, 'get', "float"])
220  self.assertEquals('1.0', b.getvalue().strip())
221  with fakestdout() as b:
222  rosparam.yamlmain([cmd, 'get', '-p', "float"])
223  self.assertEquals('1.0', b.getvalue().strip())
224  with fakestdout() as b:
225  rosparam.yamlmain([cmd, 'get', '-p', "g1/float"])
226  self.assertEquals('10.0', b.getvalue().strip())
227  with fakestdout() as b:
228  rosparam.yamlmain([cmd, 'get', "g1"])
229  import yaml
230  d = yaml.load(b.getvalue())
231  self.assertEquals(d['float'], 10.0)
232  self.assertEquals(d['int'], 10.0)
233  self.assertEquals(d['string'], "g1-foo-value")
234  self.assertEquals(set(['float', 'int', 'string']), set(d.keys()))
235  with fakestdout() as b:
236  rosparam.yamlmain([cmd, 'get', '-p', "g1"])
237  with fakestdout() as b:
238  rosparam.yamlmain([cmd, 'get', '-pv', "g1"])
239 
240  def test_rosparam_set(self):
241  import rosparam
242  cmd = 'rosparam'
243 
244  ps = get_param_server()
245  with fakestdout() as b:
246  rosparam.yamlmain([cmd, 'set', "/rosparam_set/test1", "1"])
247  self.assertEquals(1, ps.getParam('/rosparam_set/test1'))
248  with fakestdout() as b:
249  # -- verbose
250  rosparam.yamlmain([cmd, 'set', '-v', "/rosparam_set/test1", "1"])
251  self.assertEquals(1, ps.getParam('/rosparam_set/test1'))
252  with fakestdout() as b:
253  rosparam.yamlmain([cmd, 'set', "rosparam_set/test1", "2"])
254  self.assertEquals(2, ps.getParam('/rosparam_set/test1'))
255 
256  with fakestdout() as b:
257  # - floats
258  rosparam.yamlmain([cmd, 'set', "/rosparam_set/test2", "1.0"])
259  self.assertEquals(1., ps.getParam('/rosparam_set/test2'))
260  with fakestdout() as b:
261  # - floats
262  rosparam.yamlmain([cmd, 'set', "/rosparam_set/test2", "2.0"])
263  self.assertEquals(2., ps.getParam('/rosparam_set/test2'))
264  with fakestdout() as b:
265  # - booleans
266  rosparam.yamlmain([cmd, 'set', "/rosparam_set/testbool", "true"])
267  self.assertEquals(True, ps.getParam('/rosparam_set/testbool'))
268  with fakestdout() as b:
269  # - strings
270  rosparam.yamlmain([cmd, 'set', "/rosparam_set/teststr", "hi"])
271  self.assertEquals("hi", ps.getParam('/rosparam_set/teststr'))
272  with fakestdout() as b:
273  # - list
274  rosparam.yamlmain([cmd, 'set', "/rosparam_set/testlist", "[1, 2, 3]"])
275  self.assertEquals([1, 2, 3], ps.getParam('/rosparam_set/testlist'))
276  with fakestdout() as b:
277  # - dictionary
278  rosparam.yamlmain([cmd, 'set', "/rosparam_set/testdict", "{a: b, c: d}"])
279  self.assertEquals('b', ps.getParam('/rosparam_set/testdict/a'))
280  self.assertEquals('d', ps.getParam('/rosparam_set/testdict/c'))
281  with fakestdout() as b:
282  # - empty dictionary should be a noop
283  rosparam.yamlmain([cmd, 'set', "set/testdict", "{}"])
284  self.assertEquals('b', ps.getParam('/rosparam_set/testdict/a'))
285  self.assertEquals('d', ps.getParam('/rosparam_set/testdict/c'))
286  with fakestdout() as b:
287  # - this should be an update
288  rosparam.yamlmain([cmd, 'set', "/rosparam_set/testdict", "{e: f, g: h}"])
289  self.assertEquals('b', ps.getParam('/rosparam_set/testdict/a'))
290  self.assertEquals('d', ps.getParam('/rosparam_set/testdict/c'))
291  self.assertEquals('f', ps.getParam('/rosparam_set/testdict/e'))
292  self.assertEquals('h', ps.getParam('/rosparam_set/testdict/g'))
293  with fakestdout() as b:
294  # -- verbose
295  rosparam.yamlmain([cmd, 'set', '-v', "/rosparam_set/testdictverbose", "{e: f, g: h}"])
296  self.assertEquals('f', ps.getParam('/rosparam_set/testdictverbose/e'))
297  self.assertEquals('h', ps.getParam('/rosparam_set/testdictverbose/g'))
298 
300  import rosparam
301  cmd = 'rosparam'
302  ps = get_param_server()
303 
304  try:
305  rosparam.yamlmain([cmd, 'delete'])
306  except SystemExit as e:
307  self.assertNotEquals(0, e.code)
308  try:
309  rosparam.yamlmain([cmd, 'delete', 'one', 'two'])
310  except SystemExit as e:
311  self.assertNotEquals(0, e.code)
312 
313  # delete
314  ps.setParam('/delete/me', True)
315  self.assert_(ps.hasParam('/delete/me'))
316  rosparam.yamlmain([cmd, 'delete', "/delete/me"])
317  self.failIf(ps.hasParam('/delete/me'))
318 
319  ps.setParam('/delete/me2', True)
320  self.assert_(ps.hasParam('/delete/me2'))
321  rosparam.yamlmain([cmd, 'delete', '-v', "/delete/me2"])
322  self.failIf(ps.hasParam('/delete/me2'))
323 
325  import rosparam
326  f = os.path.join(get_test_path(), 'test.yaml')
327  f_out = os.path.join(get_test_path(), 'test_dump.yaml')
328 
329  cmd = 'rosparam'
330  ps = get_param_server()
331 
332  try:
333  rosparam.yamlmain([cmd, 'dump'])
334  except SystemExit as e:
335  self.assertNotEquals(0, e.code)
336  try:
337  rosparam.yamlmain([cmd, 'dump', f_out, 'rosparam_dump', 'rosparam_dump2'])
338  except SystemExit as e:
339  self.assertNotEquals(0, e.code)
340 
341  rosparam.yamlmain([cmd, 'load', f, 'rosparam_dump'])
342  self.assertEquals('bar', ps.getParam('rosparam_dump/foo'))
343 
344  rosparam.yamlmain([cmd, 'dump', f_out, 'rosparam_dump'])
345  # yaml files should be equal
346  import yaml
347  with open(f_out) as b:
348  with open(f) as b2:
349  self.assertEquals(yaml.load(b.read()), yaml.load(b2.read()))
350 
351  rosparam.yamlmain([cmd, 'dump', '-v', f_out, 'rosparam_dump'])
352  with open(f_out) as b:
353  with open(f) as b2:
354  self.assertEquals(yaml.load(b.read()), yaml.load(b2.read()))
355 
356  # yaml file and std_out should be the same
357  with fakestdout() as b:
358  rosparam.yamlmain([cmd, 'dump'])
359  with open(f) as b2:
360  self.assertEquals(yaml.load(b.getvalue())['rosparam_dump'], yaml.load(b2.read()))
361 
362  def test_fullusage(self):
363  import rosparam
364  try:
365  rosparam._fullusage()
366  except SystemExit: pass
367  try:
368  rosparam.yamlmain(['rosparam'])
369  except SystemExit: pass
370  try:
371  rosparam.yamlmain(['rosparam', 'invalid'])
372  except SystemExit: pass
373 
374 PKG = 'test_rosparam'
375 NAME = 'test_rosparam_command_line_online'
376 if __name__ == '__main__':
377  rostest.unitrun(PKG, NAME, TestRosparam, sys.argv, coverage_packages=['rosparam'])
def _check(self, expected, actual)
def _notcheck(self, not_expected, actual)
def fakestdin(input_str)


test_rosparam
Author(s): Ken Conley
autogenerated on Sun Feb 3 2019 03:30:21