Skip to content

Commit

Permalink
Extract test dependencies from [options.extras_require]test (#61)
Browse files Browse the repository at this point in the history
It's becoming an increasingly common pattern to declare test
dependencies using [options.extras_require]test. This change merges the
test dependencies declared using that mechanism with any discovered
using [options]test_requires.
  • Loading branch information
cottsay authored Oct 8, 2024
1 parent 3c4d291 commit 8856423
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
28 changes: 22 additions & 6 deletions colcon_python_setup_py/package_augmentation/python_setup_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,22 @@ def augment_package( # noqa: D102

config = get_setup_information(setup_py)

for dependency_type, option_name in [
mapping = {
('build', 'setup_requires'),
('run', 'install_requires'),
('test', 'tests_require')
]:
desc.dependencies[dependency_type] = {
create_dependency_descriptor(d)
for d in config.get(option_name) or ()}
('test', 'tests_require'),
}
_map_dependencies(config, mapping, desc.dependencies)

extras_mapping = {
('test', 'test'),
('test', 'tests'),
('test', 'testing'),
}
_map_dependencies(
config.get('extras_require') or {},
extras_mapping,
desc.dependencies)

def getter(env):
nonlocal setup_py
Expand All @@ -53,3 +61,11 @@ def getter(env):
desc.metadata['get_python_setup_options'] = getter

desc.metadata['version'] = config['metadata'].get('version')


def _map_dependencies(options, mapping, dependencies):
for dependency_type, option_name in mapping:
dependencies.setdefault(dependency_type, set())
dependencies[dependency_type].update(
create_dependency_descriptor(d)
for d in options.get(option_name) or ())
7 changes: 7 additions & 0 deletions test/test_package_identification_python_setup_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ def test_identify():
" 'runB',\n"
' ],\n'
' zip_safe=False,\n'
' extras_require={\n'
" 'test': ['test2 == 3.0.0'],\n"
" 'tests': ['test3'],\n"
" 'testing': ['test4'],\n"
" 'other': ['not-test'],\n"
' },\n'
')\n')
_setup_information_cache.clear()
assert extension.identify(desc) is None
Expand All @@ -87,3 +93,4 @@ def test_identify():
assert desc.dependencies['run'] == {'runA', 'runB'}
dep = next(x for x in desc.dependencies['run'] if x == 'runA')
assert dep.metadata['version_gt'] == '1.2.3'
assert desc.dependencies['test'] == {'test2', 'test3', 'test4'}

0 comments on commit 8856423

Please sign in to comment.