|
1 | 1 | # Copyright 2016-2018 Dirk Thomas |
2 | 2 | # Licensed under the Apache License, Version 2.0 |
3 | 3 |
|
4 | | -import os |
5 | 4 | from pathlib import Path |
6 | | -from tempfile import TemporaryDirectory |
7 | 5 | from unittest.mock import Mock |
8 | 6 | from unittest.mock import patch |
9 | 7 |
|
@@ -51,66 +49,60 @@ class Extension4(ShellExtensionPoint): |
51 | 49 | pass |
52 | 50 |
|
53 | 51 |
|
54 | | -def test_create_environment_scripts(): |
55 | | - with TemporaryDirectory(prefix='test_colcon_') as basepath: |
56 | | - pkg = Mock() |
57 | | - pkg.name = 'name' |
58 | | - pkg.dependencies = {} |
59 | | - pkg.hooks = [] |
60 | | - args = Mock() |
61 | | - args.install_base = basepath |
| 52 | +def test_create_environment_scripts(tmp_path): |
| 53 | + pkg = Mock() |
| 54 | + pkg.name = 'name' |
| 55 | + pkg.dependencies = {} |
| 56 | + pkg.hooks = [] |
| 57 | + args = Mock() |
| 58 | + args.install_base = str(tmp_path) |
62 | 59 |
|
63 | | - # no hooks at all |
| 60 | + # no hooks at all |
| 61 | + with patch( |
| 62 | + 'colcon_core.environment.create_environment_hooks', return_value=[], |
| 63 | + ): |
64 | 64 | with patch( |
65 | | - 'colcon_core.environment.create_environment_hooks', return_value=[] |
| 65 | + 'colcon_core.environment.get_shell_extensions', return_value={}, |
66 | 66 | ): |
67 | | - with patch( |
68 | | - 'colcon_core.environment.get_shell_extensions', return_value={} |
69 | | - ): |
70 | | - create_environment_scripts(pkg, args) |
71 | | - |
72 | | - pkg.hooks = [os.path.join(basepath, 'subA')] |
73 | | - with ExtensionPointContext( |
74 | | - extension3=Extension3, extension4=Extension4 |
75 | | - ): |
76 | | - extensions = get_shell_extensions() |
77 | | - # one invalid return value, one check correct hooks argument |
78 | | - extensions[100]['extension3'].create_package_script = Mock() |
79 | | - extensions[100]['extension4'].create_package_script = Mock( |
80 | | - return_value=None) |
81 | | - with patch('colcon_core.environment.logger.error') as error: |
82 | | - create_environment_scripts( |
83 | | - pkg, args, default_hooks=[('subB', )], |
84 | | - additional_hooks=[['subC', 'arg1', 'arg2']]) |
85 | | - # the raised exception is catched and results in an error message |
86 | | - assert error.call_count == 1 |
87 | | - assert len(error.call_args[0]) == 1 |
88 | | - assert error.call_args[0][0].startswith( |
89 | | - "Exception in shell extension 'extension3': " |
90 | | - 'create_package_script() should return a list\n') |
91 | | - # check for correct hooks argument |
92 | | - mock = extensions[100]['extension4'].create_package_script |
93 | | - assert mock.call_count == 1 |
94 | | - assert len(mock.call_args[0]) == 3 |
95 | | - assert mock.call_args[0][0] == Path(args.install_base) |
96 | | - assert mock.call_args[0][1] == pkg.name |
97 | | - hook_tuples = mock.call_args[0][2] |
98 | | - assert len(hook_tuples) == 3 |
99 | | - assert hook_tuples[0] == ('subB', ()) |
100 | | - assert hook_tuples[1] == ('subC', ['arg1', 'arg2']) |
101 | | - assert hook_tuples[2] == ('subA', []) |
102 | | - |
103 | | - |
104 | | -def test_create_environment_hooks(): |
105 | | - with TemporaryDirectory(prefix='test_colcon_') as basepath: |
106 | | - with ExtensionPointContext( |
107 | | - extension1=Extension1, extension2=Extension2 |
108 | | - ): |
109 | | - with patch('colcon_core.environment.logger.error') as error: |
110 | | - hooks = create_environment_hooks(basepath, 'pkg_name') |
| 67 | + create_environment_scripts(pkg, args) |
| 68 | + |
| 69 | + pkg.hooks = [str(tmp_path / 'subA')] |
| 70 | + with ExtensionPointContext(extension3=Extension3, extension4=Extension4): |
| 71 | + extensions = get_shell_extensions() |
| 72 | + # one invalid return value, one check correct hooks argument |
| 73 | + extensions[100]['extension3'].create_package_script = Mock() |
| 74 | + extensions[100]['extension4'].create_package_script = Mock( |
| 75 | + return_value=None) |
| 76 | + with patch('colcon_core.environment.logger.error') as error: |
| 77 | + create_environment_scripts( |
| 78 | + pkg, args, default_hooks=[('subB', )], |
| 79 | + additional_hooks=[['subC', 'arg1', 'arg2']]) |
| 80 | + # the raised exception is catched and results in an error message |
| 81 | + assert error.call_count == 1 |
| 82 | + assert len(error.call_args[0]) == 1 |
| 83 | + assert error.call_args[0][0].startswith( |
| 84 | + "Exception in shell extension 'extension3': " |
| 85 | + 'create_package_script() should return a list\n') |
| 86 | + # check for correct hooks argument |
| 87 | + mock = extensions[100]['extension4'].create_package_script |
| 88 | + assert mock.call_count == 1 |
| 89 | + assert len(mock.call_args[0]) == 3 |
| 90 | + assert mock.call_args[0][0] == Path(args.install_base) |
| 91 | + assert mock.call_args[0][1] == pkg.name |
| 92 | + hook_tuples = mock.call_args[0][2] |
| 93 | + assert len(hook_tuples) == 3 |
| 94 | + assert hook_tuples[0] == ('subB', ()) |
| 95 | + assert hook_tuples[1] == ('subC', ['arg1', 'arg2']) |
| 96 | + assert hook_tuples[2] == ('subA', []) |
| 97 | + |
| 98 | + |
| 99 | +def test_create_environment_hooks(tmp_path): |
| 100 | + with ExtensionPointContext(extension1=Extension1, extension2=Extension2): |
| 101 | + with patch('colcon_core.environment.logger.error') as error: |
| 102 | + hooks = create_environment_hooks(str(tmp_path), 'pkg_name') |
111 | 103 | assert len(hooks) == 2 |
112 | | - assert hooks[0] == f'{basepath}/share/pkg_name/hook/one.ext' |
113 | | - assert hooks[1] == f'{basepath}/share/pkg_name/hook/two.ext' |
| 104 | + assert hooks[0] == f'{tmp_path}/share/pkg_name/hook/one.ext' |
| 105 | + assert hooks[1] == f'{tmp_path}/share/pkg_name/hook/two.ext' |
114 | 106 | # the raised exception is catched and results in an error message |
115 | 107 | assert error.call_count == 1 |
116 | 108 | assert len(error.call_args[0]) == 1 |
|
0 commit comments