Commit 9d5ae147 authored by Jérome Perrin's avatar Jérome Perrin

testing/utils: support choosing working directory for ManagedHTTPServer

This allows easily serving a directory content with a server like this:

    from six.moves import SimpleHTTPServer

    class FileHTTPServer(ManagedHTTPServer):
      working_directory = profile_dir
      RequestHandler = SimpleHTTPServer.SimpleHTTPRequestHandler
parent cec143b4
...@@ -113,8 +113,10 @@ class ManagedHTTPServer(ManagedResource): ...@@ -113,8 +113,10 @@ class ManagedHTTPServer(ManagedResource):
proto = 'http' proto = 'http'
# hostname to listen to, default to ipv4 address of the current test # hostname to listen to, default to ipv4 address of the current test
hostname = None # type: str hostname = None # type: str
# port to listen to, default # port to listen to, default to a free port selected with `findFreeTCPPort`
port = None # type: int port = None # type: int
# current working directory of the server process
working_directory = None # type: str
@property @property
def url(self): def url(self):
...@@ -160,10 +162,19 @@ class ManagedHTTPServer(ManagedResource): ...@@ -160,10 +162,19 @@ class ManagedHTTPServer(ManagedResource):
if not self.port: if not self.port:
self.port = findFreeTCPPort(self.hostname) self.port = findFreeTCPPort(self.hostname)
def serve_forever(server, cwd):
if cwd:
os.chdir(cwd)
server.serve_forever()
server = self._makeServer() server = self._makeServer()
self._process = multiprocessing.Process( self._process = multiprocessing.Process(
target=server.serve_forever, target=serve_forever,
name=self._name, name=self._name,
kwargs={
'server': server,
'cwd': self.working_directory,
}
) )
self._process.start() self._process.start()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment