diff --git a/mopidy/utils/path.py b/mopidy/utils/path.py index 8f842741..2ad51368 100644 --- a/mopidy/utils/path.py +++ b/mopidy/utils/path.py @@ -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() diff --git a/tests/utils/path_test.py b/tests/utils/path_test.py index d40c822f..9d1c16d3 100644 --- a/tests/utils/path_test.py +++ b/tests/utils/path_test.py @@ -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)