diff --git a/src/criteriaSorter/modules/criteriaSorter.py b/src/criteriaSorter/modules/criteriaSorter.py index a113de3..003347a 100644 --- a/src/criteriaSorter/modules/criteriaSorter.py +++ b/src/criteriaSorter/modules/criteriaSorter.py @@ -1,5 +1,6 @@ import sys +import rich.pretty import yaml import argparse import logging @@ -16,7 +17,7 @@ def action_sort(argsp): try: config = yaml.load(stream, Loader=yaml.FullLoader) except yaml.YAMLError as exc: - logging.error(exc) + logging.critical(exc) sys.exit(1) # Load the operations @@ -28,7 +29,7 @@ def action_sort(argsp): operations_name = config[argsp.operation] operations_config = config["operations"][operations_name] except Exception: - logging.error("Could not load operations") + logging.critical("Could not load operations") sys.exit(1) # Pick the right handler @@ -37,7 +38,7 @@ def action_sort(argsp): elif config["general"]["handler"] == "ArtistHandler": Handler = ArtistHandler else: - logging.error("Could not load handler "+config["general"]["handler"]) + logging.critical("Could not load handler "+config["general"]["handler"]) sys.exit(1) # Create the DirectorySorter @@ -118,10 +119,10 @@ def action_list(argsp): logging.error(exc) sys.exit(1) if argsp.verbose: - print(config["operations"]) + rich.print(config["operations"]) else: for line in config["operations"].keys(): - print(line) + rich.print(line) # def action_help(argsp): diff --git a/src/criteriaSorter/modules/fileops.py b/src/criteriaSorter/modules/fileops.py index 41bd71a..078fb7c 100644 --- a/src/criteriaSorter/modules/fileops.py +++ b/src/criteriaSorter/modules/fileops.py @@ -2,8 +2,6 @@ import logging import os import re -# import pathlib - EXTENTION_BY_TYPE = { 'image': ["jpg", "jpeg", "png", "gif", "bmp", "tiff", "tif"], diff --git a/tests/modules/test_criteriaSorter.py b/tests/modules/test_criteriaSorter.py index df2538f..a32b71f 100644 --- a/tests/modules/test_criteriaSorter.py +++ b/tests/modules/test_criteriaSorter.py @@ -1,4 +1,6 @@ # Test file for the criteriaSorter module +import io +import os import logging import importlib import pytest @@ -23,6 +25,23 @@ _EXIT_ARGS_LIST = [ # ('help', "help"), ] +_JUNK_FILE_LIST = """junk +path/to/file4 : path/to/destination4 +also/junk""" + +_JUNK_RESULT = """[MOVED] path/to/destination4 to path/to/file4""" + +_GOOD_FILE_LIST = """ +path/to/file1 : path/to/destination1 +path/to/file2/. : path/to/destination2 +path/to/file3/ : path/to/destination3/. +""" + +_GOOD_RESULT = """[MOVED] path/to/destination1 to path/to/file1 +[MOVED] path/to/destination2 to path/to/file2/. +[MOVED] path/to/destination3/. to path/to/file3/ +""" + @pytest.mark.parametrize("args, outp", _EXIT_ARGS_LIST) def test_parse_args_exit(args, outp, capsys): @@ -49,7 +68,7 @@ def test_parse_args(args, capsys): ]) def test_config_log(vlevel, login, logout, caplog): if vlevel != "": - vlevel = vlevel + " " + vlevel += " " arg = (vlevel+"sort . -o .").split(" ") args = criteriaSorter.parse_args(arg) criteriaSorter.config_log(args) @@ -66,3 +85,52 @@ def test_config_log(vlevel, login, logout, caplog): def test_action_sort(): pass + + +def move(path, dest): + print("[MOVED] "+str(path.strip())+" to "+str(dest.strip())) + return True + + +class falseArg: + def __init__(self, path): + self.cancel_file = path + cancel_file = "nocancel.txt" + + +@pytest.mark.parametrize("list_of_cancels, result_expected", [ + (io.StringIO(_GOOD_FILE_LIST), _GOOD_RESULT), (io.StringIO(_JUNK_FILE_LIST), _JUNK_RESULT) +]) +def test_action_cancel(monkeypatch, list_of_cancels, result_expected, capsys): + monkeypatch.setattr(os, "rename", move) + monkeypatch.setattr(os, "remove", move) + monkeypatch.setattr(os, "rmdir", move) + monkeypatch.setattr(os.path, "join", lambda a: a) + + with open("cancelfile.txt", "w") as f: + f.write(list_of_cancels.read()) + criteriaSorter.action_cancel(falseArg("cancelfile.txt")) + assert result_expected in capsys.readouterr().out + + +@pytest.mark.parametrize("verbose", [0, 1, 2, 3, 4]) +def test_action_list(verbose, capsys): + args = ["-"+"v"*verbose]+["list"] if verbose else ["list"] + args = criteriaSorter.parse_args(args) + criteriaSorter.action_list(args) + if verbose == 0: + assert "sort_junk" in capsys.readouterr().out + else: + assert "condition" in capsys.readouterr().out + + +# @pytest.mark.parametrize("args", [["--config=noconf.yaml","list"]]) +# def test_action_list_error(args, caplog, monkeypatch): +# +# with open("noconf.yaml", "w") as f: +# f.write("Bad config =:/\n+3") +# +# args = criteriaSorter.parse_args(args) +# with pytest.raises(SystemExit): +# with caplog.at_level(logging.ERROR): +# criteriaSorter.action_list(args)