Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion obsah/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,8 @@ def rotate_log(log_path: str):
backup_path = f"{log_path[:-4]}.{timestamp}.log"
else:
backup_path = f"{log_path}.{timestamp}"
os.rename(log_path, backup_path)
with contextlib.suppress(FileNotFoundError):
os.rename(log_path, backup_path)

def main(cliargs=None, application_config=ApplicationConfig): # pylint: disable=R0914
"""
Expand Down
19 changes: 19 additions & 0 deletions tests/test_log_rotate.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,22 @@ def test_rotate_log(tmp_path):
assert 'test.log' not in log_dir_contents[0].name
assert 'test.' in log_dir_contents[0].name
assert not log_file.exists()

def test_rotate_log_tolerates_concurrent_rotation(tmp_path):
log_file = tmp_path / 'test.log'
log_file.touch()
stolen = tmp_path / 'stolen.log'
original_exists = os.path.exists

def exists_then_steal(path):
result = original_exists(path)
if path == str(log_file) and result and not stolen.exists():
os.rename(path, str(stolen))
return result

with mock.patch('os.path.exists', side_effect=exists_then_steal):
obsah.rotate_log(str(log_file)) # must not raise

assert stolen.exists()
assert not log_file.exists()