|
1 | 1 | # Copyright 2016-2018 Dirk Thomas |
2 | 2 | # Licensed under the Apache License, Version 2.0 |
3 | 3 |
|
| 4 | +import os |
4 | 5 | import shutil |
5 | 6 | import signal |
6 | 7 | import sys |
7 | 8 | from tempfile import mkdtemp |
| 9 | +from tempfile import TemporaryDirectory |
8 | 10 | from unittest.mock import Mock |
9 | 11 | from unittest.mock import patch |
10 | 12 |
|
11 | 13 | from colcon_core.command import CommandContext |
12 | 14 | from colcon_core.command import create_parser |
| 15 | +from colcon_core.command import get_prog_name |
13 | 16 | from colcon_core.command import main |
14 | 17 | from colcon_core.command import verb_main |
15 | 18 | from colcon_core.environment_variable import EnvironmentVariable |
@@ -151,3 +154,86 @@ def test_verb_main(): |
151 | 154 | assert logger.error.call_args[0][0].startswith( |
152 | 155 | 'command_name verb_name: custom error message\n') |
153 | 156 | assert 'Exception: custom error message' in logger.error.call_args[0][0] |
| 157 | + |
| 158 | + |
| 159 | +def test_prog_name_module(): |
| 160 | + argv = [os.path.join('foo', 'bar', '__main__.py')] |
| 161 | + with patch('colcon_core.command.sys.argv', argv): |
| 162 | + # prog should be the module containing __main__.py |
| 163 | + assert get_prog_name() == 'bar' |
| 164 | + |
| 165 | + |
| 166 | +def test_prog_name_on_path(): |
| 167 | + # use __file__ since we know it exists |
| 168 | + argv = [__file__] |
| 169 | + with patch('colcon_core.command.sys.argv', argv): |
| 170 | + with patch( |
| 171 | + 'colcon_core.command.shutil.which', |
| 172 | + return_value=__file__ |
| 173 | + ): |
| 174 | + # prog should be shortened to the basename |
| 175 | + assert get_prog_name() == 'test_command.py' |
| 176 | + |
| 177 | + |
| 178 | +def test_prog_name_not_on_path(): |
| 179 | + # use __file__ since we know it exists |
| 180 | + argv = [__file__] |
| 181 | + with patch('colcon_core.command.sys.argv', argv): |
| 182 | + with patch('colcon_core.command.shutil.which', return_value=None): |
| 183 | + # prog should remain unchanged |
| 184 | + assert get_prog_name() == __file__ |
| 185 | + |
| 186 | + |
| 187 | +def test_prog_name_different_on_path(): |
| 188 | + # use __file__ since we know it exists |
| 189 | + argv = [__file__] |
| 190 | + with patch('colcon_core.command.sys.argv', argv): |
| 191 | + with patch( |
| 192 | + 'colcon_core.command.shutil.which', |
| 193 | + return_value=sys.executable |
| 194 | + ): |
| 195 | + # prog should remain unchanged |
| 196 | + assert get_prog_name() == __file__ |
| 197 | + |
| 198 | + |
| 199 | +def test_prog_name_not_a_file(): |
| 200 | + # pick some file that doesn't actually exist on disk |
| 201 | + no_such_file = os.path.join(__file__, 'foobar') |
| 202 | + argv = [no_such_file] |
| 203 | + with patch('colcon_core.command.sys.argv', argv): |
| 204 | + with patch( |
| 205 | + 'colcon_core.command.shutil.which', |
| 206 | + return_value=no_such_file |
| 207 | + ): |
| 208 | + # prog should remain unchanged |
| 209 | + assert get_prog_name() == no_such_file |
| 210 | + |
| 211 | + |
| 212 | +@pytest.mark.skipif(sys.platform == 'win32', reason='Symlinks not supported.') |
| 213 | +def test_prog_name_symlink(): |
| 214 | + # use __file__ since we know it exists |
| 215 | + with TemporaryDirectory(prefix='test_colcon_') as temp_dir: |
| 216 | + linked_file = os.path.join(temp_dir, 'test_command.py') |
| 217 | + os.symlink(__file__, linked_file) |
| 218 | + |
| 219 | + argv = [linked_file] |
| 220 | + with patch('colcon_core.command.sys.argv', argv): |
| 221 | + with patch( |
| 222 | + 'colcon_core.command.shutil.which', |
| 223 | + return_value=__file__ |
| 224 | + ): |
| 225 | + # prog should be shortened to the basename |
| 226 | + assert get_prog_name() == 'test_command.py' |
| 227 | + |
| 228 | + |
| 229 | +@pytest.mark.skipif(sys.platform != 'win32', reason='Only valid on Windows.') |
| 230 | +def test_prog_name_easy_install(): |
| 231 | + # use __file__ since we know it exists |
| 232 | + argv = [__file__[:-3]] |
| 233 | + with patch('colcon_core.command.sys.argv', argv): |
| 234 | + with patch( |
| 235 | + 'colcon_core.command.shutil.which', |
| 236 | + return_value=__file__ |
| 237 | + ): |
| 238 | + # prog should be shortened to the basename |
| 239 | + assert get_prog_name() == 'test_command' |
0 commit comments