Skip to content

Commit f1b5f66

Browse files
committed
test conda language directly
1 parent 0316676 commit f1b5f66

File tree

6 files changed

+57
-79
lines changed

6 files changed

+57
-79
lines changed

pre_commit/store.py

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,26 @@ def _get_default_directory() -> str:
3636
return os.path.realpath(ret)
3737

3838

39+
_LOCAL_RESOURCES = (
40+
'Cargo.toml', 'main.go', 'go.mod', 'main.rs', '.npmignore',
41+
'package.json', 'pre-commit-package-dev-1.rockspec',
42+
'pre_commit_placeholder_package.gemspec', 'setup.py',
43+
'environment.yml', 'Makefile.PL', 'pubspec.yaml',
44+
'renv.lock', 'renv/activate.R', 'renv/LICENSE.renv',
45+
)
46+
47+
48+
def _make_local_repo(directory: str) -> None:
49+
for resource in _LOCAL_RESOURCES:
50+
resource_dirname, resource_basename = os.path.split(resource)
51+
contents = resource_text(f'empty_template_{resource_basename}')
52+
target_dir = os.path.join(directory, resource_dirname)
53+
target_file = os.path.join(target_dir, resource_basename)
54+
os.makedirs(target_dir, exist_ok=True)
55+
with open(target_file, 'w') as f:
56+
f.write(contents)
57+
58+
3959
class Store:
4060
get_default_directory = staticmethod(_get_default_directory)
4161

@@ -185,27 +205,9 @@ def _git_cmd(*args: str) -> None:
185205

186206
return self._new_repo(repo, ref, deps, clone_strategy)
187207

188-
LOCAL_RESOURCES = (
189-
'Cargo.toml', 'main.go', 'go.mod', 'main.rs', '.npmignore',
190-
'package.json', 'pre-commit-package-dev-1.rockspec',
191-
'pre_commit_placeholder_package.gemspec', 'setup.py',
192-
'environment.yml', 'Makefile.PL', 'pubspec.yaml',
193-
'renv.lock', 'renv/activate.R', 'renv/LICENSE.renv',
194-
)
195-
196208
def make_local(self, deps: Sequence[str]) -> str:
197-
def make_local_strategy(directory: str) -> None:
198-
for resource in self.LOCAL_RESOURCES:
199-
resource_dirname, resource_basename = os.path.split(resource)
200-
contents = resource_text(f'empty_template_{resource_basename}')
201-
target_dir = os.path.join(directory, resource_dirname)
202-
target_file = os.path.join(target_dir, resource_basename)
203-
os.makedirs(target_dir, exist_ok=True)
204-
with open(target_file, 'w') as f:
205-
f.write(contents)
206-
207209
return self._new_repo(
208-
'local', C.LOCAL_REPO_VERSION, deps, make_local_strategy,
210+
'local', C.LOCAL_REPO_VERSION, deps, _make_local_repo,
209211
)
210212

211213
def _create_config_table(self, db: sqlite3.Connection) -> None:

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

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

testing/resources/conda_hooks_repo/environment.yml

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

tests/languages/conda_test.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
from __future__ import annotations
22

3+
import os.path
4+
35
import pytest
46

57
from pre_commit import envcontext
6-
from pre_commit.languages.conda import _conda_exe
8+
from pre_commit.languages import conda
9+
from pre_commit.store import _make_local_repo
10+
from testing.language_helpers import run_language
711

812

913
@pytest.mark.parametrize(
@@ -37,4 +41,32 @@
3741
)
3842
def test_conda_exe(ctx, expected):
3943
with envcontext.envcontext(ctx):
40-
assert _conda_exe() == expected
44+
assert conda._conda_exe() == expected
45+
46+
47+
def test_conda_language(tmp_path):
48+
environment_yml = '''\
49+
channels: [conda-forge, defaults]
50+
dependencies: [python, pip]
51+
'''
52+
tmp_path.joinpath('environment.yml').write_text(environment_yml)
53+
54+
ret, out = run_language(
55+
tmp_path,
56+
conda,
57+
'python -c "import sys; print(sys.prefix)"',
58+
)
59+
assert ret == 0
60+
assert os.path.basename(out.strip()) == b'conda-default'
61+
62+
63+
def test_conda_additional_deps(tmp_path):
64+
_make_local_repo(tmp_path)
65+
66+
ret = run_language(
67+
tmp_path,
68+
conda,
69+
'python -c "import botocore; print(1)"',
70+
deps=('botocore',),
71+
)
72+
assert ret == (0, b'1\n')

tests/repository_test.py

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -88,47 +88,6 @@ def _test_hook_repo(
8888
assert _norm_out(out) == expected
8989

9090

91-
def test_conda_hook(tempdir_factory, store):
92-
_test_hook_repo(
93-
tempdir_factory, store, 'conda_hooks_repo',
94-
'sys-exec', [os.devnull],
95-
b'conda-default\n',
96-
)
97-
98-
99-
def test_conda_with_additional_dependencies_hook(tempdir_factory, store):
100-
_test_hook_repo(
101-
tempdir_factory, store, 'conda_hooks_repo',
102-
'additional-deps', [os.devnull],
103-
b'OK\n',
104-
config_kwargs={
105-
'hooks': [{
106-
'id': 'additional-deps',
107-
'args': ['-c', 'import tzdata; print("OK")'],
108-
'additional_dependencies': ['python-tzdata'],
109-
}],
110-
},
111-
)
112-
113-
114-
def test_local_conda_additional_dependencies(store):
115-
config = {
116-
'repo': 'local',
117-
'hooks': [{
118-
'id': 'local-conda',
119-
'name': 'local-conda',
120-
'entry': 'python',
121-
'language': 'conda',
122-
'args': ['-c', 'import botocore; print("OK")'],
123-
'additional_dependencies': ['botocore'],
124-
}],
125-
}
126-
hook = _get_hook(config, store, 'local-conda')
127-
ret, out = _hook_run(hook, (), color=False)
128-
assert ret == 0
129-
assert _norm_out(out) == b'OK\n'
130-
131-
13291
def test_python_hook(tempdir_factory, store):
13392
_test_hook_repo(
13493
tempdir_factory, store, 'python_hooks_repo',

tests/store_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
from pre_commit import git
1111
from pre_commit.store import _get_default_directory
12+
from pre_commit.store import _LOCAL_RESOURCES
1213
from pre_commit.store import Store
1314
from pre_commit.util import CalledProcessError
1415
from pre_commit.util import cmd_output
@@ -188,7 +189,7 @@ def test_local_resources_reflects_reality():
188189
for res in os.listdir('pre_commit/resources')
189190
if res.startswith('empty_template_')
190191
}
191-
assert on_disk == {os.path.basename(x) for x in Store.LOCAL_RESOURCES}
192+
assert on_disk == {os.path.basename(x) for x in _LOCAL_RESOURCES}
192193

193194

194195
def test_mark_config_as_used(store, tmpdir):

0 commit comments

Comments
 (0)