3 from typing
import Callable
7 from tests
.conftest
import Dotfiles
10 def test_except_create(
11 capfd
: pytest
.CaptureFixture
[str], home
: str, dotfiles
: Dotfiles
, run_dotbot
: Callable
[..., None]
13 """Verify that `--except` works as intended."""
15 dotfiles
.write_config(
20 {"command": "echo success", "stdout": True},
25 run_dotbot("--except", "create")
27 assert not os
.path
.exists(os
.path
.join(home
, "a"))
28 stdout
= capfd
.readouterr().out
.splitlines()
29 assert any(line
.startswith("success") for line
in stdout
)
32 def test_except_shell(
33 capfd
: pytest
.CaptureFixture
[str], home
: str, dotfiles
: Dotfiles
, run_dotbot
: Callable
[..., None]
35 """Verify that `--except` works as intended."""
37 dotfiles
.write_config(
42 {"command": "echo failure", "stdout": True},
47 run_dotbot("--except", "shell")
49 assert os
.path
.exists(os
.path
.join(home
, "a"))
50 stdout
= capfd
.readouterr().out
.splitlines()
51 assert not any(line
.startswith("failure") for line
in stdout
)
54 def test_except_multiples(
55 capfd
: pytest
.CaptureFixture
[str], home
: str, dotfiles
: Dotfiles
, run_dotbot
: Callable
[..., None]
57 """Verify that `--except` works with multiple exceptions."""
59 dotfiles
.write_config(
64 {"command": "echo failure", "stdout": True},
69 run_dotbot("--except", "create", "shell")
71 assert not os
.path
.exists(os
.path
.join(home
, "a"))
72 stdout
= capfd
.readouterr().out
.splitlines()
73 assert not any(line
.startswith("failure") for line
in stdout
)
76 def test_exit_on_failure(home
: str, dotfiles
: Dotfiles
, run_dotbot
: Callable
[..., None]) -> None:
77 """Verify that processing can halt immediately on failures."""
79 dotfiles
.write_config(
82 {"shell": ["this_is_not_a_command"]},
86 with pytest
.raises(SystemExit):
89 assert os
.path
.isdir(os
.path
.join(home
, "a"))
90 assert not os
.path
.isdir(os
.path
.join(home
, "b"))
94 capfd
: pytest
.CaptureFixture
[str], home
: str, dotfiles
: Dotfiles
, run_dotbot
: Callable
[..., None]
96 """Verify that `--only` works as intended."""
98 dotfiles
.write_config(
101 {"shell": [{"command": "echo success", "stdout": True}]},
104 run_dotbot("--only", "shell")
106 assert not os
.path
.exists(os
.path
.join(home
, "a"))
107 stdout
= capfd
.readouterr().out
.splitlines()
108 assert any(line
.startswith("success") for line
in stdout
)
111 def test_only_with_defaults(
112 capfd
: pytest
.CaptureFixture
[str], home
: str, dotfiles
: Dotfiles
, run_dotbot
: Callable
[..., None]
114 """Verify that `--only` does not suppress defaults."""
116 dotfiles
.write_config(
118 {"defaults": {"shell": {"stdout": True}}},
120 {"shell": [{"command": "echo success"}]},
123 run_dotbot("--only", "shell")
125 assert not os
.path
.exists(os
.path
.join(home
, "a"))
126 stdout
= capfd
.readouterr().out
.splitlines()
127 assert any(line
.startswith("success") for line
in stdout
)
130 def test_only_with_multiples(
131 capfd
: pytest
.CaptureFixture
[str], home
: str, dotfiles
: Dotfiles
, run_dotbot
: Callable
[..., None]
133 """Verify that `--only` works as intended."""
135 dotfiles
.write_config(
138 {"shell": [{"command": "echo success", "stdout": True}]},
142 run_dotbot("--only", "create", "shell")
144 assert os
.path
.isdir(os
.path
.join(home
, "a"))
145 stdout
= capfd
.readouterr().out
.splitlines()
146 assert any(line
.startswith("success") for line
in stdout
)
147 assert not os
.path
.exists(os
.path
.join(home
, ".f"))
150 def test_plugin_loading_file(home
: str, dotfiles
: Dotfiles
, run_dotbot
: Callable
[..., None]) -> None:
151 """Verify that plugins can be loaded by file."""
153 plugin_file
= os
.path
.join(os
.path
.dirname(os
.path
.abspath(__file__
)), "dotbot_plugin_file.py")
154 shutil
.copy(plugin_file
, os
.path
.join(dotfiles
.directory
, "file.py"))
155 dotfiles
.write_config([{"plugin_file": "~"}])
156 run_dotbot("--plugin", os
.path
.join(dotfiles
.directory
, "file.py"))
158 with
open(os
.path
.join(home
, "flag")) as file:
159 assert file.read() == "file plugin loading works"
162 def test_plugin_loading_directory(home
: str, dotfiles
: Dotfiles
, run_dotbot
: Callable
[..., None]) -> None:
163 """Verify that plugins can be loaded from a directory."""
165 dotfiles
.makedirs("plugins")
166 plugin_file
= os
.path
.join(os
.path
.dirname(os
.path
.abspath(__file__
)), "dotbot_plugin_directory.py")
167 shutil
.copy(plugin_file
, os
.path
.join(dotfiles
.directory
, "plugins", "directory.py"))
168 dotfiles
.write_config([{"plugin_directory": "~"}])
169 run_dotbot("--plugin-dir", os
.path
.join(dotfiles
.directory
, "plugins"))
171 with
open(os
.path
.join(home
, "flag")) as file:
172 assert file.read() == "directory plugin loading works"
176 capfd
: pytest
.CaptureFixture
[str], home
: str, dotfiles
: Dotfiles
, run_dotbot
: Callable
[..., None]
178 """Verify that built-in plugins are only executed once, when
179 using a plugin that imports from dotbot.plugins."""
182 plugin_file
= os
.path
.join(os
.path
.dirname(os
.path
.abspath(__file__
)), "dotbot_plugin_issue_357.py")
183 dotfiles
.write_config([{"shell": [{"command": "echo apple", "stdout": True}]}])
185 run_dotbot("--plugin", plugin_file
)
187 assert len([line
for line
in capfd
.readouterr().out
.splitlines() if line
.strip() == "apple"]) == 1
190 def test_disable_builtin_plugins(home
: str, dotfiles
: Dotfiles
, run_dotbot
: Callable
[..., None]) -> None:
191 """Verify that builtin plugins can be disabled."""
193 dotfiles
.write("f", "apple")
194 dotfiles
.write_config([{"link": {"~/.f": "f"}}])
196 # The link directive will be unhandled so dotbot will raise SystemExit.
197 with pytest
.raises(SystemExit):
198 run_dotbot("--disable-built-in-plugins")
200 assert not os
.path
.exists(os
.path
.join(home
, ".f"))
203 def test_plugin_context_plugin(
204 capfd
: pytest
.CaptureFixture
[str], home
: str, dotfiles
: Dotfiles
, run_dotbot
: Callable
[..., None]
206 """Verify that the plugin context is available to plugins."""
209 plugin_file
= os
.path
.join(os
.path
.dirname(os
.path
.abspath(__file__
)), "dotbot_plugin_context_plugin.py")
210 shutil
.copy(plugin_file
, os
.path
.join(dotfiles
.directory
, "plugin.py"))
211 dotfiles
.write_config([{"dispatch": [{"shell": [{"command": "echo apple", "stdout": True}]}]}])
212 run_dotbot("--plugin", os
.path
.join(dotfiles
.directory
, "plugin.py"))
214 stdout
= capfd
.readouterr().out
.splitlines()
215 assert any(line
.startswith("apple") for line
in stdout
)
218 def test_plugin_dispatcher_no_plugins(
219 capfd
: pytest
.CaptureFixture
[str], home
: str, dotfiles
: Dotfiles
, run_dotbot
: Callable
[..., None]
221 """Verify that plugins instantiating Dispatcher without plugins work."""
224 plugin_file
= os
.path
.join(os
.path
.dirname(os
.path
.abspath(__file__
)), "dotbot_plugin_dispatcher_no_plugins.py")
225 shutil
.copy(plugin_file
, os
.path
.join(dotfiles
.directory
, "plugin.py"))
226 dotfiles
.write_config([{"dispatch": [{"shell": [{"command": "echo apple", "stdout": True}]}]}])
227 run_dotbot("--plugin", os
.path
.join(dotfiles
.directory
, "plugin.py"))
229 stdout
= capfd
.readouterr().out
.splitlines()
230 assert any(line
.startswith("apple") for line
in stdout
)