44import pytest
55import shutil
66import signal
7+ import time
78
89from ctypes .util import find_library
910from tempfile import TemporaryDirectory
@@ -21,7 +22,7 @@ async def set_death_signal():
2122 libc .prctl (PR_SET_PDEATHSIG , signal .SIGTERM , 0 , 0 , 0 )
2223
2324
24- async def _run_server ():
25+ async def _run_server (storage ):
2526 global temp_folder , python_proc
2627
2728 # Make sure we can find our way to the code in the temporary folder.
@@ -43,18 +44,22 @@ async def _run_server():
4344 f .write ("end-to-end test file" )
4445 # Make sure a few languages exist.
4546 os .makedirs ("data/Page/en" )
47+ with open ("data/Page/en/.keep" , "w" ) as f :
48+ pass
4649 os .makedirs ("data/Page/de" )
50+ with open ("data/Page/de/.keep" , "w" ) as f :
51+ pass
4752
4853 # Run TrueWiki, with coverage enabled.
49- command = ["coverage" , "run" , "--branch" , "--source" , "truewiki" ]
54+ command = ["coverage" , "run" ]
5055 command .extend (
5156 [
5257 "-m" ,
5358 "truewiki" ,
5459 "--port" ,
5560 "8080" ,
5661 "--storage" ,
57- "local" ,
62+ storage ,
5863 "--user" ,
5964 "developer" ,
6065 "--frontend-url" ,
@@ -63,6 +68,35 @@ async def _run_server():
6368 "cache/" ,
6469 ]
6570 )
71+
72+ if storage == "github" :
73+ branch_name = "e2e-" + str (time .time ()).replace ("." , "-" )
74+
75+ command .extend (
76+ [
77+ "--storage-github-url" ,
78+ "git@github.com:TrueBrain/truewiki-e2e-test.git" ,
79+ "--storage-github-history-url" ,
80+ "https://github.com/TrueBrain/truewiki-e2e-test" ,
81+ "--storage-github-branch" ,
82+ branch_name ,
83+ ]
84+ )
85+
86+ if storage == "gitlab" :
87+ branch_name = "e2e-" + str (time .time ()).replace ("." , "-" )
88+
89+ command .extend (
90+ [
91+ "--storage-gitlab-url" ,
92+ "git@gitlab.com:TrueBrain/truewiki-e2e-test.git" ,
93+ "--storage-gitlab-history-url" ,
94+ "https://gitlab.com/TrueBrain/truewiki-e2e-test" ,
95+ "--storage-gitlab-branch" ,
96+ branch_name ,
97+ ]
98+ )
99+
66100 python_proc = await asyncio .create_subprocess_exec (
67101 command [0 ],
68102 * command [1 :],
@@ -72,17 +106,34 @@ async def _run_server():
72106 # Make sure we switch back to the project folder.
73107 os .chdir (cwd )
74108
75- # Wait for the startup line to be shown.
76- await python_proc .stdout .readline ()
109+ # Wait till the server reports "running".
110+ lines = []
111+ while python_proc .returncode is None and not python_proc .stdout .at_eof ():
112+ line = await python_proc .stdout .readline ()
113+ if line is None :
114+ break
115+ lines .append (line )
116+
117+ if "Running on http://" in line .decode ():
118+ break
119+
120+ # If either the process closed or the stdout closed, the server didn't
121+ # actual start. And yes, it can happen stdout is already closed, but
122+ # the returncode isn't in yet.
123+ if python_proc .returncode is not None or python_proc .stdout .at_eof ():
124+ print ((b"\n " .join (lines ) + await python_proc .stdout .read ()).decode ())
125+ raise Exception ("Failed to start TrueWiki server" )
126+
127+ return None
77128
78129
79130@pytest .fixture (scope = "session" , autouse = True )
80- def run_server ():
131+ def run_server (request ):
81132 # Create a new event loop for our server.
82133 loop = asyncio .new_event_loop ()
83134
84135 # Start the server.
85- loop .run_until_complete (_run_server ())
136+ loop .run_until_complete (_run_server (request . config . getoption ( "--storage" ) ))
86137
87138 # Give control back to PyTest.
88139 yield
@@ -93,6 +144,10 @@ def run_server():
93144 temp_folder .cleanup ()
94145
95146
147+ def pytest_addoption (parser ):
148+ parser .addoption ("--storage" , action = "store" , default = "local" , help = "Storage backend" )
149+
150+
96151@pytest .fixture (autouse = True )
97152def timeout (page : Page ):
98153 page .set_default_timeout (1000 )
0 commit comments