Add initial implementation for running SBT commands.

This commit is contained in:
Stewart Hutchins 2022-11-23 18:13:43 +00:00
parent 81db32e148
commit 163e418754
10 changed files with 185 additions and 1 deletions

View file

@ -4,6 +4,7 @@ import functools
import io
import logging
import os.path
from pathlib import Path
from unittest import mock
import pytest
@ -16,6 +17,7 @@ from pre_commit.util import cmd_output
from pre_commit.util import make_executable
from testing.fixtures import git_dir
from testing.fixtures import make_consuming_repo
from testing.fixtures import make_repo
from testing.fixtures import write_config
from testing.util import cwd
from testing.util import git_commit
@ -250,3 +252,9 @@ def set_git_templatedir(tmpdir_factory):
tdir = str(tmpdir_factory.mktemp('git_template_dir'))
with envcontext((('GIT_TEMPLATE_DIR', tdir),)):
yield
@pytest.fixture
def sbt_project_with_touch_command(tempdir_factory):
project_repo = make_repo(tempdir_factory, 'sbt_repo_with_touch_command')
return Path(project_repo)

View file

@ -0,0 +1,63 @@
from __future__ import annotations
from itertools import product
from pathlib import Path
from typing import Any
import pytest
from pre_commit.hook import Hook
from pre_commit.languages import sbt
from testing.util import cwd
from testing.util import skipif_cant_run_sbt
@skipif_cant_run_sbt
@pytest.mark.parametrize(
['args', 'files'],
product(
[
[], ['argfile1.txt'], ['argfile1.txt', 'argfile2.txt'],
['\"arg file1.txt\"'], ['\"arg file1.txt\"', '\"arg file2.txt\"'],
],
[
[], ['filesfile1.txt'], ['filesfile1.txt', 'filesfile2.txt'],
['files file1.txt'], ['files file1.txt', 'files file2.txt'],
],
),
)
def test_sbt_hook(
sbt_project_with_touch_command: Path,
args: list[str],
files: list[str],
) -> None:
# arrange
project_root = sbt_project_with_touch_command
hook = _create_hook(
language='sbt',
entry='touch',
args=args,
)
# act
with cwd(project_root):
ret, out = sbt.run_hook(hook, files, False)
# assert
output = out.decode('UTF-8')
assert ret == 0
for file in args + files:
unquoted_file = _unquote(file)
expected_file = project_root.joinpath(unquoted_file).absolute()
assert expected_file.exists()
assert f'Creating file: {expected_file}' in output
def _unquote(s: str) -> str:
return s.strip("\"")
def _create_hook(**kwargs: Any) -> Hook:
default_values = {field: None for field in Hook._fields}
actual_values = {**default_values, **kwargs}
return Hook(**actual_values) # type: ignore

View file

@ -35,6 +35,7 @@ from testing.util import get_resource_path
from testing.util import skipif_cant_run_coursier
from testing.util import skipif_cant_run_docker
from testing.util import skipif_cant_run_lua
from testing.util import skipif_cant_run_sbt
from testing.util import skipif_cant_run_swift
from testing.util import xfailif_windows
@ -1150,3 +1151,40 @@ def test_local_lua_additional_dependencies(store):
ret, out = _hook_run(hook, (), color=False)
assert b'Luacheck' in out
assert ret == 0
@skipif_cant_run_sbt
def test_sbt_hook(
sbt_project_with_touch_command,
tempdir_factory,
store,
):
# arrange
project_root = sbt_project_with_touch_command
hooks_repo = make_repo(tempdir_factory, 'sbt_hooks_repo')
config = make_config_from_repo(hooks_repo)
hook = _get_hook(config, store, 'sbt-create-files')
# act
with cwd(project_root):
ret, out = _hook_run(
hook,
['file3.txt', 'file4 with space.txt'],
color=False,
)
# assert
output = out.decode('UTF-8')
assert ret == 0
file1 = project_root.joinpath('file1.txt').absolute()
assert file1.exists()
assert f'Creating file: {file1}' in output
file2 = project_root.joinpath('file2 with space.txt').absolute()
assert file2.exists()
assert f'Creating file: {file2}' in output
file3 = project_root.joinpath('file3.txt').absolute()
assert file3.exists()
assert f'Creating file: {file3}' in output
file4 = project_root.joinpath('file4 with space.txt').absolute()
assert file4.exists()
assert f'Creating file: {file4}' in output