test conda language directly

This commit is contained in:
Anthony Sottile
2023-01-17 11:39:48 -05:00
parent 0316676fdc
commit f1b5f66374
6 changed files with 57 additions and 79 deletions

View File

@@ -36,6 +36,26 @@ def _get_default_directory() -> str:
return os.path.realpath(ret)
_LOCAL_RESOURCES = (
'Cargo.toml', 'main.go', 'go.mod', 'main.rs', '.npmignore',
'package.json', 'pre-commit-package-dev-1.rockspec',
'pre_commit_placeholder_package.gemspec', 'setup.py',
'environment.yml', 'Makefile.PL', 'pubspec.yaml',
'renv.lock', 'renv/activate.R', 'renv/LICENSE.renv',
)
def _make_local_repo(directory: str) -> None:
for resource in _LOCAL_RESOURCES:
resource_dirname, resource_basename = os.path.split(resource)
contents = resource_text(f'empty_template_{resource_basename}')
target_dir = os.path.join(directory, resource_dirname)
target_file = os.path.join(target_dir, resource_basename)
os.makedirs(target_dir, exist_ok=True)
with open(target_file, 'w') as f:
f.write(contents)
class Store:
get_default_directory = staticmethod(_get_default_directory)
@@ -185,27 +205,9 @@ class Store:
return self._new_repo(repo, ref, deps, clone_strategy)
LOCAL_RESOURCES = (
'Cargo.toml', 'main.go', 'go.mod', 'main.rs', '.npmignore',
'package.json', 'pre-commit-package-dev-1.rockspec',
'pre_commit_placeholder_package.gemspec', 'setup.py',
'environment.yml', 'Makefile.PL', 'pubspec.yaml',
'renv.lock', 'renv/activate.R', 'renv/LICENSE.renv',
)
def make_local(self, deps: Sequence[str]) -> str:
def make_local_strategy(directory: str) -> None:
for resource in self.LOCAL_RESOURCES:
resource_dirname, resource_basename = os.path.split(resource)
contents = resource_text(f'empty_template_{resource_basename}')
target_dir = os.path.join(directory, resource_dirname)
target_file = os.path.join(target_dir, resource_basename)
os.makedirs(target_dir, exist_ok=True)
with open(target_file, 'w') as f:
f.write(contents)
return self._new_repo(
'local', C.LOCAL_REPO_VERSION, deps, make_local_strategy,
'local', C.LOCAL_REPO_VERSION, deps, _make_local_repo,
)
def _create_config_table(self, db: sqlite3.Connection) -> None:

View File

@@ -1,10 +0,0 @@
- id: sys-exec
name: sys-exec
entry: python -c 'import os; import sys; print(sys.executable.split(os.path.sep)[-2]) if os.name == "nt" else print(sys.executable.split(os.path.sep)[-3])'
language: conda
files: \.py$
- id: additional-deps
name: additional-deps
entry: python
language: conda
files: \.py$

View File

@@ -1,6 +0,0 @@
channels:
- conda-forge
- defaults
dependencies:
- python
- pip

View File

@@ -1,9 +1,13 @@
from __future__ import annotations
import os.path
import pytest
from pre_commit import envcontext
from pre_commit.languages.conda import _conda_exe
from pre_commit.languages import conda
from pre_commit.store import _make_local_repo
from testing.language_helpers import run_language
@pytest.mark.parametrize(
@@ -37,4 +41,32 @@ from pre_commit.languages.conda import _conda_exe
)
def test_conda_exe(ctx, expected):
with envcontext.envcontext(ctx):
assert _conda_exe() == expected
assert conda._conda_exe() == expected
def test_conda_language(tmp_path):
environment_yml = '''\
channels: [conda-forge, defaults]
dependencies: [python, pip]
'''
tmp_path.joinpath('environment.yml').write_text(environment_yml)
ret, out = run_language(
tmp_path,
conda,
'python -c "import sys; print(sys.prefix)"',
)
assert ret == 0
assert os.path.basename(out.strip()) == b'conda-default'
def test_conda_additional_deps(tmp_path):
_make_local_repo(tmp_path)
ret = run_language(
tmp_path,
conda,
'python -c "import botocore; print(1)"',
deps=('botocore',),
)
assert ret == (0, b'1\n')

View File

@@ -88,47 +88,6 @@ def _test_hook_repo(
assert _norm_out(out) == expected
def test_conda_hook(tempdir_factory, store):
_test_hook_repo(
tempdir_factory, store, 'conda_hooks_repo',
'sys-exec', [os.devnull],
b'conda-default\n',
)
def test_conda_with_additional_dependencies_hook(tempdir_factory, store):
_test_hook_repo(
tempdir_factory, store, 'conda_hooks_repo',
'additional-deps', [os.devnull],
b'OK\n',
config_kwargs={
'hooks': [{
'id': 'additional-deps',
'args': ['-c', 'import tzdata; print("OK")'],
'additional_dependencies': ['python-tzdata'],
}],
},
)
def test_local_conda_additional_dependencies(store):
config = {
'repo': 'local',
'hooks': [{
'id': 'local-conda',
'name': 'local-conda',
'entry': 'python',
'language': 'conda',
'args': ['-c', 'import botocore; print("OK")'],
'additional_dependencies': ['botocore'],
}],
}
hook = _get_hook(config, store, 'local-conda')
ret, out = _hook_run(hook, (), color=False)
assert ret == 0
assert _norm_out(out) == b'OK\n'
def test_python_hook(tempdir_factory, store):
_test_hook_repo(
tempdir_factory, store, 'python_hooks_repo',

View File

@@ -9,6 +9,7 @@ import pytest
from pre_commit import git
from pre_commit.store import _get_default_directory
from pre_commit.store import _LOCAL_RESOURCES
from pre_commit.store import Store
from pre_commit.util import CalledProcessError
from pre_commit.util import cmd_output
@@ -188,7 +189,7 @@ def test_local_resources_reflects_reality():
for res in os.listdir('pre_commit/resources')
if res.startswith('empty_template_')
}
assert on_disk == {os.path.basename(x) for x in Store.LOCAL_RESOURCES}
assert on_disk == {os.path.basename(x) for x in _LOCAL_RESOURCES}
def test_mark_config_as_used(store, tmpdir):