1515
1616import os
1717import tempfile
18- import tarfile
1918
20- import pytest
21- from unittest .mock import Mock , patch , MagicMock
19+ from unittest .mock import Mock , patch
2220
2321from sagemaker .core .common_utils import (
2422 _get_resolved_path ,
@@ -204,18 +202,20 @@ def test_get_safe_members_filters_bad_hardlink_member():
204202def test_custom_extractall_tarfile_with_data_filter_uses_filter_param ():
205203 """Test custom_extractall_tarfile uses data_filter when available.
206204
207- We set mock_tarfile.data_filter explicitly to ensure hasattr returns True.
208- The MagicMock would auto-create the attribute anyway, but we set it
209- explicitly for clarity. The key assertion is that filter="data" is passed.
205+ We patch the module-level `tarfile` import in common_utils (not the `tar` parameter).
206+ Setting mock_tarfile.data_filter explicitly ensures hasattr(tarfile, 'data_filter')
207+ returns True inside custom_extractall_tarfile. The key assertion is that
208+ filter="data" is passed to tar.extractall.
210209 """
211210 mock_tar = Mock ()
212211 mock_tar .extractall = Mock ()
213212
214213 with tempfile .TemporaryDirectory () as tmpdir :
215214 extract_path = os .path .join (tmpdir , "extract" )
216215
216+ # Patch the module-level tarfile import in common_utils
217217 with patch ("sagemaker.core.common_utils.tarfile" ) as mock_tarfile :
218- # Explicitly set data_filter to ensure the hasattr check passes
218+ # Explicitly set data_filter so hasattr check passes
219219 mock_tarfile .data_filter = True
220220
221221 custom_extractall_tarfile (mock_tar , extract_path )
@@ -226,6 +226,9 @@ def test_custom_extractall_tarfile_with_data_filter_uses_filter_param():
226226def test_custom_extractall_tarfile_without_data_filter_uses_safe_members ():
227227 """Test custom_extractall_tarfile uses safe members when data_filter is unavailable.
228228
229+ Uses spec=['TarFile'] to restrict the mock so that
230+ hasattr(mock_tarfile, 'data_filter') returns False, forcing the fallback path.
231+
229232 Verifies that:
230233 1. tar.getmembers() is called (not iterating over tar directly)
231234 2. _get_safe_members is called with the members list and resolved extract_path as base
0 commit comments