Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6804100

Browse files
committed
test golang directly
1 parent f7df13f commit 6804100

7 files changed

Lines changed: 117 additions & 161 deletions

File tree

testing/resources/golang_hooks_repo/.pre-commit-hooks.yaml

Lines changed: 0 additions & 5 deletions
This file was deleted.

testing/resources/golang_hooks_repo/go.mod

Lines changed: 0 additions & 5 deletions
This file was deleted.

testing/resources/golang_hooks_repo/go.sum

Lines changed: 0 additions & 2 deletions
This file was deleted.

testing/resources/golang_hooks_repo/golang-hello-world/main.go

Lines changed: 0 additions & 23 deletions
This file was deleted.

tests/languages/golang_test.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66
import re_assert
77

88
import pre_commit.constants as C
9+
from pre_commit.envcontext import envcontext
910
from pre_commit.languages import golang
1011
from pre_commit.languages import helpers
12+
from pre_commit.store import _make_local_repo
13+
from testing.language_helpers import run_language
1114

1215

1316
ACTUAL_GET_DEFAULT_VERSION = golang.get_default_version.__wrapped__
@@ -41,3 +44,93 @@ def test_golang_infer_go_version_default():
4144

4245
assert version != C.DEFAULT
4346
re_assert.Matches(r'^\d+\.\d+(?:\.\d+)?$').assert_matches(version)
47+
48+
49+
def _make_hello_world(tmp_path):
50+
go_mod = '''\
51+
module golang-hello-world
52+
53+
go 1.18
54+
55+
require github.com/BurntSushi/toml v1.1.0
56+
'''
57+
go_sum = '''\
58+
github.com/BurntSushi/toml v1.1.0 h1:ksErzDEI1khOiGPgpwuI7x2ebx/uXQNw7xJpn9Eq1+I=
59+
github.com/BurntSushi/toml v1.1.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
60+
''' # noqa: E501
61+
hello_world_go = '''\
62+
package main
63+
64+
65+
import (
66+
"fmt"
67+
"github.com/BurntSushi/toml"
68+
)
69+
70+
type Config struct {
71+
What string
72+
}
73+
74+
func main() {
75+
var conf Config
76+
toml.Decode("What = 'world'\\n", &conf)
77+
fmt.Printf("hello %v\\n", conf.What)
78+
}
79+
'''
80+
tmp_path.joinpath('go.mod').write_text(go_mod)
81+
tmp_path.joinpath('go.sum').write_text(go_sum)
82+
mod_dir = tmp_path.joinpath('golang-hello-world')
83+
mod_dir.mkdir()
84+
main_file = mod_dir.joinpath('main.go')
85+
main_file.write_text(hello_world_go)
86+
87+
88+
def test_golang_system(tmp_path):
89+
_make_hello_world(tmp_path)
90+
91+
ret = run_language(tmp_path, golang, 'golang-hello-world')
92+
assert ret == (0, b'hello world\n')
93+
94+
95+
def test_golang_default_version(tmp_path):
96+
_make_hello_world(tmp_path)
97+
98+
ret = run_language(
99+
tmp_path,
100+
golang,
101+
'golang-hello-world',
102+
version=C.DEFAULT,
103+
)
104+
assert ret == (0, b'hello world\n')
105+
106+
107+
def test_golang_versioned(tmp_path):
108+
_make_local_repo(str(tmp_path))
109+
110+
ret, out = run_language(
111+
tmp_path,
112+
golang,
113+
'go version',
114+
version='1.18.4',
115+
)
116+
117+
assert ret == 0
118+
assert out.startswith(b'go version go1.18.4')
119+
120+
121+
def test_local_golang_additional_deps(tmp_path):
122+
_make_local_repo(str(tmp_path))
123+
124+
ret = run_language(
125+
tmp_path,
126+
golang,
127+
'hello',
128+
deps=('golang.org/x/example/hello@latest',),
129+
)
130+
131+
assert ret == (0, b'Hello, Go examples!\n')
132+
133+
134+
def test_golang_hook_still_works_when_gobin_is_set(tmp_path):
135+
with envcontext((('GOBIN', str(tmp_path.joinpath('gobin'))),)):
136+
test_golang_system(tmp_path)

tests/repository_test.py

Lines changed: 0 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,9 @@
1010
import re_assert
1111

1212
import pre_commit.constants as C
13-
from pre_commit import git
1413
from pre_commit.clientlib import CONFIG_SCHEMA
1514
from pre_commit.clientlib import load_manifest
16-
from pre_commit.envcontext import envcontext
1715
from pre_commit.hook import Hook
18-
from pre_commit.languages import golang
1916
from pre_commit.languages import helpers
2017
from pre_commit.languages import python
2118
from pre_commit.languages.all import languages
@@ -169,92 +166,6 @@ def test_system_hook_with_spaces(tempdir_factory, store):
169166
)
170167

171168

