Skip to content
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

fix: Skip empty lines in environment spec files #3662

Merged
merged 5 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dev/environment-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ dependencies:
- pytest-asyncio
- pytest-timeout
- pytest-xprocess
- memory_profiler
- requests
- sel(win): pywin32
- sel(win): menuinst
Expand Down
10 changes: 9 additions & 1 deletion libmamba/src/api/install.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,15 @@ namespace mamba
std::vector<std::string> f_specs;
for (auto& line : file_contents)
{
if (line[0] != '#' && line[0] != '@')
auto lstrip_line = util::lstrip(line);

// Skip comment lines and empty lines
// Comment lines start with '#' or '@' preceded by whitespaces or tabs
auto is_comment = util::starts_with(lstrip_line, "#")
jjerphan marked this conversation as resolved.
Show resolved Hide resolved
|| util::starts_with(lstrip_line, "@");
auto is_empty = lstrip_line.empty();
jjerphan marked this conversation as resolved.
Show resolved Hide resolved

if (!is_comment && !is_empty)
{
f_specs.push_back(line);
jjerphan marked this conversation as resolved.
Show resolved Hide resolved
}
Expand Down
54 changes: 54 additions & 0 deletions micromamba/tests/test_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

from . import helpers

from memory_profiler import memory_usage

__this_dir__ = Path(__file__).parent.resolve()

env_file_requires_pip_install_path = __this_dir__ / "env-requires-pip-install.yaml"
Expand Down Expand Up @@ -1441,3 +1443,55 @@ def test_create_empty_or_absent_dependencies(tmp_path):
"-p", env_prefix, "-f", tmp_path / "env_spec_absent_dependencies.yaml", "--json"
)
assert res["success"]


env_spec_empty_lines_and_comments = """
# The line below are empty (various number of spaces)
"""

env_spec_empty_lines_and_comments += "\n"
env_spec_empty_lines_and_comments += " \n"
env_spec_empty_lines_and_comments += " \n"
env_spec_empty_lines_and_comments += " \n"
env_spec_empty_lines_and_comments += "# One comment \n"
env_spec_empty_lines_and_comments += " @ yet another one with a prefixed by a tab\n"
env_spec_empty_lines_and_comments += "wheel\n"

env_repro_1 = """
wheel

setuptools
"""

env_repro_2 = """
wheel
setuptools

# comment
"""


@pytest.mark.parametrize("env_spec", [env_spec_empty_lines_and_comments, env_repro_1, env_repro_2])
def test_create_with_empty_lines_and_comments(tmp_path, env_spec):
# Non-regression test for:
# - https://github.com/mamba-org/mamba/issues/3289
# - https://github.com/mamba-org/mamba/issues/3659
memory_limit = 100 # in MB

def memory_intensive_operation():
env_prefix = tmp_path / "env-one_empty_line"

env_spec_file = tmp_path / "env_spec.txt"

with open(env_spec_file, "w") as f:
f.write(env_spec)

res = helpers.create("-p", env_prefix, "-f", env_spec_file, "--json")
assert res["success"]

max_memory = max(memory_usage(proc=memory_intensive_operation))

if max_memory > memory_limit:
pytest.fail(
f"test_create_with_empty_lines_and_comments exceeded memory limit of {memory_limit} MB (used {max_memory:.2f} MB)"
)
Loading