Merge branch 'kurtmckee/prevent-source-deletio...'
[dotbot.git] / tests / test_bin_dotbot.py
blobd0ea8dafd50e6d67c822c60710b2fb2e7d46081b
1 import os
2 import shutil
3 import subprocess
4 from typing import Optional
6 import pytest
8 from tests.conftest import Dotfiles
11 @pytest.mark.skipif(
12 "sys.platform == 'win32'",
13 reason="The hybrid sh/Python dotbot script doesn't run on Windows platforms",
15 @pytest.mark.parametrize("python_name", [None, "python", "python3"])
16 def test_find_python_executable(python_name: Optional[str], home: str, dotfiles: Dotfiles) -> None:
17 """Verify that the sh/Python hybrid dotbot executable can find Python."""
19 dotfiles.write_config([])
20 dotbot_executable = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "bin", "dotbot")
22 # Create a link to sh.
23 tmp_bin = os.path.join(home, "tmp_bin")
24 os.makedirs(tmp_bin)
25 sh_path = shutil.which("sh")
26 assert sh_path is not None
27 os.symlink(sh_path, os.path.join(tmp_bin, "sh"))
29 if python_name is not None:
30 with open(os.path.join(tmp_bin, python_name), "w") as file:
31 file.write("#!" + tmp_bin + "/sh\n")
32 file.write("exit 0\n")
33 os.chmod(os.path.join(tmp_bin, python_name), 0o777)
34 env = dict(os.environ)
35 env["PATH"] = tmp_bin
37 if python_name is not None:
38 subprocess.check_call(
39 [dotbot_executable, "-c", dotfiles.config_filename],
40 env=env,
42 else:
43 with pytest.raises(subprocess.CalledProcessError):
44 subprocess.check_call(
45 [dotbot_executable, "-c", dotfiles.config_filename],
46 env=env,