Skip to content

Commit

Permalink
Rename class 'MockPrint' -> 'MockStdStream' for unittest
Browse files Browse the repository at this point in the history
  • Loading branch information
raimon49 committed Dec 16, 2019
1 parent b704023 commit da1a7e6
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions test_piplicenses.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,9 +446,9 @@ def tearDown(self):
pass


class MockPrint(object):
class MockStdStream(object):

def __init_(self):
def __init__(self):
self.printed = ''

def write(self, p):
Expand All @@ -460,30 +460,36 @@ def mocked_open(*args, **kwargs):
import tempfile
return tempfile.TemporaryFile('w')

mocked_print = MockPrint()
mocked_stdout = MockStdStream()
mocked_stderr = MockStdStream()
import codecs
import sys
monkeypatch.setattr(codecs, 'open', mocked_open)
monkeypatch.setattr(sys.stdout, 'write', mocked_print.write)
monkeypatch.setattr(sys.stdout, 'write', mocked_stdout.write)
monkeypatch.setattr(sys.stderr, 'write', mocked_stderr.write)
monkeypatch.setattr(sys, 'exit', lambda n: None)

save_if_needs('/foo/bar.txt', 'license list')
assert 'created path: ' in mocked_print.printed
assert 'created path: ' in mocked_stdout.printed
assert '' == mocked_stderr.printed


def test_output_file_error(monkeypatch):
def mocked_open(*args, **kwargs):
raise IOError

mocked_print = MockPrint()
mocked_stdout = MockStdStream()
mocked_stderr = MockStdStream()
import codecs
import sys
monkeypatch.setattr(codecs, 'open', mocked_open)
monkeypatch.setattr(sys.stderr, 'write', mocked_print.write)
monkeypatch.setattr(sys.stdout, 'write', mocked_stdout.write)
monkeypatch.setattr(sys.stderr, 'write', mocked_stderr.write)
monkeypatch.setattr(sys, 'exit', lambda n: None)

save_if_needs('/foo/bar.txt', 'license list')
assert 'check path: ' in mocked_print.printed
assert '' == mocked_stdout.printed
assert 'check path: ' in mocked_stderr.printed


def test_output_file_none(monkeypatch):
Expand Down

0 comments on commit da1a7e6

Please sign in to comment.