Skip to content

Commit ec96e4e

Browse files
simonwclaude
andcommitted
Fix tests and publish temp directory cleanup on Windows
datasette publish/package deleted their temporary directory while it was still the working directory, which Windows refuses. Test fixes cover backslashes in generated plugin source, CRLF line endings, a hard-coded /tmp path, and signal tests Windows cannot run. Refs #1627 Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
1 parent 3eba8fe commit ec96e4e

7 files changed

Lines changed: 100 additions & 91 deletions

File tree

‎datasette/publish/heroku.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,5 +250,6 @@ def temporary_heroku_directory(
250250
yield
251251

252252
finally:
253-
tmp.cleanup()
253+
# Windows cannot delete the current working directory, so leave it first
254254
os.chdir(saved_cwd)
255+
tmp.cleanup()

‎datasette/utils/__init__.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -806,8 +806,9 @@ def temporary_docker_directory(
806806
)
807807
yield datasette_dir
808808
finally:
809-
tmp.cleanup()
809+
# Windows cannot delete the current working directory, so leave it first
810810
os.chdir(saved_cwd)
811+
tmp.cleanup()
811812

812813

813814
def detect_primary_keys(conn, table):

‎tests/test_cli.py‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,12 @@ def test_serve_with_inspect_file_prepopulates_table_counts_cache():
8181
assert db.cached_table_counts == {"hithere": 44}
8282

8383

84-
@pytest.mark.parametrize(
85-
"spatialite_paths,should_suggest_load_extension",
86-
(
87-
([], False),
88-
(["/tmp"], True),
89-
),
90-
)
84+
@pytest.mark.parametrize("should_suggest_load_extension", (False, True))
9185
def test_spatialite_error_if_attempt_to_open_spatialite(
92-
spatialite_paths, should_suggest_load_extension
86+
should_suggest_load_extension, tmp_path
9387
):
88+
# find_spatialite() only checks that one of these paths exists
89+
spatialite_paths = [str(tmp_path)] if should_suggest_load_extension else []
9490
with mock.patch("datasette.utils.SPATIALITE_PATHS", spatialite_paths):
9591
runner = CliRunner()
9692
result = runner.invoke(

‎tests/test_cli_serve_get.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def test_serve_with_get(tmp_path_factory):
1616
1717
@hookimpl
1818
def startup(datasette):
19-
with open("{}", "w") as fp:
19+
with open({!r}, "w") as fp:
2020
fp.write("hello")
2121
""".format(str(plugins_dir / "hello.txt")),
2222
),
@@ -70,7 +70,7 @@ def test_serve_with_get_does_not_launch_background_tasks(tmp_path_factory):
7070
def startup(datasette):
7171
async def inner():
7272
async def task(datasette):
73-
with open("{sentinel!s}", "w") as fp:
73+
with open({str(sentinel)!r}, "w") as fp:
7474
fp.write("ran")
7575
7676
datasette.add_background_task(task, name="get-sentinel-task")

‎tests/test_cli_serve_server.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import signal
22
import socket
33
import subprocess
4+
import sys
45
import time
56

67
import httpx2
@@ -179,7 +180,17 @@ def _start_serve_with_shutdown_sentinel(serve_with_plugins, tmp_path):
179180
return proc, sentinel_path
180181

181182

183+
# On Windows send_signal(SIGTERM) calls TerminateProcess(), which kills the
184+
# server without giving it a chance to run anything, and send_signal() does
185+
# not support SIGINT at all
186+
skip_on_windows = pytest.mark.skipif(
187+
sys.platform == "win32",
188+
reason="Windows cannot deliver SIGTERM or SIGINT to a subprocess",
189+
)
190+
191+
182192
@pytest.mark.serial
193+
@skip_on_windows
183194
def test_sigterm_runs_shutdown_hooks(serve_with_plugins, tmp_path):
184195
ds_proc, sentinel_path = _start_serve_with_shutdown_sentinel(
185196
serve_with_plugins, tmp_path
@@ -206,6 +217,7 @@ def test_sigterm_runs_shutdown_hooks(serve_with_plugins, tmp_path):
206217
@pytest.mark.skipif(
207218
not hasattr(signal, "SIGINT"), reason="Requires signal.SIGINT support"
208219
)
220+
@skip_on_windows
209221
def test_sigint_runs_shutdown_hooks(serve_with_plugins, tmp_path):
210222
ds_proc, sentinel_path = _start_serve_with_shutdown_sentinel(
211223
serve_with_plugins, tmp_path

‎tests/test_config_dir.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ def config_dir(tmp_path_factory):
4545

4646
static_dir = config_dir / "static"
4747
static_dir.mkdir()
48-
(static_dir / "hello.css").write_text(CSS, "utf-8")
48+
# newline="\n" stops Windows from writing \r\n line endings
49+
(static_dir / "hello.css").write_text(CSS, "utf-8", newline="\n")
4950

5051
(config_dir / "metadata.json").write_text(json.dumps(METADATA), "utf-8")
5152
(config_dir / "datasette.json").write_text(json.dumps(CONFIG), "utf-8")

‎tests/test_utils.py‎

Lines changed: 76 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import json
77
import os
88
import pathlib
9-
import tempfile
109
from unittest.mock import patch
1110

1211
import pytest
@@ -422,88 +421,87 @@ def test_to_css_class(s, expected):
422421
assert utils.to_css_class(s) == expected
423422

424423

425-
def test_temporary_docker_directory_uses_hard_link():
426-
with tempfile.TemporaryDirectory() as td:
427-
os.chdir(td)
428-
with open("hello", "w") as fp:
429-
fp.write("world")
430-
# Default usage of this should use symlink
431-
with utils.temporary_docker_directory(
432-
files=["hello"],
433-
name="t",
434-
metadata=None,
435-
extra_options=None,
436-
branch=None,
437-
template_dir=None,
438-
plugins_dir=None,
439-
static=[],
440-
install=[],
441-
spatialite=False,
442-
version_note=None,
443-
secret="secret",
444-
) as temp_docker:
445-
hello = os.path.join(temp_docker, "hello")
446-
with open(hello) as fp:
447-
assert fp.read() == "world"
448-
# It should be a hard link
449-
assert os.stat(hello).st_nlink == 2
424+
def test_temporary_docker_directory_uses_hard_link(tmp_path, monkeypatch):
425+
monkeypatch.chdir(tmp_path)
426+
with open("hello", "w") as fp:
427+
fp.write("world")
428+
# Default usage of this should use symlink
429+
with utils.temporary_docker_directory(
430+
files=["hello"],
431+
name="t",
432+
metadata=None,
433+
extra_options=None,
434+
branch=None,
435+
template_dir=None,
436+
plugins_dir=None,
437+
static=[],
438+
install=[],
439+
spatialite=False,
440+
version_note=None,
441+
secret="secret",
442+
) as temp_docker:
443+
hello = os.path.join(temp_docker, "hello")
444+
with open(hello) as fp:
445+
assert fp.read() == "world"
446+
# It should be a hard link
447+
assert os.stat(hello).st_nlink == 2
450448

451449

452450
@patch("os.link")
453-
def test_temporary_docker_directory_uses_copy_if_hard_link_fails(mock_link):
451+
def test_temporary_docker_directory_uses_copy_if_hard_link_fails(
452+
mock_link, tmp_path, monkeypatch
453+
):
454454
# Copy instead if os.link raises OSError (normally due to different device)
455455
mock_link.side_effect = OSError
456-
with tempfile.TemporaryDirectory() as td:
457-
os.chdir(td)
458-
with open("hello", "w") as fp:
459-
fp.write("world")
460-
# Default usage of this should use symlink
461-
with utils.temporary_docker_directory(
462-
files=["hello"],
463-
name="t",
464-
metadata=None,
465-
extra_options=None,
466-
branch=None,
467-
template_dir=None,
468-
plugins_dir=None,
469-
static=[],
470-
install=[],
471-
spatialite=False,
472-
version_note=None,
473-
secret=None,
474-
) as temp_docker:
475-
hello = os.path.join(temp_docker, "hello")
476-
with open(hello) as fp:
477-
assert fp.read() == "world"
478-
# It should be a copy, not a hard link
479-
assert os.stat(hello).st_nlink == 1
480-
481-
482-
def test_temporary_docker_directory_quotes_args():
483-
with tempfile.TemporaryDirectory() as td:
484-
os.chdir(td)
485-
with open("hello", "w") as fp:
486-
fp.write("world")
487-
with utils.temporary_docker_directory(
488-
files=["hello"],
489-
name="t",
490-
metadata=None,
491-
extra_options="--$HOME",
492-
branch=None,
493-
template_dir=None,
494-
plugins_dir=None,
495-
static=[],
496-
install=[],
497-
spatialite=False,
498-
version_note="$PWD",
499-
secret="secret",
500-
) as temp_docker:
501-
df = os.path.join(temp_docker, "Dockerfile")
502-
with open(df) as fp:
503-
df_contents = fp.read()
504-
assert "'$PWD'" in df_contents
505-
assert "'--$HOME'" in df_contents
506-
assert "ENV DATASETTE_SECRET 'secret'" in df_contents
456+
monkeypatch.chdir(tmp_path)
457+
with open("hello", "w") as fp:
458+
fp.write("world")
459+
# Default usage of this should use symlink
460+
with utils.temporary_docker_directory(
461+
files=["hello"],
462+
name="t",
463+
metadata=None,
464+
extra_options=None,
465+
branch=None,
466+
template_dir=None,
467+
plugins_dir=None,
468+
static=[],
469+
install=[],
470+
spatialite=False,
471+
version_note=None,
472+
secret=None,
473+
) as temp_docker:
474+
hello = os.path.join(temp_docker, "hello")
475+
with open(hello) as fp:
476+
assert fp.read() == "world"
477+
# It should be a copy, not a hard link
478+
assert os.stat(hello).st_nlink == 1
479+
480+
481+
def test_temporary_docker_directory_quotes_args(tmp_path, monkeypatch):
482+
monkeypatch.chdir(tmp_path)
483+
with open("hello", "w") as fp:
484+
fp.write("world")
485+
with utils.temporary_docker_directory(
486+
files=["hello"],
487+
name="t",
488+
metadata=None,
489+
extra_options="--$HOME",
490+
branch=None,
491+
template_dir=None,
492+
plugins_dir=None,
493+
static=[],
494+
install=[],
495+
spatialite=False,
496+
version_note="$PWD",
497+
secret="secret",
498+
) as temp_docker:
499+
df = os.path.join(temp_docker, "Dockerfile")
500+
with open(df) as fp:
501+
df_contents = fp.read()
502+
assert "'$PWD'" in df_contents
503+
assert "'--$HOME'" in df_contents
504+
assert "ENV DATASETTE_SECRET 'secret'" in df_contents
507505

508506

509507
def test_compound_keys_after_sql():

0 commit comments

Comments
 (0)