Skip to content

Commit 6ddc1ef

Browse files
committed
Add timeout tests
1 parent 84c2a7c commit 6ddc1ef

1 file changed

Lines changed: 61 additions & 0 deletions

File tree

test/test_clone.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import pytest
22
import subprocess
3+
from .conftest import GIT2CPP_TEST_WASM
34

45
xtl_url = "https://github.com/xtensor-stack/xtl.git"
56

@@ -144,3 +145,63 @@ def test_clone_gitlab(git2cpp_path, tmp_path, run_in_tmp_path, protocol):
144145
assert p_status.returncode == 0
145146
assert "On branch main" in p_status.stdout
146147
assert "Your branch is up to date with 'origin/main'" in p_status.stdout
148+
149+
150+
@pytest.mark.skipif(not GIT2CPP_TEST_WASM, reason="Only test in WebAssembly")
151+
def test_clone_timeout(git2cpp_path, tmp_path, run_in_tmp_path):
152+
# Set very short timeout.
153+
subprocess.run(["export", "GIT_HTTP_TIMEOUT=0.001"], check=True)
154+
155+
# Check timeout is set.
156+
check_env_cmd = ["env"]
157+
p_check_env = subprocess.run(check_env_cmd, capture_output=True, cwd=tmp_path, text=True)
158+
assert "GIT_HTTP_TIMEOUT=0.001" in p_check_env.stdout
159+
160+
# Clone fails with timeout.
161+
clone_cmd = [git2cpp_path, "clone", xtl_url]
162+
p_clone = subprocess.run(clone_cmd, capture_output=True, cwd=tmp_path, text=True)
163+
assert p_clone.returncode != 0
164+
assert "network request timed out connecting to" in p_clone.stderr
165+
assert (
166+
"set a longer timeout in seconds using the environment variable GIT_HTTP_TIMEOUT"
167+
in p_clone.stderr
168+
)
169+
170+
# Set more reasonable timeout.
171+
subprocess.run(["export", "GIT_HTTP_TIMEOUT=10"], check=True)
172+
173+
# Check timeout is set.
174+
p_check_env = subprocess.run(check_env_cmd, capture_output=True, cwd=tmp_path, text=True)
175+
assert "GIT_HTTP_TIMEOUT=10" in p_check_env.stdout
176+
177+
# Clone succeeds.
178+
clone_cmd = [git2cpp_path, "clone", xtl_url]
179+
p_clone = subprocess.run(clone_cmd, capture_output=True, cwd=tmp_path, text=True)
180+
assert p_clone.returncode == 0
181+
182+
assert (tmp_path / "xtl").exists()
183+
assert (tmp_path / "xtl/include").exists()
184+
185+
186+
@pytest.mark.skipif(not GIT2CPP_TEST_WASM, reason="Only test in WebAssembly")
187+
def test_clone_negative_timeout_ignored(git2cpp_path, tmp_path, run_in_tmp_path):
188+
# Set negative timeout.
189+
subprocess.run(["export", "GIT_HTTP_TIMEOUT=-1"], check=True)
190+
191+
# Check timeout is set.
192+
check_env_cmd = ["env"]
193+
p_check_env = subprocess.run(check_env_cmd, capture_output=True, cwd=tmp_path, text=True)
194+
assert "GIT_HTTP_TIMEOUT=-1" in p_check_env.stdout
195+
196+
# Clone succeeds, ignoring invalid timeout.
197+
clone_cmd = [git2cpp_path, "clone", xtl_url]
198+
p_clone = subprocess.run(clone_cmd, capture_output=True, cwd=tmp_path, text=True)
199+
assert p_clone.returncode == 0
200+
201+
assert (tmp_path / "xtl").exists()
202+
assert (tmp_path / "xtl/include").exists()
203+
204+
assert (
205+
"environment variable GIT_HTTP_TIMEOUT must be a positive number of seconds"
206+
in p_clone.stdout
207+
)

0 commit comments

Comments
 (0)