From b50c9ce4578155a027b1be5bae494a566263ef39 Mon Sep 17 00:00:00 2001 From: Tres Seaver Date: Mon, 17 Aug 2020 16:33:44 -0400 Subject: [PATCH] tests: replace unsafe 'tempfile.mktemp' usage Closes #247 --- tests/system/test_system.py | 63 ++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 29 deletions(-) diff --git a/tests/system/test_system.py b/tests/system/test_system.py index 3fb701d39..8a6f36053 100644 --- a/tests/system/test_system.py +++ b/tests/system/test_system.py @@ -568,12 +568,12 @@ def test_large_encrypted_file_write_from_stream(self): md5_hash = md5_hash.encode("utf-8") self.assertEqual(md5_hash, file_data["hash"]) - temp_filename = tempfile.mktemp() - with open(temp_filename, "wb") as file_obj: - blob.download_to_file(file_obj) + with tempfile.NamedTemporaryFile() as temp_f: + with open(temp_f.name, "wb") as file_obj: + blob.download_to_file(file_obj) - with open(temp_filename, "rb") as file_obj: - md5_temp_hash = _base64_md5hash(file_obj) + with open(temp_f.name, "rb") as file_obj: + md5_temp_hash = _base64_md5hash(file_obj) self.assertEqual(md5_temp_hash, file_data["hash"]) @@ -777,12 +777,14 @@ def test_direct_write_and_read_into_file(self): same_blob = self.bucket.blob("MyBuffer") same_blob.reload() # Initialize properties. - temp_filename = tempfile.mktemp() - with open(temp_filename, "wb") as file_obj: - same_blob.download_to_file(file_obj) - with open(temp_filename, "rb") as file_obj: - stored_contents = file_obj.read() + with tempfile.NamedTemporaryFile() as temp_f: + + with open(temp_f.name, "wb") as file_obj: + same_blob.download_to_file(file_obj) + + with open(temp_f.name, "rb") as file_obj: + stored_contents = file_obj.read() self.assertEqual(file_contents, stored_contents) @@ -796,21 +798,23 @@ def test_download_w_generation_match(self): same_blob = self.bucket.blob("MyBuffer") same_blob.reload() # Initialize properties. - temp_filename = tempfile.mktemp() - with open(temp_filename, "wb") as file_obj: - with self.assertRaises(google.api_core.exceptions.PreconditionFailed): + + with tempfile.NamedTemporaryFile() as temp_f: + + with open(temp_f.name, "wb") as file_obj: + with self.assertRaises(google.api_core.exceptions.PreconditionFailed): + same_blob.download_to_file( + file_obj, if_generation_match=WRONG_GENERATION_NUMBER + ) + same_blob.download_to_file( - file_obj, if_generation_match=WRONG_GENERATION_NUMBER + file_obj, + if_generation_match=blob.generation, + if_metageneration_match=blob.metageneration, ) - same_blob.download_to_file( - file_obj, - if_generation_match=blob.generation, - if_metageneration_match=blob.metageneration, - ) - - with open(temp_filename, "rb") as file_obj: - stored_contents = file_obj.read() + with open(temp_f.name, "rb") as file_obj: + stored_contents = file_obj.read() self.assertEqual(file_contents, stored_contents) @@ -835,14 +839,15 @@ def test_download_blob_w_uri(self): blob.upload_from_string(file_contents) self.case_blobs_to_delete.append(blob) - temp_filename = tempfile.mktemp() - with open(temp_filename, "wb") as file_obj: - Config.CLIENT.download_blob_to_file( - "gs://" + self.bucket.name + "/MyBuffer", file_obj - ) + with tempfile.NamedTemporaryFile() as temp_f: + + with open(temp_f.name, "wb") as file_obj: + Config.CLIENT.download_blob_to_file( + "gs://" + self.bucket.name + "/MyBuffer", file_obj + ) - with open(temp_filename, "rb") as file_obj: - stored_contents = file_obj.read() + with open(temp_f.name, "rb") as file_obj: + stored_contents = file_obj.read() self.assertEqual(file_contents, stored_contents)