klen / nvim-test

A Neovim wrapper for running tests
MIT License
176 stars 26 forks source link

Possibility to pass custom cmd and working directly to runners #2

Open bellini666 opened 2 years ago

bellini666 commented 2 years ago

Hi,

First of all, awesome lib! :)

It would be nice to have the possibility to define the command at runtime that should be used to run the tests. This is useful for local binaries. For example, when using poetry, to run pytest I either have to run poetry run pytest or call it directly from the venv's bin (which is not activated). I solved that locally by doing:

local pytest = require("nvim-test.runners.pytest")
function pytest:build_cmd(filename, opts)
  local cmd
  if vim.env.VIRTUAL_ENV then
    cmd = vim.env.VIRTUAL_ENV .. "bin/pytest"
  else
    cmd = require("utils").find_cmd("pytest", ".venv/bin", filename)
  end
  return ("%s%s"):format(cmd, pytest:build_args(filename, opts or {}))
end

But it would be nice to have a proper config for it. (obs. that find_cmd is this function: https://github.com/bellini666/dotfiles/blob/master/vim/lua/utils.lua#L186)

Also, I work with monorepos and because of that the config for pytest is inside a subdirectory of where my cwd is. To properly run pytest, I need to "cd" to the subdirectory before running it.

It would be nice to have an option to pass a function similar to the above, that returns a directory that will be used to run the command from.

Both could also be the same function, that could return a table with both options (the ones not set would fallback to the default ones)

klen commented 2 years ago

I've added virtualenv support for python runners.

As an alternative for the current moment, you may use plugins like nvim-config-local and setup test runners locally in your repos:

-- .vimrc.lua
require('nvim-test.runners.pytest'):setup {command='any command you want here'}
mrjones2014 commented 2 years ago

I have a similar use case, but I'd like to determine the command dynamically, based on the file path.

I work in a Rust monorepo, with like, a lot of crates. I don't need to compile the whole monorepo to run tests from one crate.

I'd like the ability to, based on the file path you're in, find the nearest crate root, then run

cargo test -p nearest-crate-name test-name

so that we're only running tests in the crate the current file is in.

klen commented 2 years ago

@mrjones2014 I don't know rust, could you please provide an example repo?

mrjones2014 commented 2 years ago

Sure, take the Rust OpenCV monorepo for example: https://github.com/rust-cv/cv

there are multiple crates (in rust ecosystem a crate is basically just a project or package), each crate contains a Cargo.toml file and a src/ directory.

the -p flag of cargo test specified which crate to run tests in, otherwise it runs tests for all crates.

taking the repo I linked as an example, running cargo test -p cv-core would run tests only in the cv-core crate. You can then also give it a test name to run like cargo test -p cv-core some_test_name

klen commented 2 years ago

@mrjones2014 I've add the option:

Before use, you have to enable it for cargo-test runner:

require('nvim-test.runners.cargo-test'):setup { package = true }
rhodee commented 2 years ago

@klen thank you for this library. I am wondering how you would recommend using this within a docker environment? Thank you!