pytest-xprocess icon indicating copy to clipboard operation
pytest-xprocess copied to clipboard

How to control the current working directory of the launched process?

Open bryanloz-xilinx opened this issue 3 years ago • 3 comments

What would you like to know?. How to control the current working directory of the launched process?

Additional context When using pytest-xprocess to launch a server process I notice that it is launched in a different directory: <test_root>/.pytest_cache/d/.xprocess/<test_fixture_name>

I want the freedom to change that, because my poc server is unfortunately relying on local files.

bryanloz-xilinx avatar Feb 23 '22 18:02 bryanloz-xilinx

It seems not so possible...

I did succeed at a nasty hack (conftest.py):

# Reference https://pytest-xprocess.readthedocs.io/en/latest/
import pytest, os, py
from xprocess import XProcess, ProcessStarter

@pytest.fixture(scope="session")
def myxprocess(request):
    rootdir = py._path.local.LocalPath(os.environ['MY_WORKING_DIR'])
    with XProcess(request.config, rootdir) as xproc:
        request.config._xprocess = xproc
        yield xproc

@pytest.fixture
def myserver(myxprocess):
    class Starter(ProcessStarter):
        pattern = "Running Dispatch Server on port"
        args = ['myserver', '--profile']

    logfile = myxprocess.ensure("", Starter) # Notice that the empty string is key here (nasty)

    yield

    myxprocess.getinfo("").terminate() # Notice that the empty string is key here (nasty)

bryanloz-xilinx avatar Feb 24 '22 03:02 bryanloz-xilinx

Hey @bryanloz-xilinx , thanks for letting us know about this.

Normally you would just customize process initialization using popen_kwargs as showed in our docs by setting cwd (check official Popen docs for details on this). But this will not work for cwd (specifically) since it's being used internally for setting up xProcessInfo.controldir where logs and PID files will be kept for process management.

I'll go ahead and mark it as a potential feature to be looked into in the future since it seems to be convenient to have. If anyone feels like tackling this, just let me know by commenting here and I'll be happy to review a PR.

northernSage avatar Feb 27 '22 22:02 northernSage

@bryanloz-xilinx I couldn't make your work for me, but thanks for highlighting the problem here. I found a slightly cleaner workaround: spawn a new bash process with the command, e.g. instead of args = ['myserver', '--profile'], use args = ["bash", "-c", 'cd ../../../../ && myserver --profile']

motin avatar Jun 29 '23 09:06 motin