Skip to content

Commit 2f21e8c

Browse files
committed
Fix tests on Windows.
1 parent c888661 commit 2f21e8c

6 files changed

Lines changed: 43 additions & 23 deletions

File tree

src/_pytask/build.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ def build( # noqa: C901, PLR0912, PLR0913
275275
session.exit_code = ExitCode.FAILED
276276

277277
except Exception: # noqa: BLE001
278-
console.print(Traceback(sys.exc_info()))
278+
console.print(Traceback(sys.exc_info(), show_locals=True))
279279
session.exit_code = ExitCode.FAILED
280280

281281
session.hook.pytask_unconfigure(session=session)

tests/conftest.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,22 @@ class Result(NamedTuple):
121121
stderr: str
122122

123123

124-
def run_in_subprocess(
125-
cmd: tuple[str, ...], cwd: Path | None = None, env: dict[str, str] | None = None
126-
) -> Result:
124+
def run_in_subprocess(cmd: tuple[str, ...], cwd: Path | None = None) -> Result:
127125
"""Run a command in a subprocess and return the output."""
128-
result = subprocess.run(cmd, cwd=cwd, check=False, capture_output=True, env=env)
126+
kwargs = (
127+
{
128+
"env": os.environ | {"PYTHONIOENCODING": "utf-8", "TERM": "unknown"},
129+
"encoding": "utf-8",
130+
}
131+
if sys.platform == "win32"
132+
else {}
133+
)
134+
135+
result = subprocess.run(
136+
cmd, cwd=cwd, check=False, capture_output=True, text=True, **kwargs
137+
)
129138
return Result(
130-
exit_code=result.returncode,
131-
stdout=result.stdout.decode("utf-8", "replace").replace("\r\n", "\n"),
132-
stderr=result.stderr.decode("utf-8", "replace").replace("\r\n", "\n"),
139+
exit_code=result.returncode, stdout=result.stdout, stderr=result.stderr
133140
)
134141

135142

tests/test_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def test_paths_are_relative_to_configuration_file(tmp_path):
120120
session = build(paths=[Path("src")])
121121
"""
122122
tmp_path.joinpath("script.py").write_text(textwrap.dedent(source))
123-
result = run_in_subprocess(("python", "script.py"), cwd=tmp_path)
123+
result = run_in_subprocess(("uv", "run", "python", "script.py"), cwd=tmp_path)
124124
assert result.exit_code == ExitCode.OK
125125
assert "1 Succeeded" in result.stdout
126126

tests/test_execute.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@
2020
from pytask import build
2121
from pytask import cli
2222
from tests.conftest import enter_directory
23+
from tests.conftest import run_in_subprocess
2324

2425

25-
@pytest.mark.xfail(sys.platform == "win32", reason="See #293.")
2626
@pytest.mark.end_to_end
2727
def test_python_m_pytask(tmp_path):
2828
tmp_path.joinpath("task_module.py").write_text("def task_example(): pass")
29-
subprocess.run(["python", "-m", "pytask", tmp_path.as_posix()], check=False)
29+
result = run_in_subprocess(
30+
("uv", "run", "python", "-m", "pytask", tmp_path.as_posix())
31+
)
32+
assert result.exit_code == ExitCode.OK
3033

3134

3235
@pytest.mark.end_to_end
@@ -657,10 +660,10 @@ def task2() -> None: pass
657660
sys.exit(session2.exit_code)
658661
"""
659662
tmp_path.joinpath("task_module.py").write_text(textwrap.dedent(source))
660-
result = subprocess.run(
661-
("python", tmp_path.joinpath("task_module.py").as_posix()), check=False
663+
result = run_in_subprocess(
664+
("uv", "run", "python", tmp_path.joinpath("task_module.py").as_posix())
662665
)
663-
assert result.returncode == ExitCode.OK
666+
assert result.exit_code == ExitCode.OK
664667

665668

666669
@pytest.mark.end_to_end
@@ -765,14 +768,14 @@ def task_example() -> Annotated[str, Path("file.txt")]:
765768
"""
766769
tmp_path.joinpath("task_example.py").write_text(textwrap.dedent(source))
767770

768-
result = subprocess.run(("pytask"), cwd=tmp_path) # noqa: PLW1510
769-
assert result.returncode == ExitCode.OK
771+
result = run_in_subprocess(("pytask",), cwd=tmp_path)
772+
assert result.exit_code == ExitCode.OK
770773

771774
hashes = json.loads(tmp_path.joinpath(".pytask", "file_hashes.json").read_text())
772775
assert len(hashes) == 2
773776

774-
result = subprocess.run(("pytask"), cwd=tmp_path) # noqa: PLW1510
775-
assert result.returncode == ExitCode.OK
777+
result = run_in_subprocess(("pytask",), cwd=tmp_path)
778+
assert result.exit_code == ExitCode.OK
776779

777780
hashes_ = json.loads(tmp_path.joinpath(".pytask", "file_hashes.json").read_text())
778781
assert hashes == hashes_

tests/test_hook_module.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import os
4+
from pathlib import Path
45
import subprocess
56
import sys
67
import textwrap
@@ -41,6 +42,8 @@ def pytask_extend_command_line_interface(cli):
4142

4243
if module_name:
4344
args = (
45+
"uv",
46+
"run",
4447
"python",
4548
"-m",
4649
"pytask",
@@ -50,7 +53,15 @@ def pytask_extend_command_line_interface(cli):
5053
"--help",
5154
)
5255
else:
53-
args = ("pytask", "build", "--hook-module", "hooks/hooks.py", "--help")
56+
args = (
57+
"uv",
58+
"run",
59+
"pytask",
60+
"build",
61+
"--hook-module",
62+
"hooks/hooks.py",
63+
"--help",
64+
)
5465

5566
result = run_in_subprocess(args, cwd=tmp_path)
5667
assert result.exit_code == ExitCode.OK
@@ -89,9 +100,9 @@ def pytask_extend_command_line_interface(cli):
89100
tmp_path.joinpath("hooks", "hooks.py").write_text(textwrap.dedent(hooks))
90101

91102
if module_name:
92-
args = ("python", "-m", "pytask", "build", "--help")
103+
args = ("uv", "run", "--no-project", "python", "-m", "pytask", "build", "--help")
93104
else:
94-
args = ("pytask", "build", "--help")
105+
args = ("uv", "run", "--no-project", "pytask", "build", "--help")
95106

96107
result = run_in_subprocess(args, cwd=tmp_path)
97108
assert result.exit_code == ExitCode.OK

tests/test_warnings.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ def task_example():
127127

128128

129129
@pytest.mark.end_to_end
130-
@pytest.mark.xfail(sys.platform == "win32", reason="See #293.")
131130
@pytest.mark.parametrize("warning", ["DeprecationWarning", "PendingDeprecationWarning"])
132131
def test_deprecation_warnings_are_not_captured(tmp_path, warning):
133132
path_to_warn_module = tmp_path.joinpath("warning.py")
@@ -156,7 +155,7 @@ def warn_now():
156155
path_to_warn_module.write_text(textwrap.dedent(warn_module))
157156

158157
# Cannot use runner since then warnings are not ignored by default.
159-
result = run_in_subprocess(("pytask"), cwd=tmp_path)
158+
result = run_in_subprocess(("uv", "run", "pytask"), cwd=tmp_path)
160159
assert result.exit_code == ExitCode.OK
161160
assert "Warnings" not in result.stdout
162161
assert "warning!!!" not in result.stdout

0 commit comments

Comments
 (0)