Skip to content

Commit b586c7e

Browse files
committed
tests: add new test, update runttest.vim
1 parent b27031a commit b586c7e

File tree

6 files changed

+65
-53
lines changed

6 files changed

+65
-53
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
doc/tags
22
.DS_Store
33

4-
messages.log
4+
# Test specific files
5+
FAILED
56
test.log

.travis.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
language: ruby
2-
3-
sudo: required
1+
language: go
42

53
env:
64
global:

autoload/go/fmt.vim

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ function! go#fmt#Format(withGoimport) abort
7575

7676
let out = go#fmt#run(bin_name, l:tmpname, expand('%'))
7777
if go#util#ShellError() == 0
78-
call s:update_file(l:tmpname, expand('%'))
78+
call go#fmt#update_file(l:tmpname, expand('%'))
7979
elseif g:go_fmt_fail_silently == 0
8080
let errors = s:parse_errors(out)
8181
call s:show_errors(errors)
@@ -102,7 +102,7 @@ function! go#fmt#Format(withGoimport) abort
102102
endfunction
103103

104104
" update_file updates the target file with the given formatted source
105-
function! s:update_file(source, target)
105+
function! go#fmt#update_file(source, target)
106106
" remove undo point caused via BufWritePre
107107
try | silent undojoin | catch | endtry
108108

@@ -120,7 +120,7 @@ function! s:update_file(source, target)
120120
endif
121121

122122
" reload buffer to reflect latest changes
123-
silent edit!
123+
silent! edit!
124124

125125
let &fileformat = old_fileformat
126126
let &syntax = &syntax

autoload/go/fmt_test.vim

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,20 @@ func Test_run_fmt()
1212

1313
call assert_equal(expected, actual)
1414
endfunc
15+
16+
func Test_update_file()
17+
let expected = join(readfile("test-fixtures/fmt/hello_golden.go"), "\n")
18+
let source_file = tempname()
19+
call writefile(readfile("test-fixtures/fmt/hello_golden.go"), source_file)
20+
21+
let target_file = tempname()
22+
call writefile([""], target_file)
23+
24+
" update_file now
25+
call go#fmt#update_file(source_file, target_file)
26+
27+
" this should now contain the formatted code
28+
let actual = join(readfile(target_file), "\n")
29+
30+
call assert_equal(expected, actual)
31+
endfunc

scripts/runtest.vim

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,80 @@
1-
" add vim-go the only plugin inside the runtimepath
2-
"
31
let total_started = reltime()
42

3+
" add vim-go the only plugin inside the runtimepath
54
let git_root_path = system("git rev-parse --show-toplevel | tr -d '\\n'")
65
exe 'set rtp=' . git_root_path
76

8-
" source test files
9-
for s:vim_file in globpath(git_root_path . "/autoload/go", "*.vim", 0, 1)
10-
if s:vim_file =~# '^\f\+_test\.vim$'
11-
exec 'source ' . s:vim_file
12-
endif
13-
endfor
7+
" source the passed test file
8+
source %
9+
10+
" cd into the folder of the test file
11+
let cd = exists('*haslocaldir') && haslocaldir() ? 'lcd ' : 'cd '
12+
let dir = getcwd()
13+
execute cd . expand('%:p:h')
1414

15+
" initialize variables
16+
let g:testname = expand('%')
1517
let s:fail = 0
1618
let s:done = 0
17-
let s:errors = []
18-
let s:messages = []
19+
let s:logs = []
1920

20-
" get a list of all Test_ functions (sourced above)
21+
" get a list of all Test_ functions for the given file
2122
set nomore
2223
redir @q
2324
silent function /^Test_
2425
redir END
2526
let s:tests = split(substitute(@q, 'function \(\k*()\)', '\1', 'g'))
2627

