-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fix: guard against IndexError in decompress_list on empty string #3018
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 1 commit
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,81 @@ | ||
| """ | ||
| Unit tests for compress_list / decompress_list in metaflow/util.py | ||
|
|
||
| Bug: decompress_list("") raised IndexError on lststr[0] because there | ||
| was no guard for an empty input string. compress_list([]) legitimately | ||
| produces "" (joining an empty list), so the round-trip | ||
| decompress_list(compress_list([])) | ||
| crashed instead of returning []. | ||
|
|
||
| Fix: add `if not lststr: return []` before the index access. | ||
| """ | ||
|
|
||
| import sys | ||
| from unittest.mock import MagicMock | ||
|
|
||
| # fcntl is Linux/macOS-only. Stub it out so the test runs on Windows too. | ||
| if "fcntl" not in sys.modules: | ||
| sys.modules["fcntl"] = MagicMock() | ||
| if "metaflow.sidecar.sidecar_subprocess" not in sys.modules: | ||
| sys.modules["metaflow.sidecar.sidecar_subprocess"] = MagicMock() | ||
|
|
||
| from metaflow.util import compress_list, decompress_list # noqa: E402 | ||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Core regression tests | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| def test_compress_empty_list_returns_empty_string(): | ||
| """compress_list([]) must produce '' (join of zero elements).""" | ||
| assert compress_list([]) == "" | ||
|
Comment on lines
+29
to
+31
Contributor
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. Missing edge-case test:
This is a pre-existing design-level ambiguity (both an empty list and a list containing one empty string compress to the same token), but the fix silently resolves it in favor of def test_roundtrip_single_empty_string():
# compress_list([""]) == "" == compress_list([]),
# so decompress cannot distinguish the two; [] is the chosen result.
assert decompress_list(compress_list([""])) == []If |
||
|
|
||
|
|
||
| def test_decompress_empty_string_returns_empty_list(): | ||
| """ | ||
| decompress_list("") must return [] without raising. | ||
|
|
||
| This is the direct crash case: before the fix, lststr[0] on an | ||
| empty string raised IndexError: string index out of range. | ||
| Fails without the fix, passes with it. | ||
| """ | ||
| result = decompress_list("") | ||
| assert result == [] | ||
|
|
||
|
|
||
| def test_roundtrip_empty_list(): | ||
| """ | ||
| decompress_list(compress_list([])) must return []. | ||
|
|
||
| This is the end-to-end round-trip that exposed the bug: the output | ||
| of compress_list is fed straight into decompress_list, so an empty | ||
| list must survive the round-trip unharmed. | ||
| Fails without the fix, passes with it. | ||
| """ | ||
| assert decompress_list(compress_list([])) == [] | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Regression guard: existing non-empty round-trips must still work | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| def test_roundtrip_single_item(): | ||
| """A single-element list survives the round-trip.""" | ||
| items = ["abc"] | ||
| assert decompress_list(compress_list(items)) == items | ||
|
|
||
|
|
||
| def test_roundtrip_multiple_items_with_common_prefix(): | ||
| """ | ||
| Lists with a long common prefix use the prefix-encoding path; | ||
| verify the round-trip is correct after the guard was added. | ||
| """ | ||
| items = ["MyFlow/1234/start/1", "MyFlow/1234/start/2", "MyFlow/1234/start/3"] | ||
| assert decompress_list(compress_list(items)) == items | ||
|
|
||
|
|
||
| def test_roundtrip_items_without_common_prefix(): | ||
| """Lists with no common prefix use the plain comma-separated path.""" | ||
| items = ["alpha", "beta", "gamma"] | ||
| assert decompress_list(compress_list(items)) == items | ||
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.
Module-level
sys.modulesinjection may leak across testsThe
sys.modulesmutation at module import time is permanent for the entire test session. On Linux/macOS, iffcntlhas not yet been imported by a prior test when this file is collected, the realfcntlmodule will be replaced with aMagicMock()for all subsequent tests in the same pytest process — including tests that legitimately need the realfcntl.A safer pattern is to scope the stub to only the tests in this file using an autouse session/module fixture:
This restores the original state after the module's tests finish, preventing leakage.