$search
00001 # Software License Agreement (BSD License) 00002 # 00003 # Copyright (c) 2008, Willow Garage, Inc. 00004 # All rights reserved. 00005 # 00006 # Redistribution and use in source and binary forms, with or without 00007 # modification, are permitted provided that the following conditions 00008 # are met: 00009 # 00010 # * Redistributions of source code must retain the above copyright 00011 # notice, this list of conditions and the following disclaimer. 00012 # * Redistributions in binary form must reproduce the above 00013 # copyright notice, this list of conditions and the following 00014 # disclaimer in the documentation and/or other materials provided 00015 # with the distribution. 00016 # * Neither the name of Willow Garage, Inc. nor the names of its 00017 # contributors may be used to endorse or promote products derived 00018 # from this software without specific prior written permission. 00019 # 00020 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00021 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00022 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00023 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00024 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00025 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00026 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00027 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00028 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00029 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00030 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00031 # POSSIBILITY OF SUCH DAMAGE. 00032 # 00033 # Author: Brian Gerkey/Ken Conley 00034 00035 from __future__ import with_statement 00036 00037 PKG = 'test_rxdeps' 00038 import roslib; roslib.load_manifest(PKG) 00039 00040 import os 00041 import unittest 00042 import tempfile 00043 import shutil 00044 from subprocess import Popen, PIPE 00045 00046 ROS_ROOT = 'ROS_ROOT' 00047 ROS_PACKAGE_PATH = 'ROS_PACKAGE_PATH' 00048 ROS_LANG_DISABLE = 'ROS_LANG_DISABLE' 00049 00050 ## Process-level tests of rxdeps executable 00051 class RxdepsTestCase(unittest.TestCase): 00052 00053 def setUp(self): 00054 self.rxdeps_dir = roslib.packages.get_pkg_dir("rxdeps") 00055 self.pkg_dir = roslib.packages.get_pkg_dir("test_rxdeps") 00056 self.assert_(os.path.isdir(self.pkg_dir), "cannot locate test_rxdeps") 00057 self.test_dir = os.path.join(self.pkg_dir,"test/test_packages") 00058 self.assert_(os.path.isdir(self.test_dir), "cannot locate test dirs") 00059 00060 ## runs rxdeps with ROS_ROOT set to ./test and ROS_PACKAGE_PATH unset 00061 ## @return int, str: return code, stdout 00062 def _run_rxdeps(self, ros_package_path, pkgname, command): 00063 env = os.environ.copy() 00064 if ros_package_path is not None: 00065 env[ROS_PACKAGE_PATH] = ros_package_path+os.pathsep+self.rxdeps_dir 00066 00067 # Must split up the command string into its whitespace separated 00068 # components; otherwise you get multiple words as one element of 00069 # argv. 00070 #args = ["rxdeps", command, pkgname] 00071 args = ["rxdeps"] 00072 if command: 00073 for s in command.split(): 00074 args.append(s) 00075 if pkgname is not None: 00076 args.append("--target=%s"%pkgname) 00077 p = Popen(args, stdout=PIPE, stderr=PIPE, env=env) 00078 stdout, stderr = p.communicate() 00079 00080 # Also test command aliases, verifying that they give the same 00081 # return code and console output 00082 if command: 00083 aliases = {} 00084 cmd = command.split()[-1] 00085 if cmd in aliases: 00086 args[-2] = aliases[cmd] 00087 alias_p = Popen(args, stdout=PIPE, stderr=PIPE, env=env) 00088 alias_stdout, alias_stderr = alias_p.communicate() 00089 self.assertEquals(p.returncode, alias_p.returncode) 00090 self.assertEquals(stdout, alias_stdout) 00091 #self.assertEquals(stderr, alias_stderr) 00092 00093 return p.returncode, stdout.strip(), stderr 00094 00095 00096 def test_utest(self): 00097 ret, out, err = self._run_rxdeps(self.test_dir, "pkg1", "--graphviz-output=deps.gv") 00098 self.assertTrue(ret == 0) 00099 #print ret, out, err 00100 with open("deps.gv") as fh: 00101 lines = fh.read().split("\n") 00102 self.assertTrue(" \"pkg2\" -> \"pkg1\";" in lines) 00103 self.assertTrue(" \"pkg3\" -> \"pkg2\";" in lines) 00104 self.assertTrue(" \"pkg4\" -> \"pkg2\";" in lines) 00105 self.assertTrue(" \"pkg5\" -> \"pkg3\";" in lines) 00106 self.assertTrue(" \"pkg5\" -> \"pkg4\";" in lines) 00107 00108 # make sure the intermediate is cleaned up 00109 os.remove("deps.gv") 00110 00111 # clean up the output too 00112 self.assertTrue(os.path.exists("deps.pdf")) 00113 os.remove("deps.pdf") 00114 00115 def test_exclude(self): 00116 ret, out, err = self._run_rxdeps(self.test_dir, "pkg1", "--graphviz-output=deps.gv --exclude=pkg2") 00117 self.assertTrue(ret == 0) 00118 #print ret, out, err 00119 with open("deps.gv") as fh: 00120 lines = fh.read().split("\n") 00121 self.assertFalse("pkg2" in lines) 00122 self.assertTrue(" \"pkg5\" -> \"pkg3\";" in lines) 00123 self.assertTrue(" \"pkg5\" -> \"pkg4\";" in lines) 00124 00125 # make sure the intermediate is cleaned up 00126 os.remove("deps.gv") 00127 00128 # clean up the output too 00129 self.assertTrue(os.path.exists("deps.pdf")) 00130 os.remove("deps.pdf") 00131 00132 00133 def test_output_arg(self): 00134 ret, out, err = self._run_rxdeps(self.test_dir, "pkg1", "--graphviz-output=deps.gv -oout.pdf") 00135 self.assertTrue(ret == 0) 00136 #print ret, out, err 00137 with open("deps.gv") as fh: 00138 lines = fh.read().split("\n") 00139 self.assertTrue(" \"pkg2\" -> \"pkg1\";" in lines) 00140 self.assertTrue(" \"pkg3\" -> \"pkg2\";" in lines) 00141 self.assertTrue(" \"pkg4\" -> \"pkg2\";" in lines) 00142 self.assertTrue(" \"pkg5\" -> \"pkg3\";" in lines) 00143 self.assertTrue(" \"pkg5\" -> \"pkg4\";" in lines) 00144 00145 00146 # make sure the intermediate is cleaned up 00147 os.remove("deps.gv") 00148 00149 # clean up the output too 00150 self.assertTrue(os.path.exists("out.pdf")) 00151 os.remove("out.pdf") 00152 00153 def test_usage(self): 00154 ret, out, err = self._run_rxdeps(self.test_dir, "pkg1", "-h") 00155 self.assertTrue(ret == 0) 00156 self.assertEqual(out.count("Usage:"), 1) 00157 #print ret, out, err 00158 00159 if __name__ == '__main__': 00160 import rostest 00161 rostest.unitrun(PKG, 'rxdeps_exe_process', RxdepsTestCase)