| 1 |
# -*- coding: UTF-8 -*- |
| 2 |
|
| 3 |
from __future__ import absolute_import |
| 4 |
import struct |
| 5 |
import sys |
| 6 |
import tempfile |
| 7 |
import unittest |
| 8 |
import six |
| 9 |
from mock import Mock, patch |
| 10 |
from .tools import eq_ |
| 11 |
|
| 12 |
from behave.formatter._registry import make_formatters |
| 13 |
from behave.formatter import pretty |
| 14 |
from behave.formatter.base import StreamOpener |
| 15 |
from behave.model import Tag, Feature, Scenario, Step |
| 16 |
from behave.model_core import Status |
| 17 |
from behave.matchers import Match |
| 18 |
|
| 19 |
|
| 20 |
class TestGetTerminalSize(unittest.TestCase): |
| 21 |
def setUp(self): |
| 22 |
try: |
| 23 |
self.ioctl_patch = patch("fcntl.ioctl") |
| 24 |
self.ioctl = self.ioctl_patch.start() |
| 25 |
except ImportError: |
| 26 |
self.ioctl_patch = None |
| 27 |
self.ioctl = None |
| 28 |
self.zero_struct = struct.pack("HHHH", 0, 0, 0, 0) |
| 29 |
|
| 30 |
def tearDown(self): |
| 31 |
if self.ioctl_patch: |
| 32 |
self.ioctl_patch.stop() |
| 33 |
|
| 34 |
def test_windows_fallback(self): # pylint: disable=no-self-use |
| 35 |
platform = sys.platform |
| 36 |
sys.platform = "windows" |
| 37 |
|
| 38 |
eq_(pretty.get_terminal_size(), (80, 24)) |
| 39 |
|
| 40 |
sys.platform = platform |
| 41 |
|
| 42 |
def test_termios_fallback(self): # pylint: disable=no-self-use |
| 43 |
try: |
| 44 |
import termios |
| 45 |
return |
| 46 |
except ImportError: |
| 47 |
pass |
| 48 |
|
| 49 |
eq_(pretty.get_terminal_size(), (80, 24)) |
| 50 |
|
| 51 |
def test_exception_in_ioctl(self): |
| 52 |
try: |
| 53 |
import termios |
| 54 |
except ImportError: |
| 55 |
return |
| 56 |
|
| 57 |
def raiser(*args, **kwargs): # pylint: disable=unused-argument |
| 58 |
raise Exception("yeehar!") |
| 59 |
|
| 60 |
self.ioctl.side_effect = raiser |
| 61 |
|
| 62 |
eq_(pretty.get_terminal_size(), (80, 24)) |
| 63 |
self.ioctl.assert_called_with(0, termios.TIOCGWINSZ, self.zero_struct) |
| 64 |
|
| 65 |
def test_happy_path(self): |
| 66 |
try: |
| 67 |
import termios |
| 68 |
except ImportError: |
| 69 |
return |
| 70 |
|
| 71 |
self.ioctl.return_value = struct.pack("HHHH", 17, 23, 5, 5) |
| 72 |
|
| 73 |
eq_(pretty.get_terminal_size(), (23, 17)) |
| 74 |
self.ioctl.assert_called_with(0, termios.TIOCGWINSZ, self.zero_struct) |
| 75 |
|
| 76 |
def test_zero_size_fallback(self): |
| 77 |
try: |
| 78 |
import termios |
| 79 |
except ImportError: |
| 80 |
return |
| 81 |
|
| 82 |
self.ioctl.return_value = self.zero_struct |
| 83 |
|
| 84 |
eq_(pretty.get_terminal_size(), (80, 24)) |
| 85 |
self.ioctl.assert_called_with(0, termios.TIOCGWINSZ, self.zero_struct) |
| 86 |
|
| 87 |
|
| 88 |
def _tf(): |
| 89 |
"""Open a temp file that looks a bunch like stdout.""" |
| 90 |
if six.PY3: |
| 91 |
# in python3 it's got an encoding and accepts new-style strings |
| 92 |
return tempfile.TemporaryFile(mode="w", encoding="UTF-8") |
| 93 |
|
| 94 |
# pre-python3 it's not got an encoding and accepts encoded data |
| 95 |
# (old-style strings) |
| 96 |
return tempfile.TemporaryFile(mode="w") |
| 97 |
|
| 98 |
|
| 99 |
class FormatterTests(unittest.TestCase): |
| 100 |
formatter_name = "plain" # SANE DEFAULT, overwritten by concrete classes |
| 101 |
|
| 102 |
def setUp(self): |
| 103 |
self.config = Mock() |
| 104 |
self.config.color = True |
| 105 |
self.config.outputs = [StreamOpener(stream=sys.stdout)] |
| 106 |
self.config.format = [self.formatter_name] |
| 107 |
|
| 108 |
_line = 0 |
| 109 |
@property |
| 110 |
def line(self): |
| 111 |
self._line += 1 |
| 112 |
return self._line |
| 113 |
|
| 114 |
def _formatter(self, file_object, config): # pylint: disable=no-self-use |
| 115 |
stream_opener = StreamOpener(stream=file_object) |
| 116 |
f = make_formatters(config, [stream_opener])[0] |
| 117 |
f.uri("<string>") |
| 118 |
return f |
| 119 |
|
| 120 |
def _feature(self, keyword=u"k\xe9yword", name=u"name", tags=None, |
| 121 |
location=u"location", # pylint: disable=unused-argument |
| 122 |
description=None, scenarios=None, background=None): |
| 123 |
if tags is None: |
| 124 |
tags = [u"spam", u"ham"] |
| 125 |
if description is None: |
| 126 |
description = [u"description"] |
| 127 |
if scenarios is None: |
| 128 |
scenarios = [] |
| 129 |
line = self.line |
| 130 |
tags = [Tag(name, line) for name in tags] |
| 131 |
return Feature("<string>", line, keyword, name, tags=tags, |
| 132 |
description=description, scenarios=scenarios, |
| 133 |
background=background) |
| 134 |
|
| 135 |
def _scenario(self, keyword=u"k\xe9yword", name=u"name", tags=None, steps=None): |
| 136 |
if tags is None: |
| 137 |
tags = [] |
| 138 |
if steps is None: |
| 139 |
steps = [] |
| 140 |
line = self.line |
| 141 |
tags = [Tag(name, line) for name in tags] |
| 142 |
return Scenario("<string>", line, keyword, name, tags=tags, steps=steps) |
| 143 |
|
| 144 |
def _step(self, keyword=u"k\xe9yword", step_type="given", name=u"name", |
| 145 |
text=None, table=None): |
| 146 |
line = self.line |
| 147 |
return Step("<string>", line, keyword, step_type, name, text=text, |
| 148 |
table=table) |
| 149 |
|
| 150 |
def _match(self, arguments=None): # pylint: disable=no-self-use |
| 151 |
def dummy(): |
| 152 |
pass |
| 153 |
|
| 154 |
return Match(dummy, arguments) |
| 155 |
|
| 156 |
def test_feature(self): |
| 157 |
# this test does not actually check the result of the formatting; it |
| 158 |
# just exists to make sure that formatting doesn't explode in the face of |
| 159 |
# unicode and stuff |
| 160 |
p = self._formatter(_tf(), self.config) |
| 161 |
f = self._feature() |
| 162 |
p.feature(f) |
| 163 |
|
| 164 |
def test_scenario(self): |
| 165 |
p = self._formatter(_tf(), self.config) |
| 166 |
f = self._feature() |
| 167 |
p.feature(f) |
| 168 |
s = self._scenario() |
| 169 |
p.scenario(s) |
| 170 |
|
| 171 |
def test_step(self): |
| 172 |
p = self._formatter(_tf(), self.config) |
| 173 |
f = self._feature() |
| 174 |
p.feature(f) |
| 175 |
scenario = self._scenario() |
| 176 |
p.scenario(scenario) |
| 177 |
s = self._step() |
| 178 |
p.step(s) |
| 179 |
p.match(self._match([])) |
| 180 |
s.status = Status.passed |
| 181 |
p.result(s) |
| 182 |
|
| 183 |
|
| 184 |
class TestPretty(FormatterTests): |
| 185 |
formatter_name = "pretty" |
| 186 |
|
| 187 |
|
| 188 |
class TestPlain(FormatterTests): |
| 189 |
formatter_name = "plain" |
| 190 |
|
| 191 |
|
| 192 |
class TestJson(FormatterTests): |
| 193 |
formatter_name = "json" |
| 194 |
|
| 195 |
|
| 196 |
class TestTagsCount(FormatterTests): |
| 197 |
formatter_name = "tags" |
| 198 |
|
| 199 |
def test_tag_counts(self): |
| 200 |
p = self._formatter(_tf(), self.config) |
| 201 |
|
| 202 |
s = self._scenario(tags=[u"ham", u"foo"]) |
| 203 |
f = self._feature(scenarios=[s]) # feature.tags= ham, spam |
| 204 |
p.feature(f) |
| 205 |
p.scenario(s) |
| 206 |
|
| 207 |
eq_(p.tag_counts, {"ham": [f, s], "spam": [f], "foo": [s]}) |
| 208 |
|
| 209 |
|
| 210 |
class MultipleFormattersTests(FormatterTests): |
| 211 |
formatters = [] |
| 212 |
|
| 213 |
def setUp(self): |
| 214 |
self.config = Mock() |
| 215 |
self.config.color = True |
| 216 |
self.config.outputs = [StreamOpener(stream=sys.stdout) |
| 217 |
for i in self.formatters] |
| 218 |
self.config.format = self.formatters |
| 219 |
|
| 220 |
def _formatters(self, file_object, config): # pylint: disable=no-self-use |
| 221 |
stream_opener = StreamOpener(stream=file_object) |
| 222 |
formatters = make_formatters(config, [stream_opener]) |
| 223 |
for f in formatters: |
| 224 |
f.uri("<string>") |
| 225 |
return formatters |
| 226 |
|
| 227 |
def test_feature(self): |
| 228 |
# this test does not actually check the result of the formatting; it |
| 229 |
# just exists to make sure that formatting doesn't explode in the face of |
| 230 |
# unicode and stuff |
| 231 |
formatters = self._formatters(_tf(), self.config) |
| 232 |
f = self._feature() |
| 233 |
for p in formatters: |
| 234 |
p.feature(f) |
| 235 |
|
| 236 |
def test_scenario(self): |
| 237 |
formatters = self._formatters(_tf(), self.config) |
| 238 |
f = self._feature() |
| 239 |
for p in formatters: |
| 240 |
p.feature(f) |
| 241 |
s = self._scenario() |
| 242 |
p.scenario(s) |
| 243 |
|
| 244 |
def test_step(self): |
| 245 |
formatters = self._formatters(_tf(), self.config) |
| 246 |
f = self._feature() |
| 247 |
for p in formatters: |
| 248 |
p.feature(f) |
| 249 |
scenario = self._scenario() |
| 250 |
p.scenario(scenario) |
| 251 |
s = self._step() |
| 252 |
p.step(s) |
| 253 |
p.match(self._match([])) |
| 254 |
s.status = Status.passed |
| 255 |
p.result(s) |
| 256 |
|
| 257 |
|
| 258 |
class TestPrettyAndPlain(MultipleFormattersTests): |
| 259 |
formatters = ["pretty", "plain"] |
| 260 |
|
| 261 |
class TestPrettyAndJSON(MultipleFormattersTests): |
| 262 |
formatters = ["pretty", "json"] |
| 263 |
|
| 264 |
class TestJSONAndPlain(MultipleFormattersTests): |
| 265 |
formatters = ["json", "plain"] |