Skip to content

Commit 70bfd76

Browse files
committed
coursier: additional_dependencies support
1 parent 59ed51a commit 70bfd76

File tree

10 files changed

+132
-66
lines changed

10 files changed

+132
-66
lines changed

.github/actions/pre-test/action.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ runs:
1919
echo 'C:\Strawberry\perl\site\bin' >> "$GITHUB_PATH"
2020
echo 'C:\Strawberry\c\bin' >> "$GITHUB_PATH"
2121
22+
testing/get-coursier.sh
2223
testing/get-dart.sh
2324
- name: setup (linux)
2425
shell: bash

pre_commit/languages/coursier.py

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
from __future__ import annotations
22

33
import contextlib
4-
import os
4+
import os.path
55
from typing import Generator
66
from typing import Sequence
77

88
from pre_commit.envcontext import envcontext
99
from pre_commit.envcontext import PatchesT
1010
from pre_commit.envcontext import Var
11+
from pre_commit.errors import FatalError
1112
from pre_commit.languages import helpers
1213
from pre_commit.parse_shebang import find_executable
1314
from pre_commit.prefix import Prefix
@@ -23,9 +24,8 @@ def install_environment(
2324
prefix: Prefix,
2425
version: str,
2526
additional_dependencies: Sequence[str],
26-
) -> None: # pragma: win32 no cover
27+
) -> None:
2728
helpers.assert_version_default('coursier', version)
28-
helpers.assert_no_additional_deps('coursier', additional_dependencies)
2929

3030
# Support both possible executable names (either "cs" or "coursier")
3131
executable = find_executable('cs') or find_executable('coursier')
@@ -37,29 +37,40 @@ def install_environment(
3737

3838
envdir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, version)
3939
channel = prefix.path('.pre-commit-channel')
40-
for app_descriptor in os.listdir(channel):
41-
_, app_file = os.path.split(app_descriptor)
42-
app, _ = os.path.splitext(app_file)
43-
helpers.run_setup_cmd(
44-
prefix,
45-
(
46-
executable,
47-
'install',
48-
'--default-channels=false',
49-
f'--channel={channel}',
50-
app,
51-
f'--dir={envdir}',
52-
),
40+
if os.path.isdir(channel):
41+
for app_descriptor in os.listdir(channel):
42+
_, app_file = os.path.split(app_descriptor)
43+
app, _ = os.path.splitext(app_file)
44+
helpers.run_setup_cmd(
45+
prefix,
46+
(
47+
executable,
48+
'install',
49+
'--default-channels=false',
50+
'--channel', channel,
51+
'--dir', envdir,
52+
app,
53+
),
54+
)
55+
elif not additional_dependencies:
56+
raise FatalError(
57+
'expected .pre-commit-channel dir or additional_dependencies',
5358
)
5459

60+
if additional_dependencies:
61+
install_cmd = (
62+
executable, 'install', '--dir', envdir, *additional_dependencies,
63+
)
64+
helpers.run_setup_cmd(prefix, install_cmd)
65+
5566

56-
def get_env_patch(target_dir: str) -> PatchesT: # pragma: win32 no cover
67+
def get_env_patch(target_dir: str) -> PatchesT:
5768
return (
5869
('PATH', (target_dir, os.pathsep, Var('PATH'))),
5970
)
6071

6172

62-
@contextlib.contextmanager # pragma: win32 no cover
73+
@contextlib.contextmanager
6374
def in_env(prefix: Prefix, version: str) -> Generator[None, None, None]:
6475
envdir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, version)
6576
with envcontext(get_env_patch(envdir)):

testing/get-coursier.ps1

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

testing/get-coursier.sh

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
11
#!/usr/bin/env bash
2-
# This is a script used in CI to install coursier
32
set -euo pipefail
43

5-
COURSIER_URL="https://github.com/coursier/coursier/releases/download/v2.0.0/cs-x86_64-pc-linux"
6-
COURSIER_HASH="e2e838b75bc71b16bcb77ce951ad65660c89bda7957c79a0628ec7146d35122f"
7-
ARTIFACT="/tmp/coursier/cs"
4+
if [ "$OSTYPE" = msys ]; then
5+
URL='https://github.com/coursier/coursier/releases/download/v2.1.0-RC4/cs-x86_64-pc-win32.zip'
6+
SHA256='0d07386ff0f337e3e6264f7dde29d137dda6eaa2385f29741435e0b93ccdb49d'
7+
TARGET='/tmp/coursier/cs.zip'
88

9-
mkdir -p /tmp/coursier
10-
rm -f "$ARTIFACT"
11-
curl --location --silent --output "$ARTIFACT" "$COURSIER_URL"
12-
echo "$COURSIER_HASH $ARTIFACT" | sha256sum --check
13-
chmod ugo+x /tmp/coursier/cs
9+
unpack() {
10+
unzip "$TARGET" -d /tmp/coursier
11+
mv /tmp/coursier/cs-*.exe /tmp/coursier/cs.exe
12+
cygpath -w /tmp/coursier >> "$GITHUB_PATH"
13+
}
14+
else
15+
URL='https://github.com/coursier/coursier/releases/download/v2.1.0-RC4/cs-x86_64-pc-linux.gz'
16+
SHA256='176e92e08ab292531aa0c4993dbc9f2c99dec79578752f3b9285f54f306db572'
17+
TARGET=/tmp/coursier/cs.gz
18+
19+
unpack() {
20+
gunzip "$TARGET"
21+
chmod +x /tmp/coursier/cs
22+
echo /tmp/coursier >> "$GITHUB_PATH"
23+
}
24+
fi
1425

