mirror of
https://github.com/airblade/vim-gitgutter.git
synced 2025-11-08 11:33:48 -05:00
Modernise tests.
Use Vim's built-in assert_*() functions and vim-contest test runner.
This commit is contained in:
@@ -171,3 +171,9 @@ function! gitgutter#sign#next_sign_id() abort
|
|||||||
let s:next_sign_id += 1
|
let s:next_sign_id += 1
|
||||||
return next_id
|
return next_id
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
|
" Only for testing.
|
||||||
|
function! gitgutter#sign#reset()
|
||||||
|
let s:next_sign_id = s:first_sign_id
|
||||||
|
endfunction
|
||||||
|
|||||||
@@ -1,30 +0,0 @@
|
|||||||
## Testing vim-gitgutter
|
|
||||||
|
|
||||||
### Run the tests
|
|
||||||
|
|
||||||
```sh
|
|
||||||
$ cd test
|
|
||||||
$ ./test
|
|
||||||
```
|
|
||||||
|
|
||||||
### Add a new test
|
|
||||||
|
|
||||||
- Add a test file named like `testFoo.vim`. It should have this structure:
|
|
||||||
|
|
||||||
```viml
|
|
||||||
source helper.vim
|
|
||||||
call Setup()
|
|
||||||
|
|
||||||
" test code here
|
|
||||||
```
|
|
||||||
|
|
||||||
- Run the tests.
|
|
||||||
- Inspect output (in `foo.actual`) from the new test. If good, copy it to `foo.expected`.
|
|
||||||
- Run the tests to ensure new test's output is verified.
|
|
||||||
- Commit changes.
|
|
||||||
|
|
||||||
### Limitations
|
|
||||||
|
|
||||||
- Currently tests are done by writing out a file and comparing it to a known good one.
|
|
||||||
- There's no support for assertions within the testcase code.
|
|
||||||
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
|
|
||||||
--- Signs ---
|
|
||||||
Signs for fixture.txt:
|
|
||||||
line=2 id=3000 name=GitGutterLineAdded
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
|
|
||||||
--- Signs ---
|
|
||||||
Signs for fixture.txt:
|
|
||||||
line=2 id=3000 name=GitGutterLineAdded
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
|
|
||||||
--- Signs ---
|
|
||||||
Signs for fixture.txt:
|
|
||||||
line=5 id=3000 name=GitGutterLineModified
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
|
|
||||||
--- Signs ---
|
|
||||||
Signs for fileAddedToGit.tmp:
|
|
||||||
line=1 id=3000 name=GitGutterLineAdded
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
|
|
||||||
--- Signs ---
|
|
||||||
Signs for =fixture=.txt:
|
|
||||||
line=1 id=3000 name=GitGutterLineAdded
|
|
||||||
line=2 id=3001 name=GitGutterLineAdded
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
|
|
||||||
--- Signs ---
|
|
||||||
Signs for fix[tu]re.txt:
|
|
||||||
line=1 id=3000 name=GitGutterLineAdded
|
|
||||||
line=2 id=3001 name=GitGutterLineAdded
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
|
|
||||||
--- Signs ---
|
|
||||||
Signs for symlink:
|
|
||||||
line=5 id=3000 name=GitGutterLineRemoved
|
|
||||||
@@ -1,27 +0,0 @@
|
|||||||
set runtimepath+=../
|
|
||||||
source ../plugin/gitgutter.vim
|
|
||||||
|
|
||||||
function! Setup()
|
|
||||||
edit! fixture.txt
|
|
||||||
sign unplace *
|
|
||||||
endfunction
|
|
||||||
|
|
||||||
function! DumpSigns(filename)
|
|
||||||
execute 'redir! > ' a:filename.'.actual'
|
|
||||||
silent execute 'sign place'
|
|
||||||
redir END
|
|
||||||
endfunction
|
|
||||||
|
|
||||||
function! DumpGitDiff(filename)
|
|
||||||
call system('git diff fixture.txt > '.a:filename.'.actual')
|
|
||||||
endfunction
|
|
||||||
|
|
||||||
function! DumpGitDiffStaged(filename)
|
|
||||||
call system('git diff --staged fixture.txt > '.a:filename.'.actual')
|
|
||||||
endfunction
|
|
||||||
|
|
||||||
function! Dump(text, filename)
|
|
||||||
let msg = type(a:text) == 1 ? split(a:text, '\n') : a:text
|
|
||||||
call writefile(msg, a:filename.'.actual', 'a')
|
|
||||||
endfunction
|
|
||||||
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
|
|
||||||
--- Signs ---
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
|
|
||||||
--- Signs ---
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
diff --git a/test/fixture.txt b/test/fixture.txt
|
|
||||||
index f5c6aff..ae8e546 100644
|
|
||||||
--- a/test/fixture.txt
|
|
||||||
+++ b/test/fixture.txt
|
|
||||||
@@ -2,7 +2,7 @@ a
|
|
||||||
b
|
|
||||||
c
|
|
||||||
d
|
|
||||||
-e
|
|
||||||
+*e
|
|
||||||
f
|
|
||||||
g
|
|
||||||
h
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
diff --git a/test/fixture.txt b/test/fixture.txt
|
|
||||||
index 53b13df..8fdfda7 100644
|
|
||||||
--- a/test/fixture.txt
|
|
||||||
+++ b/test/fixture.txt
|
|
||||||
@@ -1,5 +1,8 @@
|
|
||||||
a
|
|
||||||
b
|
|
||||||
+x
|
|
||||||
+y
|
|
||||||
+z
|
|
||||||
c
|
|
||||||
e
|
|
||||||
f
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
diff --git a/test/fixture.txt b/test/fixture.txt
|
|
||||||
index f5c6aff..53b13df 100644
|
|
||||||
--- a/test/fixture.txt
|
|
||||||
+++ b/test/fixture.txt
|
|
||||||
@@ -1,7 +1,6 @@
|
|
||||||
a
|
|
||||||
b
|
|
||||||
c
|
|
||||||
-d
|
|
||||||
e
|
|
||||||
f
|
|
||||||
g
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
|
|
||||||
--- Signs ---
|
|
||||||
Signs for fixture.txt:
|
|
||||||
line=3 id=3000 name=GitGutterLineAdded
|
|
||||||
line=4 id=3001 name=GitGutterLineAdded
|
|
||||||
line=5 id=3002 name=GitGutterLineAdded
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
|
|
||||||
--- Signs ---
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
diff --git a/test/fixture.txt b/test/fixture.txt
|
|
||||||
index f5c6aff..3fbde56 100644
|
|
||||||
--- a/test/fixture.txt
|
|
||||||
+++ b/test/fixture.txt
|
|
||||||
@@ -1,5 +1,8 @@
|
|
||||||
a
|
|
||||||
b
|
|
||||||
+x
|
|
||||||
+y
|
|
||||||
+z
|
|
||||||
c
|
|
||||||
d
|
|
||||||
e
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
|
|
||||||
--- Signs ---
|
|
||||||
Signs for fixture.txt:
|
|
||||||
line=3 id=3000 name=GitGutterLineAdded
|
|
||||||
line=4 id=3001 name=GitGutterLineAdded
|
|
||||||
line=5 id=3002 name=GitGutterLineAdded
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
|
|
||||||
--- Signs ---
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
buffer: fixture.txt
|
|
||||||
altbuffer:
|
|
||||||
altbuffer:
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
modified: 1
|
|
||||||
modified: 1
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
'[ mark: 0,6,1,0
|
|
||||||
'] mark: 0,6,2,0
|
|
||||||
'[ mark: 0,6,1,0
|
|
||||||
'] mark: 0,6,2,0
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
|
|
||||||
--- Signs ---
|
|
||||||
Signs for fixture.txt:
|
|
||||||
line=1 id=3000 name=GitGutterLineModified
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
|
|
||||||
--- Signs ---
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
|
|
||||||
--- Signs ---
|
|
||||||
Signs for fixture.txt:
|
|
||||||
line=6 id=3001 name=GitGutterLineAdded
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
|
|
||||||
--- Signs ---
|
|
||||||
Signs for fixture.txt:
|
|
||||||
line=1 id=3000 name=GitGutterLineRemovedFirstLine
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
|
|
||||||
--- Signs ---
|
|
||||||
Signs for fixture.txt:
|
|
||||||
line=4 id=3000 name=GitGutterLineRemoved
|
|
||||||
162
test/runner.vim
Normal file
162
test/runner.vim
Normal file
@@ -0,0 +1,162 @@
|
|||||||
|
"
|
||||||
|
" Adapted from https://github.com/vim/vim/blob/master/src/testdir/runtest.vim
|
||||||
|
"
|
||||||
|
" When debugging tests it can help to write debug output:
|
||||||
|
" call Log('oh noes')
|
||||||
|
"
|
||||||
|
|
||||||
|
function RunTest(test)
|
||||||
|
if exists("*SetUp")
|
||||||
|
call SetUp()
|
||||||
|
endif
|
||||||
|
|
||||||
|
try
|
||||||
|
execute 'call '.a:test
|
||||||
|
catch
|
||||||
|
call Exception()
|
||||||
|
let s:errored = 1
|
||||||
|
endtry
|
||||||
|
|
||||||
|
if exists("*TearDown")
|
||||||
|
call TearDown()
|
||||||
|
endif
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function Log(msg)
|
||||||
|
if type(a:msg) == type('')
|
||||||
|
call add(s:messages, a:msg)
|
||||||
|
elseif type(a:msg) == type([])
|
||||||
|
call extend(s:messages, a:msg)
|
||||||
|
else
|
||||||
|
call add(v:errors, 'Exception: unsupported type: '.type(a:msg))
|
||||||
|
endif
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function Exception()
|
||||||
|
call add(v:errors, v:throwpoint.'..'.'Exception: '.v:exception)
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
" Shuffles list in place.
|
||||||
|
function Shuffle(list)
|
||||||
|
" Fisher-Yates-Durstenfeld-Knuth
|
||||||
|
let n = len(a:list)
|
||||||
|
if n < 2
|
||||||
|
return a:list
|
||||||
|
endif
|
||||||
|
for i in range(0, n-2)
|
||||||
|
let j = Random(0, n-i-1)
|
||||||
|
let e = a:list[i]
|
||||||
|
let a:list[i] = a:list[i+j]
|
||||||
|
let a:list[i+j] = e
|
||||||
|
endfor
|
||||||
|
return a:list
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
" Returns a pseudorandom integer i such that 0 <= i <= max
|
||||||
|
function Random(min, max)
|
||||||
|
if has('unix')
|
||||||
|
let i = system('echo $RANDOM') " 0 <= i <= 32767
|
||||||
|
else
|
||||||
|
let i = system('echo %RANDOM%') " 0 <= i <= 32767
|
||||||
|
endif
|
||||||
|
return i * (a:max - a:min + 1) / 32768 + a:min
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function FriendlyName(test_name)
|
||||||
|
return substitute(a:test_name[5:-3], '_', ' ', 'g')
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function Align(left, right)
|
||||||
|
if type(a:right) == type([])
|
||||||
|
let result = []
|
||||||
|
for s in a:right
|
||||||
|
if empty(result)
|
||||||
|
call add(result, printf('%-'.s:indent.'S', a:left).s)
|
||||||
|
else
|
||||||
|
call add(result, printf('%-'.s:indent.'S', '').s)
|
||||||
|
endif
|
||||||
|
endfor
|
||||||
|
return result
|
||||||
|
endif
|
||||||
|
|
||||||
|
return printf('%-'.s:indent.'S', a:left).a:right
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
let g:testname = expand('%')
|
||||||
|
let s:errored = 0
|
||||||
|
let s:done = 0
|
||||||
|
let s:fail = 0
|
||||||
|
let s:errors = 0
|
||||||
|
let s:messages = []
|
||||||
|
let s:indent = ''
|
||||||
|
|
||||||
|
call Log(g:testname.':')
|
||||||
|
|
||||||
|
" Source the test script.
|
||||||
|
try
|
||||||
|
source %
|
||||||
|
catch
|
||||||
|
let s:errors += 1
|
||||||
|
call Exception()
|
||||||
|
endtry
|
||||||
|
|
||||||
|
" Locate the test functions.
|
||||||
|
set nomore
|
||||||
|
redir @q
|
||||||
|
silent function /^Test_
|
||||||
|
redir END
|
||||||
|
let s:tests = split(substitute(@q, 'function \(\k*()\)', '\1', 'g'))
|
||||||
|
|
||||||
|
" If there is another argument, filter test-functions' names against it.
|
||||||
|
if argc() > 1
|
||||||
|
let s:tests = filter(s:tests, 'v:val =~ argv(1)')
|
||||||
|
endif
|
||||||
|
|
||||||
|
let s:indent = max(map(copy(s:tests), {_, val -> len(FriendlyName(val))}))
|
||||||
|
|
||||||
|
" Run the tests in random order.
|
||||||
|
for test in Shuffle(s:tests)
|
||||||
|
call RunTest(test)
|
||||||
|
let s:done += 1
|
||||||
|
|
||||||
|
let friendly_name = FriendlyName(test)
|
||||||
|
if len(v:errors) == 0
|
||||||
|
call Log(Align(friendly_name, ' - ok'))
|
||||||
|
else
|
||||||
|
if s:errored
|
||||||
|
let s:errors += 1
|
||||||
|
let s:errored = 0
|
||||||
|
else
|
||||||
|
let s:fail += 1
|
||||||
|
endif
|
||||||
|
call Log(Align(friendly_name, ' - not ok'))
|
||||||
|
|
||||||
|
let i = 0
|
||||||
|
for error in v:errors
|
||||||
|
if i != 0
|
||||||
|
call Log(Align('',' ! ----'))
|
||||||
|
endif
|
||||||
|
for trace in reverse(split(error, '\.\.'))
|
||||||
|
call Log(Align('', ' ! '.trace))
|
||||||
|
endfor
|
||||||
|
let i += 1
|
||||||
|
endfor
|
||||||
|
|
||||||
|
let v:errors = []
|
||||||
|
endif
|
||||||
|
endfor
|
||||||
|
|
||||||
|
let summary = [
|
||||||
|
\ s:done.( s:done == 1 ? ' test' : ' tests'),
|
||||||
|
\ s:errors.(s:errors == 1 ? ' error' : ' errors'),
|
||||||
|
\ s:fail.( s:fail == 1 ? ' failure' : ' failures'),
|
||||||
|
\ ]
|
||||||
|
call Log('')
|
||||||
|
call Log(join(summary, ', '))
|
||||||
|
|
||||||
|
split messages.log
|
||||||
|
call append(line('$'), s:messages)
|
||||||
|
write
|
||||||
|
|
||||||
|
qall!
|
||||||
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
|
|
||||||
--- Signs ---
|
|
||||||
Signs for fixture.txt:
|
|
||||||
line=9999 id=2999 name=GitGutterDummy
|
|
||||||
53
test/test
53
test/test
@@ -2,49 +2,18 @@
|
|||||||
|
|
||||||
VIM="/Applications/MacVim.app/Contents/MacOS/Vim -v"
|
VIM="/Applications/MacVim.app/Contents/MacOS/Vim -v"
|
||||||
|
|
||||||
status=0
|
$VIM -u NONE -U NONE -N \
|
||||||
|
--cmd 'set rtp+=../' \
|
||||||
|
--cmd 'let g:gitgutter_async=0' \
|
||||||
|
--cmd 'source ../plugin/gitgutter.vim' \
|
||||||
|
-S runner.vim \
|
||||||
|
test_*.vim \
|
||||||
|
$*
|
||||||
|
|
||||||
# Execute the tests.
|
cat messages.log
|
||||||
for editor in "$VIM" nvim; do
|
|
||||||
|
|
||||||
for testcase in test*.vim; do
|
|
||||||
$editor -N -u NONE --cmd 'let g:gitgutter_async=0' -S $testcase -c 'quit!'
|
|
||||||
|
|
||||||
git reset HEAD fixture.txt > /dev/null
|
|
||||||
git checkout fixture.txt
|
|
||||||
done
|
|
||||||
|
|
||||||
# Verify the results.
|
|
||||||
echo "$editor:"
|
|
||||||
echo
|
|
||||||
|
|
||||||
count_ok=0
|
|
||||||
count_fail=0
|
|
||||||
|
|
||||||
for expected in *.expected; do
|
|
||||||
name=${expected%.*}
|
|
||||||
actual=$name.actual
|
|
||||||
|
|
||||||
if diff $expected $actual; then
|
|
||||||
count_ok=$((count_ok + 1))
|
|
||||||
echo "$name ok"
|
|
||||||
rm $actual
|
|
||||||
else
|
|
||||||
count_fail=$((count_fail + 1))
|
|
||||||
echo "$name failed"
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
|
|
||||||
# Print results.
|
|
||||||
echo
|
|
||||||
echo "$((count_ok + count_fail)) tests"
|
|
||||||
echo "$count_ok ok"
|
|
||||||
echo "$count_fail failed"
|
|
||||||
echo
|
|
||||||
|
|
||||||
status=$(($status + $count_fail))
|
|
||||||
|
|
||||||
done
|
|
||||||
|
|
||||||
|
grep -q "0 errors, 0 failures" messages.log
|
||||||
|
status=$?
|
||||||
|
rm messages.log
|
||||||
exit $status
|
exit $status
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +0,0 @@
|
|||||||
source helper.vim
|
|
||||||
call Setup()
|
|
||||||
|
|
||||||
normal ggo*
|
|
||||||
write
|
|
||||||
call DumpSigns('addLines')
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
set shell=/usr/local/bin/fish
|
|
||||||
source helper.vim
|
|
||||||
call Setup()
|
|
||||||
|
|
||||||
normal ggo*
|
|
||||||
write
|
|
||||||
call DumpSigns('addLinesFish')
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
source helper.vim
|
|
||||||
call Setup()
|
|
||||||
|
|
||||||
normal 5Gi*
|
|
||||||
call system('git checkout -b fixture.txt')
|
|
||||||
write
|
|
||||||
call DumpSigns('ambiguousFile')
|
|
||||||
|
|
||||||
call system('git checkout - && git branch -d fixture.txt')
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
source helper.vim
|
|
||||||
|
|
||||||
let tmpfile = 'fileAddedToGit.tmp'
|
|
||||||
call system('touch '.tmpfile)
|
|
||||||
call system('git add '.tmpfile)
|
|
||||||
execute 'edit '.tmpfile
|
|
||||||
normal ihello
|
|
||||||
write
|
|
||||||
call DumpSigns('fileAddedToGit')
|
|
||||||
|
|
||||||
call system('git reset HEAD '.tmpfile)
|
|
||||||
call system('rm '.tmpfile)
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
source helper.vim
|
|
||||||
|
|
||||||
edit =fixture=.txt
|
|
||||||
normal ggo*
|
|
||||||
try
|
|
||||||
write
|
|
||||||
write
|
|
||||||
call DumpSigns('filenameWithEquals')
|
|
||||||
finally
|
|
||||||
call system('git reset HEAD =fixture=.txt')
|
|
||||||
call system('git checkout =fixture=.txt')
|
|
||||||
endtry
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
source helper.vim
|
|
||||||
|
|
||||||
edit fix[tu]re.txt
|
|
||||||
normal ggo*
|
|
||||||
write
|
|
||||||
call DumpSigns('filenameWithSquareBrackets')
|
|
||||||
|
|
||||||
call system('git reset HEAD fix[tu]re.txt')
|
|
||||||
call system('git checkout fix[tu]re.txt')
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
source helper.vim
|
|
||||||
|
|
||||||
let tmpfile = 'symlink'
|
|
||||||
call system('ln -nfs fixture.txt '.tmpfile)
|
|
||||||
execute 'edit '.tmpfile
|
|
||||||
execute '6d'
|
|
||||||
write
|
|
||||||
call DumpSigns('followSymlink')
|
|
||||||
|
|
||||||
call system('rm '.tmpfile)
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
source helper.vim
|
|
||||||
call Setup()
|
|
||||||
|
|
||||||
normal 5G
|
|
||||||
|
|
||||||
execute 'GitGutterStageHunk'
|
|
||||||
call DumpSigns('hunkOutsideNoopStageSigns')
|
|
||||||
call DumpGitDiffStaged('hunkHunkOutsideNoopStageGitDiffStaged')
|
|
||||||
|
|
||||||
execute 'GitGutterUndoHunk'
|
|
||||||
call DumpSigns('hunkOutsideNoopUndoSigns')
|
|
||||||
call DumpGitDiffStaged('hunkHunkOutsideNoopUndoGitDiffStaged')
|
|
||||||
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
source helper.vim
|
|
||||||
call Setup()
|
|
||||||
|
|
||||||
set shell=foo
|
|
||||||
|
|
||||||
normal 5Gi*
|
|
||||||
execute 'GitGutterStageHunk'
|
|
||||||
|
|
||||||
call assert_equal('foo', &shell) " NOTE: current test runner ignores v:errors so this line has no effect
|
|
||||||
|
|
||||||
set shell=/bin/bash
|
|
||||||
call DumpSigns('hunkStageSigns')
|
|
||||||
call DumpGitDiffStaged('hunkStageGitDiff')
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
source helper.vim
|
|
||||||
call Setup()
|
|
||||||
|
|
||||||
execute "normal! 2Gox\<CR>y\<CR>z"
|
|
||||||
normal 2jdd
|
|
||||||
normal k
|
|
||||||
execute 'GitGutterStageHunk'
|
|
||||||
call DumpSigns('hunkStageNearbySigns')
|
|
||||||
call DumpGitDiff('hunkStageNearbyGitDiff')
|
|
||||||
call DumpGitDiffStaged('hunkStageNearbyGitDiffStaged')
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
source helper.vim
|
|
||||||
call Setup()
|
|
||||||
|
|
||||||
set shell=foo
|
|
||||||
|
|
||||||
normal 5Gi*
|
|
||||||
execute 'GitGutterUndoHunk'
|
|
||||||
|
|
||||||
call assert_equal('foo', &shell) " NOTE: current test runner ignores v:errors so this line has no effect
|
|
||||||
set shell=/bin/bash
|
|
||||||
call DumpSigns('hunkUndoSigns')
|
|
||||||
call DumpGitDiffStaged('hunkUndoGitDiff')
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
source helper.vim
|
|
||||||
call Setup()
|
|
||||||
|
|
||||||
execute "normal! 2Gox\<CR>y\<CR>z"
|
|
||||||
normal 2jdd
|
|
||||||
normal k
|
|
||||||
execute 'GitGutterUndoHunk'
|
|
||||||
call DumpSigns('hunkUndoNearbySigns')
|
|
||||||
call DumpGitDiff('hunkUndoNearbyGitDiff')
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
source helper.vim
|
|
||||||
call Setup()
|
|
||||||
|
|
||||||
enew
|
|
||||||
execute "normal! \<C-^>"
|
|
||||||
call Dump('buffer: '.bufname(''), 'keepAlt')
|
|
||||||
call Dump('altbuffer: '.bufname('#'), 'keepAlt')
|
|
||||||
|
|
||||||
normal ggx
|
|
||||||
doautocmd CursorHold
|
|
||||||
call Dump('altbuffer: '.bufname('#'), 'keepAlt')
|
|
||||||
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
source helper.vim
|
|
||||||
call Setup()
|
|
||||||
|
|
||||||
normal 5Go*
|
|
||||||
call Dump("modified: ".getbufvar('', '&modified'), 'keepModified')
|
|
||||||
doautocmd CursorHold
|
|
||||||
call Dump("modified: ".getbufvar('', '&modified'), 'keepModified')
|
|
||||||
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
source helper.vim
|
|
||||||
call Setup()
|
|
||||||
|
|
||||||
normal 5Go*
|
|
||||||
call Dump("'[ mark: ".join(getpos("'["), ','), 'keepOpMarks')
|
|
||||||
call Dump("'] mark: ".join(getpos("']"), ','), 'keepOpMarks')
|
|
||||||
doautocmd CursorHold
|
|
||||||
call Dump("'[ mark: ".join(getpos("'["), ','), 'keepOpMarks')
|
|
||||||
call Dump("'] mark: ".join(getpos("']"), ','), 'keepOpMarks')
|
|
||||||
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
source helper.vim
|
|
||||||
call Setup()
|
|
||||||
|
|
||||||
normal ggi*
|
|
||||||
write
|
|
||||||
call DumpSigns('modifyLines')
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
source helper.vim
|
|
||||||
call Setup()
|
|
||||||
|
|
||||||
call DumpSigns('noModifications')
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
source helper.vim
|
|
||||||
call Setup()
|
|
||||||
|
|
||||||
execute "normal 5GoX\<CR>Y"
|
|
||||||
write
|
|
||||||
execute '6d'
|
|
||||||
write
|
|
||||||
call DumpSigns('orphanedSigns')
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
source helper.vim
|
|
||||||
call Setup()
|
|
||||||
|
|
||||||
execute '1d'
|
|
||||||
write
|
|
||||||
call DumpSigns('removeFirstLines')
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
source helper.vim
|
|
||||||
call Setup()
|
|
||||||
|
|
||||||
execute '5d'
|
|
||||||
write
|
|
||||||
call DumpSigns('removeLines')
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
source helper.vim
|
|
||||||
call Setup()
|
|
||||||
|
|
||||||
let g:gitgutter_sign_column_always=1
|
|
||||||
write
|
|
||||||
|
|
||||||
call DumpSigns('signColumnAlways')
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
source helper.vim
|
|
||||||
|
|
||||||
let tmpfile = tempname()
|
|
||||||
call system('touch '.tmpfile)
|
|
||||||
execute 'edit '.tmpfile
|
|
||||||
call DumpSigns('untrackedFileOutsideRepo')
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
source helper.vim
|
|
||||||
|
|
||||||
let tmpfile = '[un]trackedFileWithinRepo.tmp'
|
|
||||||
call system('touch '.tmpfile)
|
|
||||||
execute 'edit '.tmpfile
|
|
||||||
normal ggo*
|
|
||||||
doautocmd CursorHold
|
|
||||||
call DumpSigns('untrackedFileSquareBracketsWithinRepo')
|
|
||||||
|
|
||||||
call system('rm '.tmpfile)
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
source helper.vim
|
|
||||||
|
|
||||||
let tmpfile = 'untrackedFileWithinRepo.tmp'
|
|
||||||
call system('touch '.tmpfile)
|
|
||||||
execute 'edit '.tmpfile
|
|
||||||
normal ggo*
|
|
||||||
doautocmd CursorHold
|
|
||||||
call DumpSigns('untrackedFileWithinRepo')
|
|
||||||
|
|
||||||
call system('rm '.tmpfile)
|
|
||||||
400
test/test_gitgutter.vim
Normal file
400
test/test_gitgutter.vim
Normal file
@@ -0,0 +1,400 @@
|
|||||||
|
let s:current_dir = expand('%:p:h')
|
||||||
|
let s:test_repo = s:current_dir.'/test-repo'
|
||||||
|
let s:bufnr = bufnr('')
|
||||||
|
|
||||||
|
"
|
||||||
|
" Helpers
|
||||||
|
"
|
||||||
|
|
||||||
|
function s:signs(filename)
|
||||||
|
redir => signs
|
||||||
|
silent execute 'sign place'
|
||||||
|
redir END
|
||||||
|
|
||||||
|
let signs = split(signs, '\n')
|
||||||
|
|
||||||
|
" filter out signs for this test file
|
||||||
|
" assumes a:filename's signs are last set listed
|
||||||
|
let i = index(signs, 'Signs for '.a:filename.':')
|
||||||
|
let signs = (i > -1 ? signs[i+1:] : [])
|
||||||
|
|
||||||
|
call map(signs, {_, v -> substitute(v, ' ', '', '')})
|
||||||
|
|
||||||
|
return signs
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function s:git_diff()
|
||||||
|
return split(system('git diff -U0 fixture.txt'), '\n')
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function s:git_diff_staged()
|
||||||
|
return split(system('git diff -U0 --staged fixture.txt'), '\n')
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
|
"
|
||||||
|
" SetUp / TearDown
|
||||||
|
"
|
||||||
|
|
||||||
|
function SetUp()
|
||||||
|
call system("git init ".s:test_repo.
|
||||||
|
\ " && cd ".s:test_repo.
|
||||||
|
\ " && cp ../fixture.txt .".
|
||||||
|
\ " && git add . && git commit -m 'initial'")
|
||||||
|
execute ':cd' s:test_repo
|
||||||
|
edit! fixture.txt
|
||||||
|
call gitgutter#sign#reset()
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function TearDown()
|
||||||
|
" delete all buffers except this one
|
||||||
|
" TODO: move to runner.vim, accounting for multiple test files
|
||||||
|
if s:bufnr > 1
|
||||||
|
silent! execute '1,'.s:bufnr-1.'bdelete!'
|
||||||
|
endif
|
||||||
|
silent! execute s:bufnr+1.',$bdelete!'
|
||||||
|
|
||||||
|
execute ':cd' s:current_dir
|
||||||
|
call system("rm -rf ".s:test_repo)
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
"
|
||||||
|
" The tests
|
||||||
|
"
|
||||||
|
|
||||||
|
function Test_add_lines()
|
||||||
|
normal ggo*
|
||||||
|
write
|
||||||
|
|
||||||
|
let expected = ["line=2 id=3000 name=GitGutterLineAdded"]
|
||||||
|
call assert_equal(expected, s:signs('fixture.txt'))
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
|
function Test_add_lines_fish()
|
||||||
|
let _shell = &shell
|
||||||
|
set shell=/usr/local/bin/fish
|
||||||
|
|
||||||
|
normal ggo*
|
||||||
|
write
|
||||||
|
|
||||||
|
let expected = ["line=2 id=3000 name=GitGutterLineAdded"]
|
||||||
|
call assert_equal(expected, s:signs('fixture.txt'))
|
||||||
|
|
||||||
|
let &shell = _shell
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
|
function Test_modify_lines()
|
||||||
|
normal ggi*
|
||||||
|
write
|
||||||
|
|
||||||
|
let expected = ["line=1 id=3000 name=GitGutterLineModified"]
|
||||||
|
call assert_equal(expected, s:signs('fixture.txt'))
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
|
function Test_remove_lines()
|
||||||
|
execute '5d'
|
||||||
|
write
|
||||||
|
|
||||||
|
let expected = ["line=4 id=3000 name=GitGutterLineRemoved"]
|
||||||
|
call assert_equal(expected, s:signs('fixture.txt'))
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
|
function Test_remove_first_lines()
|
||||||
|
execute '1d'
|
||||||
|
write
|
||||||
|
|
||||||
|
let expected = ["line=1 id=3000 name=GitGutterLineRemovedFirstLine"]
|
||||||
|
call assert_equal(expected, s:signs('fixture.txt'))
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
|
function Test_edit_file_with_same_name_as_a_branch()
|
||||||
|
normal 5Gi*
|
||||||
|
call system('git checkout -b fixture.txt')
|
||||||
|
write
|
||||||
|
|
||||||
|
let expected = ["line=5 id=3000 name=GitGutterLineModified"]
|
||||||
|
call assert_equal(expected, s:signs('fixture.txt'))
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
|
function Test_file_added_to_git()
|
||||||
|
let tmpfile = 'fileAddedToGit.tmp'
|
||||||
|
call system('touch '.tmpfile.' && git add '.tmpfile)
|
||||||
|
execute 'edit '.tmpfile
|
||||||
|
normal ihello
|
||||||
|
write
|
||||||
|
|
||||||
|
let expected = ["line=1 id=3000 name=GitGutterLineAdded"]
|
||||||
|
call assert_equal(expected, s:signs('fileAddedToGit.tmp'))
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
|
function Test_filename_with_equals()
|
||||||
|
call system('touch =fixture=.txt && git add =fixture=.txt')
|
||||||
|
edit =fixture=.txt
|
||||||
|
normal ggo*
|
||||||
|
write
|
||||||
|
|
||||||
|
let expected = [
|
||||||
|
\ 'line=1 id=3000 name=GitGutterLineAdded',
|
||||||
|
\ 'line=2 id=3001 name=GitGutterLineAdded'
|
||||||
|
\ ]
|
||||||
|
call assert_equal(expected, s:signs('=fixture=.txt'))
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
|
function Test_filename_with_square_brackets()
|
||||||
|
call system('touch fix[tu]re.txt && git add fix[tu]re.txt')
|
||||||
|
edit fix[tu]re.txt
|
||||||
|
normal ggo*
|
||||||
|
write
|
||||||
|
|
||||||
|
let expected = [
|
||||||
|
\ 'line=1 id=3000 name=GitGutterLineAdded',
|
||||||
|
\ 'line=2 id=3001 name=GitGutterLineAdded'
|
||||||
|
\ ]
|
||||||
|
call assert_equal(expected, s:signs('fix[tu]re.txt'))
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
|
" FIXME: this test fails when it is the first (or only) test to be run
|
||||||
|
function Test_follow_symlink()
|
||||||
|
let tmp = 'symlink'
|
||||||
|
call system('ln -nfs fixture.txt '.tmp)
|
||||||
|
execute 'edit '.tmp
|
||||||
|
6d
|
||||||
|
write
|
||||||
|
|
||||||
|
let expected = ['line=5 id=3000 name=GitGutterLineRemoved']
|
||||||
|
call assert_equal(expected, s:signs('symlink'))
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
|
function Test_keep_alt()
|
||||||
|
enew
|
||||||
|
execute "normal! \<C-^>"
|
||||||
|
|
||||||
|
call assert_equal('fixture.txt', bufname(''))
|
||||||
|
call assert_equal('', bufname('#'))
|
||||||
|
|
||||||
|
normal ggx
|
||||||
|
doautocmd CursorHold
|
||||||
|
|
||||||
|
call assert_equal('', bufname('#'))
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
|
function Test_keep_modified()
|
||||||
|
normal 5Go*
|
||||||
|
call assert_equal(1, getbufvar('', '&modified'))
|
||||||
|
|
||||||
|
doautocmd CursorHold
|
||||||
|
|
||||||
|
call assert_equal(1, getbufvar('', '&modified'))
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
|
function Test_keep_op_marks()
|
||||||
|
normal 5Go*
|
||||||
|
call assert_equal([0,6,1,0], getpos("'["))
|
||||||
|
call assert_equal([0,6,2,0], getpos("']"))
|
||||||
|
|
||||||
|
doautocmd CursorHold
|
||||||
|
|
||||||
|
call assert_equal([0,6,1,0], getpos("'["))
|
||||||
|
call assert_equal([0,6,2,0], getpos("']"))
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
|
function Test_no_modifications()
|
||||||
|
call assert_equal([], s:signs('fixture.txt'))
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
|
function Test_orphaned_signs()
|
||||||
|
execute "normal 5GoX\<CR>Y"
|
||||||
|
write
|
||||||
|
6d
|
||||||
|
write
|
||||||
|
|
||||||
|
let expected = ['line=6 id=3001 name=GitGutterLineAdded']
|
||||||
|
call assert_equal(expected, s:signs('fixture.txt'))
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
|
function Test_sign_column_always()
|
||||||
|
let g:gitgutter_sign_column_always=1
|
||||||
|
write
|
||||||
|
|
||||||
|
let expected = ['line=9999 id=2999 name=GitGutterDummy']
|
||||||
|
call assert_equal(expected, s:signs('fixture.txt'))
|
||||||
|
|
||||||
|
let g:gitgutter_sign_column_always=0
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
|
function Test_untracked_file_outside_repo()
|
||||||
|
let tmp = tempname()
|
||||||
|
call system('touch '.tmp)
|
||||||
|
execute 'edit '.tmp
|
||||||
|
|
||||||
|
call assert_equal([], s:signs(tmp))
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
|
function Test_untracked_file_within_repo()
|
||||||
|
let tmp = 'untrackedFileWithinRepo.tmp'
|
||||||
|
call system('touch '.tmp)
|
||||||
|
execute 'edit '.tmp
|
||||||
|
normal ggo*
|
||||||
|
doautocmd CursorHold
|
||||||
|
|
||||||
|
call assert_equal([], s:signs(tmp))
|
||||||
|
|
||||||
|
call system('rm '.tmp)
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
|
function Test_untracked_file_square_brackets_within_repo()
|
||||||
|
let tmp = '[un]trackedFileWithinRepo.tmp'
|
||||||
|
call system('touch '.tmp)
|
||||||
|
execute 'edit '.tmp
|
||||||
|
normal ggo*
|
||||||
|
doautocmd CursorHold
|
||||||
|
|
||||||
|
call assert_equal([], s:signs(tmp))
|
||||||
|
|
||||||
|
call system('rm '.tmp)
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
|
function Test_hunk_outside_noop()
|
||||||
|
normal 5G
|
||||||
|
GitGutterStageHunk
|
||||||
|
|
||||||
|
call assert_equal([], s:signs('fixture.txt'))
|
||||||
|
call assert_equal([], s:git_diff())
|
||||||
|
call assert_equal([], s:git_diff_staged())
|
||||||
|
|
||||||
|
GitGutterUndoHunk
|
||||||
|
|
||||||
|
call assert_equal([], s:signs('fixture.txt'))
|
||||||
|
call assert_equal([], s:git_diff())
|
||||||
|
call assert_equal([], s:git_diff_staged())
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
|
function Test_hunk_stage()
|
||||||
|
let _shell = &shell
|
||||||
|
set shell=foo
|
||||||
|
|
||||||
|
normal 5Gi*
|
||||||
|
GitGutterStageHunk
|
||||||
|
|
||||||
|
call assert_equal('foo', &shell)
|
||||||
|
let &shell = _shell
|
||||||
|
|
||||||
|
call assert_equal([], s:signs('fixture.txt'))
|
||||||
|
|
||||||
|
call assert_equal([], s:git_diff())
|
||||||
|
|
||||||
|
let expected = [
|
||||||
|
\ 'diff --git a/fixture.txt b/fixture.txt',
|
||||||
|
\ 'index f5c6aff..ae8e546 100644',
|
||||||
|
\ '--- a/fixture.txt',
|
||||||
|
\ '+++ b/fixture.txt',
|
||||||
|
\ '@@ -5 +5 @@ d',
|
||||||
|
\ '-e',
|
||||||
|
\ '+*e'
|
||||||
|
\ ]
|
||||||
|
call assert_equal(expected, s:git_diff_staged())
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
|
function Test_hunk_stage_nearby_hunk()
|
||||||
|
execute "normal! 2Gox\<CR>y\<CR>z"
|
||||||
|
normal 2jdd
|
||||||
|
normal k
|
||||||
|
GitGutterStageHunk
|
||||||
|
|
||||||
|
let expected = [
|
||||||
|
\ 'line=3 id=3000 name=GitGutterLineAdded',
|
||||||
|
\ 'line=4 id=3001 name=GitGutterLineAdded',
|
||||||
|
\ 'line=5 id=3002 name=GitGutterLineAdded'
|
||||||
|
\ ]
|
||||||
|
call assert_equal(expected, s:signs('fixture.txt'))
|
||||||
|
|
||||||
|
let expected = [
|
||||||
|
\ 'diff --git a/fixture.txt b/fixture.txt',
|
||||||
|
\ 'index 53b13df..8fdfda7 100644',
|
||||||
|
\ '--- a/fixture.txt',
|
||||||
|
\ '+++ b/fixture.txt',
|
||||||
|
\ '@@ -2,0 +3,3 @@ b',
|
||||||
|
\ '+x',
|
||||||
|
\ '+y',
|
||||||
|
\ '+z',
|
||||||
|
\ ]
|
||||||
|
call assert_equal(expected, s:git_diff())
|
||||||
|
|
||||||
|
let expected = [
|
||||||
|
\ 'diff --git a/fixture.txt b/fixture.txt',
|
||||||
|
\ 'index f5c6aff..53b13df 100644',
|
||||||
|
\ '--- a/fixture.txt',
|
||||||
|
\ '+++ b/fixture.txt',
|
||||||
|
\ '@@ -4 +3,0 @@ c',
|
||||||
|
\ '-d',
|
||||||
|
\ ]
|
||||||
|
call assert_equal(expected, s:git_diff_staged())
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
|
function Test_hunk_undo()
|
||||||
|
let _shell = &shell
|
||||||
|
set shell=foo
|
||||||
|
|
||||||
|
normal 5Gi*
|
||||||
|
GitGutterUndoHunk
|
||||||
|
write " write file so we can verify git diff (--staged)
|
||||||
|
|
||||||
|
call assert_equal('foo', &shell)
|
||||||
|
let &shell = _shell
|
||||||
|
|
||||||
|
call assert_equal([], s:signs('fixture.txt'))
|
||||||
|
call assert_equal([], s:git_diff())
|
||||||
|
call assert_equal([], s:git_diff_staged())
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
|
function Test_undo_nearby_hunk()
|
||||||
|
execute "normal! 2Gox\<CR>y\<CR>z"
|
||||||
|
normal 2jdd
|
||||||
|
normal k
|
||||||
|
GitGutterUndoHunk
|
||||||
|
write " write file so we can verify git diff (--staged)
|
||||||
|
|
||||||
|
let expected = [
|
||||||
|
\ 'line=3 id=3000 name=GitGutterLineAdded',
|
||||||
|
\ 'line=4 id=3001 name=GitGutterLineAdded',
|
||||||
|
\ 'line=5 id=3002 name=GitGutterLineAdded'
|
||||||
|
\ ]
|
||||||
|
call assert_equal(expected, s:signs('fixture.txt'))
|
||||||
|
|
||||||
|
let expected = [
|
||||||
|
\ 'diff --git a/fixture.txt b/fixture.txt',
|
||||||
|
\ 'index f5c6aff..3fbde56 100644',
|
||||||
|
\ '--- a/fixture.txt',
|
||||||
|
\ '+++ b/fixture.txt',
|
||||||
|
\ '@@ -2,0 +3,3 @@ b',
|
||||||
|
\ '+x',
|
||||||
|
\ '+y',
|
||||||
|
\ '+z',
|
||||||
|
\ ]
|
||||||
|
call assert_equal(expected, s:git_diff())
|
||||||
|
|
||||||
|
call assert_equal([], s:git_diff_staged())
|
||||||
|
endfunction
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
|
|
||||||
--- Signs ---
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
|
|
||||||
--- Signs ---
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
|
|
||||||
--- Signs ---
|
|
||||||
Reference in New Issue
Block a user