27-
28-
" cd into autoload/go directory for tests
29-
let cd = exists('*haslocaldir') && haslocaldir() ? 'lcd ' : 'cd '
30-
let dir = getcwd()
31-
execute cd . fnameescape(git_root_path."/autoload/go")
32-
3328
" Iterate over all tests and execute them
3429
for s:test in sort(s:tests)
3530
let started = reltime()
3631

32+
call add(s:logs, printf("=== RUN %s", s:test[:-3]))
3733
exe 'call ' . s:test
3834

3935
let elapsed_time = reltimestr(reltime(started))
4036
let elapsed_time = substitute(elapsed_time, '^\s*\(.\{-}\)\s*$', '\1', '')
4137

42-
call add(s:messages, printf("=== %s (%ss)", s:test, elapsed_time))
43-
4438
let s:done += 1
4539

4640
if len(v:errors) > 0
4741
let s:fail += 1
48-
call add(s:errors, 'Found errors in ' . s:test . ':')
49-
call extend(s:errors, v:errors)
42+
call add(s:logs, printf("--- FAIL: %s (%ss)", s:test[:-3], elapsed_time))
43+
call extend(s:logs, map(v:errors, '" ". v:val'))
44+
45+
" reset so we can capture failures of next test
5046
let v:errors = []
47+
else
48+
call add(s:logs, printf("--- PASS: %s (%ss)", s:test[:-3], elapsed_time))
5149
endif
5250
endfor
5351

52+
" pop out into the scripts folder
5453
execute cd . fnameescape(dir)
5554

56-
if len(s:errors) > 0 || s:done == 0
57-
" Append errors to test.log
58-
split test.log
59-
call append(line('$'), '')
60-
call append(line('$'), s:errors)
55+
" create an empty fail to indicate that the test failed
56+
if s:fail > 0
57+
split FAILED
6158
write
6259
endif
6360

6461
let total_elapsed_time = reltimestr(reltime(total_started))
6562
let total_elapsed_time = substitute(total_elapsed_time, '^\s*\(.\{-}\)\s*$', '\1', '')
6663

6764
let message = 'Executed ' . s:done . (s:done > 1 ? ' tests' : ' test') . '. Total test time: '. total_elapsed_time .'s'
68-
call add(s:messages, "")
69-
call add(s:messages, message)
65+
call add(s:logs, "")
66+
call add(s:logs, message)
7067

68+
" store all error messages from within vim into test.log
69+
redir > test.log
70+
silent messages
71+
redir END
7172

72-
split messages.log
73+
" also store all internal messages from s:logs: as well
74+
split test.log
7375
call append(line('$'), '')
74-
call append(line('$'), s:messages)
76+
call append(line('$'), 'From ' . g:testname . ':')
77+
call append(line('$'), s:logs)
7578
write
7679

7780
" bye, bye!

scripts/test.sh

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,23 @@ cd $(dirname $0)
88
if [ -f "test.log" ]; then
99
rm test.log
1010
fi
11-
if [ -f "messages.log" ]; then
12-
rm messages.log
13-
fi
1411

15-
fail=0
12+
if [ -f "FAILED" ]; then
13+
rm FAILED
14+
fi
1615

17-
vim -u NONE -S runtest.vim
16+
for test_file in ../autoload/go/*_test.vim
17+
do
18+
vim -u NONE -S runtest.vim $test_file
19+
done
1820

19-
# test.log only exists if a test fails, output it so we see it
2021
if [ -f "test.log" ]; then
21-
fail=1
2222
cat test.log
2323
fi
2424

25-
if [ -f "messages.log" ]; then
26-
cat messages.log
27-
else
28-
fail=1
29-
echo "Couldn't find messages.log file"
30-
fi
31-
32-
if [ $fail -gt 0 ]; then
25+
# if Failed exists, test failed
26+
if [ -f "FAILED" ]; then
3327
echo 2>&1 "FAIL"
3428
exit 1
3529
fi
3630
echo 2>&1 "PASS"
37-

0 commit comments

Comments
 (0)