import io import logging from ldap2nextcloud.progress import BarProgress, LogProgress, make_progress def messages(caplog): return [r.getMessage() for r in caplog.records if "Progression" in r.getMessage()] def test_log_progress_every_100(caplog): progress = LogProgress(every=100) with caplog.at_level(logging.INFO): progress.start(250) for _ in range(250): progress.advance() progress.close() assert messages(caplog) == [ "Progression : 100/250", "Progression : 200/250", "Progression : 250/250", ] def test_log_progress_exact_multiple_has_no_duplicate(caplog): progress = LogProgress(every=100) with caplog.at_level(logging.INFO): progress.start(200) for _ in range(200): progress.advance() progress.close() assert messages(caplog) == ["Progression : 100/200", "Progression : 200/200"] def test_log_progress_nothing_to_do_is_silent(caplog): progress = LogProgress() with caplog.at_level(logging.INFO): progress.start(0) progress.close() assert messages(caplog) == [] def test_bar_progress_draws_to_stream(): stream = io.StringIO() progress = BarProgress(file=stream) progress.start(3) for _ in range(3): progress.advance() progress.close() assert "3/3" in stream.getvalue() def test_bar_progress_keeps_log_messages_visible(caplog): stream = io.StringIO() progress = BarProgress(file=stream) with caplog.at_level(logging.WARNING): progress.start(2) logging.getLogger("ldap2nextcloud.test").warning("fiche en erreur") progress.advance() progress.close() assert "fiche en erreur" in caplog.text def test_make_progress_depends_on_terminal(): assert isinstance(make_progress(interactive=True), BarProgress) assert isinstance(make_progress(interactive=False), LogProgress)