Skip to content

Commit 5bc9e6f

Browse files
Create test_server.py
1 parent 992d684 commit 5bc9e6f

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

tests/test_server.py

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"""Tests for the documentation server."""
2+
3+
import pytest
4+
import socket
5+
import threading
6+
import time
7+
import requests
8+
from vyper_sphinx_docs.server import serve_docs
9+
10+
11+
def test_server_start(tmp_path):
12+
"""Test server startup and accessibility."""
13+
# Create mock build directory
14+
build_dir = tmp_path / "docs" / "_build" / "html"
15+
build_dir.mkdir(parents=True)
16+
(build_dir / "index.html").write_text("<html><body>Test</body></html>")
17+
18+
# Start server in a thread
19+
port = _get_free_port()
20+
server_thread = threading.Thread(
21+
target=lambda: serve_docs(port=port),
22+
daemon=True
23+
)
24+
server_thread.start()
25+
26+
# Wait for server to start
27+
time.sleep(1)
28+
29+
# Test server response
30+
try:
31+
response = requests.get(f"http://localhost:{port}")
32+
assert response.status_code == 200
33+
assert "Test" in response.text
34+
finally:
35+
# Cleanup (server will be stopped when thread is terminated)
36+
pass
37+
38+
39+
def test_server_missing_docs(tmp_path):
40+
"""Test server behavior with missing documentation."""
41+
with pytest.raises(FileNotFoundError):
42+
serve_docs()
43+
44+
45+
def _get_free_port():
46+
"""Get an available port number."""
47+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
48+
s.bind(('', 0))
49+
s.listen(1)
50+
port = s.getsockname()[1]
51+
return port

0 commit comments

Comments
 (0)