Skip to content

A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.

Tools tests: complete on Windows (localhost hang, fcntl collection) (#1350)

Eyal Golanb756c21087ed
diff --git a/tools/tests/test_machine_lock.py b/tools/tests/test_machine_lock.py
index 567599286..3e5fbfd3e 100644
--- a/tools/tests/test_machine_lock.py
+++ b/tools/tests/test_machine_lock.py
@@ -1,4 +1,3 @@
-import fcntl
import os
import subprocess
import sys
@@ -7,7 +6,10 @@ from pathlib import Path
import pytest
-import machine_lock as ml
+# machine_lock is the Mac's flock-based lock; fcntl does not exist on Windows, so the module skips there.
+fcntl = pytest.importorskip("fcntl", reason="fcntl is POSIX-only (machine_lock uses flock); not available on Windows")
+
+import machine_lock as ml # noqa: E402
def test_the_command_follows_the_double_dash_and_a_mode_is_required():
diff --git a/tools/tests/test_unity_postmerge.py b/tools/tests/test_unity_postmerge.py
index 7cae46b28..65d1a64f9 100644
--- a/tools/tests/test_unity_postmerge.py
+++ b/tools/tests/test_unity_postmerge.py
@@ -1,4 +1,3 @@
-import fcntl
import json
import os
import plistlib
@@ -10,7 +9,10 @@ from pathlib import Path
import pytest
-import unity_postmerge as up
+# unity_postmerge is Mac tooling that locks with flock; fcntl does not exist on Windows, so the module skips there.
+fcntl = pytest.importorskip("fcntl", reason="fcntl is POSIX-only (unity_postmerge uses flock); not on Windows")
+
+import unity_postmerge as up # noqa: E402
SHA = "0123456789abcdef0123456789abcdef01234567"
PREV = "fedcba9876543210fedcba9876543210fedcba98"
diff --git a/tools/tests/test_web_check.py b/tools/tests/test_web_check.py
index e29c139fe..a28d2c4f6 100644
--- a/tools/tests/test_web_check.py
+++ b/tools/tests/test_web_check.py
@@ -12,6 +12,15 @@ import pytest
import web_check as wc
+# A client connects to the address serve() bound (127.0.0.1), not "localhost": Windows resolves localhost to ::1 first,
+# where nothing listens, and with no timeout the connect waits with no end. The timeout bounds every request.
+TIMEOUT = 10
+
+
+def base_url(server) -> str:
+ host, port = server.server_address[:2]
+ return f"http://{host}:{port}"
+
@pytest.mark.parametrize("error", [BrokenPipeError, ConnectionResetError])
def test_server_client_disconnects_do_not_print_a_false_failure(error, capsys):
@@ -56,12 +65,12 @@ def test_precompressed_files_get_content_encoding_and_wasm_its_type(tmp_path):
(tmp_path / "index.html").write_text("<html></html>")
server = wc.serve(tmp_path)
try:
- base = f"http://localhost:{server.server_address[1]}"
- r = urllib.request.urlopen(base + "/Build/game.wasm.br")
+ base = base_url(server)
+ r = urllib.request.urlopen(base + "/Build/game.wasm.br", timeout=TIMEOUT)
assert (r.headers["Content-Encoding"], r.headers["Content-Type"]) == ("br", "application/wasm")
r = urllib.request.Request(base + "/Build/game.data.gz", headers={"Accept-Encoding": "identity"})
- assert urllib.request.urlopen(r).headers["Content-Encoding"] == "gzip"
- assert urllib.request.urlopen(base + "/index.html").headers["Content-Encoding"] is None
+ assert urllib.request.urlopen(r, timeout=TIMEOUT).headers["Content-Encoding"] == "gzip"
+ assert urllib.request.urlopen(base + "/index.html", timeout=TIMEOUT).headers["Content-Encoding"] is None
finally:
server.shutdown()
server.server_close()
@@ -135,10 +144,12 @@ def test_compressed_scripts_and_data_keep_their_types_and_traversal_is_404(tmp_p
(tmp_path / "secret.txt").write_text("secret")
server = wc.serve(tmp_path / "site")
try:
- base = f"http://localhost:{server.server_address[1]}"
- assert urllib.request.urlopen(base + "/a.framework.js.gz").headers["Content-Type"] == "application/javascript"
- assert urllib.request.urlopen(base + "/a.data.br").headers["Content-Type"] == "application/octet-stream"
- conn = http.client.HTTPConnection("localhost", server.server_address[1])
+ base = base_url(server)
+ js = urllib.request.urlopen(base + "/a.framework.js.gz", timeout=TIMEOUT)
+ assert js.headers["Content-Type"] == "application/javascript"
+ data = urllib.request.urlopen(base + "/a.data.br", timeout=TIMEOUT)
+ assert data.headers["Content-Type"] == "application/octet-stream"
+ conn = http.client.HTTPConnection(*server.server_address[:2], timeout=TIMEOUT)
for path in ("/../secret.txt", "/%2e%2e/secret.txt"):
conn.request("GET", path)
r = conn.getresponse(); r.read()
diff --git a/tools/tests/test_webgl_favicon.py b/tools/tests/test_webgl_favicon.py
index 4331f11c9..3729dd0d1 100644
--- a/tools/tests/test_webgl_favicon.py
+++ b/tools/tests/test_webgl_favicon.py
@@ -35,8 +35,9 @@ def test_webgl_template_links_a_local_multisize_icon():
def test_favicon_is_served_at_template_relative_url():
server = wc.serve(TEMPLATE)
try:
- base = f"http://localhost:{server.server_address[1]}"
- with urlopen(base + "/favicon.ico") as response:
+ # serve()'s own address, not "localhost" (Windows tries ::1 first, where nothing listens), and a timeout.
+ host, port = server.server_address[:2]
+ with urlopen(f"http://{host}:{port}/favicon.ico", timeout=10) as response:
assert response.status == 200
assert response.read() == (TEMPLATE / "favicon.ico").read_bytes()
finally: