2022-05-06 17:35:41 -04:00
|
|
|
import os
|
2023-09-09 20:39:45 -04:00
|
|
|
import shutil
|
2022-05-06 17:35:41 -04:00
|
|
|
import subprocess
|
2024-12-28 01:01:05 -05:00
|
|
|
from typing import Optional
|
2022-05-06 17:35:41 -04:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2024-12-28 01:01:05 -05:00
|
|
|
from tests.conftest import Dotfiles
|
|
|
|
|
2022-05-06 17:35:41 -04:00
|
|
|
|
|
|
|
@pytest.mark.skipif(
|
2024-12-28 01:01:05 -05:00
|
|
|
"sys.platform == 'win32'",
|
2022-05-06 17:35:41 -04:00
|
|
|
reason="The hybrid sh/Python dotbot script doesn't run on Windows platforms",
|
|
|
|
)
|
2024-12-28 01:01:05 -05:00
|
|
|
@pytest.mark.parametrize("python_name", [None, "python", "python3"])
|
|
|
|
def test_find_python_executable(python_name: Optional[str], home: str, dotfiles: Dotfiles) -> None:
|
2022-05-06 17:35:41 -04:00
|
|
|
"""Verify that the sh/Python hybrid dotbot executable can find Python."""
|
|
|
|
|
|
|
|
dotfiles.write_config([])
|
2024-12-28 01:01:05 -05:00
|
|
|
dotbot_executable = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "bin", "dotbot")
|
2022-05-06 17:35:41 -04:00
|
|
|
|
|
|
|
# Create a link to sh.
|
|
|
|
tmp_bin = os.path.join(home, "tmp_bin")
|
|
|
|
os.makedirs(tmp_bin)
|
2023-09-09 20:39:45 -04:00
|
|
|
sh_path = shutil.which("sh")
|
2024-12-28 01:01:05 -05:00
|
|
|
assert sh_path is not None
|
2022-05-06 17:35:41 -04:00
|
|
|
os.symlink(sh_path, os.path.join(tmp_bin, "sh"))
|
|
|
|
|
2024-12-28 01:01:05 -05:00
|
|
|
if python_name is not None:
|
2022-05-06 17:35:41 -04:00
|
|
|
with open(os.path.join(tmp_bin, python_name), "w") as file:
|
|
|
|
file.write("#!" + tmp_bin + "/sh\n")
|
|
|
|
file.write("exit 0\n")
|
|
|
|
os.chmod(os.path.join(tmp_bin, python_name), 0o777)
|
|
|
|
env = dict(os.environ)
|
|
|
|
env["PATH"] = tmp_bin
|
|
|
|
|
2024-12-28 01:01:05 -05:00
|
|
|
if python_name is not None:
|
2022-05-06 17:35:41 -04:00
|
|
|
subprocess.check_call(
|
|
|
|
[dotbot_executable, "-c", dotfiles.config_filename],
|
|
|
|
env=env,
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
with pytest.raises(subprocess.CalledProcessError):
|
|
|
|
subprocess.check_call(
|
|
|
|
[dotbot_executable, "-c", dotfiles.config_filename],
|
|
|
|
env=env,
|
|
|
|
)
|