15-
echo '/tmp/coursier' >> "$GITHUB_PATH"
26+
mkdir -p /tmp/coursier
27+
curl --location --silent --output "$TARGET" "$URL"
28+
echo "$SHA256 $TARGET" | sha256sum --check
29+
unpack

testing/language_helpers.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from __future__ import annotations
2+
3+
import os
4+
from typing import Sequence
5+
6+
import pre_commit.constants as C
7+
from pre_commit.languages.all import Language
8+
from pre_commit.prefix import Prefix
9+
10+
11+
def run_language(
12+
path: os.PathLike[str],
13+
language: Language,
14+
exe: str,
15+
args: Sequence[str] = (),
16+
file_args: Sequence[str] = (),
17+
version: str = C.DEFAULT,
18+
deps: Sequence[str] = (),
19+
) -> tuple[int, bytes]:
20+
prefix = Prefix(str(path))
21+
22+
language.install_environment(prefix, version, deps)
23+
with language.in_env(prefix, version):
24+
ret, out = language.run_hook(
25+
prefix,
26+
exe,
27+
args,
28+
file_args,
29+
require_serial=True,
30+
color=False,
31+
)
32+
out = out.replace(b'\r\n', b'\n')
33+
return ret, out

testing/resources/coursier_hooks_repo/.pre-commit-channel/echo-java.json

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

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

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

testing/util.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,6 @@ def cmd_output_mocked_pre_commit_home(
4242
return ret, out.replace('\r\n', '\n'), None
4343

4444

45-
skipif_cant_run_coursier = pytest.mark.skipif(
46-
os.name == 'nt' or parse_shebang.find_executable('cs') is None,
47-
reason="coursier isn't installed or can't be found",
48-
)
4945
skipif_cant_run_docker = pytest.mark.skipif(
5046
os.name == 'nt' or not docker_is_running(),
5147
reason="Docker isn't running or can't be accessed",

tests/languages/coursier_test.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from __future__ import annotations
2+
3+
import pytest
4+
5+
from pre_commit.errors import FatalError
6+
from pre_commit.languages import coursier
7+
from testing.language_helpers import run_language
8+
9+
10+
def test_coursier_hook(tmp_path):
11+
echo_java_json = '''\
12+
{
13+
"repositories": ["central"],
14+
"dependencies": ["io.get-coursier:echo:latest.stable"]
15+
}
16+
'''
17+
18+
channel_dir = tmp_path.joinpath('.pre-commit-channel')
19+
channel_dir.mkdir()
20+
channel_dir.joinpath('echo-java.json').write_text(echo_java_json)
21+
22+
ret = run_language(
23+
tmp_path,
24+
coursier,
25+
'echo-java',
26+
args=('Hello', 'World', 'from', 'coursier'),
27+
)
28+
assert ret == (0, b'Hello World from coursier\n')
29+
30+
31+
def test_coursier_hook_additional_dependencies(tmp_path):
32+
ret = run_language(
33+
tmp_path,
34+
coursier,
35+
'scalafmt --version',
36+
deps=('scalafmt:3.6.1',),
37+
)
38+
assert ret == (0, b'scalafmt 3.6.1\n')
39+
40+
41+
def test_error_if_no_deps_or_channel(tmp_path):
42+
with pytest.raises(FatalError) as excinfo:
43+
run_language(tmp_path, coursier, 'dne')
44+
msg, = excinfo.value.args
45+
assert msg == 'expected .pre-commit-channel dir or additional_dependencies'

tests/repository_test.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
from testing.fixtures import modify_manifest
3333
from testing.util import cwd
3434
from testing.util import get_resource_path
35-
from testing.util import skipif_cant_run_coursier
3635
from testing.util import skipif_cant_run_docker
3736
from testing.util import skipif_cant_run_lua
3837
from testing.util import skipif_cant_run_swift
@@ -199,15 +198,6 @@ def test_language_versioned_python_hook(tempdir_factory, store):
199198
)
200199

201200

202-
@skipif_cant_run_coursier # pragma: win32 no cover
203-
def test_run_a_coursier_hook(tempdir_factory, store):
204-
_test_hook_repo(
205-
tempdir_factory, store, 'coursier_hooks_repo',
206-
'echo-java',
207-
['Hello World from coursier'], b'Hello World from coursier\n',
208-
)
209-
210-
211201
@skipif_cant_run_docker # pragma: win32 no cover
212202
def test_run_a_docker_hook(tempdir_factory, store):
213203
_test_hook_repo(

0 commit comments

Comments
 (0)