Skip to content

Commit

Permalink
tests: replace unsafe 'tempfile.mktemp' usage (#248)
Browse files Browse the repository at this point in the history
Closes #247
  • Loading branch information
tseaver committed Aug 24, 2020
1 parent cd95200 commit 413f7b5
Showing 1 changed file with 34 additions and 29 deletions.
63 changes: 34 additions & 29 deletions tests/system/test_system.py
Expand Up @@ -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"])

Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand Down

0 comments on commit 413f7b5

Please sign in to comment.