nvim-neotest / neotest-python

MIT License
132 stars 40 forks source link

Tests fail with import error due to missing PYTHONPATH in executionEnvironments #42

Closed alexander-born closed 1 year ago

alexander-born commented 1 year ago

The projects at work use execution environments with extra paths which are defined in a pyproject.toml file.

Example of pyproject.toml:

[tool.pyright]
include = ["src"]
exclude = ["**/node_modules",
    "**/__pycache__",
    "src/experimental",
    "src/typestubs"
]
ignore = ["src/oldstuff"]
defineConstant = { DEBUG = true }
stubPath = "src/stubs"
venv = "env367"

reportMissingImports = true
reportMissingTypeStubs = false

pythonVersion = "3.6"
pythonPlatform = "Linux"

executionEnvironments = [
  { root = "src/web", pythonVersion = "3.5", pythonPlatform = "Windows", extraPaths = [ "src/service_libs" ] },
  { root = "src/sdk", pythonVersion = "3.0", extraPaths = [ "src/backend" ] },
  { root = "src/tests", extraPaths = ["src/tests/e2e", "src/sdk" ]},
  { root = "src" }
]

In order to make the test not fail on imports I need to modify the PYTHONPATH with both the root and all the extraPaths for the correct executionEnvironment.

rcarriga commented 1 year ago

Thanks for the PR to handle this but I'd rather not get into parsing another tool's config. Instead I'd like the interfaces to be open enough to allow users to perform these more niche integrations themselves.

Setting env variables is already possible with neotest.run.run however this doesn't cover running from the summary. The run consumer could accept an augment function in the config which would be run for args to each call of neotest.run.run. Inside that function, reading of pyright's config could be done and the PYTHONPATH would be set in the env

alexander-born commented 1 year ago

I understand and respect that decision. I thought this is a common configuration, so I threw this pr together which would benefit others as well.

I am fine with your suggestion and just put this code in my personal config.