|
11 | 11 | from pre_commit.envcontext import envcontext |
12 | 12 | from pre_commit.languages import golang |
13 | 13 | from pre_commit.store import _make_local_repo |
| 14 | +from pre_commit.util import CalledProcessError |
14 | 15 | from pre_commit.util import cmd_output |
15 | 16 | from testing.fixtures import add_config_to_repo |
16 | 17 | from testing.fixtures import make_config_from_repo |
17 | 18 | from testing.language_helpers import run_language |
18 | 19 | from testing.util import cmd_output_mocked_pre_commit_home |
| 20 | +from testing.util import cwd |
19 | 21 | from testing.util import git_commit |
20 | 22 |
|
21 | 23 |
|
@@ -165,3 +167,70 @@ def test_during_commit_all(tmp_path, tempdir_factory, store, in_git_dir): |
165 | 167 | fn=cmd_output_mocked_pre_commit_home, |
166 | 168 | tempdir_factory=tempdir_factory, |
167 | 169 | ) |
| 170 | + |
| 171 | + |
| 172 | +def test_automatic_toolchain_switching(tmp_path): |
| 173 | + go_mod = '''\ |
| 174 | +module toolchain-version-test |
| 175 | +
|
| 176 | +go 1.23.1 |
| 177 | +''' |
| 178 | + main_go = '''\ |
| 179 | +package main |
| 180 | +
|
| 181 | +func main() {} |
| 182 | +''' |
| 183 | + tmp_path.joinpath('go.mod').write_text(go_mod) |
| 184 | + mod_dir = tmp_path.joinpath('toolchain-version-test') |
| 185 | + mod_dir.mkdir() |
| 186 | + main_file = mod_dir.joinpath('main.go') |
| 187 | + main_file.write_text(main_go) |
| 188 | + |
| 189 | + with pytest.raises(CalledProcessError) as excinfo: |
| 190 | + run_language( |
| 191 | + path=tmp_path, |
| 192 | + language=golang, |
| 193 | + version='1.22.0', |
| 194 | + exe='golang-version-test', |
| 195 | + ) |
| 196 | + |
| 197 | + assert 'go.mod requires go >= 1.23.1' in excinfo.value.stderr.decode() |
| 198 | + |
| 199 | + |
| 200 | +def test_automatic_toolchain_switching_go_fmt(tmp_path, monkeypatch): |
| 201 | + go_mod_hook = '''\ |
| 202 | +module toolchain-version-test |
| 203 | +
|
| 204 | +go 1.22.0 |
| 205 | +''' |
| 206 | + go_mod = '''\ |
| 207 | +module toolchain-version-test |
| 208 | +
|
| 209 | +go 1.23.1 |
| 210 | +''' |
| 211 | + main_go = '''\ |
| 212 | +package main |
| 213 | +
|
| 214 | +func main() {} |
| 215 | +''' |
| 216 | + hook_dir = tmp_path.joinpath('hook') |
| 217 | + hook_dir.mkdir() |
| 218 | + hook_dir.joinpath('go.mod').write_text(go_mod_hook) |
| 219 | + |
| 220 | + test_dir = tmp_path.joinpath('test') |
| 221 | + test_dir.mkdir() |
| 222 | + test_dir.joinpath('go.mod').write_text(go_mod) |
| 223 | + main_file = test_dir.joinpath('main.go') |
| 224 | + main_file.write_text(main_go) |
| 225 | + |
| 226 | + with cwd(test_dir): |
| 227 | + ret, out = run_language( |
| 228 | + path=hook_dir, |
| 229 | + language=golang, |
| 230 | + version='1.22.0', |
| 231 | + exe='go fmt', |
| 232 | + file_args=(str(main_file),), |
| 233 | + ) |
| 234 | + |
| 235 | + assert ret == 1 |
| 236 | + assert 'go.mod requires go >= 1.23.1' in out.decode() |
0 commit comments