1# Copyright (c) 2011, Willow Garage, Inc.
2# Copyright (c) 2012, Intermodalics, BVBA
3# All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions are met:
7#
8#     * Redistributions of source code must retain the above copyright
9#       notice, this list of conditions and the following disclaimer.
10#     * Redistributions in binary form must reproduce the above copyright
11#       notice, this list of conditions and the following disclaimer in the
12#       documentation and/or other materials provided with the distribution.
13#     * Neither the name of the Willow Garage, Inc. nor the names of its
14#       contributors may be used to endorse or promote products derived from
15#       this software without specific prior written permission.
16#
17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27# POSSIBILITY OF SUCH DAMAGE.
28
29# Author Ken Conley/kwc@willowgarage.com
30# Author Ruben Smits/ruben.smits@intermodalics.eu
31
32import os
33import traceback
34from mock import Mock, patch
35
36
37def get_test_dir():
38    return os.path.abspath(os.path.join(os.path.dirname(__file__), 'gem'))
39
40
41def test_gem_detect():
42    from rosdep2.platforms.gem import gem_detect
43
44    m = Mock()
45
46    # test behavior with empty freeze
47    m.return_value = ''
48    val = gem_detect([], exec_fn=m)
49    assert val == [], val
50
51    val = gem_detect(['rdoc'], exec_fn=m)
52    assert val == [], val
53
54    # read list output into mock exec_fn
55    with open(os.path.join(get_test_dir(), 'list_output'), 'r') as f:
56        m.return_value = f.read()
57    val = gem_detect(['rdoc'], exec_fn=m)
58    assert val == ['rdoc'], val
59
60    val = gem_detect(['rdoc', 'fakito', 'rake'], exec_fn=m)
61    assert val == ['rake', 'rdoc'], val
62
63
64def test_GemInstaller_get_depends():
65    # make sure GemInstaller supports depends
66    from rosdep2.platforms.gem import GemInstaller
67    installer = GemInstaller()
68    assert ['foo'] == installer.get_depends(dict(depends=['foo']))
69
70
71def test_GemInstaller():
72    from rosdep2 import InstallFailed
73    from rosdep2.platforms.gem import GemInstaller
74
75    @patch('rosdep2.platforms.gem.is_gem_installed')
76    def test_no_gem(mock_method):
77        mock_method.return_value = False
78        try:
79            installer = GemInstaller()
80            installer.get_install_command(['whatever'])
81            assert False, 'should have raised'
82        except InstallFailed:
83            pass
84
85    test_no_gem()
86
87    @patch('rosdep2.platforms.gem.is_gem_installed')
88    @patch.object(GemInstaller, 'get_packages_to_install')
89    def test(mock_method, mock_is_gem_installed):
90        mock_is_gem_installed.return_value = True
91        installer = GemInstaller()
92        mock_method.return_value = []
93        assert [] == installer.get_install_command(['fake'])
94
95        # no interactive option with GEM
96        mock_method.return_value = ['a', 'b']
97        expected = [['sudo', '-H', 'gem', 'install', 'a'],
98                    ['sudo', '-H', 'gem', 'install', 'b']]
99        val = installer.get_install_command(['whatever'], interactive=False)
100        assert val == expected, val
101        expected = [['sudo', '-H', 'gem', 'install', 'a'],
102                    ['sudo', '-H', 'gem', 'install', 'b']]
103        val = installer.get_install_command(['whatever'], interactive=True)
104        assert val == expected, val
105    try:
106        test()
107    except AssertionError:
108        traceback.print_exc()
109        raise
110