Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
bryanloz-xilinx opened this issue Feb 23, 2022 · 3 comments

Comments

@bryanloz-xilinx
Copy link

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
Copy link
Author

bryanloz-xilinx commented Feb 24, 2022

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)

@northernSage
Copy link
Member

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.

@motin
Copy link

motin commented Jun 29, 2023

@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']

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants