Skip to content

Commit

Permalink
Merge pull request #149 from oddstr13/pr-tests-pathlib-1
Browse files Browse the repository at this point in the history
Replace py.path LocalPath with pathlib.Path
  • Loading branch information
oddstr13 authored Oct 3, 2023
2 parents eabb903 + e55553d commit fd1863e
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 112 deletions.
39 changes: 18 additions & 21 deletions tests/test_plugin.py
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
67 changes: 30 additions & 37 deletions tests/test_repo.py
Original file line number Diff line number Diff line change
@@ -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)]
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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()
22 changes: 10 additions & 12 deletions tests/test_repo_add_external.py
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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:
Expand All @@ -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(
Expand All @@ -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()
34 changes: 16 additions & 18 deletions tests/test_repo_remove.py
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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]
Expand All @@ -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),
)
Loading

0 comments on commit fd1863e

Please sign in to comment.