Skip to content

Commit

Permalink
Fix issues causing tests to fail n CI on windows
Browse files Browse the repository at this point in the history
- Fix poetry plugin tests with latest poetry version
- Remove new lines from args in script tasks
- Also look for .com and .bat files in venv bin dir on windows
- Skip unncessary poetry configuration step in plugin tests
  • Loading branch information
nat-n committed Oct 8, 2022
1 parent 87137fb commit b3490be
Show file tree
Hide file tree
Showing 17 changed files with 111 additions and 40 deletions.
3 changes: 1 addition & 2 deletions poethepoet/app.py
Expand Up @@ -90,12 +90,11 @@ def resolve_task(self) -> bool:
return True

def run_task(self, context: Optional[RunContext] = None) -> Optional[int]:
_, *extra_args = self.ui["task"]
if context is None:
context = self.get_run_context()
try:
assert self.task
return self.task.run(context=context, extra_args=extra_args)
return self.task.run(context=context, extra_args=self.ui["task"][1:])
except PoeException as error:
self.print_help(error=error)
return 1
Expand Down
10 changes: 8 additions & 2 deletions poethepoet/task/script.py
@@ -1,4 +1,5 @@
import ast
import re
from typing import (
Any,
Dict,
Expand Down Expand Up @@ -104,7 +105,12 @@ def parse_script_content(self, args: Optional[Dict[str, Any]]) -> Tuple[str, str
return target_module, f"{target_ref}(**({args}))"
return target_module, f"{target_ref}()"

return target_module, resolve_function_call(target_ref, set(args or tuple()))
function_call = resolve_function_call(target_ref, set(args or tuple()))
# Strip out any new lines because they can be problematic on windows
function_call = re.sub(r"((\r\n|\r|\n) | (\r\n|\r|\n))", " ", function_call)
function_call = re.sub(r"(\r\n|\r|\n)", " ", function_call)

return target_module, function_call

@staticmethod
def format_args_class(
Expand All @@ -117,7 +123,7 @@ def format_args_class(
if named_args is None:
return ""
return (
f'__args=type("{classname}",(object,),'
f'{classname}=type("{classname}",(object,),'
"{"
+ ",".join(f"{name!r}:{value!r}" for name, value in named_args.items())
+ "});"
Expand Down
13 changes: 7 additions & 6 deletions poethepoet/virtualenv.py
Expand Up @@ -30,12 +30,13 @@ def resolve_executable(self, executable: str) -> str:
bin_dir = self.bin_dir()
if bin_dir.joinpath(executable).is_file():
return str(bin_dir.joinpath(executable))
if (
self._is_windows
and not executable.endswith(".exe")
and bin_dir.joinpath(f"{executable}.exe").is_file()
):
return str(bin_dir.joinpath(f"{executable}.exe"))
if self._is_windows:
if bin_dir.joinpath(f"{executable}.com").is_file():
return str(bin_dir.joinpath(f"{executable}.com"))
if bin_dir.joinpath(f"{executable}.exe").is_file():
return str(bin_dir.joinpath(f"{executable}.exe"))
if bin_dir.joinpath(f"{executable}.bat").is_file():
return str(bin_dir.joinpath(f"{executable}.bat"))
return executable

@staticmethod
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Expand Up @@ -55,6 +55,7 @@ poethepoet = "poethepoet.plugin:PoetryPlugin"
htmlcov
./tests/fixtures/simple_project/venv
./tests/fixtures/venv_project/myvenv
./tests/fixtures/poetry_plugin_project/**/.venv
./tests/temp
"""

Expand Down
10 changes: 5 additions & 5 deletions tests/conftest.py
Expand Up @@ -207,15 +207,17 @@ def run_poetry(use_venv, poe_project_path):

def run_poetry(args: List[str], cwd: str, env: Optional[Dict[str, str]] = None):
venv = Virtualenv(venv_location)

cmd = (venv.resolve_executable("python"), "-m", "poetry", *args)
print("Poetry cmd:", cmd[0])
poetry_proc = Popen(
(venv.resolve_executable("poetry"), *args),
cmd,
env=venv.get_env_vars({**os.environ, **(env or {})}),
stdout=PIPE,
stderr=PIPE,
cwd=cwd,
)
poetry_out, poetry_err = poetry_proc.communicate()

result = PoeRunResult(
code=poetry_proc.returncode,
path=cwd,
Expand All @@ -230,9 +232,7 @@ def run_poetry(args: List[str], cwd: str, env: Optional[Dict[str, str]] = None):
venv_location,
[
".[poetry_plugin]",
# TODO: poetry install from a release
"./tests/fixtures/packages/poetry-1.2.0b2.dev0-py3-none-any.whl",
"--pre",
"./tests/fixtures/packages/poetry-1.2.1-py3-none-any.whl",
],
require_empty=True,
):
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
35 changes: 35 additions & 0 deletions tests/fixtures/poetry_plugin_project/empty_prefix/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions tests/fixtures/poetry_plugin_project/empty_prefix/poetry.toml
@@ -0,0 +1,2 @@
[virtualenvs]
in-project = true
Expand Up @@ -8,14 +8,15 @@ authors = ["Nat Noordanus <n@natn.me>"]
python = "^3.7"

[tool.poetry.dev-dependencies]
cowsay = "../../packages/python_cowsay-1.0.1-py3-none-any.whl"
cowpy = { path = "../../packages/cowpy-1.1.5-py3-none-any.whl" }
poe_test_helpers = { path = "../../packages/poe_test_helpers" }

[tool.poe]
poetry_command = ""

[tool.poe.tasks]
echo = { cmd = "poe_test_echo", help = "It's like echo"}
cow-greet = "cowsay 'good day sir!'"
cow-greet = "cowpy 'good day sir!'"

[build-system]
requires = ["poetry-core"]
Expand Down
33 changes: 25 additions & 8 deletions tests/fixtures/poetry_plugin_project/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions tests/fixtures/poetry_plugin_project/pyproject.toml
Expand Up @@ -8,16 +8,17 @@ authors = ["Nat Noordanus <n@natn.me>"]
python = "^3.7"

[tool.poetry.dev-dependencies]
cowsay = "../packages/python_cowsay-1.0.1-py3-none-any.whl"
cowpy = { path = "../packages/cowpy-1.1.5-py3-none-any.whl" }
poe_test_helpers = { path = "../packages/poe_test_helpers" }

[tool.poe.poetry_hooks]
pre_env_info = "echo 'THIS IS YOUR ENV!'"
post_env_info = "echo 'THAT WAS YOUR ENV!'"

[tool.poe.tasks]
echo = { cmd = "poe_test_echo", help = "It's like echo"}
cow-greet = "cowsay 'good day sir!'"
cow-cheese.shell = "import cowsay; print(list(cowsay.char_names)[1])"
cow-greet = "cowpy" # Passing args to cowpy doesn't work with subprocess
cow-cheese.shell = "from cowpy import cow; print(list(cow.COWACTERS)[5])"
cow-cheese.interpreter = "python"

[build-system]
Expand Down
32 changes: 20 additions & 12 deletions tests/test_poetry_plugin.py
Expand Up @@ -5,13 +5,14 @@

@pytest.fixture(scope="session")
def setup_poetry_project(run_poetry, projects):
run_poetry(
["config", "--local", "virtualenvs.in-project", "true"],
cwd=projects["poetry_plugin"],
)
run_poetry(["install"], cwd=projects["poetry_plugin"])


@pytest.fixture(scope="session")
def setup_poetry_project_empty_prefix(run_poetry, projects):
run_poetry(["install"], cwd=projects["poetry_plugin/empty_prefix"].parent)


@pytest.mark.slow
@pytest.mark.skipif(version_info < (3, 7), reason="dependencies require python>=3.7")
def test_poetry_help(run_poetry, projects):
Expand All @@ -27,13 +28,20 @@ def test_poetry_help(run_poetry, projects):
def test_task_with_cli_dependency(
run_poetry, projects, setup_poetry_project, is_windows
):
result = run_poetry(["poe", "cow-greet"], cwd=projects["poetry_plugin"])
result = run_poetry(
["poe", "cow-greet", "yo yo yo"],
cwd=projects["poetry_plugin"],
)
if is_windows:
assert result.stdout.startswith("Poe => cowsay 'good day sir!'")
assert "| 'good day sir!' |" in result.stdout
assert result.stdout.startswith("Poe => cowpy yo yo yo")
assert "< yo yo yo >" in result.stdout
else:
assert result.stdout.startswith("Poe => cowsay good day sir!")
assert "| good day sir! |" in result.stdout
# On POSIX cowpy expects notices its being called as a subprocess and tries
# unproductively to take input from stdin
assert result.stdout.startswith("Poe => cowpy yo yo yo")
assert (
"< Cowacter, eyes:default, tongue:False, thoughts:False >" in result.stdout
)
# assert result.stderr == ""


Expand All @@ -44,7 +52,7 @@ def test_task_with_lib_dependency(
):
result = run_poetry(["poe", "cow-cheese"], cwd=projects["poetry_plugin"])
assert result.stdout == (
"Poe => import cowsay; print(list(cowsay.char_names)[1])\ncheese\n"
"Poe => from cowpy import cow; print(list(cow.COWACTERS)[5])\ncheese\n"
)
# assert result.stderr == ""

Expand All @@ -65,7 +73,7 @@ def test_task_accepts_any_args(run_poetry, projects, setup_poetry_project):
@pytest.mark.slow
@pytest.mark.skipif(version_info < (3, 7), reason="dependencies require python>=3.7")
def test_poetry_help_without_poe_command_prefix(
run_poetry, projects, setup_poetry_project
run_poetry, projects, setup_poetry_project_empty_prefix
):
result = run_poetry([], cwd=projects["poetry_plugin/empty_prefix"].parent)
assert result.stdout.startswith("Poetry (version ")
Expand All @@ -77,7 +85,7 @@ def test_poetry_help_without_poe_command_prefix(
@pytest.mark.slow
@pytest.mark.skipif(version_info < (3, 7), reason="dependencies require python>=3.7")
def test_running_tasks_without_poe_command_prefix(
run_poetry, projects, setup_poetry_project
run_poetry, projects, setup_poetry_project_empty_prefix
):
result = run_poetry(
["echo", "--lol=:D", "--version", "--help"],
Expand Down

0 comments on commit b3490be

Please sign in to comment.