Skip to content

Commit

Permalink
Improved coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
wolph committed Dec 11, 2023
1 parent 10469d0 commit 609cfc1
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 19 deletions.
9 changes: 4 additions & 5 deletions progressbar/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def _render_bar(
now,
expired,
) -> typing.Iterable[str]:
def update(force=True, write=True):
def update(force=True, write=True): # pragma: no cover
self._label_bar(bar_)
bar_.update(force=force)
if write:
Expand Down Expand Up @@ -325,15 +325,14 @@ def run(self, join=True):
Start the multibar render loop and run the progressbars until they
have force _thread_finished.
'''
while not self._thread_finished.is_set():
while not self._thread_finished.is_set(): # pragma: no branch
self.render()
time.sleep(self.update_interval)

if join or self._thread_closed.is_set():
# If the thread is closed, we need to check if force
# progressbars
# If the thread is closed, we need to check if the progressbars
# have finished. If they have, we can exit the loop
for bar_ in self.values():
for bar_ in self.values(): # pragma: no cover
if not bar_.finished():
break
else:
Expand Down
6 changes: 4 additions & 2 deletions pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ python_files =

addopts =
--cov progressbar
--cov-report html
--cov-report term-missing
--cov-report=html
--cov-report=term-missing
--cov-report=xml
--cov-append
--no-cov-on-fail
--doctest-modules

Expand Down
97 changes: 91 additions & 6 deletions tests/test_multibar.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import threading
import random
import time

import progressbar
Expand All @@ -22,12 +23,6 @@ def test_multi_progress_bar_out_of_range():
bar.update(multivalues=[-1])


def test_multi_progress_bar_fill_left():
import examples

return examples.multi_progress_bar_example(False)


def test_multibar():
multibar = progressbar.MultiBar(
sort_keyfunc=lambda bar: bar.label,
Expand Down Expand Up @@ -160,3 +155,93 @@ def test_multibar_empty_key():
bar.update(1)

multibar.render(force=True)


def test_multibar_print():

bars = 5
n = 10


def print_sometimes(bar, probability):
for i in bar(range(n)):
# Sleep up to 0.1 seconds
time.sleep(random.random() * 0.1)

# print messages at random intervals to show how extra output works
if random.random() < probability:
bar.print('random message for bar', bar, i)

with progressbar.MultiBar() as multibar:
for i in range(bars):
# Get a progressbar
bar = multibar[f'Thread label here {i}']
bar.max_error = False
# Create a thread and pass the progressbar
# Print never, sometimes and always
threading.Thread(target=print_sometimes, args=(bar, 0)).start()
threading.Thread(target=print_sometimes, args=(bar, 0.5)).start()
threading.Thread(target=print_sometimes, args=(bar, 1)).start()


for i in range(5):
multibar.print(f'{i}', flush=False)

multibar.update(force=True, flush=False)
multibar.update(force=True, flush=True)

def test_multibar_no_format():
with progressbar.MultiBar(initial_format=None, finished_format=None) as multibar:
bar = multibar['a']

for i in bar(range(5)):
bar.print(i)


def test_multibar_finished():
multibar = progressbar.MultiBar(initial_format=None, finished_format=None)
bar = multibar['bar'] = progressbar.ProgressBar(max_value=5)
bar2 = multibar['bar2']
multibar.render(force=True)
multibar.print('Hi')
multibar.render(force=True, flush=False)

for i in range(6):
bar.update(i)
bar2.update(i)

multibar.render(force=True)



def test_multibar_finished_format():
multibar = progressbar.MultiBar(finished_format='Finished {label}', show_finished=True)
bar = multibar['bar'] = progressbar.ProgressBar(max_value=5)
bar2 = multibar['bar2']
multibar.render(force=True)
multibar.print('Hi')
multibar.render(force=True, flush=False)
bar.start()
bar2.start()
multibar.render(force=True)
multibar.print('Hi')
multibar.render(force=True, flush=False)

for i in range(6):
bar.update(i)
bar2.update(i)

multibar.render(force=True)


def test_multibar_threads():
multibar = progressbar.MultiBar(finished_format=None, show_finished=True)
bar = multibar['bar'] = progressbar.ProgressBar(max_value=5)
multibar.start()
time.sleep(0.1)
bar.update(3)
time.sleep(0.1)
multibar.join()
bar.finish()
multibar.join()
multibar.render(force=True)
10 changes: 4 additions & 6 deletions tests/test_progressbar.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import contextlib
import time

Expand All @@ -11,10 +12,11 @@
except ImportError:
import sys

sys.path.append('..')
_project_dir = os.path.dirname(os.path.dirname(__file__))
sys.path.append(_project_dir)
import examples

sys.path.remove('..')
sys.path.remove(_project_dir)


def test_examples(monkeypatch):
Expand All @@ -40,8 +42,6 @@ def test_examples_nullbar(monkeypatch, example):


def test_reuse():
import progressbar

bar = progressbar.ProgressBar()
bar.start()
for i in range(10):
Expand All @@ -60,8 +60,6 @@ def test_reuse():


def test_dirty():
import progressbar

bar = progressbar.ProgressBar()
bar.start()
assert bar.started()
Expand Down

0 comments on commit 609cfc1

Please sign in to comment.