|
5 | 5 | from pathlib import Path |
6 | 6 | import sys |
7 | 7 | from tempfile import TemporaryDirectory |
| 8 | +from unittest.mock import patch |
8 | 9 |
|
9 | 10 | from colcon_core import shell |
10 | 11 | from colcon_core.plugin_system import SkipExtensionException |
@@ -39,33 +40,36 @@ def _test_extension(prefix_path): |
39 | 40 | extension.create_prefix_script(prefix_path, False) |
40 | 41 | assert (prefix_path / 'local_setup.bat').exists() |
41 | 42 |
|
| 43 | + # create_hook_append_value |
| 44 | + append_hook_path = extension.create_hook_append_value( |
| 45 | + 'append_env_hook_name', prefix_path, 'pkg_name', |
| 46 | + 'APPEND_NAME', 'subdirectory') |
| 47 | + assert append_hook_path.exists() |
| 48 | + assert append_hook_path.name == 'append_env_hook_name.bat' |
| 49 | + content = append_hook_path.read_text() |
| 50 | + assert 'APPEND_NAME' in content |
| 51 | + |
| 52 | + # create_hook_prepend_value |
| 53 | + prepend_hook_path = extension.create_hook_prepend_value( |
| 54 | + 'prepend_env_hook_name', prefix_path, 'pkg_name', |
| 55 | + 'PREPEND_NAME', 'subdirectory') |
| 56 | + assert prepend_hook_path.exists() |
| 57 | + assert prepend_hook_path.name == 'prepend_env_hook_name.bat' |
| 58 | + content = prepend_hook_path.read_text() |
| 59 | + assert 'PREPEND_NAME' in content |
| 60 | + |
42 | 61 | # create_package_script |
43 | 62 | extension.create_package_script( |
44 | 63 | prefix_path, 'pkg_name', [ |
45 | | - ('hookA.bat', '/some/path/hookA.bat'), |
| 64 | + (append_hook_path.relative_to(prefix_path), ()), |
| 65 | + (prepend_hook_path.relative_to(prefix_path), ()), |
46 | 66 | ('hookB.other', '/some/path/hookB.other')]) |
47 | 67 | assert (prefix_path / 'share' / 'pkg_name' / 'package.bat').exists() |
48 | 68 | content = (prefix_path / 'share' / 'pkg_name' / 'package.bat').read_text() |
49 | | - assert 'hookA' in content |
| 69 | + assert append_hook_path.name in content |
| 70 | + assert prepend_hook_path.name in content |
50 | 71 | assert 'hookB' not in content |
51 | 72 |
|
52 | | - # create_hook_append_value |
53 | | - hook_path = extension.create_hook_append_value( |
54 | | - 'append_env_hook_name', prefix_path, 'pkg_name', |
55 | | - 'APPEND_NAME', 'append_subdirectory') |
56 | | - assert hook_path.exists() |
57 | | - assert hook_path.name == 'append_env_hook_name.bat' |
58 | | - content = hook_path.read_text() |
59 | | - assert 'APPEND_NAME' in content |
60 | | - |
61 | | - # create_hook_prepend_value |
62 | | - hook_path = extension.create_hook_prepend_value( |
63 | | - 'env_hook_name', prefix_path, 'pkg_name', 'NAME', 'subdirectory') |
64 | | - assert hook_path.exists() |
65 | | - assert hook_path.name == 'env_hook_name.bat' |
66 | | - content = hook_path.read_text() |
67 | | - assert 'NAME' in content |
68 | | - |
69 | 73 | # generate_command_environment |
70 | 74 | if sys.platform != 'win32': |
71 | 75 | with pytest.raises(SkipExtensionException) as e: |
@@ -93,3 +97,43 @@ def _test_extension(prefix_path): |
93 | 97 | 'task_name', prefix_path, {'dep': str(prefix_path)}) |
94 | 98 | env = run_until_complete(coroutine) |
95 | 99 | assert isinstance(env, dict) |
| 100 | + |
| 101 | + subdirectory_path = str(prefix_path / 'subdirectory') |
| 102 | + |
| 103 | + # validate appending/prepending |
| 104 | + with patch.dict(os.environ, { |
| 105 | + 'APPEND_NAME': os.pathsep.join(('', '', 'control', '', '')), |
| 106 | + 'PREPEND_NAME': os.pathsep.join(('', '', 'control', '', '')), |
| 107 | + }): |
| 108 | + coroutine = extension.generate_command_environment( |
| 109 | + 'task_name', prefix_path, {'pkg_name': str(prefix_path)}) |
| 110 | + env = run_until_complete(coroutine) |
| 111 | + # Expect excess separators to be removed |
| 112 | + assert env.get('APPEND_NAME') == os.pathsep.join(( |
| 113 | + 'control', |
| 114 | + subdirectory_path, |
| 115 | + )) |
| 116 | + # Expect excess separators to be removed |
| 117 | + assert env.get('PREPEND_NAME') == os.pathsep.join(( |
| 118 | + subdirectory_path, |
| 119 | + 'control', |
| 120 | + )) |
| 121 | + |
| 122 | + # validate appending/prepending unique values |
| 123 | + with patch.dict(os.environ, { |
| 124 | + 'APPEND_NAME': os.pathsep.join((subdirectory_path, 'control')), |
| 125 | + 'PREPEND_NAME': os.pathsep.join(('control', subdirectory_path)), |
| 126 | + }): |
| 127 | + coroutine = extension.generate_command_environment( |
| 128 | + 'task_name', prefix_path, {'pkg_name': str(prefix_path)}) |
| 129 | + env = run_until_complete(coroutine) |
| 130 | + # Expect no change, value already appears earlier in the list |
| 131 | + assert env.get('APPEND_NAME') == os.pathsep.join(( |
| 132 | + subdirectory_path, |
| 133 | + 'control', |
| 134 | + )) |
| 135 | + # Expect value to be *moved* to the front of the list |
| 136 | + assert env.get('PREPEND_NAME') == os.pathsep.join(( |
| 137 | + subdirectory_path, |
| 138 | + 'control', |
| 139 | + )) |
0 commit comments