77import ast
88import os
99import signal
10+ import subprocess
1011import threading
1112from contextlib import contextmanager
13+ from unittest import mock
1214from pathlib import Path
1315from typing import Optional
1416
@@ -110,6 +112,21 @@ def load_http(request):
110112 odoo .service .server .start (stop = True )
111113 signal .signal (signal .SIGINT , signal .default_int_handler )
112114
115+ @contextmanager
116+ def _shared_filestore (original_db_name , db_name ):
117+ # This method ensure that if tests are ran in a distributed way
118+ # we share the filestore between the original database and the
119+ # copy of the database. This is useful to avoid copying the
120+ # filestore for each worker.
121+ # This is done by patching the filestore method of the odoo
122+ # configuration to point to the original filestore.
123+ if original_db_name == db_name :
124+ yield
125+ return
126+ with mock .patch .object (odoo .tools .config , "filestore" ) as filestore :
127+ fs_path = os .path .join (odoo .tools .config ['data_dir' ], 'filestore' , original_db_name )
128+ filestore .return_value = fs_path
129+ yield
113130
114131@contextmanager
115132def _worker_db_name ():
@@ -122,15 +139,18 @@ def _worker_db_name():
122139 try :
123140 if xdist_worker :
124141 db_name = f"{ original_db_name } -{ xdist_worker } "
125- os . system ( f "dropdb { db_name } --if-exists" )
126- os . system ( f "createdb -T { original_db_name } { db_name } " )
142+ subprocess . run ([ "dropdb" , db_name , " --if-exists"], check = True )
143+ subprocess . run ([ "createdb" , "-T" , original_db_name , db_name ], check = True )
127144 odoo .tools .config ["db_name" ] = db_name
128- yield db_name
145+ odoo .tools .config ["dbfilter" ] = f"^{ db_name } $"
146+ with _shared_filestore (original_db_name , db_name ):
147+ yield db_name
129148 finally :
130149 if db_name != original_db_name :
131150 odoo .sql_db .close_db (db_name )
132- os . system ( f "dropdb { db_name } " )
151+ subprocess . run ([ "dropdb" , db_name , "--if-exists" ], check = True )
133152 odoo .tools .config ["db_name" ] = original_db_name
153+ odoo .tools .config ["dbfilter" ] = f"^{ original_db_name } $"
134154
135155
136156@pytest .fixture (scope = 'session' , autouse = True )
0 commit comments