diff options
author | Thomas Feldmann <mail@tfeldmann.de> | 2022-09-05 16:34:24 +0200 |
---|---|---|
committer | Thomas Feldmann <mail@tfeldmann.de> | 2022-09-05 16:34:24 +0200 |
commit | a31b972478f3b6238fc7ab3353c37af51cd39fef (patch) | |
tree | f5c0871d13a8bb9f9e8bbf247e8ea063fa593c31 /tests | |
parent | a21016fd74f3c2935ae0e22f46fb2c3f94c2e9e3 (diff) |
add filesystem tests
Diffstat (limited to 'tests')
-rw-r--r-- | tests/actions/test_conflict_resolution.py | 11 | ||||
-rw-r--r-- | tests/actions/test_copy.py | 81 | ||||
-rw-r--r-- | tests/actions/test_move.py | 4 | ||||
-rw-r--r-- | tests/actions/test_rename.py | 14 | ||||
-rw-r--r-- | tests/actions/test_write.py | 33 | ||||
-rw-r--r-- | tests/conftest.py | 2 | ||||
-rw-r--r-- | tests/filters/test_duplicate.py | 3 | ||||
-rw-r--r-- | tests/filters/test_size.py | 55 | ||||
-rw-r--r-- | tests/todo/integration/__init__.py | 0 | ||||
-rw-r--r-- | tests/todo/integration/test_integration.py | 40 |
10 files changed, 125 insertions, 118 deletions
diff --git a/tests/actions/test_conflict_resolution.py b/tests/actions/test_conflict_resolution.py index 07d665d..820d9fb 100644 --- a/tests/actions/test_conflict_resolution.py +++ b/tests/actions/test_conflict_resolution.py @@ -12,9 +12,9 @@ from organize.utils import Template @pytest.mark.parametrize( "template,wanted,result", ( - ("{name}-{counter}{extension}", "file.txt", "file-1.txt"), - ("{name}-{counter}{extension}", "file.txt", "file-1.txt"), - ("{name}-{'%02d' % counter}{extension}", "file.txt", "file-02.txt"), + ("{name}-{counter}{extension}", "file.txt", "file-2.txt"), + ("{name}-{counter}{extension}", "file.txt", "file-2.txt"), + ("{name}-{'%02d' % counter}{extension}", "file.txt", "file-03.txt"), ("{name}{counter}{extension}", "file.txt", "file4.txt"), ("{name}{counter}{extension}", "other.txt", "other.txt"), ), @@ -24,6 +24,7 @@ def test_next_free_name(template, wanted, result): mem.touch("file.txt") mem.touch("file1.txt") mem.touch("file-01.txt") + mem.touch("file-02.txt") mem.touch("file2.txt") mem.touch("file3.txt") name, ext = fs.path.splitext(wanted) @@ -46,8 +47,8 @@ def test_next_free_name_exception(): ("skip", None, {"file.txt": "file", "other.txt": "other"}), ("overwrite", "other.txt", {"file.txt": "file"}), # ("trash", None, {"file.txt": "file", "other.txt": "other"}), - ("rename_new", "other1.txt", {"file.txt": "file", "other.txt": "other"}), - ("rename_existing", "other.txt", {"file.txt": "file", "other1.txt": "other"}), + ("rename_new", "other2.txt", {"file.txt": "file", "other.txt": "other"}), + ("rename_existing", "other.txt", {"file.txt": "file", "other2.txt": "other"}), ), ) def test_resolve_overwrite_conflict(mode, result, files): diff --git a/tests/actions/test_copy.py b/tests/actions/test_copy.py index a497d17..325771d 100644 --- a/tests/actions/test_copy.py +++ b/tests/actions/test_copy.py @@ -1,6 +1,8 @@ -from fs.base import FS +from copy import deepcopy + import pytest from conftest import make_files, read_files +from fs.base import FS from organize import core @@ -32,6 +34,26 @@ def test_copy_on_itself(testfiles): assert result == files +def test_copy_basic(testfs): + config = """ + rules: + - locations: "." + filters: + - name: test + actions: + - copy: newname.pdf + """ + make_files(testfs, ["asd.txt", "newname 2.pdf", "newname.pdf", "test.pdf"]) + core.run(config, simulate=False, working_dir=testfs) + assert read_files(testfs) == { + "newname.pdf": "", + "newname 2.pdf": "", + "newname 3.pdf": "", + "test.pdf": "", + "asd.txt": "", + } + + def test_copy_into_dir(testfiles): config = """ rules: @@ -95,8 +117,8 @@ def test_copy_into_dir_subfolders(testfiles): [ ("skip", ["file.txt", "test.txt"], "old"), ("overwrite", ["file.txt", "test.txt"], "new"), - ("rename_new", ["file.txt", "test.txt", "test 1.txt"], "old"), - ("rename_existing", ["file.txt", "test.txt", "test 1.txt"], "new"), + ("rename_new", ["file.txt", "test.txt", "test 2.txt"], "old"), + ("rename_existing", ["file.txt", "test.txt", "test 2.txt"], "new"), ], ) def test_copy_conflict(testfs: FS, mode, files, test_txt_content): @@ -120,33 +142,26 @@ def test_copy_conflict(testfs: FS, mode, files, test_txt_content): assert testfs.readtext("test.txt") == test_txt_content -# def test_does_not_create_folder_in_simulation(): -# with fs.open_fs("mem://") as mem: -# config = { -# "rules": [ -# { -# "locations": [ -# {"path": "files", "filesystem": mem}, -# ], -# "actions": [ -# {"copy": {"dest": "files/new-subfolder/", "filesystem": mem}}, -# {"copy": {"dest": "files/copyhere/", "filesystem": mem}}, -# ], -# }, -# ] -# } -# make_files(mem, files) -# core.run(config, simulate=True) -# result = read_files(mem) -# assert result == files - -# core.run(config, simulate=False, validate=False) -# result = read_files(mem) - -# expected = deepcopy(files) -# expected["files"]["new-subfolder"] = deepcopy(files["files"]) -# expected["files"]["new-subfolder"].pop("folder") -# expected["files"]["copyhere"] = deepcopy(files["files"]) -# expected["files"]["copyhere"].pop("folder") - -# assert result == expected +def test_does_not_create_folder_in_simulation(testfs): + config = """ + rules: + - locations: "." + actions: + - copy: "new-subfolder/" + - copy: "copyhere/" + """ + make_files(testfs, files) + core.run(config, simulate=True, working_dir=testfs) + result = read_files(testfs) + assert result == files + + core.run(config, simulate=False, working_dir=testfs) + result = read_files(testfs) + + expected = deepcopy(files) + expected["new-subfolder"] = deepcopy(files) + expected["new-subfolder"].pop("folder") + expected["copyhere"] = deepcopy(files) + expected["copyhere"].pop("folder") + + assert result == expected diff --git a/tests/actions/test_move.py b/tests/actions/test_move.py index 042430d..685de92 100644 --- a/tests/actions/test_move.py +++ b/tests/actions/test_move.py @@ -37,8 +37,8 @@ def test_copy_on_itself(testfiles): [ ("skip", ["file.txt", "test.txt"], "old"), ("overwrite", ["test.txt"], "new"), - ("rename_new", ["test.txt", "test 1.txt"], "old"), - ("rename_existing", ["test.txt", "test 1.txt"], "new"), + ("rename_new", ["test.txt", "test 2.txt"], "old"), + ("rename_existing", ["test.txt", "test 2.txt"], "new"), ], ) def test_move_conflict(testfs: FS, mode, files, test_txt_content): diff --git a/tests/actions/test_rename.py b/tests/actions/test_rename.py index 220142a..a1b477c 100644 --- a/tests/actions/test_rename.py +++ b/tests/actions/test_rename.py @@ -114,3 +114,17 @@ def test_rename_in_subfolders(testfs): "Metadata": "", }, } + + +def test_filename_move(tempfs): + config = """ + rules: + - locations: "." + filters: + - extension + actions: + - rename: '{path.stem}{path.stem}.{extension.lower()}' + """ + make_files(tempfs, {"test.PY": ""}) + run(rules=config, simulate=False, working_dir=tempfs) + assert read_files(tempfs) == {"testtest.py": ""} diff --git a/tests/actions/test_write.py b/tests/actions/test_write.py index 3bb9cb4..f58769b 100644 --- a/tests/actions/test_write.py +++ b/tests/actions/test_write.py @@ -16,23 +16,40 @@ from organize import core ("overwrite", "false", "c"), ], ) -def test_append(testfs: FS, mode, newline, result): +def test_write(testfs: FS, mode, newline, result): files = ["a.txt", "b.txt", "c.txt"] make_files(testfs, files) config = """ rules: - locations: "." filters: - - name + - name: "a" actions: - write: - text: "{name}" + text: "{text}" path: "out.txt" - mode: %s - newline: %s - """ % ( - mode, - newline, + mode: {mode} + newline: {newline} + - locations: "." + filters: + - name: "b" + actions: + - write: + text: "{text}" + path: "out.txt" + mode: {mode} + newline: {newline} + - locations: "." + filters: + - name: "c" + actions: + - write: + text: "{text}" + path: "out.txt" + mode: {mode} + newline: {newline} + """.format( + text="{name}", mode=mode, newline=newline ) core.run(config, simulate=False, working_dir=testfs) assert testfs.readtext("out.txt") == result diff --git a/tests/conftest.py b/tests/conftest.py index 3f9b18f..d09ad5a 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -25,7 +25,7 @@ def memfs(): @pytest.fixture( params=[ "mem://", - # pytest.param("temp://", marks=pytest.mark.skip), + pytest.param("temp://"), ], ) def testfs(request) -> FS: diff --git a/tests/filters/test_duplicate.py b/tests/filters/test_duplicate.py index b04fcc4..ee7f75a 100644 --- a/tests/filters/test_duplicate.py +++ b/tests/filters/test_duplicate.py @@ -10,7 +10,8 @@ rules: - locations: "." subfolders: true filters: - - duplicate + - duplicate: + detect_original_by: name actions: - delete """ diff --git a/tests/filters/test_size.py b/tests/filters/test_size.py index 8dda501..95d5479 100644 --- a/tests/filters/test_size.py +++ b/tests/filters/test_size.py @@ -78,31 +78,30 @@ def test_basic(testfs): } -# @pytest.mark.skip(reason="TODO - template vars in filters not supported") -# def test_python_args(tmp_path, mock_echo): -# create_filesystem( -# tmp_path, -# files=[ -# "empty", -# ("full", "0" * 2000), -# ("halffull", "0" * 1010), -# ("two_thirds.txt", "0" * 666), -# ], -# config=""" -# rules: -# - folders: files -# filters: -# - python: | -# return 2000 -# - filesize: '= {python}b' -# actions: -# - echo: '{path.name} {filesize.bytes}' -# """, -# ) -# main(["run", "--config-file=%s" % (tmp_path / "config.yaml")]) -# mock_echo.assert_has_calls( -# [ -# call("full 2000"), -# ], -# any_order=True, -# ) +@pytest.mark.skip(reason="TODO - template vars in filters not supported") +def test_python_args(testfs): + make_files( + testfs, + { + "empty": "", + "full": "0" * 2000, + "halffull": "0" * 1010, + "two_thirds.txt": "0" * 666, + }, + ) + config = """ + rules: + - folders: files + filters: + - python: | + return 2000 + - filesize: '= {python}b' + actions: + - delete + """ + core.run(config=config, working_dir=testfs, simulate=False) + assert read_files(testfs) == { + "empty": "", + "halffull": "0" * 1010, + "two_thirds.txt": "0" * 666, + } diff --git a/tests/todo/integration/__init__.py b/tests/todo/integration/__init__.py deleted file mode 100644 index e69de29..0000000 --- a/tests/todo/integration/__init__.py +++ /dev/null diff --git a/tests/todo/integration/test_integration.py b/tests/todo/integration/test_integration.py deleted file mode 100644 index 44095c4..0000000 --- a/tests/todo/integration/test_integration.py +++ /dev/null @@ -1,40 +0,0 @@ -import pytest -from conftest import assertdir, create_filesystem - -from organize.cli import main - - -def test_filename_move(tmp_path): - create_filesystem( - tmp_path, - files=["test.PY"], - config=""" - rules: - - folders: files - filters: - - Extension - actions: - - rename: '{path.stem}{path.stem}.{extension.lower}' - """, - ) - main(["run", "--config-file=%s" % (tmp_path / "config.yaml")]) - assertdir(tmp_path, "testtest.py") - - -def test_basic(tmp_path): - create_filesystem( - tmp_path, - files=["asd.txt", "newname 2.pdf", "newname.pdf", "test.pdf"], - config=""" - rules: - - folders: files - filters: - - filename: test - actions: - - copy: files/newname.pdf - """, - ) - main(["run", "--config-file=%s" % (tmp_path / "config.yaml")]) - assertdir( - tmp_path, "newname.pdf", "newname 2.pdf", "newname 3.pdf", "test.pdf", "asd.txt" - ) |