mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-01-14 04:50:20 -06:00
test golang directly
This commit is contained in:
@@ -1,5 +0,0 @@
|
||||
- id: golang-hook
|
||||
name: golang example hook
|
||||
entry: golang-hello-world
|
||||
language: golang
|
||||
files: ''
|
||||
@@ -1,5 +0,0 @@
|
||||
module golang-hello-world
|
||||
|
||||
go 1.18
|
||||
|
||||
require github.com/BurntSushi/toml v1.1.0
|
||||
@@ -1,2 +0,0 @@
|
||||
github.com/BurntSushi/toml v1.1.0 h1:ksErzDEI1khOiGPgpwuI7x2ebx/uXQNw7xJpn9Eq1+I=
|
||||
github.com/BurntSushi/toml v1.1.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
||||
@@ -1,23 +0,0 @@
|
||||
package main
|
||||
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
"github.com/BurntSushi/toml"
|
||||
"os"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
What string
|
||||
}
|
||||
|
||||
func main() {
|
||||
message := runtime.Version()
|
||||
if len(os.Args) > 1 {
|
||||
message = os.Args[1]
|
||||
}
|
||||
var conf Config
|
||||
toml.Decode("What = 'world'\n", &conf)
|
||||
fmt.Printf("hello %v from %s\n", conf.What, message)
|
||||
}
|
||||
@@ -6,8 +6,11 @@ import pytest
|
||||
import re_assert
|
||||
|
||||
import pre_commit.constants as C
|
||||
from pre_commit.envcontext import envcontext
|
||||
from pre_commit.languages import golang
|
||||
from pre_commit.languages import helpers
|
||||
from pre_commit.store import _make_local_repo
|
||||
from testing.language_helpers import run_language
|
||||
|
||||
|
||||
ACTUAL_GET_DEFAULT_VERSION = golang.get_default_version.__wrapped__
|
||||
@@ -41,3 +44,93 @@ def test_golang_infer_go_version_default():
|
||||
|
||||
assert version != C.DEFAULT
|
||||
re_assert.Matches(r'^\d+\.\d+(?:\.\d+)?$').assert_matches(version)
|
||||
|
||||
|
||||
def _make_hello_world(tmp_path):
|
||||
go_mod = '''\
|
||||
module golang-hello-world
|
||||
|
||||
go 1.18
|
||||
|
||||
require github.com/BurntSushi/toml v1.1.0
|
||||
'''
|
||||
go_sum = '''\
|
||||
github.com/BurntSushi/toml v1.1.0 h1:ksErzDEI1khOiGPgpwuI7x2ebx/uXQNw7xJpn9Eq1+I=
|
||||
github.com/BurntSushi/toml v1.1.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
||||
''' # noqa: E501
|
||||
hello_world_go = '''\
|
||||
package main
|
||||
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/BurntSushi/toml"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
What string
|
||||
}
|
||||
|
||||
func main() {
|
||||
var conf Config
|
||||
toml.Decode("What = 'world'\\n", &conf)
|
||||
fmt.Printf("hello %v\\n", conf.What)
|
||||
}
|
||||
'''
|
||||
tmp_path.joinpath('go.mod').write_text(go_mod)
|
||||
tmp_path.joinpath('go.sum').write_text(go_sum)
|
||||
mod_dir = tmp_path.joinpath('golang-hello-world')
|
||||
mod_dir.mkdir()
|
||||
main_file = mod_dir.joinpath('main.go')
|
||||
main_file.write_text(hello_world_go)
|
||||
|
||||
|
||||
def test_golang_system(tmp_path):
|
||||
_make_hello_world(tmp_path)
|
||||
|
||||
ret = run_language(tmp_path, golang, 'golang-hello-world')
|
||||
assert ret == (0, b'hello world\n')
|
||||
|
||||
|
||||
def test_golang_default_version(tmp_path):
|
||||
_make_hello_world(tmp_path)
|
||||
|
||||
ret = run_language(
|
||||
tmp_path,
|
||||
golang,
|
||||
'golang-hello-world',
|
||||
version=C.DEFAULT,
|
||||
)
|
||||
assert ret == (0, b'hello world\n')
|
||||
|
||||
|
||||
def test_golang_versioned(tmp_path):
|
||||
_make_local_repo(str(tmp_path))
|
||||
|
||||
ret, out = run_language(
|
||||
tmp_path,
|
||||
golang,
|
||||
'go version',
|
||||
version='1.18.4',
|
||||
)
|
||||
|
||||
assert ret == 0
|
||||
assert out.startswith(b'go version go1.18.4')
|
||||
|
||||
|
||||
def test_local_golang_additional_deps(tmp_path):
|
||||
_make_local_repo(str(tmp_path))
|
||||
|
||||
ret = run_language(
|
||||
tmp_path,
|
||||
golang,
|
||||
'hello',
|
||||
deps=('golang.org/x/example/hello@latest',),
|
||||
)
|
||||
|
||||
assert ret == (0, b'Hello, Go examples!\n')
|
||||
|
||||
|
||||
def test_golang_hook_still_works_when_gobin_is_set(tmp_path):
|
||||
with envcontext((('GOBIN', str(tmp_path.joinpath('gobin'))),)):
|
||||
test_golang_system(tmp_path)
|
||||
|
||||
@@ -10,12 +10,9 @@ import pytest
|
||||
import re_assert
|
||||
|
||||
import pre_commit.constants as C
|
||||
from pre_commit import git
|
||||
from pre_commit.clientlib import CONFIG_SCHEMA
|
||||
from pre_commit.clientlib import load_manifest
|
||||
from pre_commit.envcontext import envcontext
|
||||
from pre_commit.hook import Hook
|
||||
from pre_commit.languages import golang
|
||||
from pre_commit.languages import helpers
|
||||
from pre_commit.languages import python
|
||||
from pre_commit.languages.all import languages
|
||||
@@ -169,92 +166,6 @@ def test_system_hook_with_spaces(tempdir_factory, store):
|
||||
)
|
||||
|
||||
|
||||
def test_golang_system_hook(tempdir_factory, store):
|
||||
_test_hook_repo(
|
||||
tempdir_factory, store, 'golang_hooks_repo',
|
||||
'golang-hook', ['system'], b'hello world from system\n',
|
||||
config_kwargs={
|
||||
'hooks': [{
|
||||
'id': 'golang-hook',
|
||||
'language_version': 'system',
|
||||
}],
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
def test_golang_versioned_hook(tempdir_factory, store):
|
||||
_test_hook_repo(
|
||||
tempdir_factory, store, 'golang_hooks_repo',
|
||||
'golang-hook', [], b'hello world from go1.18.4\n',
|
||||
config_kwargs={
|
||||
'hooks': [{
|
||||
'id': 'golang-hook',
|
||||
'language_version': '1.18.4',
|
||||
}],
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
def test_golang_hook_still_works_when_gobin_is_set(tempdir_factory, store):
|
||||
gobin_dir = tempdir_factory.get()
|
||||
with envcontext((('GOBIN', gobin_dir),)):
|
||||
test_golang_system_hook(tempdir_factory, store)
|
||||
assert os.listdir(gobin_dir) == []
|
||||
|
||||
|
||||
def test_golang_with_recursive_submodule(tmpdir, tempdir_factory, store):
|
||||
sub_go = '''\
|
||||
package sub
|
||||
|
||||
import "fmt"
|
||||
|
||||
func Func() {
|
||||
fmt.Println("hello hello world")
|
||||
}
|
||||
'''
|
||||
sub = tmpdir.join('sub').ensure_dir()
|
||||
sub.join('sub.go').write(sub_go)
|
||||
cmd_output('git', '-C', str(sub), 'init', '.')
|
||||
cmd_output('git', '-C', str(sub), 'add', '.')
|
||||
git.commit(str(sub))
|
||||
|
||||
pre_commit_hooks = '''\
|
||||
- id: example
|
||||
name: example
|
||||
entry: example
|
||||
language: golang
|
||||
verbose: true
|
||||
'''
|
||||
go_mod = '''\
|
||||
module github.com/asottile/example
|
||||
|
||||
go 1.14
|
||||
'''
|
||||
main_go = '''\
|
||||
package main
|
||||
|
||||
import "github.com/asottile/example/sub"
|
||||
|
||||
func main() {
|
||||
sub.Func()
|
||||
}
|
||||
'''
|
||||
repo = tmpdir.join('repo').ensure_dir()
|
||||
repo.join('.pre-commit-hooks.yaml').write(pre_commit_hooks)
|
||||
repo.join('go.mod').write(go_mod)
|
||||
repo.join('main.go').write(main_go)
|
||||
cmd_output('git', '-C', str(repo), 'init', '.')
|
||||
cmd_output('git', '-C', str(repo), 'add', '.')
|
||||
cmd_output('git', '-C', str(repo), 'submodule', 'add', str(sub), 'sub')
|
||||
git.commit(str(repo))
|
||||
|
||||
config = make_config_from_repo(str(repo))
|
||||
hook = _get_hook(config, store, 'example')
|
||||
ret, out = _hook_run(hook, (), color=False)
|
||||
assert ret == 0
|
||||
assert _norm_out(out) == b'hello hello world\n'
|
||||
|
||||
|
||||
def test_missing_executable(tempdir_factory, store):
|
||||
_test_hook_repo(
|
||||
tempdir_factory, store, 'not_found_exe',
|
||||
@@ -419,43 +330,6 @@ def test_repository_state_compatibility(tempdir_factory, store, v):
|
||||
assert _hook_installed(hook) is True
|
||||
|
||||
|
||||
def test_additional_golang_dependencies_installed(
|
||||
tempdir_factory, store,
|
||||
):
|
||||
path = make_repo(tempdir_factory, 'golang_hooks_repo')
|
||||
config = make_config_from_repo(path)
|
||||
# A small go package
|
||||
deps = ['golang.org/x/example/hello@latest']
|
||||
config['hooks'][0]['additional_dependencies'] = deps
|
||||
hook = _get_hook(config, store, 'golang-hook')
|
||||
envdir = helpers.environment_dir(
|
||||
hook.prefix,
|
||||
golang.ENVIRONMENT_DIR,
|
||||
golang.get_default_version(),
|
||||
)
|
||||
binaries = os.listdir(os.path.join(envdir, 'bin'))
|
||||
# normalize for windows
|
||||
binaries = [os.path.splitext(binary)[0] for binary in binaries]
|
||||
assert 'hello' in binaries
|
||||
|
||||
|
||||
def test_local_golang_additional_dependencies(store):
|
||||
config = {
|
||||
'repo': 'local',
|
||||
'hooks': [{
|
||||
'id': 'hello',
|
||||
'name': 'hello',
|
||||
'entry': 'hello',
|
||||
'language': 'golang',
|
||||
'additional_dependencies': ['golang.org/x/example/hello@latest'],
|
||||
}],
|
||||
}
|
||||
hook = _get_hook(config, store, 'hello')
|
||||
ret, out = _hook_run(hook, (), color=False)
|
||||
assert ret == 0
|
||||
assert _norm_out(out) == b'Hello, Go examples!\n'
|
||||
|
||||
|
||||
def test_fail_hooks(store):
|
||||
config = {
|
||||
'repo': 'local',
|
||||
|
||||
@@ -246,3 +246,27 @@ def test_mark_config_as_used_readonly(tmpdir):
|
||||
# should be skipped due to readonly
|
||||
store.mark_config_used(str(cfg))
|
||||
assert store.select_all_configs() == []
|
||||
|
||||
|
||||
def test_clone_with_recursive_submodules(store, tmp_path):
|
||||
sub = tmp_path.joinpath('sub')
|
||||
sub.mkdir()
|
||||
sub.joinpath('submodule').write_text('i am a submodule')
|
||||
cmd_output('git', '-C', str(sub), 'init', '.')
|
||||
cmd_output('git', '-C', str(sub), 'add', '.')
|
||||
git.commit(str(sub))
|
||||
|
||||
repo = tmp_path.joinpath('repo')
|
||||
repo.mkdir()
|
||||
repo.joinpath('repository').write_text('i am a repo')
|
||||
cmd_output('git', '-C', str(repo), 'init', '.')
|
||||
cmd_output('git', '-C', str(repo), 'add', '.')
|
||||
cmd_output('git', '-C', str(repo), 'submodule', 'add', str(sub), 'sub')
|
||||
git.commit(str(repo))
|
||||
|
||||
rev = git.head_rev(str(repo))
|
||||
ret = store.clone(str(repo), rev)
|
||||
|
||||
assert os.path.exists(ret)
|
||||
assert os.path.exists(os.path.join(ret, str(repo), 'repository'))
|
||||
assert os.path.exists(os.path.join(ret, str(sub), 'submodule'))
|
||||
|
||||
Reference in New Issue
Block a user