Skip to content

Commit

Permalink
fix: Add gives proper error message when library.toml missing
Browse files Browse the repository at this point in the history
Added also a small helpful message about how to create a library.toml

fixes #3
  • Loading branch information
Heikki Ketoharju committed Jan 10, 2024
1 parent 054901f commit f0b3eea
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
8 changes: 7 additions & 1 deletion src/filesystem_operations/libraryreader.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@


def load_library(filename):
return tomli.load(open(filename, "rb"))
try:
return tomli.load(open(filename, "rb"))
except(FileNotFoundError):
raise LibraryFileMissing

class LibraryFileMissing(Exception):
pass
5 changes: 5 additions & 0 deletions src/gallerycmd/add/add.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sys
from filesystem_operations.libraryreader import LibraryFileMissing
from filesystem_operations.librarysaver import save_library

from gallerycmd.parser import subparsers
Expand All @@ -15,6 +16,10 @@ def main(args):
except(FileNotFoundError):
print("No such image:", args.filename)
exit(1)
except(LibraryFileMissing):
print("\nNo library.toml present!\n")
print(" You can create a new gallery by issuing 'gallery init > library.toml'")
exit(1)


def add_image(filename, title="", description="", tags=[]):
Expand Down
4 changes: 2 additions & 2 deletions test/integration/filesystem_operations/test_libraryreader.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import pytest
import tomli

from filesystem_operations.libraryreader import load_library
from filesystem_operations.libraryreader import LibraryFileMissing, load_library


def test_load_gallery_fails_without_filename():
with pytest.raises(TypeError, match="missing 1 required positional argument"):
load_library()

def test_file_not_found(tmp_path):
with pytest.raises(FileNotFoundError):
with pytest.raises(LibraryFileMissing):
load_library(tmp_path / "foo")

def test_file_not_toml(tmp_path):
Expand Down
3 changes: 2 additions & 1 deletion test/integration/imagegallery/test_imagegallery.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import pytest
from _pytest import monkeypatch
from filesystem_operations.libraryreader import LibraryFileMissing

from test.integration.filesystem_helpers import mkdir
from Imagegallery import Filetree, LibraryToml, Imagegallery

def test_throws_if_no_library_toml(tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
with pytest.raises(FileNotFoundError):
with pytest.raises(LibraryFileMissing):
gallery = Imagegallery.from_disk()

def test_works_if_file_present(environment):
Expand Down

0 comments on commit f0b3eea

Please sign in to comment.