1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
# -*- coding: utf-8 -*-
# Copyright 2017 - 2022 Avram Lubkin, All Rights Reserved
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
"""
Test module for Enlighten
"""
from contextlib import contextmanager
import fcntl
import io
import os
import pty
import struct
import sys
import termios
import unittest
from enlighten import Manager
from enlighten._basecounter import BaseCounter
from enlighten._counter import Counter
from enlighten._statusbar import StatusBar
if sys.version_info[:2] < (3, 3):
import mock
else:
from unittest import mock # noqa: F401 # pylint: disable=no-name-in-module
PY2 = sys.version_info[0] < 3
OUTPUT = io.StringIO()
os.environ['TERM'] = 'xterm-256color' # Default to xterm-256color
# pylint: disable=missing-docstring
class TestCase(unittest.TestCase):
"""
Subclass of :py:class:`unittest.TestCase` for customization
"""
# Fix deprecated methods for 2.7
def assert_regex(self, text, regex, msg=None):
"""
Wrapper for assertRegexpMatches
"""
return self.assertRegexpMatches(text, regex, msg)
def assert_not_regex(self, text, regex, msg=None):
"""
Wrapper for assertNotRegexpMatches
"""
return self.assertNotRegexpMatches(text, regex, msg)
def assert_raises_regex(self, exception, regex, *args, **kwargs):
"""
Wrapper for assertRaisesRegexp
"""
return self.assertRaisesRegexp(exception, regex, *args, **kwargs)
if not hasattr(TestCase, 'assertRegex'):
TestCase.assertRegex = assert_regex
if not hasattr(TestCase, 'assertNotRegex'):
TestCase.assertNotRegex = assert_not_regex
if not hasattr(TestCase, 'assertRaisesRegex'):
TestCase.assertRaisesRegex = assert_raises_regex
# Some tests fail if "real" stdout is does not have a file descriptor
try:
sys.__stdout__.fileno()
except ValueError:
STDOUT_NO_FD = True
else:
STDOUT_NO_FD = False
@contextmanager
def redirect_output(stream, target):
"""
Temporary redirector for stdout and stderr
"""
original = getattr(sys, stream)
try:
setattr(sys, stream, target)
yield
finally:
setattr(sys, stream, original)
class MockTTY(object):
def __init__(self, height=25, width=80):
self.master, self.slave = pty.openpty()
# pylint: disable=consider-using-with
self.stdout = io.open(self.slave, 'w', 1, encoding='UTF-8', newline='')
self.stdread = io.open(self.master, 'r', encoding='UTF-8', newline='\n')
# Make sure linefeed behavior is consistent between Python 2 and Python 3
termattrs = termios.tcgetattr(self.slave)
termattrs[1] = termattrs[1] & ~termios.ONLCR & ~termios.OCRNL
termattrs[0] = termattrs[0] & ~termios.ICRNL
termios.tcsetattr(self.slave, termios.TCSADRAIN, termattrs)
self.resize(height, width)
def flush(self):
self.stdout.flush()
def close(self):
self.stdout.flush()
self.stdout.close()
self.stdread.close()
def clear(self):
# Using TCIOFLUSH here instead of TCIFLUSH to support MacOS
termios.tcflush(self.stdread, termios.TCIOFLUSH)
def resize(self, height, width):
fcntl.ioctl(self.slave, termios.TIOCSWINSZ, struct.pack('hhhh', height, width, 0, 0))
class MockBaseCounter(BaseCounter):
"""
Mock version of base counter for testing
"""
def update(self, *args, **kwargs):
"""
Simple update that updates the count. We know it's called based on the count.
"""
self.count += 1
class MockCounter(Counter):
__slots__ = ('output', 'calls')
def __init__(self, *args, **kwargs):
super(MockCounter, self).__init__(*args, **kwargs)
self.output = []
self.calls = []
def refresh(self, flush=True, elapsed=None):
self.output.append(self.count)
self.calls.append('refresh(flush=%s, elapsed=%s)' % (flush, elapsed))
def clear(self, flush=True):
self.calls.append('clear(flush=%s)' % flush)
class MockStatusBar(StatusBar):
__slots__ = ('called', 'calls')
def __init__(self, *args, **kwargs):
super(MockStatusBar, self).__init__(*args, **kwargs)
self.called = 0
self.calls = []
def refresh(self, flush=True, elapsed=None):
self.called += 1
self.calls.append('refresh(flush=%s, elapsed=%s)' % (flush, elapsed))
class MockManager(Manager):
# pylint: disable=super-init-not-called
def __init__(self, counter_class=Counter, **kwargs):
super(MockManager, self).__init__(counter_class=counter_class, **kwargs)
self.width = 80
self.output = []
self.remove_calls = 0
def write(self, output='', flush=True, counter=None, **kwargs):
if callable(output):
output = output(**kwargs)
self.output.append('write(output=%s, flush=%s, position=%s)' %
(output, flush, counter.position))
def remove(self, counter):
self.remove_calls += 1
super(MockManager, self).remove(counter)
|