Add exit codes to all public commands #157 (#184)

* Add exit codes to update and import command
* Add tests for _import and _update exit codes
This commit is contained in:
Jaisen Mathai 2017-01-21 20:52:32 -08:00 committed by GitHub
parent 2e0a59b7d8
commit 18b5c1336e
2 changed files with 56 additions and 0 deletions

View File

@ -87,6 +87,7 @@ def import_file(_file, destination, album_from_folder, trash, allow_duplicates):
def _import(destination, source, file, album_from_folder, trash, paths, allow_duplicates):
"""Import files or directories by reading their EXIF and organizing them accordingly.
"""
has_errors = False
result = Result()
destination = _decode(destination)
@ -110,9 +111,13 @@ def _import(destination, source, file, album_from_folder, trash, paths, allow_du
dest_path = import_file(current_file, destination, album_from_folder,
trash, allow_duplicates)
result.append((current_file, dest_path))
has_errors = has_errors is True or not dest_path
result.write()
if has_errors:
sys.exit(1)
@click.command('generate-db')
@click.option('--source', type=click.Path(file_okay=False),
@ -209,6 +214,7 @@ def update_time(media, file_path, time_string):
def _update(album, location, time, title, paths):
"""Update a file's EXIF. Automatically modifies the file's location and file name accordingly.
"""
has_errors = False
result = Result()
files = set()
@ -221,6 +227,8 @@ def _update(album, location, time, title, paths):
for current_file in files:
if not os.path.exists(current_file):
has_errors = True
result.append((current_file, False))
if constants.debug:
print('Could not find %s' % current_file)
print('{"source":"%s", "error_msg":"Could not find %s"}' % \
@ -289,11 +297,17 @@ def _update(album, location, time, title, paths):
FILESYSTEM.delete_directory_if_empty(
os.path.dirname(os.path.dirname(current_file)))
result.append((current_file, dest_path))
# Trip has_errors to False if it's already False or dest_path is.
has_errors = has_errors is True or not dest_path
else:
has_errors = False
result.append((current_file, False))
result.write()
if has_errors:
sys.exit(1)
@click.group()
def main():

View File

@ -210,6 +210,27 @@ def test_import_destination_in_source():
assert dest_path is None, dest_path
def test_import_invalid_file_exit_code():
temporary_folder, folder = helper.create_working_folder()
temporary_folder_destination, folder_destination = helper.create_working_folder()
# use a good and bad
origin_invalid = '%s/invalid.jpg' % folder
shutil.copyfile(helper.get_file('invalid.jpg'), origin_invalid)
origin_valid = '%s/valid.jpg' % folder
shutil.copyfile(helper.get_file('plain.jpg'), origin_valid)
helper.reset_dbs()
runner = CliRunner()
result = runner.invoke(elodie._import, ['--destination', folder_destination, origin_invalid, origin_valid])
helper.restore_dbs()
shutil.rmtree(folder)
shutil.rmtree(folder_destination)
assert result.exit_code == 1, result.exit_code
def test_update_location_on_audio():
temporary_folder, folder = helper.create_working_folder()
temporary_folder_destination, folder_destination = helper.create_working_folder()
@ -428,6 +449,27 @@ def test_update_with_directory_passed_in():
assert updated_file_exists, updated_file_path
def test_update_invalid_file_exit_code():
temporary_folder, folder = helper.create_working_folder()
temporary_folder_destination, folder_destination = helper.create_working_folder()
# use a good and bad
origin_invalid = '%s/invalid.jpg' % folder
shutil.copyfile(helper.get_file('invalid.jpg'), origin_invalid)
origin_valid = '%s/valid.jpg' % folder
shutil.copyfile(helper.get_file('plain.jpg'), origin_valid)
helper.reset_dbs()
runner = CliRunner()
result = runner.invoke(elodie._update, ['--album', 'test', origin_invalid, origin_valid])
helper.restore_dbs()
shutil.rmtree(folder)
shutil.rmtree(folder_destination)
assert result.exit_code == 1, result.exit_code
def test_regenerate_db_invalid_source():
runner = CliRunner()
result = runner.invoke(elodie._generate_db, ['--source', '/invalid/path'])