Fix bug with fallback values in full_path variables in config.ini (#234)

* Add test for gh-234

* Force flush of cached configs in test
This commit is contained in:
Ove Stavnås 2017-08-06 23:26:28 +02:00 committed by Jaisen Mathai
parent 00ba7e2604
commit e5e811b215
2 changed files with 37 additions and 4 deletions

View File

@ -196,20 +196,18 @@ class FileSystem(object):
self.cached_folder_path_definition = [] self.cached_folder_path_definition = []
for part in path_parts: for part in path_parts:
part = part.replace('%', '')
if part in config_directory: if part in config_directory:
part = part[1:]
self.cached_folder_path_definition.append( self.cached_folder_path_definition.append(
[(part, config_directory[part])] [(part, config_directory[part])]
) )
elif part in self.default_parts: elif part in self.default_parts:
part = part[1:]
self.cached_folder_path_definition.append( self.cached_folder_path_definition.append(
[(part, '')] [(part, '')]
) )
else: else:
this_part = [] this_part = []
for p in part.split('|'): for p in part.split('|'):
p = p[1:]
this_part.append( this_part.append(
(p, config_directory[p] if p in config_directory else '') (p, config_directory[p] if p in config_directory else '')
) )

View File

@ -588,6 +588,33 @@ def test_process_video_with_album_then_title():
assert origin_checksum != destination_checksum, destination_checksum assert origin_checksum != destination_checksum, destination_checksum
assert helper.path_tz_fix(os.path.join('2015-01-Jan','test_album','2015-01-19_12-45-11-movie-test_title.mov')) in destination, destination assert helper.path_tz_fix(os.path.join('2015-01-Jan','test_album','2015-01-19_12-45-11-movie-test_title.mov')) in destination, destination
@mock.patch('elodie.config.config_file', '%s/config.ini-fallback-folder' % gettempdir())
def test_process_file_fallback_folder():
with open('%s/config.ini-fallback-folder' % gettempdir(), 'w') as f:
f.write("""
[Directory]
date=%Y-%m
full_path=%date/%album|"fallback"
""")
if hasattr(load_config, 'config'):
del load_config.config
filesystem = FileSystem()
temporary_folder, folder = helper.create_working_folder()
origin = os.path.join(folder,'plain.jpg')
shutil.copyfile(helper.get_file('plain.jpg'), origin)
media = Photo(origin)
destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
if hasattr(load_config, 'config'):
del load_config.config
assert helper.path_tz_fix(os.path.join('2015-12', 'fallback', '2015-12-05_00-59-26-plain.jpg')) in destination, destination
shutil.rmtree(folder)
shutil.rmtree(os.path.dirname(os.path.dirname(destination)))
@mock.patch('elodie.config.config_file', '%s/config.ini-multiple-directories' % gettempdir()) @mock.patch('elodie.config.config_file', '%s/config.ini-multiple-directories' % gettempdir())
def test_process_twice_more_than_two_levels_of_directories(): def test_process_twice_more_than_two_levels_of_directories():
with open('%s/config.ini-multiple-directories' % gettempdir(), 'w') as f: with open('%s/config.ini-multiple-directories' % gettempdir(), 'w') as f:
@ -599,6 +626,8 @@ day=%d
full_path=%year/%month/%day full_path=%year/%month/%day
""") """)
if hasattr(load_config, 'config'):
del load_config.config
filesystem = FileSystem() filesystem = FileSystem()
temporary_folder, folder = helper.create_working_folder() temporary_folder, folder = helper.create_working_folder()
@ -607,12 +636,18 @@ full_path=%year/%month/%day
media = Photo(origin) media = Photo(origin)
destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True) destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
if hasattr(load_config, 'config'):
del load_config.config
assert helper.path_tz_fix(os.path.join('2015','12','05', '2015-12-05_00-59-26-plain.jpg')) in destination, destination assert helper.path_tz_fix(os.path.join('2015','12','05', '2015-12-05_00-59-26-plain.jpg')) in destination, destination
if hasattr(load_config, 'config'):
del load_config.config
media_second = Photo(destination) media_second = Photo(destination)
media_second.set_title('foo') media_second.set_title('foo')
destination_second = filesystem.process_file(destination, temporary_folder, media_second, allowDuplicate=True) destination_second = filesystem.process_file(destination, temporary_folder, media_second, allowDuplicate=True)
if hasattr(load_config, 'config'):
del load_config.config
assert destination.replace('.jpg', '-foo.jpg') == destination_second, destination_second assert destination.replace('.jpg', '-foo.jpg') == destination_second, destination_second
@ -707,7 +742,7 @@ def test_get_folder_path_definition_default():
if hasattr(load_config, 'config'): if hasattr(load_config, 'config'):
del load_config.config del load_config.config
assert path_definition == [[('date', '%Y-%m-%b')], [('album', ''), ('location', '%city'), ('Unknown Location"', '')]], path_definition assert path_definition == [[('date', '%Y-%m-%b')], [('album', ''), ('location', '%city'), ('"Unknown Location"', '')]], path_definition
@mock.patch('elodie.config.config_file', '%s/config.ini-date-location' % gettempdir()) @mock.patch('elodie.config.config_file', '%s/config.ini-date-location' % gettempdir())
def test_get_folder_path_definition_date_location(): def test_get_folder_path_definition_date_location():