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

Add a 'cwd' keyword argument to the Session.run API #513

Closed
wants to merge 4 commits into from

Conversation

cblegare
Copy link

@cblegare cblegare commented Dec 9, 2021

Closes #512

Adds a cwd keyword argument for running commands. Uses directly the underlying subprocess.Popen API.

The 'cwd' keyword argument is intended to optionaly
set the current working directory in the sub process
and is passed as-is to the underlying subprocess.Popen
constructor: https://docs.python.org/3/library/subprocess.html#popen-constructor

It is consistent within the existing nox.popen API by thinly and simply
wrapping the underlying API.
after command.run both in case of a success
or a failure (exit code being 0 or else)

We do not check that the cwd stays consistent for
the whole time the subprocess is running, hence
the behavior of the new cwd keyword argument
is undefined *during* the execution of the process.

The command.run API feels like it is atomic and not intended to be used
in exotic concurent use-cases.  This explains why no concurent tests
are added at this moment.
@theacodes
Copy link
Collaborator

theacodes commented Dec 9, 2021 via email

@FollowTheProcess
Copy link
Collaborator

FollowTheProcess commented Dec 9, 2021

Just a quick note on the failing checks, apparently it's a known issue with using os.PathLike as a type hint, (see here and here)

If we go for this a simple change from

cwd: Optional[Union[str, bytes, os.PathLike[str], os.PathLike[bytes]]] = None

to

cwd: Optional[Union[str, bytes, "os.PathLike[str]", "os.PathLike[bytes]"]] = None

Should resolve the issue 👍🏻

Overall I agree with @theacodes, I think a context manager would feel and look a lot nicer.

Consider:

@nox.session
def test(session):
    with session.chdir("wherever/you/want"):
        # Now it's totally clear everything in this block is
        # running under "wherever/you/want"

As opposed to:

@nox.session
def test(session):
    # Two different cwd's used here
    session.run("pytest", cwd="./somewhere")
    session.run("flake8", cwd="./somewhere/else")

I would argue the latter is less clear?

@DiddiLeija
Copy link
Collaborator

Yes, I would also prefer a context manager.

I just edited the first post with a "close" command to link with #512.

@cblegare
Copy link
Author

cblegare commented Dec 9, 2021

Thanks for your feedbacks. While I disagree about what would feel and look nicer, since the run api already mimics some arguments to the already well known subprocess.Popen, that is your decision.

Also, that kind of context manager would not use anything from the session object, so I'd rather use a context manager that is independent from nox. In other words, since there is no pros in being attached to the Session instance for this particular use-case, I'd rather not depend on it.

Farewell!

@cblegare cblegare closed this Dec 9, 2021
@cblegare
Copy link
Author

cblegare commented Dec 9, 2021

I think I'd rather use make session.chdir a context manager.

I now better understand that sentence. I wasn't aware of the session.chdir implementation. I'd advise against making chdir a context manager since it mimics and share the same name as the standard os.chdir and having a different behavior could be suprising for users, and surprising is often not what you want in an API. In case you choose to do so, you should be able to keep backward compatibility by implementing __call__ in addition to __enter__ and __exit__.

I can't spend much more time on this particular issue at the moment, but again, thanks for the feedback.

@cjolowicz
Copy link
Collaborator

cjolowicz commented Dec 28, 2021

We may want to reconsider this if parallel session execution (#544) ever becomes a thing.

@cjolowicz
Copy link
Collaborator

See also #555

@cblegare
Copy link
Author

cblegare commented Jan 8, 2022

Well, in case you @theacodes change your mind about this PR, I'll gladly fix checks, rebase and re-submit it.

Edit: sorry about reopening and duplicated comment, I seem to have a hard time commenting on mobile

@cblegare cblegare reopened this Jan 8, 2022
@cblegare cblegare closed this Jan 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Run commands from within a specific directory
5 participants