~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_progress.py

  • Committer: John Arbash Meinel
  • Date: 2006-07-10 21:24:55 UTC
  • mto: This revision was merged to the branch mainline in revision 1854.
  • Revision ID: john@arbash-meinel.com-20060710212455-13af54c421c4d806
Add tests to assert we fall back to DotsProgressBar when appropriate.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
 
17
import os
17
18
from StringIO import StringIO
18
19
 
19
20
from bzrlib.progress import (
173
174
            pb.update('x', count+100, 200)
174
175
 
175
176
        self.assertEqual(pb._max_last_updates, len(pb.last_updates))
 
177
 
 
178
    def test_tty_progress(self):
 
179
        # Make sure the ProgressBarStack thinks it is
 
180
        # writing out to a terminal, and thus uses a TTYProgressBar
 
181
        class TTYStringIO(StringIO):
 
182
            def isatty(self):
 
183
                return True
 
184
        orig_term = os.environ.get('TERM')
 
185
        try:
 
186
            os.environ['TERM'] = 'xterm'
 
187
            out = TTYStringIO()
 
188
            stack = ProgressBarStack(to_file=out)
 
189
            pb = stack.get_nested()
 
190
            self.assertIsInstance(pb, TTYProgressBar)
 
191
            try:
 
192
                pb.start_time -= 1
 
193
                pb.width = 20
 
194
                pb.update('foo', 1, 2)
 
195
                pb.update('bar', 2, 2)
 
196
            finally:
 
197
                pb.finished()
 
198
        finally:
 
199
            if orig_term is None:
 
200
                del os.environ['TERM']
 
201
            else:
 
202
                os.environ['TERM'] = orig_term
 
203
 
 
204
        self.assertEqual('\r/ [====   ] foo 1/2'
 
205
                         '\r- [=======] bar 2/2'
 
206
                         '\r                   \r',
 
207
                         out.getvalue())
 
208
 
 
209
    def test_dots_progress(self):
 
210
        # Make sure the ProgressBarStack thinks it is
 
211
        # not writing out to a terminal, and thus uses a 
 
212
        # DotsProgressBar
 
213
        class NonTTYStringIO(StringIO):
 
214
            def isatty(self):
 
215
                return False
 
216
        orig_term = os.environ.get('TERM')
 
217
        try:
 
218
            os.environ['TERM'] = 'xterm'
 
219
            out = NonTTYStringIO()
 
220
            stack = ProgressBarStack(to_file=out)
 
221
            pb = stack.get_nested()
 
222
            try:
 
223
                pb.start_time -= 1
 
224
                pb.width = 20
 
225
                pb.update('foo', 1, 2)
 
226
                pb.update('bar', 2, 2)
 
227
            finally:
 
228
                pb.finished()
 
229
        finally:
 
230
            if orig_term is None:
 
231
                del os.environ['TERM']
 
232
            else:
 
233
                os.environ['TERM'] = orig_term
 
234
 
 
235
        self.assertEqual('foo: .'
 
236
                         '\nbar: .'
 
237
                         '\n',
 
238
                         out.getvalue())
 
239
 
 
240
    def test_dumb_progress(self):
 
241
        # Make sure the ProgressBarStack thinks it is writing out to a 
 
242
        # terminal, but it is the emacs 'dumb' terminal, so it uses
 
243
        # Dots
 
244
        class TTYStringIO(StringIO):
 
245
            def isatty(self):
 
246
                return True
 
247
        orig_term = os.environ.get('TERM')
 
248
        try:
 
249
            os.environ['TERM'] = 'dumb'
 
250
            out = TTYStringIO()
 
251
            stack = ProgressBarStack(to_file=out)
 
252
            pb = stack.get_nested()
 
253
            self.assertIsInstance(pb, DotsProgressBar)
 
254
            try:
 
255
                pb.start_time -= 1
 
256
                pb.width = 20
 
257
                pb.update('foo', 1, 2)
 
258
                pb.update('bar', 2, 2)
 
259
            finally:
 
260
                pb.finished()
 
261
        finally:
 
262
            if orig_term is None:
 
263
                del os.environ['TERM']
 
264
            else:
 
265
                os.environ['TERM'] = orig_term
 
266
 
 
267
        self.assertEqual('foo: .'
 
268
                         '\nbar: .'
 
269
                         '\n',
 
270
                         out.getvalue())
 
271