Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
doc/tags
.DS_Store

messages.log
# Test specific files
FAILED
test.log
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
language: ruby

sudo: required
language: go

env:
global:
Expand Down
6 changes: 3 additions & 3 deletions autoload/go/fmt.vim
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function! go#fmt#Format(withGoimport) abort

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

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

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

" reload buffer to reflect latest changes
silent edit!
silent! edit!

let &fileformat = old_fileformat
let &syntax = &syntax
Expand Down
17 changes: 17 additions & 0 deletions autoload/go/fmt_test.vim
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,20 @@ func Test_run_fmt()

call assert_equal(expected, actual)
endfunc

func Test_update_file()
let expected = join(readfile("test-fixtures/fmt/hello_golden.go"), "\n")
let source_file = tempname()
call writefile(readfile("test-fixtures/fmt/hello_golden.go"), source_file)

let target_file = tempname()
call writefile([""], target_file)

" update_file now
call go#fmt#update_file(source_file, target_file)

" this should now contain the formatted code
let actual = join(readfile(target_file), "\n")

call assert_equal(expected, actual)
endfunc
63 changes: 33 additions & 30 deletions scripts/runtest.vim
Original file line number Diff line number Diff line change
@@ -1,77 +1,80 @@
" add vim-go the only plugin inside the runtimepath
"
let total_started = reltime()

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

" source test files
for s:vim_file in globpath(git_root_path . "/autoload/go", "*.vim", 0, 1)
if s:vim_file =~# '^\f\+_test\.vim$'
exec 'source ' . s:vim_file
endif
endfor
" source the passed test file
source %

" cd into the folder of the test file
let cd = exists('*haslocaldir') && haslocaldir() ? 'lcd ' : 'cd '
let dir = getcwd()
execute cd . expand('%:p:h')

" initialize variables
let g:testname = expand('%')
let s:fail = 0
let s:done = 0
let s:errors = []
let s:messages = []
let s:logs = []

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


" cd into autoload/go directory for tests
let cd = exists('*haslocaldir') && haslocaldir() ? 'lcd ' : 'cd '
let dir = getcwd()
execute cd . fnameescape(git_root_path."/autoload/go")

" Iterate over all tests and execute them
for s:test in sort(s:tests)
let started = reltime()

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

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

call add(s:messages, printf("=== %s (%ss)", s:test, elapsed_time))

let s:done += 1

if len(v:errors) > 0
let s:fail += 1
call add(s:errors, 'Found errors in ' . s:test . ':')
call extend(s:errors, v:errors)
call add(s:logs, printf("--- FAIL: %s (%ss)", s:test[:-3], elapsed_time))
call extend(s:logs, map(v:errors, '" ". v:val'))

" reset so we can capture failures of next test
let v:errors = []
else
call add(s:logs, printf("--- PASS: %s (%ss)", s:test[:-3], elapsed_time))
endif
endfor

" pop out into the scripts folder
execute cd . fnameescape(dir)

if len(s:errors) > 0 || s:done == 0
" Append errors to test.log
split test.log
call append(line('$'), '')
call append(line('$'), s:errors)
" create an empty fail to indicate that the test failed
if s:fail > 0
split FAILED
write
endif

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

let message = 'Executed ' . s:done . (s:done > 1 ? ' tests' : ' test') . '. Total test time: '. total_elapsed_time .'s'
call add(s:messages, "")
call add(s:messages, message)
call add(s:logs, "")
call add(s:logs, message)

" store all error messages from within vim into test.log
redir > test.log
silent messages
redir END

split messages.log
" also store all internal messages from s:logs: as well
split test.log
call append(line('$'), '')
call append(line('$'), s:messages)
call append(line('$'), 'From ' . g:testname . ':')
call append(line('$'), s:logs)
write

" bye, bye!
Expand Down
25 changes: 9 additions & 16 deletions scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,23 @@ cd $(dirname $0)
if [ -f "test.log" ]; then
rm test.log
fi
if [ -f "messages.log" ]; then
rm messages.log
fi

fail=0
if [ -f "FAILED" ]; then
rm FAILED
fi

vim -u NONE -S runtest.vim
for test_file in ../autoload/go/*_test.vim
do
vim -u NONE -S runtest.vim $test_file
done

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

if [ -f "messages.log" ]; then
cat messages.log
else
fail=1
echo "Couldn't find messages.log file"
fi

if [ $fail -gt 0 ]; then
# if Failed exists, test failed
if [ -f "FAILED" ]; then
echo 2>&1 "FAIL"
exit 1
fi
echo 2>&1 "PASS"