Skip to content

Commit 413f7b5

Browse files
authored
tests: replace unsafe 'tempfile.mktemp' usage (#248)
Closes #247
1 parent cd95200 commit 413f7b5

File tree

1 file changed

+34
-29
lines changed

1 file changed

+34
-29
lines changed

tests/system/test_system.py

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -568,12 +568,12 @@ def test_large_encrypted_file_write_from_stream(self):
568568
md5_hash = md5_hash.encode("utf-8")
569569
self.assertEqual(md5_hash, file_data["hash"])
570570

571-
temp_filename = tempfile.mktemp()
572-
with open(temp_filename, "wb") as file_obj:
573-
blob.download_to_file(file_obj)
571+
with tempfile.NamedTemporaryFile() as temp_f:
572+
with open(temp_f.name, "wb") as file_obj:
573+
blob.download_to_file(file_obj)
574574

575-
with open(temp_filename, "rb") as file_obj:
576-
md5_temp_hash = _base64_md5hash(file_obj)
575+
with open(temp_f.name, "rb") as file_obj:
576+
md5_temp_hash = _base64_md5hash(file_obj)
577577

578578
self.assertEqual(md5_temp_hash, file_data["hash"])
579579

@@ -777,12 +777,14 @@ def test_direct_write_and_read_into_file(self):
777777

778778
same_blob = self.bucket.blob("MyBuffer")
779779
same_blob.reload() # Initialize properties.
780-
temp_filename = tempfile.mktemp()
781-
with open(temp_filename, "wb") as file_obj:
782-
same_blob.download_to_file(file_obj)
783780

784-
with open(temp_filename, "rb") as file_obj:
785-
stored_contents = file_obj.read()
781+
with tempfile.NamedTemporaryFile() as temp_f:
782+
783+
with open(temp_f.name, "wb") as file_obj:
784+
same_blob.download_to_file(file_obj)
785+
786+
with open(temp_f.name, "rb") as file_obj:
787+
stored_contents = file_obj.read()
786788

787789
self.assertEqual(file_contents, stored_contents)
788790

@@ -796,21 +798,23 @@ def test_download_w_generation_match(self):
796798

797799
same_blob = self.bucket.blob("MyBuffer")
798800
same_blob.reload() # Initialize properties.
799-
temp_filename = tempfile.mktemp()
800-
with open(temp_filename, "wb") as file_obj:
801-
with self.assertRaises(google.api_core.exceptions.PreconditionFailed):
801+
802+
with tempfile.NamedTemporaryFile() as temp_f:
803+
804+
with open(temp_f.name, "wb") as file_obj:
805+
with self.assertRaises(google.api_core.exceptions.PreconditionFailed):
806+
same_blob.download_to_file(
807+
file_obj, if_generation_match=WRONG_GENERATION_NUMBER
808+
)
809+
802810
same_blob.download_to_file(
803-
file_obj, if_generation_match=WRONG_GENERATION_NUMBER
811+
file_obj,
812+
if_generation_match=blob.generation,
813+
if_metageneration_match=blob.metageneration,
804814
)
805815

806-
same_blob.download_to_file(
807-
file_obj,
808-
if_generation_match=blob.generation,
809-
if_metageneration_match=blob.metageneration,
810-
)
811-
812-
with open(temp_filename, "rb") as file_obj:
813-
stored_contents = file_obj.read()
816+
with open(temp_f.name, "rb") as file_obj:
817+
stored_contents = file_obj.read()
814818

815819
self.assertEqual(file_contents, stored_contents)
816820

@@ -835,14 +839,15 @@ def test_download_blob_w_uri(self):
835839
blob.upload_from_string(file_contents)
836840
self.case_blobs_to_delete.append(blob)
837841

838-
temp_filename = tempfile.mktemp()
839-
with open(temp_filename, "wb") as file_obj:
840-
Config.CLIENT.download_blob_to_file(
841-
"gs://" + self.bucket.name + "/MyBuffer", file_obj
842-
)
842+
with tempfile.NamedTemporaryFile() as temp_f:
843+
844+
with open(temp_f.name, "wb") as file_obj:
845+
Config.CLIENT.download_blob_to_file(
846+
"gs://" + self.bucket.name + "/MyBuffer", file_obj
847+
)
843848

844-
with open(temp_filename, "rb") as file_obj:
845-
stored_contents = file_obj.read()
849+
with open(temp_f.name, "rb") as file_obj:
850+
stored_contents = file_obj.read()
846851

847852
self.assertEqual(file_contents, stored_contents)
848853

0 commit comments

Comments
 (0)