Request for temporary environment variables
d-miketa opened this issue · 8 comments
Hi, thanks for the plugin! It's quickly become a treasured component of my Neovim config.
One extra feature I'd appreciate is specifying per-command environment variables. Here's an example where that would be useful: https://github.com/ezyang/expecttest. This library allows you to specify a unit test without explicitly knowing the right hand side of the test. Then, if a particular environmental variable is turned on, the test runner itself overwrites the file to fill in the RHS based on the current output. This is very useful for numerically heavy projects, e.g. in ML. It would be useful to have a separate command that
- sets up an environmental variable
- runs an
ultest
command - removes the env variable whether or not the command was successfully completed
Glad you're enjoying the plugin! 😁
There is a g:ultest_env
setting which sets env variables for the tests run. You could set this, run your command and set to v:null
which is the default. Of course this could be wrapped by a custom function/command.
Thanks! I tried to implement it as a wrapper over existing commands:
local function expectTestDebug(ultest_cmd)
-- Populates expecttest fields
vim.g.ultest_env = {['EXPECTTEST_ACCEPT'] = 1}
vim.cmd(ultest_cmd)
vim.g.ultest_env = vim.NIL
end
but it's acting as if the environment variable weren't set (and expecttest
isn't overwriting the file & passing the test).
Can you use a string instead of a number for the value of the variable?
vim.g.ultest_env = {EXPECTTEST_ACCEPT = "1"}
If that doesn't work, please attach a debug log using ULTEST_LOG_FILE=ultest.log ULTEST_LOG_LEVEL=DEBUG nvim <file>
so I can see what is being sent to the process
command:
local function runExpectTest(ultest_cmd)
-- Populates expecttest fields
vim.g.ultest_env = {EXPECTTEST_ACCEPT = "1"}
vim.cmd(ultest_cmd)
vim.g.ultest_env = vim.NIL
end
ultest.log
:
14:22:00 | INFO | MainThread | logging.py:create_logger:101 | Logger created
14:22:00 | DEBUG | MainThread | __init__.py:_prepare_env:56 | Setting COLUMNS to 64
14:22:00 | DEBUG | MainThread | __init__.py:__init__:44 | Handler created
14:22:00 | INFO | MainThread | __init__.py:get_attach_script:225 | Creating script to attach to process
14:22:04 | INFO | MainThread | tracker.py:_init_test_file:97 | Initialising test file /Users/dmiketa/repos/project/tests/unit/test.py
14:22:04 | DEBUG | Thread-1 | __init__.py:_handle_coroutine:62 | Starting job with group update_positions
14:22:04 | INFO | Thread-1 | tracker.py:_async_update:57 | Updating positions in /Users/dmiketa/repos/project/tests/unit/test.py
14:22:04 | DEBUG | Thread-1 | file.py:parse_file_structure:25 | Converted pattern {'test': ['\\v^\\s*%(async )?def (test_\\w+)'], 'namespace': ['\\v^\\s*class (\\w+)']} to {'test': [re.compile('^\\s*(?:async )?def (test_\\w+)')], 'namespace': [re.compile('^\\s*class (\\w+)')]}
14:22:04 | DEBUG | Thread-1 | tracker.py:_async_update:84 | New test /Users/dmiketa/repos/project/tests/unit/test.py found in /Users/dmiketa/repos/project/tests/unit/test.py
14:22:04 | DEBUG | Thread-1 | tracker.py:_async_update:84 | New test ExpectTestCase-2229633378567866437 found in /Users/dmiketa/repos/project/tests/unit/test.py
14:22:04 | DEBUG | Thread-1 | tracker.py:_async_update:84 | New test testr5835138995120905640 found in /Users/dmiketa/repos/project/tests/unit/test.py
14:22:04 | DEBUG | Thread-1 | tracker.py:_async_update:84 | New test test_method5835138995120905640 found in /Users/dmiketa/repos/project/tests/unit/test.py
14:22:04 | DEBUG | Thread-1 | tracker.py:_async_update:84 | New test test_atom_featurizer_list5835138995120905640 found in /Users/dmiketa/repos/project/tests/unit/test.py
14:22:04 | DEBUG | Thread-1 | tracker.py:_async_update:84 | New test test_bond_featurizer_list5835138995120905640 found in /Users/dmiketa/repos/project/tests/unit/test.py
14:22:04 | DEBUG | Thread-1 | tracker.py:_remove_old_positions:129 | No tests removed
14:22:04 | DEBUG | Thread-1 | __init__.py:_handle_coroutine:82 | Finished job with group update_positions
14:22:10 | INFO | MainThread | __init__.py:run_nearest:126 | Running nearest test in tests/unit/test.py at line 31
14:22:10 | DEBUG | MainThread | __init__.py:_register_started:288 | Registering test_method5835138995120905640 as started
14:22:10 | DEBUG | Thread-1 | __init__.py:_handle_coroutine:62 | Starting job with group test_method5835138995120905640
14:22:10 | DEBUG | Thread-1 | processes.py:run:50 | Starting test process test_method5835138995120905640 with command ['pytest', 'tests/unit/test.py::ExpectTestCase::test_method'], cwd = /Users/dmiketa/repos/project, env = None
14:22:12 | DEBUG | Thread-3 | handle.py:forward:85 | Writing data to output file
14:22:12 | DEBUG | Thread-3 | handle.py:forward:85 | Writing data to output file
14:22:12 | DEBUG | Thread-3 | handle.py:forward:85 | Writing data to output file
14:22:12 | DEBUG | Thread-3 | handle.py:forward:85 | Writing data to output file
14:22:13 | DEBUG | Thread-3 | handle.py:forward:85 | Writing data to output file
14:22:13 | DEBUG | Thread-3 | handle.py:forward:85 | Writing data to output file
14:22:13 | DEBUG | Thread-3 | handle.py:forward:85 | Writing data to output file
14:22:13 | DEBUG | Thread-3 | handle.py:forward:85 | Writing data to output file
14:22:13 | DEBUG | Thread-3 | handle.py:forward:85 | Writing data to output file
14:22:13 | DEBUG | Thread-3 | handle.py:forward:85 | Writing data to output file
14:22:13 | DEBUG | Thread-3 | handle.py:forward:85 | Writing data to output file
14:22:13 | DEBUG | Thread-3 | handle.py:forward:85 | Writing data to output file
14:22:13 | DEBUG | Thread-3 | handle.py:forward:85 | Writing data to output file
14:22:13 | DEBUG | Thread-3 | handle.py:forward:85 | Writing data to output file
14:22:13 | DEBUG | Thread-1 | processes.py:run:76 | Process test_method5835138995120905640 complete with exit code: 1
14:22:13 | DEBUG | Thread-1 | __init__.py:_register_result:299 | Registering test_method5835138995120905640 as exited with result {"id": "test_method5835138995120905640", "file": "/Users/dmiketa/repos/project/tests/unit/test.py", "code": 1, "output": "/var/folders/b1/d587c0_d6hb6ndr4mv24h_gc0000gq/T/ultesthqr_hqbw/__Users__dmiketa__repos__project__tests__unit__test_py/test_method5835138995120905640_out"}
14:22:13 | DEBUG | Thread-1 | __init__.py:_handle_coroutine:82 | Finished job with group test_method5835138995120905640
14:22:10 | DEBUG | Thread-1 | processes.py:run:50 | Starting test process test_method5835138995120905640 with command ['pytest', 'tests/unit/test.py::ExpectTestCase::test_method'], cwd = /Users/dmiketa/repos/project, env = None
^ this is probably the relevant line - env = None
.
Edit: I think the env vars only enter through the Python Handler
, which is cached and re-used, so perhaps updating env vars after a certain point isn't picked up?
Ah you're right, I've changed it to update every time. Please try with the latest commit
No, still claiming env = None
.
Is
_vim.sync_call
the right method for this? As far as I can tell it'll run _vim._eval('get(g:, ultest_env)')
, no?Yes that's correct, which is what is wanted. The issue has to do with how the remote plugin call was being made (originally async and so couldn't make a synchronous call). I've adjusted to be synchronous, so please try now
Thank you, it works perfectly now. :) Very useful functionality.