wbolster / emacs-python-pytest

run pytest inside emacs
BSD 3-Clause "New" or "Revised" License
149 stars 26 forks source link

Respect `projectile-compilation-dir` for working directory. #57

Closed spookylukey closed 2 years ago

spookylukey commented 2 years ago

The projectile-test-project command and other commands set default-directory to (projectile-compilation-dir) - see https://github.com/bbatsov/projectile/blob/2c948f3a8ed378ae5fd800d2c66aece06ba058b8/projectile.el#L4705

It would be helpful if emacs-python-pytest did the same. It means that you can customize the cwd for a test run using your .dir-locals.el file e.g.:

 (python-mode .
              ((projectile-project-compilation-dir . "my_sub_project_dir")))

This seems like a much nicer workaround for the case of monorepos than the one mentioned in the readme. For example, this method means that if we have multiple different pytest.ini or conftest.py in different sub-projects, they can be picked up correctly. Running with cwd as the root directory is not going to work for many monorepo setups.

Implementation seems to be as simple as changing the definition of python-pytest---project-root:

(defun python-pytest--project-root ()
  "Find the project root directory."
  (let ((projectile-require-project-root nil))
    (projectile-compilation-dir)))

This is working for me, I don't know what other complications it might bring.

wbolster commented 2 years ago

i like this 👍🏼 and wasn't aware of this projectile behaviour.

care to open a pr?

dirodriguezm commented 1 year ago

Hey @spookylukey can you give an example on how to make this work with a monorepo structure ?

I've tried using the .dir-locals.el file but no matter what I do, the pytest cwd is always the root of the monorepo.

spookylukey commented 1 year ago

@dirodriguezm Here are two different layouts I have found work:

Given you have:

And you want the pytest CWD to be 'subdir/':

Option 1

Add the following .dir-locals.el as a sibling to .git/:

((nil .
      ((projectile-project-compilation-dir . "subdir"))))

Note that you can only have one .dir-locals.el that takes effect. If you have one in a sub directory, ones in parent directories will be ignored.

Note also that the variable name is projectile-project-compilation-dir, while projectile-compilation-dir is a function that uses it.

Option 2

Don't need .dir-locals.el at all, instead put a .projectile inside subdir

If you still can't get it to work, say so, I can give a full example or see if there is something going on with my setup.

dirodriguezm commented 1 year ago

Thank you, it worked.