2 # Copyright (c) 2014, 2016, 2019 Intel Corporation
4 # Permission is hereby granted, free of charge, to any person obtaining a copy
5 # of this software and associated documentation files (the "Software"), to deal
6 # in the Software without restriction, including without limitation the rights
7 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 # copies of the Software, and to permit persons to whom the Software is
9 # furnished to do so, subject to the following conditions:
11 # The above copyright notice and this permission notice shall be included in
12 # all copies or substantial portions of the Software.
14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 """Tests for log.py module."""
31 import framework
.log
as log
33 # pylint: disable=no-self-use,protected-access
38 """Create a unique state instance per test."""
39 return {'total': 1, 'complete': 0, 'lastlength': 0, 'running': [],
40 'summary': collections
.defaultdict(lambda: 0)}
43 class TestLogFactory(object):
44 """Tests for the LogFactory class."""
46 class TestGet(object):
47 """Tests for the LogFactory.get method."""
49 def test_returns_log(self
):
50 """Returns a BaseLog derived instance."""
51 logger
= log
.LogManager('quiet', 100)
52 log_inst
= logger
.get()
53 assert isinstance(log_inst
, log
.BaseLog
)
55 def test_log_state_update(self
):
56 """log.BaseLog.log updates shared state managed by LogManager"""
57 logger
= log
.LogManager('quiet', 100)
58 log_inst
= logger
.get()
62 assert logger
._state
['total'] == 100
63 assert logger
._state
['summary'] == {'pass': 1}
64 assert logger
._state
['complete'] == 1
67 class TestQuietLog(object):
68 """Test QuietLog class."""
70 class TestOutput(object):
71 """Test the output of the various methods."""
73 @pytest.fixture(autouse
=True, scope
='function')
74 def mock_stdout(self
, mocker
):
75 mocker
.patch
.object(sys
, 'stdout', io
.StringIO())
77 def test_log(self
, log_state
): # pylint: disable=redefined-outer-name
78 """Test the output of the log method."""
79 quiet
= log
.QuietLog(log_state
, threading
.Lock())
84 actual
= sys
.stdout
.read()
85 assert actual
== b
'[1/1] pass: 1 -\n'
87 def test_summary(self
, log_state
): # pylint: disable=redefined-outer-name
88 """Test the output of the summary method."""
89 quiet
= log
.QuietLog(log_state
, threading
.Lock())
90 # Call log to set the total correctly, then truncate and remove the
91 # values, so the we can test
100 # Because of the 'lastlength' mechanims there will likely be
101 # trainling whitespace after the the output, it's not useful to
102 # test that here, so just strip it.
103 assert sys
.stdout
.read().rstrip() == b
'[1/1] pass: 1'
105 def test_start(self
, log_state
): # pylint: disable=redefined-outer-name
106 """Test that the start method doesn't have output."""
107 quiet
= log
.QuietLog(log_state
, threading
.Lock())
112 actual
= sys
.stdout
.read()
116 class TestVerboseLog(object):
117 """Tests for the VerboseLog class."""
119 class TestOutput(object):
120 """Test the output of the various methods."""
122 @pytest.fixture(autouse
=True, scope
='function')
123 def mock_stdout(self
, mocker
):
124 mocker
.patch
.object(sys
, 'stdout', io
.StringIO())
126 def test_log(self
, log_state
): # pylint: disable=redefined-outer-name
127 """Test the output of the log method."""
128 l
= log
.VerboseLog(log_state
, threading
.Lock())
131 sys
.stdout
.truncate()
136 actual
= sys
.stdout
.read()
137 assert actual
== b
'pass: foo\n\n[1/1] pass: 1 /\n'
139 def test_summary(self
, log_state
): # pylint: disable=redefined-outer-name
140 """Test the output of the summary method."""
141 l
= log
.VerboseLog(log_state
, threading
.Lock())
145 sys
.stdout
.truncate()
150 assert sys
.stdout
.read().rstrip() == b
'[1/1] pass: 1'
152 def test_start(self
, log_state
): # pylint: disable=redefined-outer-name
153 """Test that the start method doesn't have output."""
154 l
= log
.VerboseLog(log_state
, threading
.Lock())
158 assert sys
.stdout
.read().rstrip() == b
'running: foo\n\n[0/1] \\'
161 class TestDummyLog(object):
162 """Tests for the DummyLog class."""
164 class TestOutput(object):
165 """Test the output of the various methods."""
167 @pytest.fixture(autouse
=True, scope
='function')
168 def mock_stdout(self
, mocker
):
169 mocker
.patch
.object(sys
, 'stdout', io
.StringIO())
171 def test_log(self
, log_state
): # pylint: disable=redefined-outer-name
172 """Test the output of the log method."""
173 quiet
= log
.DummyLog(log_state
, threading
.Lock())
178 actual
= sys
.stdout
.read()
181 def test_summary(self
, log_state
): # pylint: disable=redefined-outer-name
182 """Test the output of the summary method."""
183 quiet
= log
.DummyLog(log_state
, threading
.Lock())
187 actual
= sys
.stdout
.read()
190 def test_start(self
, log_state
): # pylint: disable=redefined-outer-name
191 """Test that the start method doesn't have output."""
192 quiet
= log
.DummyLog(log_state
, threading
.Lock())
196 actual
= sys
.stdout
.read()