Skip to content

Commit a0c36cb

Browse files
authored
test: Use mock for simulating permissioned systems (#102)
The `add_repository_invalid` test can fail on systems where the user has read permissions to the `/root` directory. (This is likely uncommon, but not infeasible.) This adds a mocking function to simulate the `os.access` function such that it _always_ returns `False` for the `/root` directory. I figure this is preferable than trying to find a file that will not be user-readable on every system, especially because our unit tests have no need to test the behavior of the `os.access` function itself.
1 parent fa109df commit a0c36cb

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

src/firewheel/tests/unit/control/test_repository_db.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
11

2+
import os
3+
from unittest.mock import patch
4+
25
import pytest
36
from pathlib import Path
47

58
from firewheel.control.repository_db import RepositoryDb
69

710

11+
@pytest.fixture
12+
def mock_permissioned_filesystem():
13+
# Save a reference to the original `os.access` method before mocking
14+
os_access = os.access
15+
16+
def _deny_user_access_to_root_home_directory(path, mode, **kwargs):
17+
return False if Path(path) == Path("/root") else os_access(path, mode, **kwargs)
18+
19+
with patch("os.access", side_effect=_deny_user_access_to_root_home_directory):
20+
yield
21+
22+
823
def create_test_repo(repo_path):
924
repo_path.mkdir()
1025
return str(repo_path)
@@ -97,7 +112,9 @@ def test_add_repository(self, repository_db, repo_entry):
97112
[{"path": "asdf"}, FileNotFoundError], # ----- missing directory
98113
],
99114
)
100-
def test_add_repository_invalid(self, invalid_entry, exception, repository_db):
115+
def test_add_repository_invalid(
116+
self, invalid_entry, exception, mock_permissioned_filesystem, repository_db
117+
):
101118
with pytest.raises(exception):
102119
repository_db.add_repository(invalid_entry)
103120

0 commit comments

Comments
 (0)