1515# limitations under the License.
1616#
1717
18- from distutils import core
1918import copy
2019import os
2120import functools
@@ -657,7 +656,27 @@ def teardown_method(self):
657656
658657 def test_packager_creates_and_copies_python_package (self ):
659658 tsp = source_utils ._TrainingScriptPythonPackager (_TEST_LOCAL_SCRIPT_FILE_PATH )
660- tsp .package_and_copy (copy_method = local_copy_method )
659+
660+ def create_valid_tarball (* args , ** kwargs ):
661+ cwd = kwargs .get ("cwd" )
662+ if cwd :
663+ dist_dir = pathlib .Path (cwd ) / "dist"
664+ dist_dir .mkdir (exist_ok = True )
665+ filename = f"{ source_utils ._TrainingScriptPythonPackager ._ROOT_MODULE } -{ source_utils ._TrainingScriptPythonPackager ._SETUP_PY_VERSION } .tar.gz"
666+ tarball_path = dist_dir / filename
667+ setup_py_path = pathlib .Path (cwd ) / "setup.py"
668+ arcname = f"{ source_utils ._TrainingScriptPythonPackager ._ROOT_MODULE } -{ source_utils ._TrainingScriptPythonPackager ._SETUP_PY_VERSION } /setup.py"
669+ import tarfile
670+
671+ with tarfile .open (tarball_path , "w:gz" ) as tar :
672+ tar .add (setup_py_path , arcname = arcname )
673+ mock_subprocess = mock .Mock ()
674+ mock_subprocess .communicate .return_value = (b"" , b"" )
675+ mock_subprocess .returncode = 0
676+ return mock_subprocess
677+
678+ with mock .patch ("subprocess.Popen" , side_effect = create_valid_tarball ):
679+ tsp .package_and_copy (copy_method = local_copy_method )
661680 assert pathlib .Path (
662681 f"{ tsp ._ROOT_MODULE } -{ tsp ._SETUP_PY_VERSION } .tar.gz"
663682 ).is_file ()
@@ -666,15 +685,43 @@ def test_requirements_are_in_package(self):
666685 tsp = source_utils ._TrainingScriptPythonPackager (
667686 _TEST_LOCAL_SCRIPT_FILE_PATH , requirements = _TEST_REQUIREMENTS
668687 )
669- source_dist_path = tsp .package_and_copy (copy_method = local_copy_method )
688+
689+ def create_valid_tarball (* args , ** kwargs ):
690+ cwd = kwargs .get ("cwd" )
691+ if cwd :
692+ dist_dir = pathlib .Path (cwd ) / "dist"
693+ dist_dir .mkdir (exist_ok = True )
694+ filename = f"{ source_utils ._TrainingScriptPythonPackager ._ROOT_MODULE } -{ source_utils ._TrainingScriptPythonPackager ._SETUP_PY_VERSION } .tar.gz"
695+ tarball_path = dist_dir / filename
696+ setup_py_path = pathlib .Path (cwd ) / "setup.py"
697+ arcname = f"{ source_utils ._TrainingScriptPythonPackager ._ROOT_MODULE } -{ source_utils ._TrainingScriptPythonPackager ._SETUP_PY_VERSION } /setup.py"
698+ import tarfile
699+
700+ with tarfile .open (tarball_path , "w:gz" ) as tar :
701+ tar .add (setup_py_path , arcname = arcname )
702+ mock_subprocess = mock .Mock ()
703+ mock_subprocess .communicate .return_value = (b"" , b"" )
704+ mock_subprocess .returncode = 0
705+ return mock_subprocess
706+
707+ with mock .patch ("subprocess.Popen" , side_effect = create_valid_tarball ):
708+ source_dist_path = tsp .package_and_copy (copy_method = local_copy_method )
670709 with tarfile .open (source_dist_path ) as tf :
671710 with tempfile .TemporaryDirectory () as tmpdirname :
672711 setup_py_path = f"{ source_utils ._TrainingScriptPythonPackager ._ROOT_MODULE } -{ source_utils ._TrainingScriptPythonPackager ._SETUP_PY_VERSION } /setup.py"
673712 tf .extract (setup_py_path , path = tmpdirname )
674- setup_py = core .run_setup (
675- pathlib .Path (tmpdirname , setup_py_path ), stop_after = "init"
713+ with open (pathlib .Path (tmpdirname , setup_py_path ), "r" ) as f :
714+ setup_py_content = f .read ()
715+
716+ import re
717+
718+ match = re .search (r"install_requires=\((.*?)\)" , setup_py_content )
719+ assert match is not None
720+ requirements_str = match .group (1 )
721+ expected_requirements_str = "," .join (
722+ f'"{ r } "' for r in _TEST_REQUIREMENTS
676723 )
677- assert _TEST_REQUIREMENTS == setup_py . install_requires
724+ assert requirements_str == expected_requirements_str
678725
679726 def test_packaging_fails_whith_RuntimeError (self ):
680727 with patch ("subprocess.Popen" ) as mock_popen :
@@ -693,9 +740,28 @@ def test_package_and_copy_to_gcs_copies_to_gcs(self, mock_client_bucket):
693740
694741 tsp = source_utils ._TrainingScriptPythonPackager (_TEST_LOCAL_SCRIPT_FILE_PATH )
695742
696- gcs_path = tsp .package_and_copy_to_gcs (
697- gcs_staging_dir = _TEST_BUCKET_NAME , project = _TEST_PROJECT
698- )
743+ def create_valid_tarball (* args , ** kwargs ):
744+ cwd = kwargs .get ("cwd" )
745+ if cwd :
746+ dist_dir = pathlib .Path (cwd ) / "dist"
747+ dist_dir .mkdir (exist_ok = True )
748+ filename = f"{ source_utils ._TrainingScriptPythonPackager ._ROOT_MODULE } -{ source_utils ._TrainingScriptPythonPackager ._SETUP_PY_VERSION } .tar.gz"
749+ tarball_path = dist_dir / filename
750+ setup_py_path = pathlib .Path (cwd ) / "setup.py"
751+ arcname = f"{ source_utils ._TrainingScriptPythonPackager ._ROOT_MODULE } -{ source_utils ._TrainingScriptPythonPackager ._SETUP_PY_VERSION } /setup.py"
752+ import tarfile
753+
754+ with tarfile .open (tarball_path , "w:gz" ) as tar :
755+ tar .add (setup_py_path , arcname = arcname )
756+ mock_subprocess = mock .Mock ()
757+ mock_subprocess .communicate .return_value = (b"" , b"" )
758+ mock_subprocess .returncode = 0
759+ return mock_subprocess
760+
761+ with mock .patch ("subprocess.Popen" , side_effect = create_valid_tarball ):
762+ gcs_path = tsp .package_and_copy_to_gcs (
763+ gcs_staging_dir = _TEST_BUCKET_NAME , project = _TEST_PROJECT
764+ )
699765
700766 mock_client_bucket .assert_called_once_with (_TEST_BUCKET_NAME )
701767 mock_client_bucket .return_value .blob .assert_called_once ()
0 commit comments