Skip to content

Commit

Permalink
Fix reloading .vimrc by introducing test#custom_runners
Browse files Browse the repository at this point in the history
It's not right that "test#runners" is used both for setting custom
runners and later vim-test extends it with default runners. This causes
issues with reloading .vimrc when custom runners are used.

So instead users should use the new "test#custom_runners" variable, and
this time the list of runners will be dynamically generated, which means
it will work better with .vimrc reloading.

We still keep backwards compatibility with setting "test#runners".

Fixes #306
  • Loading branch information
janko committed May 30, 2018
1 parent 94abac3 commit 062c489
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 14 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ First, add your runner to the list in your `.vimrc`:

```vim
" First letter of runner's name must be uppercase
let test#runners = {'MyLanguage': ['MyRunner']}
let test#custom_runners = {'MyLanguage': ['MyRunner']}
```

Second, create `~/.vim/autoload/test/mylanguage/myrunner.vim`, and define the following
Expand Down Expand Up @@ -412,7 +412,7 @@ let test#enabled_runners = ["mylanguage#myrunner", "ruby#rspec"]

All other runners will not be loaded.

Note that for your own custom runners, you still need to set `test#runners`.
Note that for your own custom runners, you still need to set `test#custom_runners`.

## Running tests

Expand Down
25 changes: 24 additions & 1 deletion autoload/test.vim
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ function! test#shell(cmd, strategy) abort
endfunction

function! test#determine_runner(file) abort
for [language, runners] in items(g:test#runners)
for [language, runners] in items(test#get_runners())
for runner in runners
let runner = tolower(language).'#'.tolower(runner)
if exists("g:test#enabled_runners")
Expand All @@ -127,6 +127,18 @@ function! test#determine_runner(file) abort
endfor
endfunction

function! test#get_runners() abort
if exists('g:test#runners')
let custom_runners = g:test#runners
elseif exists('g:test#custom_runners')
let custom_runners = g:test#custom_runners
else
let custom_runners = {}
endif

return s:extend(custom_runners, g:test#default_runners)
endfunction

function! test#test_file(file) abort
return !empty(test#determine_runner(a:file))
endfunction
Expand Down Expand Up @@ -186,3 +198,14 @@ function! s:echo_failure(message) abort
echo a:message
echohl None
endfunction

function! s:extend(source, dict) abort
let result = {}
for [key, value] in items(a:source)
let result[key] = value
endfor
for [key, value] in items(a:dict)
let result[key] = get(result, key, []) + value
endfor
return result
endfunction
2 changes: 1 addition & 1 deletion doc/test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ let test#enabled_runners = ["mylanguage#myrunner", "ruby#rspec"]

All other runners will not be loaded.

Note that for your own custom runners, you still need to set `test#runners`.
Note that for your own custom runners, you still need to set `test#custom_runners`.

Test.vim relies on you being cd-ed into the project root. However, sometimes
you may want to execute tests from a different directory than the current
Expand Down
13 changes: 3 additions & 10 deletions plugin/test.vim
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,7 @@ let g:loaded_test = 1

let g:test#plugin_path = expand('<sfile>:p:h:h')

function! s:extend(source, dict) abort
for [key, value] in items(a:dict)
let a:source[key] = get(a:source, key, []) + value
endfor
endfunction

let g:test#runners = get(g:, 'test#runners', {})
call s:extend(g:test#runners, {
let g:test#default_runners = {
\ 'Ruby': ['Rails', 'M', 'Minitest', 'RSpec', 'Cucumber'],
\ 'JavaScript': ['Ava', 'CucumberJS', 'Intern', 'TAP', 'Karma', 'Lab', 'Mocha', 'Jasmine', 'Jest', 'WebdriverIO'],
\ 'Python': ['DjangoTest', 'PyTest', 'PyUnit', 'Nose', 'Nose2'],
Expand All @@ -32,7 +25,7 @@ call s:extend(g:test#runners, {
\ 'Racket': ['RackUnit'],
\ 'Java': ['MavenTest'],
\ 'Crystal': ['CrystalSpec'],
\})
\}

let g:test#custom_strategies = get(g:, 'test#custom_strategies', {})
let g:test#custom_transformations = get(g:, 'test#custom_transformations', {})
Expand All @@ -44,7 +37,7 @@ command! -nargs=* -bar TestSuite call test#run('suite', split(<q-args>))
command! -nargs=* -bar TestLast call test#run_last(split(<q-args>))
command! -bar TestVisit call test#visit()

for [s:language, s:runners] in items(g:test#runners)
for [s:language, s:runners] in items(test#get_runners())
for s:runner in s:runners
if index(g:test#runner_commands, s:runner) != -1
if exists(':'.s:runner) | continue | endif
Expand Down

0 comments on commit 062c489

Please sign in to comment.