aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEugene Triguba <eugenetriguba@gmail.com>2023-04-30 12:45:18 -0400
committerEugene Triguba <eugenetriguba@gmail.com>2023-04-30 12:45:18 -0400
commit409b1b2bf3043b8aca226db39505df3ba9aab937 (patch)
treef83a437faf4f16a92585685153d41aa5d356c98b
parentUse variable in `test` task (diff)
downloadpoetry-plugin-taskipy2-409b1b2bf3043b8aca226db39505df3ba9aab937.zip
Ensure test cases are testing the command output
-rw-r--r--tests/conftest.py39
-rw-r--r--tests/test_taskipy_cmd.py65
-rw-r--r--tests/utils.py15
3 files changed, 64 insertions, 55 deletions
diff --git a/tests/conftest.py b/tests/conftest.py
index d75cfc1..10ae86c 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -1,45 +1,32 @@
+import uuid
+import subprocess
from pathlib import Path
from typing import Callable
import pytest
-from cleo.io.null_io import NullIO
-from cleo.testers.command_tester import CommandTester
-from poetry.console.application import Application as PoetryApplication
from poetry.factory import Factory
from poetry.poetry import Poetry
@pytest.fixture
-def command_tester_factory() -> Callable[[str], CommandTester]:
- def _tester(command: str) -> CommandTester:
- app = PoetryApplication()
- app._load_plugins(NullIO())
- cmd = app.find(command)
- tester = CommandTester(cmd)
- return tester
-
- return _tester
-
-
-@pytest.fixture
-def execute_task(
- command_tester_factory: Callable[[str], CommandTester]
-) -> Callable[[str], CommandTester]:
+def execute_task() -> Callable[[str], subprocess.Popen]:
def _execute_task(args: str):
- tester = command_tester_factory("task")
- tester.execute(args=args)
- return tester
+ process = subprocess.Popen(
+ f"poetry task {args}",
+ stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE,
+ shell=True,
+ )
+ process.wait()
+ return process
return _execute_task
@pytest.fixture
def project_factory(tmp_path: Path):
- def _factory(
- name: str,
- pyproject_content: str,
- ) -> Poetry:
- project_dir = tmp_path / f"poetry-fixture-{name}"
+ def _factory(pyproject_content: str) -> Poetry:
+ project_dir = tmp_path / f"poetry-fixture-{uuid.uuid4()}"
project_dir.mkdir(parents=True, exist_ok=True)
with project_dir.joinpath("pyproject.toml").open("w", encoding="utf-8") as f:
f.write(pyproject_content)
diff --git a/tests/test_taskipy_cmd.py b/tests/test_taskipy_cmd.py
index 6359dd5..23bf0de 100644
--- a/tests/test_taskipy_cmd.py
+++ b/tests/test_taskipy_cmd.py
@@ -3,20 +3,7 @@ This test suite is a basic set of sanity tests to ensure that the integration
of the taskipy command with poetry is working as expected. It is not intended
to be an exhaustive test suite for the taskipy CLI itself.
"""
-import contextlib
-import os
-import pathlib
-from typing import Iterator
-
-
-@contextlib.contextmanager
-def as_cwd(path: pathlib.Path) -> Iterator[pathlib.Path]:
- old_cwd = os.getcwd()
- os.chdir(path)
- try:
- yield path
- finally:
- os.chdir(old_cwd)
+from tests.utils import as_cwd
PYPROJECT_CONTENT = """\
@@ -32,37 +19,49 @@ test_echo = "echo 'test'"
def test_taskipy_known_task(execute_task, project_factory):
- poetry = project_factory(name="task", pyproject_content=PYPROJECT_CONTENT)
+ poetry = project_factory(PYPROJECT_CONTENT)
with as_cwd(poetry.file.path.parent):
- result = execute_task("test_echo")
- assert result.status_code == 0
+ process = execute_task("test_echo")
+
+ assert process.returncode == 0
+ assert process.stdout.read() == b"test\n"
+ assert process.stderr.read() == b""
def test_taskipy_known_task_with_args_passed_to_subcommand(
execute_task, project_factory
):
- poetry = project_factory(name="task", pyproject_content=PYPROJECT_CONTENT)
+ poetry = project_factory(PYPROJECT_CONTENT)
with as_cwd(poetry.file.path.parent):
- result = execute_task("test_echo test2 test3 test4")
- assert result.status_code == 0
+ process = execute_task("test_echo test2 test3 test4")
+
+ assert process.returncode == 0
+ assert process.stdout.read() == b"test test2 test3 test4\n"
+ assert process.stderr.read() == b""
def test_taskipy_unknown_task(execute_task, project_factory):
- poetry = project_factory(name="task", pyproject_content=PYPROJECT_CONTENT)
+ poetry = project_factory(PYPROJECT_CONTENT)
with as_cwd(poetry.file.path.parent):
- result = execute_task("unknown")
- assert result.status_code == 127
+ process = execute_task("unknown")
+
+ assert process.returncode == 127
+ assert process.stdout.read() == b'could not find task "unknown"\n'
+ assert process.stderr.read() == b""
def test_taskipy_list(execute_task, project_factory):
- poetry = project_factory(name="task", pyproject_content=PYPROJECT_CONTENT)
+ poetry = project_factory(PYPROJECT_CONTENT)
with as_cwd(poetry.file.path.parent):
- result = execute_task("--list")
- assert result.status_code == 0
+ process = execute_task("--list")
+
+ assert process.returncode == 0
+ assert process.stdout.read() == b"test_echo echo 'test'\n"
+ assert process.stderr.read() == b""
def test_utilizing_global_poetry_cwd_flag(execute_task, project_factory):
@@ -71,8 +70,16 @@ def test_utilizing_global_poetry_cwd_flag(execute_task, project_factory):
current working directory, we should now get an error because taskipy
can't find a pyproject.toml file.
"""
- poetry = project_factory(name="task", pyproject_content=PYPROJECT_CONTENT)
+ poetry = project_factory(PYPROJECT_CONTENT)
with as_cwd(poetry.file.path.parent):
- result = execute_task(f"--directory {poetry.file.path.parent.parent} test_echo")
- assert result.status_code == 1
+ process = execute_task(
+ f"--directory {poetry.file.path.parent.parent} test_echo"
+ )
+
+ assert process.returncode == 1
+ assert (
+ process.stdout.read()
+ == b"no pyproject.toml file found in this directory or parent directories\n"
+ )
+ assert process.stderr.read() == b""
diff --git a/tests/utils.py b/tests/utils.py
new file mode 100644
index 0000000..202cea5
--- /dev/null
+++ b/tests/utils.py
@@ -0,0 +1,15 @@
+import contextlib
+import os
+import io
+import pathlib
+from typing import Iterator
+
+
+@contextlib.contextmanager
+def as_cwd(path: pathlib.Path) -> Iterator[pathlib.Path]:
+ old_cwd = os.getcwd()
+ os.chdir(path)
+ try:
+ yield path
+ finally:
+ os.chdir(old_cwd)