mirror of
https://github.com/jtesta/ssh-audit.git
synced 2024-11-22 18:41:40 +01:00
Use output spy for tests.
This commit is contained in:
parent
2abbe8f229
commit
4959029c33
@ -4,55 +4,37 @@ from __future__ import print_function
|
|||||||
import pytest, io, sys
|
import pytest, io, sys
|
||||||
|
|
||||||
|
|
||||||
if sys.version_info[0] == 2:
|
|
||||||
import StringIO
|
|
||||||
StringIO = StringIO.StringIO
|
|
||||||
else:
|
|
||||||
StringIO = io.StringIO
|
|
||||||
|
|
||||||
|
|
||||||
class TestOutput(object):
|
class TestOutput(object):
|
||||||
@pytest.fixture(autouse=True)
|
@pytest.fixture(autouse=True)
|
||||||
def init(self, ssh_audit):
|
def init(self, ssh_audit):
|
||||||
self.Output = ssh_audit.Output
|
self.Output = ssh_audit.Output
|
||||||
self.OutputBuffer = ssh_audit.OutputBuffer
|
self.OutputBuffer = ssh_audit.OutputBuffer
|
||||||
|
|
||||||
def _begin(self):
|
def test_output_buffer_no_lines(self, output_spy):
|
||||||
self.__out = StringIO()
|
output_spy.begin()
|
||||||
self.__old_stdout = sys.stdout
|
|
||||||
sys.stdout = self.__out
|
|
||||||
|
|
||||||
def _flush(self):
|
|
||||||
lines = self.__out.getvalue().splitlines()
|
|
||||||
sys.stdout = self.__old_stdout
|
|
||||||
self.__out = None
|
|
||||||
return lines
|
|
||||||
|
|
||||||
def test_output_buffer_no_lines(self):
|
|
||||||
self._begin()
|
|
||||||
with self.OutputBuffer() as obuf:
|
with self.OutputBuffer() as obuf:
|
||||||
pass
|
pass
|
||||||
assert self._flush() == []
|
assert output_spy.flush() == []
|
||||||
self._begin()
|
output_spy.begin()
|
||||||
with self.OutputBuffer() as obuf:
|
with self.OutputBuffer() as obuf:
|
||||||
pass
|
pass
|
||||||
obuf.flush()
|
obuf.flush()
|
||||||
assert self._flush() == []
|
assert output_spy.flush() == []
|
||||||
|
|
||||||
def test_output_buffer_no_flush(self):
|
def test_output_buffer_no_flush(self, output_spy):
|
||||||
self._begin()
|
output_spy.begin()
|
||||||
with self.OutputBuffer() as obuf:
|
with self.OutputBuffer() as obuf:
|
||||||
print(u'abc')
|
print(u'abc')
|
||||||
assert self._flush() == []
|
assert output_spy.flush() == []
|
||||||
|
|
||||||
def test_output_buffer_flush(self):
|
def test_output_buffer_flush(self, output_spy):
|
||||||
self._begin()
|
output_spy.begin()
|
||||||
with self.OutputBuffer() as obuf:
|
with self.OutputBuffer() as obuf:
|
||||||
print(u'abc')
|
print(u'abc')
|
||||||
print()
|
print()
|
||||||
print(u'def')
|
print(u'def')
|
||||||
obuf.flush()
|
obuf.flush()
|
||||||
assert self._flush() == [u'abc', u'', u'def']
|
assert output_spy.flush() == [u'abc', u'', u'def']
|
||||||
|
|
||||||
def test_output_defaults(self):
|
def test_output_defaults(self):
|
||||||
out = self.Output()
|
out = self.Output()
|
||||||
@ -61,50 +43,50 @@ class TestOutput(object):
|
|||||||
assert out.colors is True
|
assert out.colors is True
|
||||||
assert out.minlevel == 'info'
|
assert out.minlevel == 'info'
|
||||||
|
|
||||||
def test_output_colors(self):
|
def test_output_colors(self, output_spy):
|
||||||
out = self.Output()
|
out = self.Output()
|
||||||
# test without colors
|
# test without colors
|
||||||
out.colors = False
|
out.colors = False
|
||||||
self._begin()
|
output_spy.begin()
|
||||||
out.info('info color')
|
out.info('info color')
|
||||||
assert self._flush() == [u'info color']
|
assert output_spy.flush() == [u'info color']
|
||||||
self._begin()
|
output_spy.begin()
|
||||||
out.head('head color')
|
out.head('head color')
|
||||||
assert self._flush() == [u'head color']
|
assert output_spy.flush() == [u'head color']
|
||||||
self._begin()
|
output_spy.begin()
|
||||||
out.good('good color')
|
out.good('good color')
|
||||||
assert self._flush() == [u'good color']
|
assert output_spy.flush() == [u'good color']
|
||||||
self._begin()
|
output_spy.begin()
|
||||||
out.warn('warn color')
|
out.warn('warn color')
|
||||||
assert self._flush() == [u'warn color']
|
assert output_spy.flush() == [u'warn color']
|
||||||
self._begin()
|
output_spy.begin()
|
||||||
out.fail('fail color')
|
out.fail('fail color')
|
||||||
assert self._flush() == [u'fail color']
|
assert output_spy.flush() == [u'fail color']
|
||||||
# test with colors
|
# test with colors
|
||||||
out.colors = True
|
out.colors = True
|
||||||
self._begin()
|
output_spy.begin()
|
||||||
out.info('info color')
|
out.info('info color')
|
||||||
assert self._flush() == [u'info color']
|
assert output_spy.flush() == [u'info color']
|
||||||
self._begin()
|
output_spy.begin()
|
||||||
out.head('head color')
|
out.head('head color')
|
||||||
assert self._flush() == [u'\x1b[0;36mhead color\x1b[0m']
|
assert output_spy.flush() == [u'\x1b[0;36mhead color\x1b[0m']
|
||||||
self._begin()
|
output_spy.begin()
|
||||||
out.good('good color')
|
out.good('good color')
|
||||||
assert self._flush() == [u'\x1b[0;32mgood color\x1b[0m']
|
assert output_spy.flush() == [u'\x1b[0;32mgood color\x1b[0m']
|
||||||
self._begin()
|
output_spy.begin()
|
||||||
out.warn('warn color')
|
out.warn('warn color')
|
||||||
assert self._flush() == [u'\x1b[0;33mwarn color\x1b[0m']
|
assert output_spy.flush() == [u'\x1b[0;33mwarn color\x1b[0m']
|
||||||
self._begin()
|
output_spy.begin()
|
||||||
out.fail('fail color')
|
out.fail('fail color')
|
||||||
assert self._flush() == [u'\x1b[0;31mfail color\x1b[0m']
|
assert output_spy.flush() == [u'\x1b[0;31mfail color\x1b[0m']
|
||||||
|
|
||||||
def test_output_sep(self):
|
def test_output_sep(self, output_spy):
|
||||||
out = self.Output()
|
out = self.Output()
|
||||||
self._begin()
|
output_spy.begin()
|
||||||
out.sep()
|
out.sep()
|
||||||
out.sep()
|
out.sep()
|
||||||
out.sep()
|
out.sep()
|
||||||
assert self._flush() == [u'', u'', u'']
|
assert output_spy.flush() == [u'', u'', u'']
|
||||||
|
|
||||||
def test_output_levels(self):
|
def test_output_levels(self):
|
||||||
out = self.Output()
|
out = self.Output()
|
||||||
@ -127,49 +109,49 @@ class TestOutput(object):
|
|||||||
out.minlevel = 'invalid level'
|
out.minlevel = 'invalid level'
|
||||||
assert out.minlevel == 'unknown'
|
assert out.minlevel == 'unknown'
|
||||||
|
|
||||||
def test_output_minlevel(self):
|
def test_output_minlevel(self, output_spy):
|
||||||
out = self.Output()
|
out = self.Output()
|
||||||
# visible: all
|
# visible: all
|
||||||
out.minlevel = 'info'
|
out.minlevel = 'info'
|
||||||
self._begin()
|
output_spy.begin()
|
||||||
out.info('info color')
|
out.info('info color')
|
||||||
out.head('head color')
|
out.head('head color')
|
||||||
out.good('good color')
|
out.good('good color')
|
||||||
out.warn('warn color')
|
out.warn('warn color')
|
||||||
out.fail('fail color')
|
out.fail('fail color')
|
||||||
assert len(self._flush()) == 5
|
assert len(output_spy.flush()) == 5
|
||||||
# visible: head, warn, fail
|
# visible: head, warn, fail
|
||||||
out.minlevel = 'warn'
|
out.minlevel = 'warn'
|
||||||
self._begin()
|
output_spy.begin()
|
||||||
out.info('info color')
|
out.info('info color')
|
||||||
out.head('head color')
|
out.head('head color')
|
||||||
out.good('good color')
|
out.good('good color')
|
||||||
out.warn('warn color')
|
out.warn('warn color')
|
||||||
out.fail('fail color')
|
out.fail('fail color')
|
||||||
assert len(self._flush()) == 3
|
assert len(output_spy.flush()) == 3
|
||||||
# visible: head, fail
|
# visible: head, fail
|
||||||
out.minlevel = 'fail'
|
out.minlevel = 'fail'
|
||||||
self._begin()
|
output_spy.begin()
|
||||||
out.info('info color')
|
out.info('info color')
|
||||||
out.head('head color')
|
out.head('head color')
|
||||||
out.good('good color')
|
out.good('good color')
|
||||||
out.warn('warn color')
|
out.warn('warn color')
|
||||||
out.fail('fail color')
|
out.fail('fail color')
|
||||||
assert len(self._flush()) == 2
|
assert len(output_spy.flush()) == 2
|
||||||
# visible: head
|
# visible: head
|
||||||
out.minlevel = 'invalid level'
|
out.minlevel = 'invalid level'
|
||||||
self._begin()
|
output_spy.begin()
|
||||||
out.info('info color')
|
out.info('info color')
|
||||||
out.head('head color')
|
out.head('head color')
|
||||||
out.good('good color')
|
out.good('good color')
|
||||||
out.warn('warn color')
|
out.warn('warn color')
|
||||||
out.fail('fail color')
|
out.fail('fail color')
|
||||||
assert len(self._flush()) == 1
|
assert len(output_spy.flush()) == 1
|
||||||
|
|
||||||
def test_output_batch(self):
|
def test_output_batch(self, output_spy):
|
||||||
out = self.Output()
|
out = self.Output()
|
||||||
# visible: all
|
# visible: all
|
||||||
self._begin()
|
output_spy.begin()
|
||||||
out.minlevel = 'info'
|
out.minlevel = 'info'
|
||||||
out.batch = False
|
out.batch = False
|
||||||
out.info('info color')
|
out.info('info color')
|
||||||
@ -177,9 +159,9 @@ class TestOutput(object):
|
|||||||
out.good('good color')
|
out.good('good color')
|
||||||
out.warn('warn color')
|
out.warn('warn color')
|
||||||
out.fail('fail color')
|
out.fail('fail color')
|
||||||
assert len(self._flush()) == 5
|
assert len(output_spy.flush()) == 5
|
||||||
# visible: all except head
|
# visible: all except head
|
||||||
self._begin()
|
output_spy.begin()
|
||||||
out.minlevel = 'info'
|
out.minlevel = 'info'
|
||||||
out.batch = True
|
out.batch = True
|
||||||
out.info('info color')
|
out.info('info color')
|
||||||
@ -187,4 +169,4 @@ class TestOutput(object):
|
|||||||
out.good('good color')
|
out.good('good color')
|
||||||
out.warn('warn color')
|
out.warn('warn color')
|
||||||
out.fail('fail color')
|
out.fail('fail color')
|
||||||
assert len(self._flush()) == 4
|
assert len(output_spy.flush()) == 4
|
||||||
|
Loading…
Reference in New Issue
Block a user