def test_resize_threaded(self): """ Test a resize event threading behavior """ manager = _manager.Manager(stream=self.tty.stdout, counter_class=MockCounter, threaded=True) counter3 = MockCounter(manager=manager) counter3.last_update = time.time() manager.counters[counter3] = 3 manager.scroll_offset = 4 term = manager.term # simulate resize manager._stage_resize() self.assertTrue(manager._resize) self.assertEqual(counter3.last_update, 0) with mock.patch('%s.width' % TERMINAL, new_callable=mock.PropertyMock) as mockwidth: mockwidth.return_value = 70 # resize doesn't happen until a write is called self.assertEqual(manager.width, 80) with mock.patch('enlighten._manager.Manager._set_scroll_area') as ssa: manager.write() self.assertEqual(ssa.call_count, 1) self.assertEqual(manager.width, 70) self.tty.stdout.write(u'X\n') self.assertEqual(self.tty.stdread.readline(), term.move(21, 0) + term.clear_eos + 'X\n') self.assertFalse(manager.resize_lock) self.assertFalse(manager._resize) self.assertEqual(counter3.calls, ['refresh(flush=False, elapsed=None)'])
def test_stop_no_set_scroll(self): """ set_scroll is False """ with mock.patch('%s.reset' % TERMINAL) as reset: manager = _manager.Manager(stream=self.tty.stdout, counter_class=MockCounter, set_scroll=False) manager.counters[MockCounter(manager=manager)] = 3 manager.counters[MockCounter(manager=manager)] = 4 term = manager.term with mock.patch('enlighten._manager.atexit'): with mock.patch.object(term, 'change_scroll'): manager._set_scroll_area() self.assertEqual(manager.scroll_offset, 5) self.assertEqual(signal.getsignal(signal.SIGWINCH), manager._resize_handler) self.assertTrue(manager.process_exit) # Stream empty self.tty.stdout.write('X\n') self.assertEqual(self.tty.stdread.readline(), 'X\n') manager.stop() self.assertEqual(signal.getsignal(signal.SIGWINCH), manager.sigwinch_orig) self.assertFalse(reset.called) self.tty.stdout.write('X\n') self.assertEqual(self.tty.stdread.readline(), term.move(23, 0) + term.clear_eol + term.move(24, 0) + term.clear_eol + term.move(25, 0) + 'X\n') self.assertFalse(manager.process_exit)
def test_stop_position_1(self): """ Ensure a line feed is given if there is a counter at position 1 """ manager = _manager.Manager(stream=self.tty.stdout, counter_class=MockCounter) term = manager.term manager.counters[MockCounter(manager=manager)] = 3 with mock.patch.object(manager, '_flush_streams'): manager.stop() self.assertEqual(manager._buffer, [term.normal_cursor, term.csr(0, term.height - 1), term.move(term.height, 0)]) del manager._buffer[:] manager.enabled = True manager.counters[MockCounter(manager=manager)] = 1 with mock.patch.object(manager, '_flush_streams'): manager.stop() self.assertEqual(manager._buffer, [term.normal_cursor, term.csr(0, term.height - 1), term.move(term.height, 0), term.cud1 or '\n'])
def test_stop_companion(self): """ In this case, we have a companion terminal Just make sure we have an extra reset """ manager = _manager.Manager(stream=self.tty.stdout, counter_class=MockCounter, companion_stream=self.tty.stdout) manager.counters[MockCounter(manager=manager)] = 3 manager.counters[MockCounter(manager=manager)] = 4 term = manager.term with mock.patch('enlighten._manager.atexit'): manager._set_scroll_area() del manager._buffer[:] del manager._companion_buffer[:] with mock.patch.object(manager, '_flush_streams'): manager.stop() self.assertEqual(manager._buffer, [term.move(term.height - 2, 0), term.clear_eol, term.move(term.height - 1, 0), term.clear_eol, term.normal_cursor, term.csr(0, term.height - 1), term.move(term.height, 0)]) self.assertEqual(manager._companion_buffer, [term.normal_cursor, term.csr(0, term.height - 1), term.move(term.height, 0)])
def test_refresh_total(self): counter = MockCounter(total=100, min_delta=0, manager=self.manager) counter.update() self.assertEqual(counter.output, [1]) counter.min_delta = 500 counter.update() self.assertEqual(counter.output, [1]) counter.update(98) self.assertEqual(counter.output, [1, 100])
def test_force(self): counter = MockCounter(total=100, min_delta=0, manager=self.manager) counter.update() self.assertEqual(counter.output, [1]) counter.min_delta = 500 counter.update() self.assertEqual(counter.output, [1]) counter.update(force=True) self.assertEqual(counter.output, [1, 3])
def test_enabled(self): counter = MockCounter(total=100, min_delta=0, manager=self.manager) counter.update() self.assertEqual(counter.output, [1]) counter.update() self.assertEqual(counter.output, [1, 2]) counter.enabled = False counter.update() self.assertEqual(counter.output, [1, 2])
def test_stop(self): with mock.patch('%s.reset' % TERMINAL) as reset: manager = _manager.Manager(stream=self.tty.stdout, counter_class=MockCounter) manager.counters[MockCounter(manager=manager)] = 3 manager.counters[MockCounter(manager=manager)] = 4 term = manager.term self.assertIsNone(manager.companion_term) with mock.patch('enlighten._manager.atexit'): with mock.patch.object(term, 'change_scroll'): manager._set_scroll_area() self.assertEqual(manager.scroll_offset, 5) self.assertEqual(signal.getsignal(signal.SIGWINCH), manager._resize_handler) self.assertTrue(manager.process_exit) # Clear stream self.tty.stdout.write(u'X\n') for num in range(4 + 1): # pylint: disable=unused-variable self.tty.stdread.readline() self.assertFalse(reset.called) manager.enabled = False manager.stop() # No output, No changes self.tty.stdout.write(u'X\n') self.assertEqual(self.tty.stdread.readline(), 'X\n') self.assertEqual(signal.getsignal(signal.SIGWINCH), manager._resize_handler) self.assertTrue(manager.process_exit) manager.enabled = True manager.stop() self.assertEqual(signal.getsignal(signal.SIGWINCH), manager.sigwinch_orig) self.assertEqual(reset.call_count, 1) self.tty.stdout.write(u'X\n') self.assertEqual( self.tty.stdread.readline(), term.move(23, 0) + term.clear_eol + term.move(24, 0) + term.clear_eol + 'X\n') self.assertFalse(manager.process_exit) self.assertFalse(manager.enabled) for counter in manager.counters: self.assertFalse(counter.enabled)
def test_no_resize_signal(self): # Test normal case initialization stdmgr = _manager.Manager(stream=self.tty.stdout) self.assertTrue(hasattr(stdmgr, 'sigwinch_orig')) stdmgr.counters[MockCounter(manager=stdmgr)] = 3 stdmgr.counters[MockCounter(manager=stdmgr)] = 4 # Test no resize signal initialization with mock.patch.object(_manager, 'RESIZE_SUPPORTED', False): manager = _manager.Manager(stream=self.tty.stdout) self.assertFalse(hasattr(manager, 'sigwinch_orig')) manager.counters[MockCounter(manager=manager)] = 3 manager.counters[MockCounter(manager=manager)] = 4 # Test set_scroll_area() with mock.patch.object(_manager.signal, 'signal', wraps=_manager.signal.signal) as mocksignal: with mock.patch('enlighten._manager.atexit'): # Test no resize signal set_scroll_area with mock.patch.object(_manager, 'RESIZE_SUPPORTED', False): with mock.patch.object(manager.term, 'change_scroll'): manager._set_scroll_area() self.assertFalse(mocksignal.called) # Test normal case set_scroll_area with mock.patch.object(stdmgr.term, 'change_scroll'): stdmgr._set_scroll_area() self.assertTrue(mocksignal.called) # Test stop() with mock.patch.object(_manager.signal, 'signal', wraps=_manager.signal.signal) as mocksignal: with mock.patch('%s.reset' % TERMINAL): # Test no resize signal stop with mock.patch.object(_manager, 'RESIZE_SUPPORTED', False): manager.stop() self.assertFalse(mocksignal.called) # Test normal case stop stdmgr.stop() self.assertTrue(mocksignal.called)
def test_stop(self): manager = _manager.Manager(stream=self.tty.stdout, counter_class=MockCounter) manager.counters[MockCounter(manager=manager)] = 3 manager.counters[MockCounter(manager=manager)] = 4 term = manager.term self.assertIsNone(manager.companion_term) with mock.patch('enlighten._manager.atexit'): manager._set_scroll_area() self.assertEqual(manager.scroll_offset, 5) self.assertEqual(signal.getsignal(signal.SIGWINCH), manager._stage_resize) self.assertTrue(manager.process_exit) # Clear buffer del manager._buffer[:] manager.enabled = False manager.stop() # No output, No changes self.tty.stdout.write(u'X\n') self.assertEqual(self.tty.stdread.readline(), 'X\n') self.assertEqual(signal.getsignal(signal.SIGWINCH), manager._stage_resize) self.assertTrue(manager.process_exit) manager.enabled = True manager.stop() self.assertEqual(signal.getsignal(signal.SIGWINCH), manager.sigwinch_orig) self.tty.stdout.write(u'X\n') self.assertEqual( self.tty.stdread.readline(), term.move(term.height - 2, 0) + term.clear_eol + term.move(term.height - 1, 0) + term.clear_eol + term.normal_cursor + term.csr(0, term.height - 1) + term.move(term.height, 0) + 'X\n') self.assertFalse(manager.process_exit) self.assertFalse(manager.enabled) for counter in manager.counters: self.assertFalse(counter.enabled)
def test_resize_handler_height_greater_threaded(self): with mock.patch('%s.height' % TERMINAL, new_callable=mock.PropertyMock) as mockheight: mockheight.side_effect = [25, 27] manager = _manager.Manager(stream=self.tty.stdout, counter_class=MockCounter, threaded=True) counter3 = MockCounter(manager=manager) manager.counters[counter3] = 3 manager.scroll_offset = 4 term = manager.term with mock.patch('enlighten._manager.Manager._set_scroll_area') as ssa: manager._resize_handler() self.assertEqual(ssa.call_count, 1) self.assertEqual(manager.height, 27) self.tty.stdout.write(u'X\n') self.assertEqual(self.tty.stdread.readline(), term.move(27, 0) + '\n') self.assertEqual(self.tty.stdread.readline(), '\n') self.assertEqual(self.tty.stdread.readline(), '\n') self.assertEqual(self.tty.stdread.readline(), term.move(23, 0) + term.clear_eos + 'X\n') self.assertEqual(counter3.calls, ['refresh(flush=False, elapsed=None)'])
def test_resize_handler_height_less(self): with mock.patch('%s.height' % TERMINAL, new_callable=mock.PropertyMock) as mockheight: mockheight.side_effect = [25, 23] manager = _manager.Manager(stream=self.tty.stdout, counter_class=MockCounter) counter3 = MockCounter(manager=manager) manager.counters[counter3] = 3 manager.scroll_offset = 4 with mock.patch( 'enlighten._manager.Manager._set_scroll_area') as ssa: manager._resize_handler() self.assertEqual(ssa.call_count, 1) self.assertEqual(manager.height, 23) self.assertEqual(self.tty.stdread.readline(), manager.term.move(19, 0) + '\n') for _ in range(5): self.assertEqual(self.tty.stdread.readline(), '\n') self.assertEqual(counter3.calls, ['refresh(flush=False, elapsed=None)'])
def test_resize_handler_height_only(self): with mock.patch('%s.height' % TERMINAL, new_callable=mock.PropertyMock) as mockheight: mockheight.side_effect = [25, 23, 28, 30, 30] manager = _manager.Manager(stream=self.tty.stdout, counter_class=MockCounter) counter3 = MockCounter(manager=manager) manager.counters[counter3] = 3 manager.scroll_offset = 4 with mock.patch( 'enlighten._manager.Manager._set_scroll_area') as ssa: manager._resize_handler() self.assertEqual(ssa.call_count, 1) # Height is set in _set_scroll_area which is mocked self.assertEqual(manager.height, 25) self.tty.stdout.write(u'X\n') self.assertEqual(self.tty.stdread.readline(), 'X\n') self.assertEqual(counter3.calls, ['refresh(flush=False, elapsed=None)'])
def test_stop_position_1(self): """ Ensure a line feed is given if there is a counter at position 1 """ manager = _manager.Manager(stream=self.tty.stdout, counter_class=MockCounter) manager.counters[MockCounter(manager=manager)] = 3 with mock.patch.object(manager.term, 'feed') as termfeed: manager.stop() self.assertFalse(termfeed.called) manager.enabled = True manager.counters[MockCounter(manager=manager)] = 1 with mock.patch.object(manager.term, 'feed') as termfeed: manager.stop() self.assertTrue(termfeed.called)
def test_stop_never_used(self): """ In this case, _set_scroll_area() was never called """ with mock.patch('%s.reset' % TERMINAL) as reset: manager = _manager.Manager(stream=self.tty.stdout, counter_class=MockCounter) manager.counters[MockCounter(manager=manager)] = 3 manager.counters[MockCounter(manager=manager)] = 4 self.assertFalse(manager.process_exit) manager.stop() self.assertEqual(signal.getsignal(signal.SIGWINCH), manager.sigwinch_orig) self.assertEqual(reset.call_count, 1) # No output self.tty.stdout.write('X\n') self.assertEqual(self.tty.stdread.readline(), 'X\n')
def test_stop_companion(self): """ In this case, we have a companion terminal Just make sure we have an extra reset """ manager = _manager.Manager(stream=self.tty.stdout, counter_class=MockCounter, companion_stream=self.tty.stdout) manager.counters[MockCounter(manager=manager)] = 3 manager.counters[MockCounter(manager=manager)] = 4 term = manager.term with mock.patch('enlighten._manager.atexit'): with mock.patch.object(term, 'change_scroll'): manager._set_scroll_area() with mock.patch.object(manager.companion_term, 'reset') as compReset: manager.stop() self.assertEqual(compReset.call_count, 1)
def test_stop_never_used(self): """ In this case, _set_scroll_area() was never called """ manager = _manager.Manager(stream=self.tty.stdout, counter_class=MockCounter) manager.counters[MockCounter(manager=manager)] = 3 manager.counters[MockCounter(manager=manager)] = 4 term = manager.term self.assertFalse(manager.process_exit) manager.stop() self.assertEqual(signal.getsignal(signal.SIGWINCH), manager.sigwinch_orig) # Only reset terminal self.tty.stdout.write(u'X\n') reset = term.normal_cursor + term.csr(0, term.height - 1) + term.move(term.height, 0) self.assertEqual(self.tty.stdread.readline(), reset + 'X\n')
def test_no_resize(self): with mock.patch.object(_manager.signal, 'signal', wraps=_manager.signal.signal) as mocksignal: manager = _manager.Manager(stream=self.tty.stdout, no_resize=True) self.assertFalse(hasattr(manager, 'sigwinch_orig')) self.assertFalse(mocksignal.called) manager.counters[MockCounter(manager=manager)] = 3 manager.counters[MockCounter(manager=manager)] = 4 with mock.patch.object(manager.term, 'change_scroll'): manager._set_scroll_area() self.assertFalse(mocksignal.called) manager.stop() self.assertFalse(mocksignal.called)
def test_resize(self): """ Resize lock must be False for handler to run Terminal size is cached unless resize handler runs """ manager = _manager.Manager(stream=self.tty.stdout, counter_class=MockCounter) counter3 = MockCounter(manager=manager) manager.counters[counter3] = 3 manager.scroll_offset = 4 term = manager.term with mock.patch('%s.width' % TERMINAL, new_callable=mock.PropertyMock) as mockwidth: mockwidth.return_value = 70 manager.resize_lock = True with mock.patch( 'enlighten._manager.Manager._set_scroll_area') as ssa: manager._stage_resize() self.assertFalse(ssa.called) self.assertEqual(manager.width, 80) self.assertTrue(manager.resize_lock) self.tty.stdout.write(u'X\n') self.assertEqual(self.tty.stdread.readline(), 'X\n') self.assertEqual(counter3.calls, []) manager.resize_lock = False with mock.patch( 'enlighten._manager.Manager._set_scroll_area') as ssa: manager._resize_handler() self.assertEqual(ssa.call_count, 1) self.assertEqual(manager.width, 70) self.assertFalse(manager.resize_lock) self.tty.stdout.write(u'X\n') self.assertEqual(self.tty.stdread.readline(), term.move(21, 0) + term.clear_eos + 'X\n') self.assertEqual(counter3.calls, ['refresh(flush=False, elapsed=None)'])
def test_resize_handler(self): with mock.patch('%s.width' % TERMINAL, new_callable=mock.PropertyMock) as mockheight: mockheight.side_effect = [80, 85, 87, 70, 70] manager = _manager.Manager(stream=self.tty.stdout, counter_class=MockCounter) counter3 = MockCounter(manager=manager) manager.counters[counter3] = 3 manager.scroll_offset = 4 term = manager.term manager.resize_lock = True with mock.patch( 'enlighten._manager.Manager._set_scroll_area') as ssa: manager._resize_handler() self.assertFalse(ssa.called) self.assertEqual(manager.width, 80) self.assertTrue(manager.resize_lock) self.tty.stdout.write(u'X\n') self.assertEqual(self.tty.stdread.readline(), 'X\n') self.assertEqual(counter3.calls, []) manager.resize_lock = False mockheight.side_effect = [80, 85, 87, 70, 70] with mock.patch( 'enlighten._manager.Manager._set_scroll_area') as ssa: manager._resize_handler() self.assertEqual(ssa.call_count, 1) self.assertEqual(manager.width, 70) self.assertFalse(manager.resize_lock) self.tty.stdout.write(u'X\n') self.assertEqual(self.tty.stdread.readline(), term.move(19, 0) + term.clear_eos + 'X\n') self.assertEqual(counter3.calls, ['refresh(flush=False, elapsed=None)'])
def test_resize_handler_no_change(self): with mock.patch('%s.width' % TERMINAL, new_callable=mock.PropertyMock) as mockheight: mockheight.side_effect = [80, 85, 87, 80, 80] manager = _manager.Manager(stream=self.tty.stdout, counter_class=MockCounter) counter3 = MockCounter(manager=manager) manager.counters[counter3] = 3 manager.scroll_offset = 4 with mock.patch('enlighten._manager.Manager._set_scroll_area') as ssa: manager._resize_handler() self.assertEqual(ssa.call_count, 1) self.assertEqual(manager.width, 80) self.tty.stdout.write('X\n') self.assertEqual(self.tty.stdread.readline(), 'X\n') self.assertEqual(counter3.calls, ['refresh(flush=False, elapsed=None)'])
def test_increment(self): counter = MockCounter(total=100, min_delta=0, manager=self.manager) counter.update() self.assertEqual(counter.count, 1) counter.update(5) self.assertEqual(counter.count, 6)
def test_close(self): manager = MockManager() # Clear is False ctr = MockCounter(manager=manager) ctr.close() self.assertEqual(ctr.calls, ['refresh(flush=True, elapsed=None)']) self.assertEqual(manager.remove_calls, 1) # Clear is True, leave is True ctr = MockCounter(manager=manager, leave=True) ctr.close(clear=True) self.assertEqual(ctr.calls, ['refresh(flush=True, elapsed=None)']) self.assertEqual(manager.remove_calls, 2) # Clear is True, leave is False ctr = MockCounter(manager=manager, leave=False) ctr.close(clear=True) self.assertEqual(ctr.calls, ['clear(flush=True)']) self.assertEqual(manager.remove_calls, 3)
def test_delta(self): counter = MockCounter(total=100, min_delta=0, manager=self.manager) counter.update() self.assertEqual(counter.output, [1]) counter.update() self.assertEqual(counter.output, [1, 2]) counter.min_delta = 500 counter.update() self.assertEqual(counter.output, [1, 2]) counter.min_delta = .01 time.sleep(.01) counter.update() self.assertEqual(counter.output, [1, 2, 4])