-
-
Notifications
You must be signed in to change notification settings - Fork 796
packagedcode: add CondaEnvironmentYmlHandler to parse environment.yml #4804
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| name: myenv | ||
| channels: | ||
| - conda-forge | ||
| - defaults | ||
| dependencies: | ||
| - python=3.10 | ||
| - numpy=1.24.0 | ||
| - pandas | ||
| - pip: | ||
| - requests==2.28.0 | ||
| - flask>=2.0.0 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| name: testenv | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You need to use real examples seen in the wild, not made up examples. |
||
| channels: | ||
| - conda-forge | ||
| - defaults | ||
| dependencies: | ||
| - python=3.10 | ||
| - numpy=1.24.0 | ||
| - pip: | ||
| - requests==2.28.0 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import os | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not how we run tests, see https://github.com/aboutcode-org/scancode-toolkit/blob/develop/tests/packagedcode/test_conda.py, where we have sample tests, and that is where the new tests will live. |
||
| from packages_test_utils import PackageTester | ||
| from packagedcode import conda | ||
| from scancode_config import REGEN_TEST_FIXTURES | ||
|
|
||
| class TestCondaEnvironmentYml(PackageTester): | ||
| test_data_dir = os.path.join(os.path.dirname(__file__), 'data') | ||
|
|
||
| def test_conda_environment_yml_is_datafile(self): | ||
| test_file = self.get_test_loc('conda/environment_yml/simple-environment.yml') | ||
| assert conda.CondaEnvironmentYmlHandler.is_datafile(test_file) | ||
|
|
||
| def test_parse_simple_environment_yml(self): | ||
| test_file = self.get_test_loc('conda/environment_yml/simple-environment.yml') | ||
| packages = list(conda.CondaEnvironmentYmlHandler.parse(test_file)) | ||
| assert len(packages) == 1 | ||
| package = packages[0] | ||
|
|
||
| assert package.name == 'testenv' | ||
| assert package.extra_data == {'channels': ['conda-forge', 'defaults']} | ||
|
|
||
| deps = package.dependencies | ||
|
|
||
| numpy_dep = next((d for d in deps if d.purl == 'pkg:conda/numpy'), None) | ||
| assert numpy_dep is not None | ||
| assert numpy_dep.extracted_requirement == '1.24.0' | ||
|
|
||
| requests_dep = next((d for d in deps if d.purl == 'pkg:pypi/requests'), None) | ||
| assert requests_dep is not None | ||
| assert requests_dep.extracted_requirement == '2.28.0' | ||
|
|
||
| def test_parse_real_environment_yml(self): | ||
| test_file = self.get_test_loc('conda/environment_yml/multiregex-environment.yml') | ||
| packages = list(conda.CondaEnvironmentYmlHandler.parse(test_file)) | ||
| assert len(packages) == 1 | ||
| package = packages[0] | ||
|
|
||
| assert package.name == 'myenv' | ||
| assert len(package.dependencies) > 0 | ||
| deps = [d.purl for d in package.dependencies] | ||
| assert 'pkg:conda/pandas' in deps | ||
|
|
||
| def test_parse_empty_dependencies(self): | ||
| test_file = self.get_temp_file('empty-deps.yml') | ||
| with open(test_file, 'w') as f: | ||
| f.write('name: nodeps\nchannels:\n - defaults\ndependencies:\n') | ||
|
|
||
| packages = list(conda.CondaEnvironmentYmlHandler.parse(test_file)) | ||
| assert len(packages) == 1 | ||
| package = packages[0] | ||
| assert package.name == 'nodeps' | ||
| assert package.dependencies == [] | ||
|
|
||
| def test_parse_missing_name(self): | ||
| test_file = self.get_temp_file('noname.yml') | ||
| with open(test_file, 'w') as f: | ||
| f.write('channels:\n - defaults\ndependencies:\n - python=3.10\n') | ||
|
|
||
| packages = list(conda.CondaEnvironmentYmlHandler.parse(test_file)) | ||
| assert len(packages) == 1 | ||
| package = packages[0] | ||
| assert package.name is None | ||
| assert len(package.dependencies) == 1 | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you actually looked at the code/run a scan on the test file to see this is not already detected somewhat?
See https://github.com/aboutcode-org/scancode-toolkit/blob/develop/src/packagedcode/conda.py#L357 where we already support this (but this needs enhancement of course)
Yes we should have updated/closed #3694 when this was added, but you should always do basic checks and read the code before writing code blindly.