|
1 | 1 | import os |
| 2 | +import pathlib |
2 | 3 | import sys |
3 | 4 | from typing import Callable, Optional |
4 | 5 |
|
@@ -962,6 +963,50 @@ def test_link_relink_relative_leaves_file(home: str, dotfiles: Dotfiles, run_dot |
962 | 963 | assert mtime == new_mtime |
963 | 964 |
|
964 | 965 |
|
| 966 | +def test_source_is_not_overwritten_by_symlink_trickery( |
| 967 | + capsys: pytest.CaptureFixture[str], home: str, dotfiles: Dotfiles, run_dotbot: Callable[..., None] |
| 968 | +) -> None: |
| 969 | + dotfiles_path = pathlib.Path(dotfiles.directory) |
| 970 | + home_path = pathlib.Path(home) |
| 971 | + |
| 972 | + # Setup: |
| 973 | + # * A symlink exists from `~/.ssh` to `ssh` in the dotfiles directory. |
| 974 | + # * Dotbot is configured to force-recreate a symlink between two files |
| 975 | + # when, in reality, it's actually the same file when resolved. |
| 976 | + ssh_config = (dotfiles_path / "ssh/config").absolute() |
| 977 | + os.mkdir(str(ssh_config.parent)) |
| 978 | + ssh_config.write_text("preserve me!") |
| 979 | + os.symlink(str(ssh_config.parent), str(home_path / ".ssh")) |
| 980 | + dotfiles.write_config( |
| 981 | + [ |
| 982 | + { |
| 983 | + "defaults": { |
| 984 | + "link": { |
| 985 | + "relink": True, |
| 986 | + "create": True, |
| 987 | + "force": True, |
| 988 | + }, |
| 989 | + } |
| 990 | + }, |
| 991 | + { |
| 992 | + "link": { |
| 993 | + # When symlinks are resolved, these are actually the same file. |
| 994 | + "~/.ssh/config": "ssh/config", |
| 995 | + }, |
| 996 | + }, |
| 997 | + ] |
| 998 | + ) |
| 999 | + |
| 1000 | + # Execute dotbot. |
| 1001 | + with pytest.raises(SystemExit): |
| 1002 | + run_dotbot() |
| 1003 | + |
| 1004 | + stdout, _ = capsys.readouterr() |
| 1005 | + assert "appears to be the same file" in stdout |
| 1006 | + # Verify that the file was not overwritten. |
| 1007 | + assert ssh_config.read_text() == "preserve me!" |
| 1008 | + |
| 1009 | + |
965 | 1010 | def test_link_defaults_1(home: str, dotfiles: Dotfiles, run_dotbot: Callable[..., None]) -> None: |
966 | 1011 | """Verify that link doesn't overwrite non-dotfiles links by default.""" |
967 | 1012 |
|
|
0 commit comments