test_roslib_gentools.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # Software License Agreement (BSD License)
3 #
4 # Copyright (c) 2008, 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 from __future__ import print_function
35 
36 import os
37 import string
38 import sys
39 import unittest
40 
41 import roslib.names
42 import roslib.packages
43 
44 TEST_CTX = 'rosgraph_msgs'
45 
46 import roslib
47 PKG='test_roslib_comm'
48 
49 class TestGentools(unittest.TestCase):
50 
51  def setUp(self):
52  pass
53 
54  def _load_md5_tests(self, dir):
55  test_dir = os.path.join(roslib.packages.get_pkg_dir(PKG), 'test', 'md5tests', dir)
56  tests = {}
57  for f in os.listdir(test_dir):
58  path = os.path.join(test_dir, f)
59  if not f.endswith('.txt'):
60  continue
61  name = f[:-4]
62  while name and name[-1].isdigit():
63  name = name[:-1]
64  self.assert_(name)
65  if name in tests:
66  tests[name].append(path)
67  else:
68  tests[name] = [path]
69  return tests
70 
71  def _compute_md5(self, f):
72  from roslib.gentools import compute_md5, get_dependencies
73  from roslib.msgs import load_from_string
74 
75  text = open(f, 'r').read()
76  spec = load_from_string(text, package_context=TEST_CTX)
77  get_deps_dict = get_dependencies(spec, TEST_CTX, compute_files=False)
78  return compute_md5(get_deps_dict)
79 
80  def _compute_md5_text(self, f):
81  from roslib.gentools import compute_md5_text, get_dependencies
82  from roslib.msgs import load_from_string
83 
84  text = open(f, 'r').read()
85  spec = load_from_string(text, package_context=TEST_CTX)
86  get_deps_dict = get_dependencies(spec, TEST_CTX, compute_files=False)
87  return compute_md5_text(get_deps_dict, spec)
88 
90  from std_msgs.msg import Header
91  Header_md5 = Header._md5sum
92  rg_msg_dir = os.path.join(roslib.packages.get_pkg_dir(TEST_CTX), 'msg')
93  clock_msg = os.path.join(rg_msg_dir, 'Clock.msg')
94  # a bit gory, but go ahead and regression test these important messages
95  self.assertEquals("time clock", self._compute_md5_text(clock_msg))
96  log_msg = os.path.join(rg_msg_dir, 'Log.msg')
97  self.assertEquals("byte DEBUG=1\nbyte INFO=2\nbyte WARN=4\nbyte ERROR=8\nbyte FATAL=16\n%s header\nbyte level\nstring name\nstring msg\nstring file\nstring function\nuint32 line\nstring[] topics"%Header_md5, self._compute_md5_text(log_msg))
98 
99  tests = self._load_md5_tests('md5text')
100  # text file #1 is the reference
101  for k, files in tests.items():
102  print("running tests", k)
103  ref_file = [f for f in files if f.endswith('%s1.txt'%k)]
104  if not ref_file:
105  self.fail("failed to load %s"%k)
106  ref_file = ref_file[0]
107  ref_text = open(ref_file, 'r').read().strip()
108  print("KEY", k)
109  files = [f for f in files if not f.endswith('%s1.txt'%k)]
110  for f in files[1:]:
111  f_text = self._compute_md5_text(f)
112  self.assertEquals(ref_text, f_text, "failed on %s\n%s\n%s: \n[%s]\nvs.\n[%s]\n"%(k, ref_file, f, ref_text, f_text))
113 
114  def test_md5_equals(self):
115  tests = self._load_md5_tests('same')
116  for k, files in tests.items():
117  print("running tests", k)
118  md5sum = self._compute_md5(files[0])
119  for f in files[1:]:
120  self.assertEquals(md5sum, self._compute_md5(f), "failed on %s: \n[%s]\nvs.\n[%s]\n"%(k, self._compute_md5_text(files[0]), self._compute_md5_text(f)))
121 
123  tests = self._load_md5_tests('different')
124  for k, files in tests.items():
125  print("running tests", k)
126  md5s = set()
127  md6md5sum = self._compute_md5(files[0])
128  for f in files:
129  md5s.add(self._compute_md5(f))
130  # each md5 should be unique
131  self.assertEquals(len(md5s), len(files))


test_roslib_comm
Author(s): Jeremy Leibs, Ken Conley, Dirk Thomas
autogenerated on Mon Nov 2 2020 03:52:23