Merge branch 'kurtmckee/prevent-source-deletio...'
[dotbot.git] / tests / test_create.py
blob1136b2eb22edf1f50e83273f7e1344beaee9682f
1 import os
2 import stat
3 from typing import Callable
5 import pytest
7 from tests.conftest import Dotfiles
10 @pytest.mark.parametrize("directory", ["~/a", "~/b/c"])
11 def test_directory_creation(home: str, directory: str, dotfiles: Dotfiles, run_dotbot: Callable[..., None]) -> None:
12 """Test creating directories, including nested directories."""
14 _ = home
15 dotfiles.write_config([{"create": [directory]}])
16 run_dotbot()
18 expanded_directory = os.path.abspath(os.path.expanduser(directory))
19 assert os.path.isdir(expanded_directory)
20 assert os.stat(expanded_directory).st_mode & 0o777 == 0o777
23 def test_default_mode(home: str, dotfiles: Dotfiles, run_dotbot: Callable[..., None]) -> None:
24 """Test creating a directory with an explicit default mode.
26 Note: `os.chmod()` on Windows only supports changing write permissions.
27 Therefore, this test is restricted to testing read-only access.
28 """
30 _ = home
31 read_only = 0o777 - stat.S_IWUSR - stat.S_IWGRP - stat.S_IWOTH
32 config = [{"defaults": {"create": {"mode": read_only}}}, {"create": ["~/a"]}]
33 dotfiles.write_config(config)
34 run_dotbot()
36 directory = os.path.abspath(os.path.expanduser("~/a"))
37 assert os.stat(directory).st_mode & stat.S_IWUSR == 0
38 assert os.stat(directory).st_mode & stat.S_IWGRP == 0
39 assert os.stat(directory).st_mode & stat.S_IWOTH == 0
42 def test_default_mode_override(home: str, dotfiles: Dotfiles, run_dotbot: Callable[..., None]) -> None:
43 """Test creating a directory that overrides an explicit default mode.
45 Note: `os.chmod()` on Windows only supports changing write permissions.
46 Therefore, this test is restricted to testing read-only access.
47 """
49 _ = home
50 read_only = 0o777 - stat.S_IWUSR - stat.S_IWGRP - stat.S_IWOTH
51 config = [
52 {"defaults": {"create": {"mode": read_only}}},
53 {"create": {"~/a": {"mode": 0o777}}},
55 dotfiles.write_config(config)
56 run_dotbot()
58 directory = os.path.abspath(os.path.expanduser("~/a"))
59 assert os.stat(directory).st_mode & stat.S_IWUSR == stat.S_IWUSR
60 assert os.stat(directory).st_mode & stat.S_IWGRP == stat.S_IWGRP
61 assert os.stat(directory).st_mode & stat.S_IWOTH == stat.S_IWOTH