Skip to content

Commit 7efc3fe

Browse files
authored
Better testing (fatih#1476)
Better testing - Support multiple Vim versions (currently 7.4, 8.0, and Neovim). - Make sure that we always run Vim from a temporary installation in `/tmp/vim-go-test` without loading `~/.vim`. This makes it a lot easier to run on people's computers. - Also add a handy `./script/run-vim` script to run the installed Vim from the temp directory. Useful for testing, debugging, etc. without a (potentially large) ~/.vim/ dir. - Previously the tests weren't actually being run correctly; see: https://travis-ci.org/fatih/vim-go/builds/279277579 - Format the output of the tests a wee bit nicer, roughly similar to the `go test` output. - Expand docs on testing a bit. I also attempted to integrate code coverage support with https://github.com/Vimjas/covimerage (which was the reason I started working on this), but couldn't really get that to work. Need to look in to that later.
1 parent 40e0289 commit 7efc3fe

14 files changed

Lines changed: 340 additions & 128 deletions

File tree

.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.git/

.github/CONTRIBUTING.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
Thanks for improving vim-go! Before you dive in please read the following:
22

33
1. Please read our
4-
[Documentation](https://github.com/fatih/vim-go/blob/master/doc/vim-go.txt), it might
5-
have answers for your problem
6-
2. If you add a new feature please don't forget to update the documentation:
4+
[Documentation](https://github.com/fatih/vim-go/blob/master/doc/vim-go.txt),
5+
it might have a solution to your problem.
6+
2. If you add a new feature then please don't forget to update the documentation:
77
[doc/vim-go.txt](https://github.com/fatih/vim-go/blob/master/doc/vim-go.txt).
8-
3. If it's a breaking change or exceed +100 lines please open an issue first
9-
and describe the changes you want to make.
8+
3. If it's a breaking change or exceeds 100 lines of code then please open an
9+
issue first and describe the changes you want to make.
10+
4. See `:help go-development` for instructions on how to run and write tests. If
11+
you add a new feature be sure you also include a test if feasible.
12+

.gitignore

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,2 @@
11
doc/tags
22
.DS_Store
3-
4-
# Test specific files
5-
FAILED
6-
test.log
7-
scripts/vim-vimhelplint

.travis.yml

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,11 @@
11
language: go
2-
3-
env:
4-
global:
5-
- DEPS=$HOME/deps
6-
- PATH=$DEPS/bin:$PATH
7-
- PATCH="v8.0.0134"
8-
9-
install: |
10-
git config --global user.email "you@example.com"
11-
git config --global user.name "Your Name"
12-
13-
# check out if we can pre-compiled Vim releases somehow,
14-
git clone --branch $PATCH --depth 1 https://github.com/vim/vim
15-
cd vim
16-
./configure --prefix=$DEPS --with-features=huge --disable-gui
17-
make
18-
make install
19-
cd -
20-
21-
script: ./scripts/test.sh
2+
matrix:
3+
include:
4+
- env: SCRIPT=test VIM_VERSION=vim-7.4
5+
- env: SCRIPT=test VIM_VERSION=vim-8.0
6+
- env: SCRIPT=test VIM_VERSION=nvim
7+
- env: SCRIPT=lint VIM_VERSION=vim-8.0
8+
install:
9+
- ./scripts/install-vim $VIM_VERSION
10+
script:
11+
- ./scripts/$SCRIPT $VIM_VERSION

Dockerfile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM golang:1.9.1
2+
3+
RUN apt-get update -y && \
4+
apt-get install -y build-essential curl git libncurses5-dev && \
5+
apt-get clean && \
6+
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
7+
8+
RUN useradd -ms /bin/bash -d /vim-go vim-go
9+
USER vim-go
10+
WORKDIR /vim-go
11+
COPY . /vim-go/
12+
13+
ENTRYPOINT ["make"]

Makefile

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,24 @@
1-
all: test
1+
all: install test lint
2+
3+
install:
4+
@echo "==> Installing Vims"
5+
@./scripts/install-vim vim-7.4
6+
@./scripts/install-vim vim-8.0
7+
@./scripts/install-vim nvim
28

39
test:
410
@echo "==> Running tests"
5-
@./scripts/test.sh
11+
@./scripts/test vim-7.4
12+
@./scripts/test vim-8.0
13+
@./scripts/test nvim
14+
15+
lint:
16+
@echo "==> Running linting tools"
17+
@./scripts/lint vim-8.0
18+
19+
clean:
20+
@echo "==> Cleaning /tmp/vim-go-test"
21+
@rm -rf /tmp/vim-go-test
22+
623

7-
.PHONY: all test
24+
.PHONY: all test install clean lint

autoload/go/tags_test.vim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ func Test_add_tags()
55
let expected = join(readfile("test-fixtures/tags/add_all_golden.go"), "\n")
66

77
" run for offset 40, which is inside the struct
8-
call go#tags#run(0, 0, 40, "add", input_file, 1)
8+
silent call go#tags#run(0, 0, 40, "add", input_file, 1)
99

1010
let actual = join(readfile(input_file), "\n")
1111

@@ -20,7 +20,7 @@ func Test_remove_tags()
2020
let expected = join(readfile("test-fixtures/tags/remove_all_golden.go"), "\n")
2121

2222
" run for offset 40, which is inside the struct
23-
call go#tags#run(0, 0, 40, "remove", input_file, 1)
23+
silent call go#tags#run(0, 0, 40, "remove", input_file, 1)
2424

2525
let actual = join(readfile(input_file), "\n")
2626

doc/vim-go.txt

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ Supported Go plugins~ *vim-go-plugins*
125125
The following plugins are supported for use with vim-go:
126126

127127
* Real-time completion (Vim):
128-
https://github.com/Shougo/neocomplete.vim
128+
https://github.com/Shougo/neocomplete.vim
129129

130130
* Real-time completion (Neovim):
131131
https://github.com/Shougo/deoplete.nvim and
@@ -566,13 +566,13 @@ CTRL-t
566566
*:GoFreevars*
567567
:GoFreevars
568568

569-
Enumerates the free variables of the selection. Free variables is a
569+
Enumerates the free variables of the selection. "Free variables" is a
570570
technical term meaning the set of variables that are referenced but not
571571
defined within the selection, or loosely speaking, its inputs.
572572

573-
This information is useful if you’re considering whether to refactor the
573+
This information is useful when considering whether to refactor the
574574
selection into a function of its own, as the free variables would be the
575-
necessary parameters of that function. It’s also useful when you want to
575+
necessary parameters of that function. It's also useful when you want to
576576
understand what the inputs are to a complex block of code even if you
577577
don’t plan to change it.
578578

@@ -1181,11 +1181,11 @@ used >
11811181
Use this option to add additional options to the |'g:go_fmt_command'|. It's
11821182
value type can be either a string or a dictionary. This is due backwards
11831183
compatibility. The string version will be removed in the future so please use
1184-
the dictionary version. Default is empty.
1184+
the dictionary version. Default is empty.
11851185
>
11861186
let g:go_fmt_options = ''
11871187
1188-
or
1188+
or
11891189
11901190
let g:go_fmt_options = {}
11911191
<
@@ -1394,7 +1394,7 @@ Specifies the type of list to use for command outputs (such as errors from
13941394
builds, results from static analysis commands, etc...). The list type for
13951395
specific commands can be overridden with |'g:go_list_type_commands'|. The
13961396
default value (empty) will use the appropriate kind of list for the command
1397-
that was called. Supported values are "", "quickfix", and "locationlist".
1397+
that was called. Supported values are "", "quickfix", and "locationlist".
13981398
>
13991399
let g:go_list_type = ""
14001400
<
@@ -1422,7 +1422,7 @@ As an example, the following settings will change all list types to
14221422

14231423
Specifies whether the quickfix/location list should be closed automatically
14241424
in the absence of errors. The default value is 1.
1425-
If you prefer to keep a long running error window open, you can disable
1425+
If you prefer to keep a long running error window open, you can disable
14261426
this by setting the value to 0.
14271427
>
14281428
let g:go_list_autoclose = 1
@@ -1815,7 +1815,7 @@ manually, usually from an |autocommand|:
18151815
If you have a lot of packages with the same prefix (`github.com/user`) you can
18161816
use a single autocommand:
18171817
>
1818-
autocmd BufRead /home/martin/go/src/*.go
1818+
autocmd BufRead /home/martin/go/src/*.go
18191819
\ let s:tmp = matchlist(expand('%:p'),
18201820
\ '/home/martin/go/src/\(github.com/user/[^/]\+\)')
18211821
\| if len(s:tmp) > 1 | exe 'silent :GoGuruScope ' . s:tmp[1] | endif
@@ -1916,23 +1916,32 @@ By default new terminals are opened in a vertical split. To change it
19161916
==============================================================================
19171917
DEVELOPMENT *go-development*
19181918

1919-
vim-go supports test files written in VimL. Please check `autoload` folder for
1920-
examples. If you add a new feature be sure you also include the `_test.vim`
1921-
file next to the script. Test functions should be starting with `Test_`,
1922-
example:
1919+
vim-go supports test files written in VimScript; the way they're run is
1920+
roughly similar to Go tests:
1921+
1922+
- A `*.vim` file has a corresponding `*_test.vim`.
1923+
- All functions starting with `Test_` are run as test.
1924+
- A test is considered to be "failed" if |v:errors| has any entries. You can
1925+
use one of the |test-functions| to set this, or append to it directly.
1926+
1927+
A simple example:
19231928
>
19241929
function Test_run_fmt()
19251930
call assert_equal(expected, actual)
19261931
...
19271932
endfunction
19281933
<
1929-
You can locally test it by running:
1930-
>
1931-
make
1932-
<
1933-
This will run all tests and print either `PASS` or `FAIL` to indicate the
1934-
final status of all tests. Additionally, each new pull request will trigger a
1935-
new Travis-ci job.
1934+
To run tests vim-go comes with three small helper scripts:
1935+
1936+
`scripts/install-vim` Install a pristine Vim to `/tmp/vim-go-test/`.
1937+
`scripts/run-vim` Run a Vim version from `/tmp/vim-go-test/`.
1938+
`scripts/test` Run all tests with a Vim from `/tmp/vim-go-test/`.
1939+
1940+
All scripts accept a Vim version as the first argument, which can be
1941+
`vim-7.4`, `vim-8.0`, or `nvim`. You will need to install a Vim version with
1942+
`install-vim` before you can use `run-vim` or `test`.
1943+
1944+
You can install and test all Vim versions by running `make`.
19361945

19371946

19381947
==============================================================================
@@ -1960,4 +1969,4 @@ CREDITS *go-credits*
19601969
* vim-go contributors: https://github.com/fatih/vim-go/graphs/contributors.
19611970

19621971

1963-
vim:ft=help:et:ts=2:sw=2:sts=2:norl
1972+
vim: ft=help tw=78 et ts=2 sw=2 sts=2 norl

scripts/install-vim

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#!/bin/sh
2+
#
3+
# Install and setup a Vim or Neovim for running tests.
4+
# This should work on both Travis and people's desktop computers, and be 100%
5+
# independent from any system installed Vim.
6+
#
7+
# It will echo the full path to a Vim binary, e.g.:
8+
# /some/path/src/vim
9+
10+
set -euC
11+
12+
vimgodir=$(cd -P "$(dirname "$0")/.." > /dev/null && pwd)
13+
cd "$vimgodir"
14+
15+
vim=${1:-}
16+
17+
case "$vim" in
18+
"vim-7.4")
19+
# This is what the most recent Ubuntu LTS (16.04) ships with.
20+
tag="v7.4.1689"
21+
giturl="https://github.com/vim/vim"
22+
;;
23+
24+
"vim-8.0")
25+
# This follows the version in Arch Linux. Vim's master branch isn't always
26+
# stable, and we don't want to have the build fail because Vim introduced a
27+
# bug.
28+
tag="v8.0.1176"
29+
giturl="https://github.com/vim/vim"
30+
;;
31+
32+
"nvim")
33+
# Use latest stable version.
34+
tag="v0.2.0"
35+
giturl="https://github.com/neovim/neovim"
36+
;;
37+
38+
*)
39+
echo "unknown version: '${1:-}'"
40+
echo "First argument must be 'vim-7.4', 'vim-8.0', or 'nvim'."
41+
exit 1
42+
;;
43+
esac
44+
45+
srcdir="/tmp/vim-go-test/$1-src"
46+
installdir="/tmp/vim-go-test/$1-install"
47+
48+
# Use cached installdir.
49+
if [ -d "$installdir" ]; then
50+
echo "$installdir exists; skipping build."
51+
52+
# The ./scripts/test script relies on this.
53+
echo "installed to: $installdir"
54+
exit 0
55+
fi
56+
57+
mkdir -p "$srcdir"
58+
cd "$srcdir"
59+
60+
# Neovim build requires more deps than Vim and is annoying, so we use the
61+
# binary.
62+
# 0.2.0 doesn't have a binary build for Linux, so we use 0.2.1-dev for now.
63+
if [ "$1" = "nvim" ]; then
64+
65+
# TODO: Use macOS binaries on macOS
66+
curl -Ls https://github.com/neovim/neovim/releases/download/nightly/nvim-linux64.tar.gz |
67+
tar xzf - -C /tmp/vim-go-test/
68+
mv /tmp/vim-go-test/nvim-linux64 /tmp/vim-go-test/nvim-install
69+
mkdir -p "$installdir/share/nvim/runtime/pack/vim-go/start"
70+
ln -s "$vimgodir" "$installdir/share/nvim/runtime/pack/vim-go/start/vim-go"
71+
72+
# Consistent paths makes calling things easier.
73+
mv "$installdir/bin/nvim" "$installdir/bin/vim"
74+
mkdir -p "$installdir/share/vim/vimgo/pack"
75+
ln -s "$installdir/share/nvim/runtime/pack/vim-go" "$installdir/share/vim/vimgo/pack/vim-go"
76+
77+
# Build Vim from source.
78+
else
79+
if [ -d "$srcdir/.git" ]; then
80+
echo "Skipping clone as $srcdir/.git exists"
81+
else
82+
echo "Cloning $tag from $giturl"
83+
git clone --branch "$tag" --depth 1 "$giturl" "$srcdir"
84+
fi
85+
86+
./configure --prefix="$installdir" --with-features=huge --disable-gui
87+
make install
88+
mkdir -p "$installdir/share/vim/vimgo/pack/vim-go/start"
89+
ln -s "$vimgodir" "$installdir/share/vim/vimgo/pack/vim-go/start/vim-go"
90+
fi
91+
92+
# Make sure all Go tools and other dependencies are installed.
93+
export GOPATH=$installdir
94+
export PATH=${GOPATH}/bin:$PATH
95+
"$vimgodir/scripts/run-vim" $vim +':silent :GoUpdateBinaries' +':qa'
96+
97+
[ -d "$installdir/share/vim/vimgo/pack/vim-go/start/vim-vimhelplint" ] || \
98+
git clone --depth 1 --quiet https://github.com/machakann/vim-vimhelplint \
99+
"$installdir/share/vim/vimgo/pack/vim-go/start/vim-vimhelplint"
100+
101+
# Don't really need source after successful install.
102+
rm -rf "$srcdir"
103+
104+
echo "installed to: $installdir"
105+
106+
# vim:ts=2:sts=2:sw=2:et

scripts/lint

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/bin/sh
2+
#
3+
# Run all linting tools.
4+
#
5+
6+
set -euC
7+
vimgodir=$(cd -P "$(dirname "$0")/.." > /dev/null && pwd)
8+
cd "$vimgodir"
9+
10+
### Setup Vim and other dependencies.
11+
#####################################
12+
if [ -z "${1:-}" ]; then
13+
echo "unknown version: '${1:-}'"
14+
echo "First argument must be 'vim-7.4', 'vim-8.0', or 'nvim'."
15+
exit 1
16+
fi
17+
18+
vim=$1
19+
vimdir="/tmp/vim-go-test/$vim-install"
20+
export GOPATH=$vimdir
21+
export PATH=${GOPATH}/bin:$PATH
22+
23+
if [ ! -f "$vimdir/bin/vim" ]; then
24+
echo "$vimdir/bin/vim doesn't exist; did you install it with the install-vim script?"
25+
exit 1
26+
fi
27+
28+
### Run vimhelplint.
29+
####################
30+
printf "Running vimhelplint ... "
31+
32+
# set modeline explicitly so that the modeline will be respected when run as root.
33+
lint=$($vimdir/bin/vim -esNR \
34+
--cmd "set rtp+=$vimdir/share/vim/vimgo/pack/vim-go/start/vim-vimhelplint/" \
35+
--cmd 'set modeline' \
36+
+'filetype plugin on' \
37+
+"e $vimgodir/doc/vim-go.txt" \
38+
+'verbose VimhelpLintEcho' \
39+
+q \
40+
2>&1)
41+
if [ "$lint" ]; then
42+
echo "FAILED"
43+
echo "$lint"
44+
exit 6
45+
else
46+
echo "PASSED"
47+
exit 0
48+
fi

0 commit comments

Comments
 (0)