Skip to content

Commit f4be8f6

Browse files
committed
Use a different template for test files
Use a template more appropriate for test files than templates/hello_world.go when creating a test file in a directory with no Go files present. A new option, g:go_template_test_file, allows users to specify their own template to use.
1 parent 16e0ccf commit f4be8f6

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

autoload/go/template.vim

+7-4
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,24 @@ function! go#template#create() abort
1414
" files) from the directory create the template or use the cwd
1515
" as the name
1616
if l:package_name == -1 && l:go_template_use_pkg != 1
17-
let l:template_file = get(g:, 'go_template_file', "hello_world.go")
17+
let l:filename = fnamemodify(expand("%"), ':t')
18+
if l:filename =~ "_test.go$"
19+
let l:template_file = get(g:, 'go_template_test_file', "hello_world_test.go")
20+
else
21+
let l:template_file = get(g:, 'go_template_file', "hello_world.go")
22+
endif
1823
let l:template_path = go#util#Join(l:root_dir, "templates", l:template_file)
1924
exe '0r ' . fnameescape(l:template_path)
20-
$delete _
2125
elseif l:package_name == -1 && l:go_template_use_pkg == 1
2226
" cwd is now the dir of the package
2327
let l:path = fnamemodify(getcwd(), ':t')
2428
let l:content = printf("package %s", l:path)
2529
call append(0, l:content)
26-
$delete _
2730
else
2831
let l:content = printf("package %s", l:package_name)
2932
call append(0, l:content)
30-
$delete _
3133
endif
34+
$delete _
3235

3336
" Remove the '... [New File]' message line from the command line
3437
echon

doc/vim-go.txt

+11-3
Original file line numberDiff line numberDiff line change
@@ -1485,9 +1485,9 @@ Specifies whether `gocode` should use a different socket type. By default
14851485
*'g:go_template_autocreate'*
14861486

14871487
When a new Go file is created, vim-go automatically fills the buffer content
1488-
with a Go code template. By default the template under
1489-
`templates/hello_world.go` is used. This can be changed with the
1490-
|'g:go_template_file'| setting.
1488+
with a Go code template. By default, the templates under the `templates`
1489+
folder are used. This can be changed with the |'g:go_template_file'| and
1490+
|'g:go_template_test_file'| settings.
14911491

14921492
If the new file is created in an already prepopulated package (with other Go
14931493
files), in this case a Go code template with only the Go package declaration
@@ -1507,6 +1507,14 @@ is created. Checkout |'g:go_template_autocreate'| for more info. By default
15071507
the `hello_world.go` file is used.
15081508
>
15091509
let g:go_template_file = "hello_world.go"
1510+
<
1511+
*'g:go_template_test_file'*
1512+
1513+
Specifies the file under the `templates` folder that is used if a new Go test
1514+
file is created. Checkout |'g:go_template_autocreate'| for more info. By
1515+
default the `hello_world_test.go` file is used.
1516+
>
1517+
let g:go_template_test_file = "hello_world_test.go"
15101518
<
15111519
*'g:go_template_use_pkg'*
15121520

templates/hello_world_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package main
2+
3+
import "testing"
4+
5+
func TestHelloWorld(t *testing.T) {
6+
// t.Fatal("not implemented")
7+
}

0 commit comments

Comments
 (0)