From e55553dafc36a607fbbb4f5fa1e1604e6bbef755 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Odd=20Str=C3=A5b=C3=B8?= Date: Tue, 3 Oct 2023 23:12:52 +0200 Subject: [PATCH] Replace py.path LocalPath with pathlib.Path --- tests/test_plugin.py | 39 +++++++++---------- tests/test_repo.py | 67 +++++++++++++++------------------ tests/test_repo_add_external.py | 22 +++++------ tests/test_repo_remove.py | 34 ++++++++--------- tests/test_utils.py | 50 ++++++++++++------------ 5 files changed, 100 insertions(+), 112 deletions(-) diff --git a/tests/test_plugin.py b/tests/test_plugin.py index ea760a0..73f8ece 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -1,37 +1,34 @@ -import os +from pathlib import Path +import shutil import pytest -from click.testing import CliRunner -from py._path.local import LocalPath import jprm - -TEST_DATA_DIR = os.path.join( - os.path.dirname(os.path.realpath(__file__)), - "data", -) +from .test_utils import TEST_DATA_DIR @pytest.mark.datafiles( - os.path.join(TEST_DATA_DIR, "jprm.yaml"), + TEST_DATA_DIR / "jprm.yaml", ) -def test_package_plugin(cli_runner: CliRunner, tmpdir_factory, datafiles: LocalPath): - bindir: LocalPath = tmpdir_factory.mktemp("bin") - plugin: LocalPath = tmpdir_factory.mktemp("plugin") - artifacts: LocalPath = tmpdir_factory.mktemp("artifacts") +def test_package_plugin(tmp_path_factory, datafiles: Path): + bindir: Path = tmp_path_factory.mktemp("bin") + plugin: Path = tmp_path_factory.mktemp("plugin") + artifacts: Path = tmp_path_factory.mktemp("artifacts") - bindir.join("dummy.dll").write_text("", "utf-8") + (bindir / "dummy.dll").write_text("", "utf-8") - datafiles.join("jprm.yaml").copy(plugin) + shutil.copy(datafiles / "jprm.yaml", plugin) - output_path = jprm.package_plugin( - str(plugin), version="5.0", binary_path=str(bindir), output=str(artifacts) + output_path = Path( + jprm.package_plugin( + str(plugin), version="5.0", binary_path=str(bindir), output=str(artifacts) + ) ) - assert os.path.exists(output_path) - assert artifacts.join("plugin-a_5.0.0.0.zip").check(file=True) - assert artifacts.join("plugin-a_5.0.0.0.zip.meta.json").check(file=True) - assert artifacts.join("plugin-a_5.0.0.0.zip.md5sum").check(file=True) + assert output_path.exists() + assert (artifacts / "plugin-a_5.0.0.0.zip").is_file() + assert (artifacts / "plugin-a_5.0.0.0.zip.meta.json").is_file() + assert (artifacts / "plugin-a_5.0.0.0.zip.md5sum").is_file() res = jprm.run_os_command( "md5sum -c plugin-a_5.0.0.0.zip.md5sum", cwd=str(artifacts) diff --git a/tests/test_repo.py b/tests/test_repo.py index f3b7c6c..a7be307 100644 --- a/tests/test_repo.py +++ b/tests/test_repo.py @@ -1,36 +1,30 @@ import os -import json +from pathlib import Path import pytest from click.testing import CliRunner -from py._path.local import LocalPath import jprm - -TEST_DATA_DIR = os.path.join( - os.path.dirname(os.path.realpath(__file__)), - "data", -) +from .test_utils import TEST_DATA_DIR, json_load -def test_init_repo_path(cli_runner: CliRunner, tmpdir: LocalPath): +def test_init_repo_path(cli_runner: CliRunner, tmp_path: Path): result = cli_runner.invoke( - jprm.cli, ["--verbosity=debug", "repo", "init", str(tmpdir)] + jprm.cli, ["--verbosity=debug", "repo", "init", str(tmp_path)] ) - manifest = tmpdir / "manifest.json" + manifest = tmp_path / "manifest.json" assert result.exit_code == 0 assert os.path.exists(manifest) - with open(manifest) as fh: - data = json.load(fh) + data = json_load(manifest) assert data == [] -def test_init_repo_file(cli_runner: CliRunner, tmpdir: LocalPath): - manifest = tmpdir / "foo.json" +def test_init_repo_file(cli_runner: CliRunner, tmp_path: Path): + manifest = tmp_path / "foo.json" result = cli_runner.invoke( jprm.cli, ["--verbosity=debug", "repo", "init", str(manifest)] @@ -39,40 +33,39 @@ def test_init_repo_file(cli_runner: CliRunner, tmpdir: LocalPath): assert result.exit_code == 0 assert os.path.exists(manifest) - with open(manifest) as fh: - data = json.load(fh) + data = json_load(manifest) assert data == [] -def test_double_init_repo_path(cli_runner: CliRunner, tmpdir: LocalPath): +def test_double_init_repo_path(cli_runner: CliRunner, tmp_path: Path): # Initializing an existing repo is not allowed - cli_runner.invoke(jprm.cli, ["--verbosity=debug", "repo", "init", str(tmpdir)]) + cli_runner.invoke(jprm.cli, ["--verbosity=debug", "repo", "init", str(tmp_path)]) result = cli_runner.invoke( - jprm.cli, ["--verbosity=debug", "repo", "init", str(tmpdir)] + jprm.cli, ["--verbosity=debug", "repo", "init", str(tmp_path)] ) assert result.exit_code == 2 @pytest.mark.datafiles( - os.path.join(TEST_DATA_DIR, "pluginA_1.0.0.zip"), - os.path.join(TEST_DATA_DIR, "pluginA_1.1.0.zip"), - os.path.join(TEST_DATA_DIR, "pluginB_1.0.0.zip"), - os.path.join(TEST_DATA_DIR, "manifest_pluginA.json"), - os.path.join(TEST_DATA_DIR, "manifest_pluginA2.json"), - os.path.join(TEST_DATA_DIR, "manifest_pluginAB.json"), + TEST_DATA_DIR / "pluginA_1.0.0.zip", + TEST_DATA_DIR / "pluginA_1.1.0.zip", + TEST_DATA_DIR / "pluginB_1.0.0.zip", + TEST_DATA_DIR / "manifest_pluginA.json", + TEST_DATA_DIR / "manifest_pluginA2.json", + TEST_DATA_DIR / "manifest_pluginAB.json", ) -def test_repo_add(cli_runner: CliRunner, tmpdir: LocalPath, datafiles: LocalPath): - manifest_file = tmpdir / "repo.json" +def test_repo_add(cli_runner: CliRunner, tmp_path: Path, datafiles: Path): + manifest_file = tmp_path / "repo.json" result = cli_runner.invoke( jprm.cli, ["--verbosity=debug", "repo", "init", str(manifest_file)] ) assert result.exit_code == 0 - manifest_a = json.load(datafiles / "manifest_pluginA.json") - manifest_a2 = json.load(datafiles / "manifest_pluginA2.json") - manifest_ab = json.load(datafiles / "manifest_pluginAB.json") + manifest_a = json_load(datafiles / "manifest_pluginA.json") + manifest_a2 = json_load(datafiles / "manifest_pluginA2.json") + manifest_ab = json_load(datafiles / "manifest_pluginAB.json") cli_runner.invoke( jprm.cli, @@ -84,9 +77,9 @@ def test_repo_add(cli_runner: CliRunner, tmpdir: LocalPath, datafiles: LocalPath str(datafiles / "pluginA_1.0.0.zip"), ], ) - manifest = json.load(manifest_file) + manifest = json_load(manifest_file) assert manifest == manifest_a - assert (tmpdir / "plugin-a" / "plugin-a_1.0.0.0.zip").exists() + assert (tmp_path / "plugin-a" / "plugin-a_1.0.0.0.zip").exists() cli_runner.invoke( jprm.cli, @@ -98,9 +91,9 @@ def test_repo_add(cli_runner: CliRunner, tmpdir: LocalPath, datafiles: LocalPath str(datafiles / "pluginA_1.1.0.zip"), ], ) - manifest = json.load(manifest_file) + manifest = json_load(manifest_file) assert manifest == manifest_a2 - assert (tmpdir / "plugin-a" / "plugin-a_1.1.0.0.zip").exists() + assert (tmp_path / "plugin-a" / "plugin-a_1.1.0.0.zip").exists() cli_runner.invoke( jprm.cli, @@ -112,7 +105,7 @@ def test_repo_add(cli_runner: CliRunner, tmpdir: LocalPath, datafiles: LocalPath str(datafiles / "pluginB_1.0.0.zip"), ], ) - manifest = json.load(manifest_file) + manifest = json_load(manifest_file) assert manifest == manifest_ab - assert (tmpdir / "plugin-b" / "plugin-b_1.0.0.0.zip").exists() - assert (tmpdir / "plugin-b" / "image.png").exists() + assert (tmp_path / "plugin-b" / "plugin-b_1.0.0.0.zip").exists() + assert (tmp_path / "plugin-b" / "image.png").exists() diff --git a/tests/test_repo_add_external.py b/tests/test_repo_add_external.py index e3156d8..8014720 100644 --- a/tests/test_repo_add_external.py +++ b/tests/test_repo_add_external.py @@ -1,18 +1,16 @@ -import os -import json +from pathlib import Path import pytest from click.testing import CliRunner -from py._path.local import LocalPath from testfixtures import compare, LogCapture import jprm -from .test_repo import TEST_DATA_DIR +from .test_utils import TEST_DATA_DIR, json_load @pytest.mark.datafiles( - os.path.join(TEST_DATA_DIR, "pluginA_1.0.0.zip"), - os.path.join(TEST_DATA_DIR, "manifest_pluginA.json"), + TEST_DATA_DIR / "pluginA_1.0.0.zip", + TEST_DATA_DIR / "manifest_pluginA.json", ) @pytest.mark.parametrize( "url", @@ -27,16 +25,16 @@ def test_repo_add( url: str, cli_runner: CliRunner, - tmpdir: LocalPath, - datafiles: LocalPath, + tmp_path: Path, + datafiles: Path, ): - manifest_file = tmpdir / "repo.json" + manifest_file = tmp_path / "repo.json" result = cli_runner.invoke( jprm.cli, ["--verbosity=debug", "repo", "init", str(manifest_file)] ) assert result.exit_code == 0 - manifest_a = json.load(datafiles / "manifest_pluginA.json") + manifest_a = json_load(datafiles / "manifest_pluginA.json") manifest_a[0]["versions"][0]["sourceUrl"] = url with LogCapture("jprm") as capture: @@ -52,7 +50,7 @@ def test_repo_add( url, ], ) - manifest = json.load(manifest_file) + manifest = json_load(manifest_file) compare(manifest, manifest_a) capture.check_present( @@ -63,4 +61,4 @@ def test_repo_add( ), ) - assert not (tmpdir / "plugin-a" / "plugin-a_1.0.0.0.zip").exists() + assert not (tmp_path / "plugin-a" / "plugin-a_1.0.0.0.zip").exists() diff --git a/tests/test_repo_remove.py b/tests/test_repo_remove.py index 4b8f5b9..043437b 100644 --- a/tests/test_repo_remove.py +++ b/tests/test_repo_remove.py @@ -1,17 +1,16 @@ -import os -import json +import shutil +from pathlib import Path import pytest from click.testing import CliRunner -from py._path.local import LocalPath from testfixtures import compare import jprm -from .test_repo import TEST_DATA_DIR +from .test_utils import TEST_DATA_DIR, json_load @pytest.mark.parametrize( - "input,args,output,guid", + "input_file,args,output_file,guid", [ ( "manifest_pluginAB.json", @@ -52,20 +51,20 @@ ], ) @pytest.mark.datafiles( - os.path.join(TEST_DATA_DIR, "manifest_pluginAB.json"), - os.path.join(TEST_DATA_DIR, "manifest_pluginAB2.json"), - os.path.join(TEST_DATA_DIR, "manifest_pluginB.json"), + TEST_DATA_DIR / "manifest_pluginAB.json", + TEST_DATA_DIR / "manifest_pluginAB2.json", + TEST_DATA_DIR / "manifest_pluginB.json", ) def test_repo_remove( - input, + input_file, args, - output, + output_file, guid, cli_runner: CliRunner, - datafiles: LocalPath, + datafiles: Path, ): - manifest_file: LocalPath = datafiles / "repo.json" - (datafiles / input).copy(manifest_file) + manifest_file = datafiles / "repo.json" + shutil.copyfile(datafiles / input_file, manifest_file) result = cli_runner.invoke( jprm.cli, ["--verbosity=debug", "repo", "remove", str(manifest_file), *args] @@ -78,8 +77,7 @@ def test_repo_remove( version = jprm.Version(args[1]).full() assert f"removed {guid} {version}" in result.stdout.splitlines(False) - with open(datafiles / output) as expected_fh, open(manifest_file) as actual_fh: - compare( - expected=json.load(expected_fh), - actual=json.load(actual_fh), - ) + compare( + expected=json_load(datafiles / output_file), + actual=json_load(manifest_file), + ) diff --git a/tests/test_utils.py b/tests/test_utils.py index 2718336..7b96611 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,53 +1,55 @@ import os import sys import json +from pathlib import Path import pytest -from py._path.local import LocalPath import jprm -TEST_DATA_DIR = os.path.join( - os.path.dirname(os.path.realpath(__file__)), - "data", -) +TEST_DATA_DIR = Path(os.path.dirname(os.path.realpath(__file__))) / "data" + + +def json_load(path: Path, **kwargs): + with open(path, "rt", encoding="utf8") as handle: + return json.load(handle, **kwargs) @pytest.mark.datafiles( - os.path.join(TEST_DATA_DIR, "jprm.yaml"), - os.path.join(TEST_DATA_DIR, "jprm.json"), + TEST_DATA_DIR / "jprm.yaml", + TEST_DATA_DIR / "jprm.json", ) -def test_load_manifest(datafiles: LocalPath): - assert jprm.load_manifest(datafiles / "jprm.yaml") == json.load( +def test_load_manifest(datafiles: Path): + assert jprm.load_manifest(datafiles / "jprm.yaml") == json_load( datafiles / "jprm.json" ) @pytest.mark.datafiles( - os.path.join(TEST_DATA_DIR, "jprm.yaml"), - os.path.join(TEST_DATA_DIR, "jprm.json"), + TEST_DATA_DIR / "jprm.yaml", + TEST_DATA_DIR / "jprm.json", ) -def test_get_config(datafiles: LocalPath): - assert jprm.get_config(datafiles) == json.load(datafiles / "jprm.json") +def test_get_config(datafiles: Path): + assert jprm.get_config(datafiles) == json_load(datafiles / "jprm.json") @pytest.mark.datafiles( - os.path.join(TEST_DATA_DIR, "jprm.yaml"), - os.path.join(TEST_DATA_DIR, "jprm.json"), + TEST_DATA_DIR / "jprm.yaml", + TEST_DATA_DIR / "jprm.json", ) -def test_get_config_old(datafiles: LocalPath): +def test_get_config_old(datafiles: Path): (datafiles / "jprm.yaml").rename(datafiles / "build.yaml") - assert jprm.get_config(datafiles) == json.load(datafiles / "jprm.json") + assert jprm.get_config(datafiles) == json_load(datafiles / "jprm.json") -def test_invalid_manifest(tmpdir: LocalPath): - with open(tmpdir / "jprm.yaml", "wt") as fh: +def test_invalid_manifest(tmp_path: Path): + with open(tmp_path / "jprm.yaml", "wt", encoding="utf8") as fh: fh.write("]]]") - assert jprm.get_config(tmpdir) is None + assert jprm.get_config(tmp_path) is None -def test_no_manifest(tmpdir: LocalPath): - assert jprm.get_config(tmpdir) is None +def test_no_manifest(tmp_path: Path): + assert jprm.get_config(tmp_path) is None @pytest.mark.parametrize( @@ -65,9 +67,9 @@ def test_run_os_command(cmd, kw, res): assert jprm.run_os_command(cmd, **kw) == res -def test_run_os_command_error(tmpdir: LocalPath): +def test_run_os_command_error(tmp_path: Path): with pytest.raises(FileNotFoundError): - jprm.run_os_command("echo", cwd=tmpdir / "potatoe") + jprm.run_os_command("echo", cwd=tmp_path / "potatoe") def test_version():