mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 00:04:42 +04:00
Add naive and untested version of Lua language support.
This commit is contained in:
parent
7a305e5d9a
commit
3f8be7400d
13 changed files with 286 additions and 2 deletions
55
tests/languages/lua_test.py
Normal file
55
tests/languages/lua_test.py
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
import os
|
||||
from unittest import mock
|
||||
|
||||
import pytest
|
||||
|
||||
import pre_commit.constants as C
|
||||
from pre_commit.languages import lua
|
||||
from testing.util import xfailif_windows
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
'lua_name', ('lua', 'lua5.4', 'lua-5.4', 'lua5.4.exe'),
|
||||
)
|
||||
def test_find_lua(tmp_path, lua_name):
|
||||
"""The language support can find common lua executable names."""
|
||||
lua_file = tmp_path / lua_name
|
||||
lua_file.touch(0o555)
|
||||
with mock.patch.dict(os.environ, {'PATH': str(tmp_path)}):
|
||||
lua_executable = lua._find_lua(C.DEFAULT)
|
||||
assert lua_name in lua_executable
|
||||
|
||||
|
||||
def test_find_lua_language_version(tmp_path):
|
||||
"""Language discovery can find a specific version."""
|
||||
lua_file = tmp_path / 'lua5.99'
|
||||
lua_file.touch(0o555)
|
||||
with mock.patch.dict(os.environ, {'PATH': str(tmp_path)}):
|
||||
lua_executable = lua._find_lua('5.99')
|
||||
assert 'lua5.99' in lua_executable
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
('invalid', 'mode'),
|
||||
(
|
||||
('foobar', 0o555),
|
||||
('luac', 0o555),
|
||||
# Windows doesn't respect the executable checking.
|
||||
pytest.param('lua5.4', 0o444, marks=xfailif_windows),
|
||||
),
|
||||
)
|
||||
def test_find_lua_fail(tmp_path, invalid, mode):
|
||||
"""No lua executable on the system will fail."""
|
||||
non_lua_file = tmp_path / invalid
|
||||
non_lua_file.touch(mode)
|
||||
with mock.patch.dict(os.environ, {'PATH': str(tmp_path)}):
|
||||
with pytest.raises(ValueError):
|
||||
lua._find_lua(C.DEFAULT)
|
||||
|
||||
|
||||
@mock.patch.object(lua, 'cmd_output')
|
||||
def test_bad_package_path(mock_cmd_output):
|
||||
"""A package path missing path info returns an unknown version."""
|
||||
mock_cmd_output.return_value = (0, '', '')
|
||||
with pytest.raises(ValueError):
|
||||
lua._get_lua_path_version('lua')
|
||||
|
|
@ -17,6 +17,7 @@ from pre_commit.envcontext import envcontext
|
|||
from pre_commit.hook import Hook
|
||||
from pre_commit.languages import golang
|
||||
from pre_commit.languages import helpers
|
||||
from pre_commit.languages import lua
|
||||
from pre_commit.languages import node
|
||||
from pre_commit.languages import python
|
||||
from pre_commit.languages import ruby
|
||||
|
|
@ -34,6 +35,7 @@ from testing.util import cwd
|
|||
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_swift
|
||||
from testing.util import xfailif_windows
|
||||
|
||||
|
|
@ -1128,3 +1130,30 @@ def test_non_installable_hook_error_for_additional_dependencies(store, caplog):
|
|||
'using language `system` which does not install an environment. '
|
||||
'Perhaps you meant to use a specific language?'
|
||||
)
|
||||
|
||||
|
||||
@skipif_cant_run_lua # pragma: win32 no cover
|
||||
def test_lua_hook(tempdir_factory, store):
|
||||
_test_hook_repo(
|
||||
tempdir_factory, store, 'lua_repo',
|
||||
'hello-world-lua', [], b'hello world\n',
|
||||
)
|
||||
|
||||
|
||||
@skipif_cant_run_lua # pragma: win32 no cover
|
||||
def test_local_lua_additional_dependencies(store):
|
||||
lua_entry = lua._find_lua(C.DEFAULT)
|
||||
config = {
|
||||
'repo': 'local',
|
||||
'hooks': [{
|
||||
'id': 'local-lua',
|
||||
'name': 'local-lua',
|
||||
'entry': lua_entry,
|
||||
'language': 'lua',
|
||||
'args': ['-e', 'require "inspect"; print("hello world")'],
|
||||
'additional_dependencies': ['inspect'],
|
||||
}],
|
||||
}
|
||||
hook = _get_hook(config, store, 'local-lua')
|
||||
ret, out = _hook_run(hook, (), color=False)
|
||||
assert (ret, _norm_out(out)) == (0, b'hello world\n')
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue