path: Let get_or_create_file() create missing dirs

This commit is contained in:
Stein Magnus Jodal 2013-04-09 12:40:28 +02:00
parent 53827aa022
commit 02f9db4518
2 changed files with 13 additions and 0 deletions

View File

@ -41,6 +41,7 @@ def get_or_create_dir(dir_path):
def get_or_create_file(file_path):
file_path = expand_path(file_path)
get_or_create_dir(os.path.dirname(file_path))
if not os.path.isfile(file_path):
logger.info('Creating file %s', file_path)
open(file_path, 'w').close()

View File

@ -70,6 +70,18 @@ class GetOrCreateFileTest(unittest.TestCase):
self.assert_(os.path.isfile(file_path))
self.assertEqual(created, file_path)
def test_creating_nested_file(self):
level2_dir = os.path.join(self.parent, 'test')
file_path = os.path.join(self.parent, 'test', 'test')
self.assert_(not os.path.exists(level2_dir))
self.assert_(not os.path.exists(file_path))
created = path.get_or_create_file(file_path)
self.assert_(os.path.exists(level2_dir))
self.assert_(os.path.isdir(level2_dir))
self.assert_(os.path.exists(file_path))
self.assert_(os.path.isfile(file_path))
self.assertEqual(created, file_path)
def test_creating_existing_file(self):
file_path = os.path.join(self.parent, 'test')
path.get_or_create_file(file_path)