← Back to Upcase

Dispatching tests to another tmux session

I’ve been using vim-tmux-runner and vim-spec-runner to run tests in a tmux pane. I like the workflow, but I’d rather run my tests in a separate tmux session I have running on a second monitor. That way I can keep my editor’s screen real-estate and still be able to easily read the test results when I want to.

I wrote some vimscript to set this up (it prompts the user for a target and sets g:spec_runner_dispatcher appropriately), but I wonder if there’s a better way.

function! Spatch()
  let sessions = system("tmux list-sessions -F '#{session_name}'")

  let target = input('Enter target: ')
  let target_session = matchstr(target, '[[:alnum:]_-]\+')

  if (sessions =~ target_session) && (target_session != '')
    let g:spec_runner_dispatcher = "call system(\"tmux send -t "
          \                        . target .
          \                        " C-L '{command}' ENTER\")"
    let g:spec_runner_dispatcher = "VtrSendCommand! {command}"

nnoremap <expr> <Leader>x Spatch()

The behavior is somewhere in between vim-tmux-runner and vim-rspec (which sends tests to a separate terminal window). Is there a plug-in that already does this?

1 Like

Hello @ivanbrennan,

Your code looks find to me. I might be missing something, but it looks like you’re not saving off the configuration, but instead would need to “reconnect” to tmux for each run. If that is the case, I would add something to save off and reuse the last configuration.

You could also check out tslime as it provides tab completion of sessions, windows, and panes.

Hey Chris. Yeah, I’m not saving the configuration yet. Between playing around with tslime, and reading Gabe’s post about you guys developing vim-spec-runner, I’ve been inspired to go a little deeper with this.

I want to build something that collapses tslime’s multi-step process (prompt for session, for window, for pane) into a single prompt with smart tab completion. It would complete the session name, then if you type : you can go on to complete the window name/number, and . will go on to complete the pane number.

I just started playing around with Vimrunner too. Cool stuff!

@ivanbrennan that’s some fancy sorcery you’re diving into. I’ve not done much with it, but I know its accomplished by defining a custom completion function which is defined in the help at :help :command-completion-custom. You can then set that as the completion for an input() function call. tslime shows a simple version of it, and Rails.vim has some intense completion going on that you could reference.

Good luck!

@christoomey I noticed in vim-tmux-runner you switched from using paste-buffer to send-keys when sending commands over to tmux. Is one better than the other?

I imagine paste-buffer might be designed to handle larger chunks of text, but I’m just guessing.

Well would you look at that. When I first saw this post I had zero memory of ever using set-buffer, but there it is! set-buffer would likely be a better mechanism and prevent some issues I have seen. Might just have to go back to it.

@ivanbrennan have you found a solution that works?