-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1205 from qwcode/issue-219
--log file object getting closed prematurely (Issue #219)
- Loading branch information
Showing
2 changed files
with
71 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import os | ||
from pip.basecommand import Command | ||
from pip.log import logger | ||
|
||
|
||
class FakeCommand(Command): | ||
name = 'fake' | ||
summary = name | ||
def __init__(self, error=False): | ||
self.error = error | ||
super(FakeCommand, self).__init__() | ||
def run(self, options, args): | ||
logger.info("fake") | ||
if self.error: | ||
raise SystemExit(1) | ||
|
||
|
||
class Test_basecommand_logging(object): | ||
""" | ||
Test `pip.basecommand.Command` setting up logging consumers based on options | ||
""" | ||
|
||
def teardown(self): | ||
logger.consumers = [] | ||
|
||
def test_log(self, tmpdir): | ||
""" | ||
Test the --log option logs. | ||
""" | ||
cmd = FakeCommand() | ||
log_path = tmpdir.join('log') | ||
cmd.main(['fake', '--log', log_path]) | ||
assert 'fake' == open(log_path).read().strip()[:4] | ||
|
||
def test_log_file_success(self, tmpdir): | ||
""" | ||
Test the --log-file option *doesn't* log when command succeeds. | ||
(It's just the historical behavior? this just confirms it) | ||
""" | ||
cmd = FakeCommand() | ||
log_file_path = tmpdir.join('log_file') | ||
cmd.main(['fake', '--log-file', log_file_path]) | ||
assert not os.path.exists(log_file_path) | ||
|
||
def test_log_file_error(self, tmpdir): | ||
""" | ||
Test the --log-file option logs (when there's an error). | ||
""" | ||
cmd = FakeCommand(error=True) | ||
log_file_path = tmpdir.join('log_file') | ||
cmd.main(['fake', '--log-file', log_file_path]) | ||
assert 'fake' == open(log_file_path).read().strip()[:4] | ||
|
||
def test_log_log_file(self, tmpdir): | ||
""" | ||
Test the --log and --log-file options log together (when there's an error). | ||
""" | ||
cmd = FakeCommand(error=True) | ||
log_path = tmpdir.join('log') | ||
log_file_path = tmpdir.join('log_file') | ||
cmd.main(['fake', '--log', log_path, '--log-file', log_file_path]) | ||
assert 'fake' == open(log_path).read().strip()[:4] | ||
assert 'fake' == open(log_file_path).read().strip()[:4] |