-
-
Notifications
You must be signed in to change notification settings - Fork 258
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve failure modes for os.rename() as used in distribution caching.
- Loading branch information
Showing
3 changed files
with
62 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Copyright 2016 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
import errno | ||
import os | ||
from contextlib import contextmanager | ||
|
||
import pytest | ||
|
||
from pex.common import rename_if_empty | ||
|
||
try: | ||
from unittest import mock | ||
except ImportError: | ||
import mock | ||
|
||
|
||
@contextmanager | ||
def maybe_raises(exception=None): | ||
@contextmanager | ||
def noop(): | ||
yield | ||
|
||
with (noop() if exception is None else pytest.raises(exception)): | ||
yield | ||
|
||
|
||
def safe_rename_test(errno, expect_raises=None): | ||
with mock.patch('os.rename', spec_set=True, autospec=True) as mock_rename: | ||
mock_rename.side_effect = OSError(errno, os.strerror(errno)) | ||
with maybe_raises(expect_raises): | ||
rename_if_empty('from.dir', 'to.dir') | ||
|
||
|
||
def test_safe_rename_eexist(): | ||
safe_rename_test(errno.EEXIST) | ||
|
||
|
||
def test_safe_rename_enotempty(): | ||
safe_rename_test(errno.ENOTEMPTY) | ||
|
||
|
||
def test_safe_rename_eperm(): | ||
safe_rename_test(errno.EPERM, expect_raises=OSError) |