1- import six
2- import time
31import pprint
4- import progressbar
2+
53pytest_plugins = 'pytester'
64
75
@@ -11,29 +9,31 @@ def test_list_example(testdir):
119 best capture its stderr. We expect to see match_lines in order in the
1210 output. This test is just a sanity check to ensure that the progress
1311 bar progresses from 1 to 10, it does not make sure that the '''
12+
1413 v = testdir .makepyfile ('''
15- import time
16- import progressbar
14+ import time
15+ import freezegun
16+ import progressbar
1717
18- bar = progressbar.ProgressBar(term_width=60)
18+ with freezegun.freeze_time() as fake_time:
19+ bar = progressbar.ProgressBar(term_width=65)
20+ bar._MINIMUM_UPDATE_INTERVAL = 1e-9
1921 for i in bar(list(range(9))):
20- time.sleep(0.1)
21-
22+ fake_time.tick(1)
2223 ''' )
2324 result = testdir .runpython (v )
2425 result .stderr .lines = [l for l in result .stderr .lines if l .strip ()]
2526 pprint .pprint (result .stderr .lines )
2627 result .stderr .re_match_lines ([
27- r'N/A% \(0 of 9\) \|\s+\| Elapsed Time: 0:00:00 ETA: --:--:--' ,
28- r' 11% \(1 of 9\) \|\s+\| Elapsed Time: 0:00:00 ETA: 0:00:0[01]' ,
29- r' 22% \(2 of 9\) \|#+\s+\| Elapsed Time: 0:00:00 ETA: 0:00:00' ,
30- r' 33% \(3 of 9\) \|#+\s+\| Elapsed Time: 0:00:00 ETA: 0:00:00' ,
31- r' 44% \(4 of 9\) \|#+\s+\| Elapsed Time: 0:00:00 ETA: 0:00:00' ,
32- r' 55% \(5 of 9\) \|#+\s+\| Elapsed Time: 0:00:00 ETA: 0:00:00' ,
33- r' 66% \(6 of 9\) \|#+\s+\| Elapsed Time: 0:00:00 ETA: 0:00:00' ,
34- r' 77% \(7 of 9\) \|#+\s+\| Elapsed Time: 0:00:00 ETA: 0:00:00' ,
35- r' 88% \(8 of 9\) \|#+\s+\| Elapsed Time: 0:00:00 ETA: 0:00:00' ,
36- r'100% \(9 of 9\) \|#+\| Elapsed Time: 0:00:0[01] Time: 0:00:0[01]' ,
28+ 'N/A% (0 of 9) | | Elapsed Time: 2:00:00 ETA: --:--:--' ,
29+ ' 11% (1 of 9) |# | Elapsed Time: 2:00:01 ETA: 0:00:08' ,
30+ ' 22% (2 of 9) |## | Elapsed Time: 2:00:02 ETA: 0:00:07' ,
31+ ' 33% (3 of 9) |#### | Elapsed Time: 2:00:03 ETA: 0:00:06' ,
32+ ' 44% (4 of 9) |##### | Elapsed Time: 2:00:04 ETA: 0:00:05' ,
33+ ' 55% (5 of 9) |###### | Elapsed Time: 2:00:05 ETA: 0:00:04' ,
34+ ' 66% (6 of 9) |######## | Elapsed Time: 2:00:06 ETA: 0:00:03' ,
35+ ' 77% (7 of 9) |######### | Elapsed Time: 2:00:07 ETA: 0:00:02' ,
36+ ' 88% (8 of 9) |########## | Elapsed Time: 2:00:08 ETA: 0:00:01' ,
3737 ])
3838
3939
@@ -43,91 +43,86 @@ def test_generator_example(testdir):
4343 best capture its stderr. We expect to see match_lines in order in the
4444 output. This test is just a sanity check to ensure that the progress
4545 bar progresses from 1 to 10, it does not make sure that the '''
46+
4647 v = testdir .makepyfile ('''
47- import time
48- import progressbar
48+ import time
49+ import freezegun
50+ import progressbar
4951
52+ with freezegun.freeze_time() as fake_time:
5053 bar = progressbar.ProgressBar(term_width=60)
54+ bar._MINIMUM_UPDATE_INTERVAL = 1e-9
5155 for i in bar(iter(range(9))):
52- time.sleep(0.1)
53-
56+ fake_time.tick(1)
5457 ''' )
5558 result = testdir .runpython (v )
5659 result .stderr .lines = [l for l in result .stderr .lines if l .strip ()]
5760 pprint .pprint (result .stderr .lines )
58- result .stderr .re_match_lines ([
59- '/ |# | 0 Elapsed Time: 0:00:00' ,
60- '- | # | 1 Elapsed Time: 0:00:00' ,
61- '\\ | # | 2 Elapsed Time: 0:00:00' ,
62- '| | # | 3 Elapsed Time: 0:00:00' ,
63- '/ | # | 4 Elapsed Time: 0:00:00' ,
64- '- | # | 5 Elapsed Time: 0:00:00' ,
65- '\\ | # | 6 Elapsed Time: 0:00:00' ,
66- '| | # | 7 Elapsed Time: 0:00:00' ,
67- '/ | # | 8 Elapsed Time: 0:00:00' ,
68- '| | # | 8 Elapsed Time: 0:00:00' ,
69- ])
61+
62+ lines = []
63+ for i in range (9 ):
64+ lines .append (r'[/\\|-] \|\s*#\s*\| %(i)d Elapsed Time: 2:00:%(i)02d' %
65+ dict (i = i ))
66+
67+ result .stderr .re_match_lines (lines )
7068
7169
7270def test_rapid_updates (testdir ):
7371 ''' Run some example code that updates 10 times, then sleeps .1 seconds,
7472 this is meant to test that the progressbar progresses normally with
7573 this sample code, since there were issues with it in the past '''
74+
7675 v = testdir .makepyfile ('''
77- import time
78- import progressbar
76+ import time
77+ import freezegun
78+ import progressbar
7979
80+ with freezegun.freeze_time() as fake_time:
8081 bar = progressbar.ProgressBar(term_width=60)
81- for i in bar(range(100)):
82- if i % 10 == 0 :
83- time.sleep(0.1)
84-
82+ bar._MINIMUM_UPDATE_INTERVAL = 1e-9
83+ for i in bar(range(10)) :
84+ if i % 2 == 0:
85+ fake_time.tick(1)
8586 ''' )
8687 result = testdir .runpython (v )
88+ result .stderr .lines = [l for l in result .stderr .lines if l .strip ()]
8789 pprint .pprint (result .stderr .lines )
8890 result .stderr .re_match_lines ([
89- r' 1% \(1 of 100\) ' ,
90- r' 11% \(11 of 100\) ' ,
91- r' 21% \(21 of 100\) ' ,
92- r' 31% \(31 of 100\) ' ,
93- r' 41% \(41 of 100\) ' ,
94- r' 51% \(51 of 100\) ' ,
95- r' 61% \(61 of 100\) ' ,
96- r' 71% \(71 of 100\) ' ,
97- r' 81% \(81 of 100\) ' ,
98- r' 91% \(91 of 100\) ' ,
99- r '100% \(100 of 100\)'
91+ 'N/A% (0 of 10) | | Elapsed Time: 2:00:00 ETA: --:--:-- ' ,
92+ ' 10% (1 of 10) | | Elapsed Time: 2:00:01 ETA: 0:00:09 ' ,
93+ ' 20% (2 of 10) |# | Elapsed Time: 2:00:01 ETA: 0:00:08 ' ,
94+ ' 30% (3 of 10) |# | Elapsed Time: 2:00:02 ETA: 0:00:04 ' ,
95+ ' 40% (4 of 10) |## | Elapsed Time: 2:00:02 ETA: 0:00:04 ' ,
96+ ' 50% (5 of 10) |### | Elapsed Time: 2:00:03 ETA: 0:00:03 ' ,
97+ ' 60% (6 of 10) |### | Elapsed Time: 2:00:03 ETA: 0:00:02 ' ,
98+ ' 70% (7 of 10) |#### | Elapsed Time: 2:00:04 ETA: 0:00:01 ' ,
99+ ' 80% (8 of 10) |#### | Elapsed Time: 2:00:04 ETA: 0:00:01 ' ,
100+ ' 90% (9 of 10) |##### | Elapsed Time: 2:00:05 ETA: 0:00:00 ' ,
101+ '100% (10 of 10) |#####| Elapsed Time: 2:00:05 Time: 2:00:05' ,
100102 ])
101103
102104
103105def test_context_wrapper (testdir ):
104- fd = six .StringIO ()
105-
106- with progressbar .ProgressBar (term_width = 60 , fd = fd ) as bar :
107- bar ._MINIMUM_UPDATE_INTERVAL = 0.0001
108- for _ in bar (list (range (5 ))):
109- time .sleep (0.001 )
110-
111- expected = (
112- '' ,
113- ' ' ,
114- '' ,
115- 'N/A% (0 of 5) | | Elapsed Time: 0:00:00 ETA: --:--:--' ,
116- ' ' ,
117- '' ,
118- ' 20% (1 of 5) |# | Elapsed Time: 0:00:00 ETA: 0:00:00' ,
119- ' ' ,
120- '' ,
121- ' 40% (2 of 5) |### | Elapsed Time: 0:00:00 ETA: 0:00:00' ,
122- ' ' ,
123- '' ,
124- ' 60% (3 of 5) |#### | Elapsed Time: 0:00:00 ETA: 0:00:00' ,
125- ' ' ,
126- '' ,
127- ' 80% (4 of 5) |###### | Elapsed Time: 0:00:00 ETA: 0:00:00' ,
128- ' ' ,
129- '' ,
130- '100% (5 of 5) |########| Elapsed Time: 0:00:00 Time: 0:00:00' ,
131- )
132- for line , expected_line in zip (fd .getvalue ().split ('\r ' ), expected ):
133- assert line == expected_line
106+ v = testdir .makepyfile ('''
107+ import time
108+ import freezegun
109+ import progressbar
110+
111+ with freezegun.freeze_time() as fake_time:
112+ with progressbar.ProgressBar(term_width=60) as bar:
113+ bar._MINIMUM_UPDATE_INTERVAL = 1e-9
114+ for _ in bar(list(range(5))):
115+ fake_time.tick(1)
116+ ''' )
117+
118+ result = testdir .runpython (v )
119+ result .stderr .lines = [l for l in result .stderr .lines if l .strip ()]
120+ pprint .pprint (result .stderr .lines )
121+ result .stderr .re_match_lines ([
122+ 'N/A% (0 of 5) | | Elapsed Time: 2:00:00 ETA: --:--:--' ,
123+ ' 20% (1 of 5) |# | Elapsed Time: 2:00:01 ETA: 0:00:04' ,
124+ ' 40% (2 of 5) |## | Elapsed Time: 2:00:02 ETA: 0:00:03' ,
125+ ' 60% (3 of 5) |#### | Elapsed Time: 2:00:03 ETA: 0:00:02' ,
126+ ' 80% (4 of 5) |##### | Elapsed Time: 2:00:04 ETA: 0:00:01' ,
127+ '100% (5 of 5) |#######| Elapsed Time: 2:00:05 Time: 2:00:05' ,
128+ ])
0 commit comments