diff --git a/testing/util.py b/testing/util.py index 63b52394..9f3a4248 100644 --- a/testing/util.py +++ b/testing/util.py @@ -12,6 +12,7 @@ from testing.auto_namedtuple import auto_namedtuple TESTING_DIR = os.path.abspath(os.path.dirname(__file__)) +SYSTEM_PATH = {'PATH': '/usr/bin:/usr/local/bin'} def docker_is_running() -> bool: # pragma: win32 no cover @@ -84,6 +85,10 @@ skipif_cant_run_swift = pytest.mark.skipif( parse_shebang.find_executable('swift') is None, reason="swift isn't installed or can't be found", ) +skipif_pre_commit_system_install = pytest.mark.skipif( + bool(parse_shebang.find_executable('pre-commit', SYSTEM_PATH)), + reason='pre-commit is installed system-wide, this test will fail', +) xfailif_windows = pytest.mark.xfail(os.name == 'nt', reason='windows') diff --git a/tests/commands/install_uninstall_test.py b/tests/commands/install_uninstall_test.py index 83399034..bae9afb5 100644 --- a/tests/commands/install_uninstall_test.py +++ b/tests/commands/install_uninstall_test.py @@ -27,6 +27,7 @@ from testing.fixtures import write_config from testing.util import cmd_output_mocked_pre_commit_home from testing.util import cwd from testing.util import git_commit +from testing.util import skipif_pre_commit_system_install def test_is_not_script(): @@ -255,6 +256,9 @@ def test_install_idempotent(tempdir_factory, store): def _path_without_us(): # Choose a path which *probably* doesn't include us + # WARNING: on system with pre-commit installer system-wide, + # this will break the test because /usr/bin will be removed from $PATH + # decorate the test using this with @skipif_pre_commit_system_install env = dict(os.environ) exe = find_executable('pre-commit', _environ=env) while exe: @@ -270,6 +274,7 @@ def _path_without_us(): return env['PATH'] +@skipif_pre_commit_system_install def test_environment_not_sourced(tempdir_factory, store): path = make_consuming_repo(tempdir_factory, 'script_hooks_repo') with cwd(path): @@ -528,6 +533,7 @@ def test_install_hooks_command(tempdir_factory, store): PRE_INSTALLED.assert_matches(output) +@skipif_pre_commit_system_install def test_installed_from_venv(tempdir_factory, store): path = make_consuming_repo(tempdir_factory, 'script_hooks_repo') with cwd(path):