From 0ffd40bf3967d544e1cdfec0aaa56be53b223ead Mon Sep 17 00:00:00 2001 From: Bo Zhou <2466956298@qq.com> Date: Tue, 3 Sep 2019 11:34:11 +0800 Subject: [PATCH] clear the directory for the function (#145) * clear the directory for the function * add the unit test * unit test bug * yapf --- parl/utils/logger.py | 4 ++-- parl/utils/tests/logger_test.py | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/parl/utils/logger.py b/parl/utils/logger.py index 402c8cf..46815a4 100644 --- a/parl/utils/logger.py +++ b/parl/utils/logger.py @@ -143,8 +143,8 @@ def set_dir(dirname): _FILE_HANDLER.close() del _FILE_HANDLER - if not os.path.isdir(dirname): - _makedirs(dirname) + shutil.rmtree(dirname, ignore_errors=True) + _makedirs(dirname) LOG_DIR = dirname _set_file(os.path.join(dirname, 'log.log')) diff --git a/parl/utils/tests/logger_test.py b/parl/utils/tests/logger_test.py index 8c57521..3a847f4 100644 --- a/parl/utils/tests/logger_test.py +++ b/parl/utils/tests/logger_test.py @@ -15,6 +15,7 @@ import unittest from parl.utils import logger import threading as th +import os.path class TestLogger(unittest.TestCase): @@ -45,6 +46,15 @@ class TestLogger(unittest.TestCase): logger.auto_set_dir(action='n') logger.auto_set_dir(action='k') + def test_set_dir(self): + logger.set_dir('./logger_dir') + temp_file = './logger_dir/tmp.file' + with open(temp_file, 'w') as t_file: + t_file.write("Are you OK? From Mr.Lei") + self.assertTrue(os.path.isfile(temp_file)) + logger.set_dir('./logger_dir') + self.assertFalse(os.path.isfile(temp_file)) + if __name__ == '__main__': unittest.main() -- GitLab