summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/lang_base_test.py27
-rw-r--r--tests/languages/golang_test.py2
-rw-r--r--tests/languages/haskell_test.py50
-rw-r--r--tests/xargs_test.py9
4 files changed, 85 insertions, 3 deletions
diff --git a/tests/lang_base_test.py b/tests/lang_base_test.py
index a532b6a..1cffa0e 100644
--- a/tests/lang_base_test.py
+++ b/tests/lang_base_test.py
@@ -30,6 +30,19 @@ def homedir_mck():
yield
+@pytest.fixture
+def no_sched_getaffinity():
+ # Simulates an OS without os.sched_getaffinity available (mac/windows)
+ # https://docs.python.org/3/library/os.html#interface-to-the-scheduler
+ with mock.patch.object(
+ os,
+ 'sched_getaffinity',
+ create=True,
+ side_effect=AttributeError,
+ ):
+ yield
+
+
def test_exe_exists_does_not_exist(find_exe_mck, homedir_mck):
find_exe_mck.return_value = None
assert lang_base.exe_exists('ruby') is False
@@ -116,7 +129,17 @@ def test_no_env_noop(tmp_path):
assert before == inside == after
-def test_target_concurrency_normal():
+def test_target_concurrency_sched_getaffinity(no_sched_getaffinity):
+ with mock.patch.object(
+ os,
+ 'sched_getaffinity',
+ return_value=set(range(345)),
+ ):
+ with mock.patch.dict(os.environ, clear=True):
+ assert lang_base.target_concurrency() == 345
+
+
+def test_target_concurrency_without_sched_getaffinity(no_sched_getaffinity):
with mock.patch.object(multiprocessing, 'cpu_count', return_value=123):
with mock.patch.dict(os.environ, {}, clear=True):
assert lang_base.target_concurrency() == 123
@@ -134,7 +157,7 @@ def test_target_concurrency_on_travis():
assert lang_base.target_concurrency() == 2
-def test_target_concurrency_cpu_count_not_implemented():
+def test_target_concurrency_cpu_count_not_implemented(no_sched_getaffinity):
with mock.patch.object(
multiprocessing, 'cpu_count', side_effect=NotImplementedError,
):
diff --git a/tests/languages/golang_test.py b/tests/languages/golang_test.py
index ec5a878..6406267 100644
--- a/tests/languages/golang_test.py
+++ b/tests/languages/golang_test.py
@@ -128,7 +128,7 @@ def test_local_golang_additional_deps(tmp_path):
deps=('golang.org/x/example/hello@latest',),
)
- assert ret == (0, b'Hello, Go examples!\n')
+ assert ret == (0, b'Hello, world!\n')
def test_golang_hook_still_works_when_gobin_is_set(tmp_path):
diff --git a/tests/languages/haskell_test.py b/tests/languages/haskell_test.py
new file mode 100644
index 0000000..f888109
--- /dev/null
+++ b/tests/languages/haskell_test.py
@@ -0,0 +1,50 @@
+from __future__ import annotations
+
+import pytest
+
+from pre_commit.errors import FatalError
+from pre_commit.languages import haskell
+from pre_commit.util import win_exe
+from testing.language_helpers import run_language
+
+
+def test_run_example_executable(tmp_path):
+ example_cabal = '''\
+cabal-version: 2.4
+name: example
+version: 0.1.0.0
+
+executable example
+ main-is: Main.hs
+
+ build-depends: base >=4
+ default-language: Haskell2010
+'''
+ main_hs = '''\
+module Main where
+
+main :: IO ()
+main = putStrLn "Hello, Haskell!"
+'''
+ tmp_path.joinpath('example.cabal').write_text(example_cabal)
+ tmp_path.joinpath('Main.hs').write_text(main_hs)
+
+ result = run_language(tmp_path, haskell, 'example')
+ assert result == (0, b'Hello, Haskell!\n')
+
+ # should not symlink things into environments
+ exe = tmp_path.joinpath(win_exe('hs_env-default/bin/example'))
+ assert exe.is_file()
+ assert not exe.is_symlink()
+
+
+def test_run_dep(tmp_path):
+ result = run_language(tmp_path, haskell, 'hello', deps=['hello'])
+ assert result == (0, b'Hello, World!\n')
+
+
+def test_run_empty(tmp_path):
+ with pytest.raises(FatalError) as excinfo:
+ run_language(tmp_path, haskell, 'example')
+ msg, = excinfo.value.args
+ assert msg == 'Expected .cabal files or additional_dependencies'
diff --git a/tests/xargs_test.py b/tests/xargs_test.py
index 7c41f98..b0a8e0d 100644
--- a/tests/xargs_test.py
+++ b/tests/xargs_test.py
@@ -147,6 +147,15 @@ def test_xargs_retcode_normal():
assert ret == 5
+@pytest.mark.xfail(sys.platform == 'win32', reason='posix only')
+def test_xargs_retcode_killed_by_signal():
+ ret, _ = xargs.xargs(
+ parse_shebang.normalize_cmd(('bash', '-c', 'kill -9 $$', '--')),
+ ('foo', 'bar'),
+ )
+ assert ret == -9
+
+
def test_xargs_concurrency():
bash_cmd = parse_shebang.normalize_cmd(('bash', '-c'))
print_pid = ('sleep 0.5 && echo $$',)