def test_makedirs(mock_parent, mock_move, mock_trash): move = Move(dest="~/some/new/folder/", overwrite=False) updates = move.run(**ARGS) mock_parent.mkdir.assert_called_with(parents=True, exist_ok=True) mock_trash.assert_not_called() mock_move.assert_called_with( src=os.path.join(USER_DIR, "test.py"), dst=os.path.join(USER_DIR, "some", "new", "folder", "test.py"), ) assert updates is not None
def test_makedirs(mock_parent, mock_move, mock_trash): basedir = Path('~') path = Path('~') / 'test.py' move = Move(dest='~/some/new/folder/', overwrite=False) new_path = move.run(basedir, path, {}, False) mock_parent.mkdir.assert_called_with(parents=True, exist_ok=True) mock_trash.assert_not_called() mock_move.assert_called_with(src=os.path.join(USER_DIR, 'test.py'), dst=os.path.join(USER_DIR, 'some', 'new', 'folder', 'test.py')) assert new_path is not None
def test_makedirs(mock_parent, mock_move, mock_trash): attrs = {"basedir": Path.home(), "path": Path.home() / "test.py"} move = Move(dest="~/some/new/folder/", overwrite=False) new_path = move.run(attrs, False) mock_parent.mkdir.assert_called_with(parents=True, exist_ok=True) mock_trash.assert_not_called() mock_move.assert_called_with( src=os.path.join(USER_DIR, "test.py"), dst=os.path.join(USER_DIR, "some", "new", "folder", "test.py"), ) assert new_path is not None
def test_keep_location(mock_exists, mock_samefile, mock_move, mock_trash, mock_mkdir): mock_exists.return_value = True mock_samefile.return_value = True move = Move(dest="~/test.py") updates = move.run(**ARGS) mock_mkdir.assert_not_called() mock_exists.assert_called_with() mock_trash.assert_not_called() mock_move.assert_not_called() assert updates is not None
def test_dont_keep_case_sensitive(mock_exists, mock_samefile, mock_move, mock_trash, mock_mkdir): mock_exists.return_value = True mock_samefile.return_value = True move = Move(dest="~/TEST.PY") updates = move.run(**ARGS) assert mock_mkdir.call_count > 0 mock_exists.assert_called_with() mock_trash.assert_not_called() assert mock_move.call_count > 0 assert updates is not None
def test_dont_keep_case_sensitive(mock_exists, mock_samefile, mock_move, mock_trash, mock_mkdir): attrs = {"basedir": Path.home(), "path": Path.home() / "test.py"} mock_exists.return_value = True mock_samefile.return_value = True move = Move(dest="~/TEST.PY") new_path = move.run(attrs, False) mock_mkdir.assert_called() mock_exists.assert_called_with() mock_trash.assert_not_called() mock_move.assert_called() assert new_path is not None
def test_args(mock_exists, mock_samefile, mock_move, mock_trash, mock_mkdir): args = ARGS.merge({"nr": {"upper": 1}}) mock_exists.return_value = False mock_samefile.return_value = False move = Move(dest="~/{nr.upper}-name.py", overwrite=False) updates = move.run(**args) mock_mkdir.assert_called_with(exist_ok=True, parents=True) mock_exists.assert_called_with() mock_trash.assert_not_called() mock_move.assert_called_with(src=os.path.join(USER_DIR, "test.py"), dst=os.path.join(USER_DIR, "1-name.py")) assert updates is not None
def test_tilde_expansion(mock_exists, mock_samefile, mock_move, mock_trash, mock_mkdir): mock_exists.return_value = False mock_samefile.return_value = False move = Move(dest="~/newname.py", overwrite=False) updates = move.run(**ARGS) mock_mkdir.assert_called_with(exist_ok=True, parents=True) mock_exists.assert_called_with() mock_trash.assert_not_called() mock_move.assert_called_with(src=os.path.join(USER_DIR, "test.py"), dst=os.path.join(USER_DIR, "newname.py")) assert updates == {"path": Path("~/newname.py").expanduser()}
def test_tilde_expansion(mock_exists, mock_samefile, mock_move, mock_trash, mock_mkdir): attrs = {"basedir": Path.home(), "path": Path.home() / "test.py"} mock_exists.return_value = False mock_samefile.return_value = False move = Move(dest="~/newname.py", overwrite=False) new_path = move.run(attrs, False) mock_mkdir.assert_called_with(exist_ok=True, parents=True) mock_exists.assert_called_with() mock_trash.assert_not_called() mock_move.assert_called_with(src=os.path.join(USER_DIR, "test.py"), dst=os.path.join(USER_DIR, "newname.py")) assert new_path == Path("~/newname.py").expanduser()
def test_makedirs(mock_parent, mock_move, mock_trash): attrs = { 'basedir': Path.home(), 'path': Path.home() / 'test.py', } move = Move(dest='~/some/new/folder/', overwrite=False) new_path = move.run(attrs, False) mock_parent.mkdir.assert_called_with(parents=True, exist_ok=True) mock_trash.assert_not_called() mock_move.assert_called_with(src=os.path.join(USER_DIR, 'test.py'), dst=os.path.join(USER_DIR, 'some', 'new', 'folder', 'test.py')) assert new_path is not None
def test_attrs(mock_exists, mock_samefile, mock_move, mock_trash, mock_mkdir): basedir = Path('~') path = Path('~') / 'test.py' mock_exists.return_value = False mock_samefile.return_value = False move = Move(dest='~/{nr.upper}-name.py', overwrite=False) new_path = move.run(basedir, path, {'nr': DotDict({'upper': 1})}, False) mock_mkdir.assert_called_with(exist_ok=True, parents=True) mock_exists.assert_called_with() mock_trash.assert_not_called() mock_move.assert_called_with(src=os.path.join(USER_DIR, 'test.py'), dst=os.path.join(USER_DIR, '1-name.py')) assert new_path is not None
def test_dont_keep_case_sensitive(mock_exists, mock_samefile, mock_move, mock_trash, mock_mkdir): basedir = Path('~') path = Path('~') / 'test.py' mock_exists.return_value = True mock_samefile.return_value = True move = Move(dest='~/TEST.PY') new_path = move.run(basedir, path, {}, False) mock_mkdir.assert_called() mock_exists.assert_called_with() mock_trash.assert_not_called() mock_move.assert_called() assert new_path is not None
def test_tilde_expansion(mock_exists, mock_samefile, mock_move, mock_trash, mock_mkdir): basedir = Path('~') path = Path('~') / 'test.py' mock_exists.return_value = False mock_samefile.return_value = False move = Move(dest='~/newname.py', overwrite=False) new_path = move.run(basedir, path, {}, False) mock_mkdir.assert_called_with(exist_ok=True, parents=True) mock_exists.assert_called_with() mock_trash.assert_not_called() mock_move.assert_called_with(src=os.path.join(USER_DIR, 'test.py'), dst=os.path.join(USER_DIR, 'newname.py')) assert new_path == Path('~/newname.py').expanduser()
def test_path(mock_exists, mock_samefile, mock_move, mock_trash, mock_mkdir): mock_exists.return_value = False mock_samefile.return_value = False move = Move(dest="~/{path.stem}/{path.suffix}/{path.name}", overwrite=False) updates = move.run(**ARGS) mock_mkdir.assert_called_with(exist_ok=True, parents=True) mock_exists.assert_called_with() mock_trash.assert_not_called() mock_move.assert_called_with( src=os.path.join(USER_DIR, "test.py"), dst=os.path.join(USER_DIR, "test", ".py", "test.py"), ) assert updates is not None
def test_into_folder(mock_exists, mock_samefile, mock_move, mock_trash, mock_mkdir): mock_exists.return_value = False mock_samefile.return_value = False move = Move(dest="~/somefolder/", overwrite=False) updates = move.run(**ARGS) mock_mkdir.assert_called_with(exist_ok=True, parents=True) mock_exists.assert_called_with() mock_trash.assert_not_called() mock_move.assert_called_with( src=os.path.join(USER_DIR, "test.py"), dst=os.path.join(USER_DIR, "somefolder", "test.py"), ) assert updates == {"path": Path(USER_DIR) / "somefolder" / "test.py"}
def test_keep_location(mock_exists, mock_samefile, mock_move, mock_trash, mock_mkdir): attrs = { 'basedir': Path.home(), 'path': Path.home() / 'test.py', } mock_exists.return_value = True mock_samefile.return_value = True move = Move(dest='~/test.py') new_path = move.run(attrs, False) mock_mkdir.assert_not_called() mock_exists.assert_called_with() mock_trash.assert_not_called() mock_move.assert_not_called() assert new_path is not None
def test_already_exists_multiple_separator(mock_exists, mock_samefile, mock_move, mock_trash, mock_mkdir): mock_exists.side_effect = [True, True, True, False] mock_samefile.return_value = False move = Move(dest="~/folder/", overwrite=False, counter_separator="_") updates = move.run(**ARGS) mock_mkdir.assert_called_with(exist_ok=True, parents=True) mock_exists.assert_called_with() mock_trash.assert_not_called() mock_move.assert_called_with( src=os.path.join(USER_DIR, "test.py"), dst=os.path.join(USER_DIR, "folder", "test_4.py"), ) assert updates is not None
def test_already_exists_multiple_separator(mock_exists, mock_samefile, mock_move, mock_trash, mock_mkdir): attrs = {"basedir": Path.home(), "path": Path.home() / "test.py"} mock_exists.side_effect = [True, True, True, False] mock_samefile.return_value = False move = Move(dest="~/folder/", overwrite=False, counter_separator="_") new_path = move.run(attrs, False) mock_mkdir.assert_called_with(exist_ok=True, parents=True) mock_exists.assert_called_with() mock_trash.assert_not_called() mock_move.assert_called_with( src=os.path.join(USER_DIR, "test.py"), dst=os.path.join(USER_DIR, "folder", "test_4.py"), ) assert new_path is not None
def test_into_folder(mock_exists, mock_samefile, mock_move, mock_trash, mock_mkdir): basedir = Path('~') path = Path('~') / 'test.py' mock_exists.return_value = False mock_samefile.return_value = False move = Move(dest='~/somefolder/', overwrite=False) new_path = move.run(basedir, path, {}, False) mock_mkdir.assert_called_with(exist_ok=True, parents=True) mock_exists.assert_called_with() mock_trash.assert_not_called() mock_move.assert_called_with(src=os.path.join(USER_DIR, 'test.py'), dst=os.path.join(USER_DIR, 'somefolder', 'test.py')) assert new_path is not None
def test_into_folder(mock_exists, mock_samefile, mock_move, mock_trash, mock_mkdir): attrs = {"basedir": Path.home(), "path": Path.home() / "test.py"} mock_exists.return_value = False mock_samefile.return_value = False move = Move(dest="~/somefolder/", overwrite=False) new_path = move.run(attrs, False) mock_mkdir.assert_called_with(exist_ok=True, parents=True) mock_exists.assert_called_with() mock_trash.assert_not_called() mock_move.assert_called_with( src=os.path.join(USER_DIR, "test.py"), dst=os.path.join(USER_DIR, "somefolder", "test.py"), ) assert new_path is not None
def test_already_exists_multiple(mock_exists, mock_samefile, mock_move, mock_trash, mock_mkdir): basedir = Path('~') path = Path('~') / 'test.py' mock_exists.side_effect = [True, True, True, False] mock_samefile.return_value = False move = Move(dest='~/folder/', overwrite=False) new_path = move.run(basedir, path, {}, False) mock_mkdir.assert_called_with(exist_ok=True, parents=True) mock_exists.assert_called_with() mock_trash.assert_not_called() mock_move.assert_called_with(src=os.path.join(USER_DIR, 'test.py'), dst=os.path.join(USER_DIR, 'folder', 'test 4.py')) assert new_path is not None
def test_already_exists(mock_exists, mock_samefile, mock_move, mock_trash, mock_mkdir): attrs = { 'basedir': Path.home(), 'path': Path.home() / 'test.py', } mock_exists.side_effect = [True, False] mock_samefile.return_value = False move = Move(dest='~/folder/', overwrite=False) new_path = move.run(attrs, False) mock_mkdir.assert_called_with(exist_ok=True, parents=True) mock_exists.assert_called_with() mock_trash.assert_not_called() mock_move.assert_called_with( src=os.path.join(USER_DIR, 'test.py'), dst=os.path.join(USER_DIR, 'folder', 'test 2.py')) assert new_path is not None
def test_attrs(mock_exists, mock_samefile, mock_move, mock_trash, mock_mkdir): attrs = { "basedir": Path.home(), "path": Path.home() / "test.py", "nr": DotDict({"upper": 1}), } mock_exists.return_value = False mock_samefile.return_value = False move = Move(dest="~/{nr.upper}-name.py", overwrite=False) new_path = move.run(attrs, False) mock_mkdir.assert_called_with(exist_ok=True, parents=True) mock_exists.assert_called_with() mock_trash.assert_not_called() mock_move.assert_called_with(src=os.path.join(USER_DIR, "test.py"), dst=os.path.join(USER_DIR, "1-name.py")) assert new_path is not None
def test_path(mock_exists, mock_samefile, mock_move, mock_trash, mock_mkdir): attrs = { 'basedir': Path.home(), 'path': Path.home() / 'test.py', } mock_exists.return_value = False mock_samefile.return_value = False move = Move(dest='~/{path.stem}/{path.suffix}/{path.name}', overwrite=False) new_path = move.run(attrs, False) mock_mkdir.assert_called_with(exist_ok=True, parents=True) mock_exists.assert_called_with() mock_trash.assert_not_called() mock_move.assert_called_with(src=os.path.join(USER_DIR, 'test.py'), dst=os.path.join(USER_DIR, 'test', '.py', 'test.py')) assert new_path is not None
def test_basic(): config = """ rules: - folders: '~/Desktop' filters: - Extension: - jpg - png - Extension: txt actions: - Move: {dest: '~/Desktop/New Folder', overwrite: true} - Echo: 'Moved {path}/{extension.upper}' - folders: - '~/test1' - /test2 filters: actions: - Shell: cmd: 'say {path.stem}' """ conf = Config.from_string(config) assert conf.rules == [ Rule( folders=['~/Desktop'], filters=[Extension('.JPG', 'PNG'), Extension('txt')], actions=[ Move(dest='~/Desktop/New Folder', overwrite=True), Echo(msg='Moved {path}/{extension.upper}') ], subfolders=False, system_files=False, ), Rule( folders=['~/test1', '/test2'], filters=[], actions=[Shell(cmd='say {path.stem}')], subfolders=False, system_files=False, ) ]
def test_basic(): config = """ rules: - folders: '~/Desktop' filters: - extension: - jpg - png - extension: txt actions: - move: {dest: '~/Desktop/New Folder', overwrite: true} - echo: 'Moved {path}/{extension.upper}' - folders: - '~/test1' - '/test2' filters: actions: - shell: cmd: 'say {path.stem}' """ conf = Config.from_string(config) assert conf.rules == [ Rule( folders=["~/Desktop"], filters=[Extension(".JPG", "PNG"), Extension("txt")], actions=[ Move(dest="~/Desktop/New Folder", overwrite=True), Echo(msg="Moved {path}/{extension.upper}"), ], subfolders=False, system_files=False, ), Rule( folders=["~/test1", "/test2"], filters=[], actions=[Shell(cmd="say {path.stem}")], subfolders=False, system_files=False, ), ]
def test_case_insensitive(): config = """ rules: - folders: '~/Desktop' filters: - extension: ['JPg', 'png'] - Extension: txt actions: - moVe: {dest: '~/Desktop/New Folder', overwrite: true} - EC_HO: 'Moved {path}/{extension.upper}' - folders: - '~/test1' - /test2 filters: actions: - SHELL: cmd: 'say {path.stem}' """ conf = Config.from_string(config) assert conf.rules == [ Rule( folders=["~/Desktop"], filters=[Extension(".JPG", "PNG"), Extension("txt")], actions=[ Move(dest="~/Desktop/New Folder", overwrite=True), Echo(msg="Moved {path}/{extension.upper}"), ], subfolders=False, system_files=False, ), Rule( folders=["~/test1", "/test2"], filters=[], actions=[Shell(cmd="say {path.stem}")], subfolders=False, system_files=False, ), ]
def test_flatten_filters_and_actions(): config = """ folder_aliases: Downloads: &downloads ~/Downloads/ Payables_due: &payables_due ~/PayablesDue/ Payables_paid: &payables_paid ~/Accounting/Expenses/ Receivables_due: &receivables_due ~/Receivables/ Receivables_paid: &receivables_paid ~/Accounting/Income/ defaults: filters: &default_filters - extension: pdf - filecontent: '(?P<date>...)' actions: &default_actions - echo: 'Dated: {filecontent.date}' - echo: 'Stem of filename: {filecontent.stem}' post_actions: &default_sorting - rename: '{python.timestamp}-{filecontent.stem}.{extension.lower}' - move: '{path.parent}/{python.quarter}/' rules: - folders: *downloads filters: - *default_filters - filecontent: 'Due Date' # regex to id as payable - filecontent: '(?P<stem>...)' # regex to extract supplier actions: - *default_actions - move: *payables_due - *default_sorting - folders: *downloads filters: - *default_filters - filecontent: 'Account: 000000000' # regex to id as receivables due - filecontent: '(?P<stem>...)' # regex to extract customer actions: - *default_actions - move: *receivables_due - *default_sorting - folders: *downloads filters: - *default_filters - filecontent: 'PAID' # regex to id as receivables paid - filecontent: '(?P<stem>...)' # regex to extract customer - filecontent: '(?P<paid>...)' # regex to extract date paid - filename: startswith: 2020 actions: - *default_actions - move: *receivables_paid - *default_sorting - rename: '{filecontent.paid}_{filecontent.stem}.{extension}' """ conf = Config.from_string(config) assert conf.rules == [ Rule( folders=["~/Downloads/"], filters=[ # default_filters Extension("pdf"), FileContent(expr="(?P<date>...)"), # added filters FileContent(expr="Due Date"), FileContent(expr="(?P<stem>...)"), ], actions=[ # default_actions Echo(msg="Dated: {filecontent.date}"), Echo(msg="Stem of filename: {filecontent.stem}"), # added actions Move(dest="~/PayablesDue/", overwrite=False), # default_sorting Rename( name= "{python.timestamp}-{filecontent.stem}.{extension.lower}", overwrite=False, ), Move(dest="{path.parent}/{python.quarter}/", overwrite=False), ], subfolders=False, system_files=False, ), Rule( folders=["~/Downloads/"], filters=[ # default_filters Extension("pdf"), FileContent(expr="(?P<date>...)"), # added filters FileContent(expr="Account: 000000000"), FileContent(expr="(?P<stem>...)"), ], actions=[ # default_actions Echo(msg="Dated: {filecontent.date}"), Echo(msg="Stem of filename: {filecontent.stem}"), # added actions Move(dest="~/Receivables/", overwrite=False), # default_sorting Rename( name= "{python.timestamp}-{filecontent.stem}.{extension.lower}", overwrite=False, ), Move(dest="{path.parent}/{python.quarter}/", overwrite=False), ], subfolders=False, system_files=False, ), Rule( folders=["~/Downloads/"], filters=[ # default_filters Extension("pdf"), FileContent(expr="(?P<date>...)"), # added filters FileContent(expr="PAID"), FileContent(expr="(?P<stem>...)"), FileContent(expr="(?P<paid>...)"), Filename(startswith="2020"), ], actions=[ # default_actions Echo(msg="Dated: {filecontent.date}"), Echo(msg="Stem of filename: {filecontent.stem}"), # added actions Move(dest="~/Accounting/Income/", overwrite=False), # default_sorting Rename( name= "{python.timestamp}-{filecontent.stem}.{extension.lower}", overwrite=False, ), Move(dest="{path.parent}/{python.quarter}/", overwrite=False), # added actions Rename( name="{filecontent.paid}_{filecontent.stem}.{extension}", overwrite=False, ), ], subfolders=False, system_files=False, ), ]