use proper temp directories in test_tensorboard.py (#89826)

The old `temp_dir` is created under `PWD`. But `PWD` may not be writable and in general is not a good place to create temporary directories. Use the standard `tempfile` instead.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/89826
Approved by: https://github.com/soumith
This commit is contained in:
Yuxin Wu
2022-12-09 19:33:03 +00:00
committed by PyTorch MergeBot
parent 09ccda0d94
commit 4e1881b8b7

View File

@ -5,8 +5,8 @@ import numpy as np
import os
import shutil
import sys
import tempfile
import unittest
import uuid
import expecttest
TEST_TENSORBOARD = True
@ -60,7 +60,9 @@ class BaseTestCase(TestCase):
self.temp_dirs = []
def createSummaryWriter(self):
temp_dir = str(uuid.uuid4())
# Just to get the name of the directory in a writable place. tearDown()
# is responsible for clean-ups.
temp_dir = tempfile.TemporaryDirectory(prefix="test_tensorboard").name
self.temp_dirs.append(temp_dir)
return SummaryWriter(temp_dir)
@ -287,11 +289,10 @@ class TestTensorBoardSummaryWriter(BaseTestCase):
def test_pathlib(self):
import pathlib
p = pathlib.Path('./pathlibtest' + str(uuid.uuid4()))
with SummaryWriter(p) as writer:
writer.add_scalar('test', 1)
import shutil
shutil.rmtree(str(p))
with tempfile.TemporaryDirectory(prefix="test_tensorboard_pathlib") as d:
p = pathlib.Path(d)
with SummaryWriter(p) as writer:
writer.add_scalar('test', 1)
class TestTensorBoardEmbedding(BaseTestCase):
def test_embedding(self):
@ -516,7 +517,7 @@ def get_expected_file(function_ptr):
def read_expected_content(function_ptr):
expected_file = get_expected_file(function_ptr)
assert os.path.exists(expected_file)
assert os.path.exists(expected_file), expected_file
with open(expected_file, "r") as f:
return f.read()