1# Software License Agreement (BSD License)
2#
3# Copyright (c) 2011, Willow Garage, Inc.
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9#
10#  * Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12#  * Redistributions in binary form must reproduce the above
13#    copyright notice, this list of conditions and the following
14#    disclaimer in the documentation and/or other materials provided
15#    with the distribution.
16#  * Neither the name of Willow Garage, Inc. nor the names of its
17#    contributors may be used to endorse or promote products derived
18#    from this software without specific prior written permission.
19#
20# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31# POSSIBILITY OF SUCH DAMAGE.
32
33import os
34import tempfile
35
36
37def test_get_ros_root():
38    from rospkg import get_ros_root
39    assert get_ros_root(env={}) is None
40
41    env = {'ROS_ROOT': '/fake/path'}
42    assert '/fake/path' == get_ros_root(env=env)
43
44    real_ros_root = get_ros_root()
45
46    if real_ros_root is not None:
47        # make sure that ros root is a directory
48        p = os.path.join(real_ros_root, 'Makefile')
49        env = {'ROS_ROOT': p}
50        assert p == get_ros_root(env=env)
51
52
53def test_get_ros_package_path():
54    from rospkg import get_ros_package_path
55    assert get_ros_package_path(env={}) is None
56    env = {'ROS_PACKAGE_PATH': ':'}
57    assert ':' == get_ros_package_path(env=env)
58
59    # trip-wire tests. Cannot guarantee that ROS_PACKAGE_PATH is set
60    # to valid value on test machine, just make sure logic doesn't crash
61    assert os.environ.get('ROS_PACKAGE_PATH', None) == get_ros_package_path()
62
63
64def test_get_log_dir():
65    from rospkg import get_log_dir, get_ros_root
66    base = tempfile.gettempdir()
67    ros_log_dir = os.path.join(base, 'ros_log_dir')
68    ros_home_dir = os.path.join(base, 'ros_home_dir')
69    home_dir = os.path.expanduser('~')
70
71    # ROS_LOG_DIR has precedence
72    env = {'ROS_ROOT': get_ros_root(), 'ROS_LOG_DIR': ros_log_dir, 'ROS_HOME': ros_home_dir}
73    assert ros_log_dir == get_log_dir(env=env)
74
75    env = {'ROS_ROOT': get_ros_root(), 'ROS_HOME': ros_home_dir}
76    assert os.path.join(ros_home_dir, 'log') == get_log_dir(env=env)
77
78    env = {'ROS_ROOT': get_ros_root()}
79    assert os.path.join(home_dir, '.ros', 'log') == get_log_dir(env=env)
80
81    # test default assignment of env. Don't both checking return value as we would duplicate get_log_dir
82    assert get_log_dir() is not None
83
84
85def test_get_test_results_dir():
86    from rospkg import get_ros_root, get_test_results_dir
87    base = tempfile.gettempdir()
88    ros_test_results_dir = os.path.join(base, 'ros_test_results_dir')
89    ros_home_dir = os.path.join(base, 'ros_home_dir')
90    home_dir = os.path.expanduser('~')
91
92    # ROS_TEST_RESULTS_DIR has precedence
93    env = {'ROS_ROOT': get_ros_root(), 'ROS_TEST_RESULTS_DIR': ros_test_results_dir, 'ROS_HOME': ros_home_dir}
94    assert ros_test_results_dir == get_test_results_dir(env=env)
95
96    env = {'ROS_ROOT': get_ros_root(), 'ROS_HOME': ros_home_dir}
97    assert os.path.join(ros_home_dir, 'test_results') == get_test_results_dir(env=env)
98
99    env = {'ROS_ROOT': get_ros_root()}
100    assert os.path.join(home_dir, '.ros', 'test_results') == get_test_results_dir(env=env)
101
102    # test default assignment of env. Don't both checking return value as we would duplicate get_test_results_dir
103    assert get_test_results_dir() is not None
104
105
106def test_get_ros_home():
107    from rospkg import get_ros_root, get_ros_home
108    base = tempfile.gettempdir()
109    ros_home_dir = os.path.join(base, 'ros_home_dir')
110    home_dir = os.path.expanduser('~')
111
112    # ROS_HOME has precedence
113    env = {'ROS_ROOT': get_ros_root(), 'ROS_HOME': ros_home_dir}
114    assert ros_home_dir == get_ros_home(env=env)
115
116    env = {'ROS_ROOT': get_ros_root()}
117    assert os.path.join(home_dir, '.ros') == get_ros_home(env=env)
118
119    # test default assignment of env. Don't both checking return value
120    assert get_ros_home() is not None
121
122
123def test_on_ros_path():
124    from rospkg import on_ros_path, get_ros_root, get_ros_package_path
125    from rospkg.environment import _resolve_paths
126
127    assert not on_ros_path(tempfile.gettempdir())
128
129    if get_ros_root() is not None:
130        assert on_ros_path(get_ros_root())
131
132        if get_ros_package_path() is not None:
133            paths = _resolve_paths(get_ros_package_path()).split(os.pathsep)
134            for p in paths:
135                assert on_ros_path(p), "failed: %s, [%s]" % (p, paths)
136
137
138def test_compute_package_paths():
139    from rospkg.environment import _compute_package_paths as compute_package_paths
140    assert compute_package_paths(None, None) == []
141    assert compute_package_paths('foo', None) == ['foo']
142    assert compute_package_paths(None, 'bar') == ['bar'], compute_package_paths(None, 'bar')
143    assert compute_package_paths('foo', '') == ['foo']
144    assert compute_package_paths('foo', 'bar') == ['foo', 'bar']
145    assert compute_package_paths('foo', 'bar:bz') == ['foo', 'bar', 'bz']
146    assert compute_package_paths('foo', 'bar:bz::blah') == ['foo', 'bar', 'bz', 'blah']
147
148
149def test_resolve_path():
150    # mainly for coverage
151    from rospkg.environment import _resolve_path
152    assert os.path.expanduser('~') == _resolve_path('~')
153
154
155def test_get_etc_ros_dir():
156    from rospkg import get_etc_ros_dir
157    from rospkg.environment import ROS_ETC_DIR
158    base = tempfile.gettempdir()
159    etc_ros_dir = os.path.join(base, 'etc_ros_dir')
160
161    assert '/usr/local/etc/ros' == get_etc_ros_dir(env={})
162
163    # ROS_ETC_DIR has precedence
164    env = {ROS_ETC_DIR: etc_ros_dir}
165    assert etc_ros_dir == get_etc_ros_dir(env=env), get_etc_ros_dir(env=env)
166
167    # test default assignment of env. Don't both checking return value as we would duplicate get_etc_ros_dir
168    assert get_etc_ros_dir() is not None
169