nvim-neotest / neotest-python

MIT License
126 stars 38 forks source link

Custom envs not working #16

Closed lukerops closed 1 year ago

lukerops commented 2 years ago

I have helper code that loads my .env file and passes it to neotest.run.run (here), but the envs are not passed to pytest.


test.py

import os

def test_1():
    print(os.environ.get('APICONFIG_URL', 'Env not found!'))
    print(list(os.environ.keys()))
    raise Exception()

.env

APICONFIG_URL='http://localhost/'

image

rcarriga commented 2 years ago

I tested by changing your code to run when running the test, instead of loading beforehand and it works just fine. Are you sure that the env is correctly loaded?

  local get_env = function()
    local env = {}
    for _, file in ipairs({ ".env" }) do
      if vim.fn.empty(vim.fn.glob(file)) ~= 0 then
        break
      end

      for _, line in ipairs(vim.fn.readfile(file)) do
        for name, value in string.gmatch(line, "(.+)=['\"]?(.*)['\"]?") do
          local str_end = string.sub(value, -1, -1)
          if str_end == "'" or str_end == '"' then
            value = string.sub(value, 1, -2)
          end

          env[name] = value
        end
      end
    end
    return env
  end
  neotest.run.run({ vim.fn.expand("%"), env = get_env() })
rcarriga commented 1 year ago

Closing as I can't reproduce and no response has been received