172-
def test_golang_system_hook(tempdir_factory, store):
173-
_test_hook_repo(
174-
tempdir_factory, store, 'golang_hooks_repo',
175-
'golang-hook', ['system'], b'hello world from system\n',
176-
config_kwargs={
177-
'hooks': [{
178-
'id': 'golang-hook',
179-
'language_version': 'system',
180-
}],
181-
},
182-
)
183-
184-
185-
def test_golang_versioned_hook(tempdir_factory, store):
186-
_test_hook_repo(
187-
tempdir_factory, store, 'golang_hooks_repo',
188-
'golang-hook', [], b'hello world from go1.18.4\n',
189-
config_kwargs={
190-
'hooks': [{
191-
'id': 'golang-hook',
192-
'language_version': '1.18.4',
193-
}],
194-
},
195-
)
196-
197-
198-
def test_golang_hook_still_works_when_gobin_is_set(tempdir_factory, store):
199-
gobin_dir = tempdir_factory.get()
200-
with envcontext((('GOBIN', gobin_dir),)):
201-
test_golang_system_hook(tempdir_factory, store)
202-
assert os.listdir(gobin_dir) == []
203-
204-
205-
def test_golang_with_recursive_submodule(tmpdir, tempdir_factory, store):
206-
sub_go = '''\
207-
package sub
208-
209-
import "fmt"
210-
211-
func Func() {
212-
fmt.Println("hello hello world")
213-
}
214-
'''
215-
sub = tmpdir.join('sub').ensure_dir()
216-
sub.join('sub.go').write(sub_go)
217-
cmd_output('git', '-C', str(sub), 'init', '.')
218-
cmd_output('git', '-C', str(sub), 'add', '.')
219-
git.commit(str(sub))
220-
221-
pre_commit_hooks = '''\
222-
- id: example
223-
name: example
224-
entry: example
225-
language: golang
226-
verbose: true
227-
'''
228-
go_mod = '''\
229-
module github.com/asottile/example
230-
231-
go 1.14
232-
'''
233-
main_go = '''\
234-
package main
235-
236-
import "github.com/asottile/example/sub"
237-
238-
func main() {
239-
sub.Func()
240-
}
241-
'''
242-
repo = tmpdir.join('repo').ensure_dir()
243-
repo.join('.pre-commit-hooks.yaml').write(pre_commit_hooks)
244-
repo.join('go.mod').write(go_mod)
245-
repo.join('main.go').write(main_go)
246-
cmd_output('git', '-C', str(repo), 'init', '.')
247-
cmd_output('git', '-C', str(repo), 'add', '.')
248-
cmd_output('git', '-C', str(repo), 'submodule', 'add', str(sub), 'sub')
249-
git.commit(str(repo))
250-
251-
config = make_config_from_repo(str(repo))
252-
hook = _get_hook(config, store, 'example')
253-
ret, out = _hook_run(hook, (), color=False)
254-
assert ret == 0
255-
assert _norm_out(out) == b'hello hello world\n'
256-
257-
258169
def test_missing_executable(tempdir_factory, store):
259170
_test_hook_repo(
260171
tempdir_factory, store, 'not_found_exe',
@@ -419,43 +330,6 @@ def test_repository_state_compatibility(tempdir_factory, store, v):
419330
assert _hook_installed(hook) is True
420331

421332

422-
def test_additional_golang_dependencies_installed(
423-
tempdir_factory, store,
424-
):
425-
path = make_repo(tempdir_factory, 'golang_hooks_repo')
426-
config = make_config_from_repo(path)
427-
# A small go package
428-
deps = ['golang.org/x/example/hello@latest']
429-
config['hooks'][0]['additional_dependencies'] = deps
430-
hook = _get_hook(config, store, 'golang-hook')
431-
envdir = helpers.environment_dir(
432-
hook.prefix,
433-
golang.ENVIRONMENT_DIR,
434-
golang.get_default_version(),
435-
)
436-
binaries = os.listdir(os.path.join(envdir, 'bin'))
437-
# normalize for windows
438-
binaries = [os.path.splitext(binary)[0] for binary in binaries]
439-
assert 'hello' in binaries
440-
441-
442-
def test_local_golang_additional_dependencies(store):
443-
config = {
444-
'repo': 'local',
445-
'hooks': [{
446-
'id': 'hello',
447-
'name': 'hello',
448-
'entry': 'hello',
449-
'language': 'golang',
450-
'additional_dependencies': ['golang.org/x/example/hello@latest'],
451-
}],
452-
}
453-
hook = _get_hook(config, store, 'hello')
454-
ret, out = _hook_run(hook, (), color=False)
455-
assert ret == 0
456-
assert _norm_out(out) == b'Hello, Go examples!\n'
457-
458-
459333
def test_fail_hooks(store):
460334
config = {
461335
'repo': 'local',

tests/store_test.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,3 +246,27 @@ def _chmod_minus_w(p):
246246
# should be skipped due to readonly
247247
store.mark_config_used(str(cfg))
248248
assert store.select_all_configs() == []
249+
250+
251+
def test_clone_with_recursive_submodules(store, tmp_path):
252+
sub = tmp_path.joinpath('sub')
253+
sub.mkdir()
254+
sub.joinpath('submodule').write_text('i am a submodule')
255+
cmd_output('git', '-C', str(sub), 'init', '.')
256+
cmd_output('git', '-C', str(sub), 'add', '.')
257+
git.commit(str(sub))
258+
259+
repo = tmp_path.joinpath('repo')
260+
repo.mkdir()
261+
repo.joinpath('repository').write_text('i am a repo')
262+
cmd_output('git', '-C', str(repo), 'init', '.')
263+
cmd_output('git', '-C', str(repo), 'add', '.')
264+
cmd_output('git', '-C', str(repo), 'submodule', 'add', str(sub), 'sub')
265+
git.commit(str(repo))
266+
267+
rev = git.head_rev(str(repo))
268+
ret = store.clone(str(repo), rev)
269+
270+
assert os.path.exists(ret)
271+
assert os.path.exists(os.path.join(ret, str(repo), 'repository'))
272+
assert os.path.exists(os.path.join(ret, str(sub), 'submodule'))

0 commit comments

Comments
 (0)