~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_osutils.py

  • Committer: Martin Pool
  • Date: 2006-06-20 05:32:16 UTC
  • mfrom: (1797 +trunk)
  • mto: This revision was merged to the branch mainline in revision 1798.
  • Revision ID: mbp@sourcefrog.net-20060620053216-817857d7ca3e9d1f
[merge] bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
import bzrlib
26
26
from bzrlib.errors import BzrBadParameterNotUnicode, InvalidURL
27
27
import bzrlib.osutils as osutils
28
 
from bzrlib.tests import TestCaseInTempDir, TestCase, TestSkipped
 
28
from bzrlib.tests import (
 
29
        StringIOWrapper,
 
30
        TestCase, 
 
31
        TestCaseInTempDir, 
 
32
        TestSkipped,
 
33
        )
29
34
 
30
35
 
31
36
class TestOSUtils(TestCaseInTempDir):
266
271
        self.assertEqual(expected_dirblocks[1:],
267
272
            [[line[0:3] for line in block] for block in result])
268
273
 
 
274
    def assertPathCompare(self, path_less, path_greater):
 
275
        """check that path_less and path_greater compare correctly."""
 
276
        self.assertEqual(0, osutils.compare_paths_prefix_order(
 
277
            path_less, path_less))
 
278
        self.assertEqual(0, osutils.compare_paths_prefix_order(
 
279
            path_greater, path_greater))
 
280
        self.assertEqual(-1, osutils.compare_paths_prefix_order(
 
281
            path_less, path_greater))
 
282
        self.assertEqual(1, osutils.compare_paths_prefix_order(
 
283
            path_greater, path_less))
 
284
 
 
285
    def test_compare_paths_prefix_order(self):
 
286
        # root before all else
 
287
        self.assertPathCompare("/", "/a")
 
288
        # alpha within a dir
 
289
        self.assertPathCompare("/a", "/b")
 
290
        self.assertPathCompare("/b", "/z")
 
291
        # high dirs before lower.
 
292
        self.assertPathCompare("/z", "/a/a")
 
293
        # except if the deeper dir should be output first
 
294
        self.assertPathCompare("/a/b/c", "/d/g")
 
295
        # lexical betwen dirs of the same height
 
296
        self.assertPathCompare("/a/z", "/z/z")
 
297
        self.assertPathCompare("/a/c/z", "/a/d/e")
 
298
 
 
299
        # this should also be consistent for no leading / paths
 
300
        # root before all else
 
301
        self.assertPathCompare("", "a")
 
302
        # alpha within a dir
 
303
        self.assertPathCompare("a", "b")
 
304
        self.assertPathCompare("b", "z")
 
305
        # high dirs before lower.
 
306
        self.assertPathCompare("z", "a/a")
 
307
        # except if the deeper dir should be output first
 
308
        self.assertPathCompare("a/b/c", "d/g")
 
309
        # lexical betwen dirs of the same height
 
310
        self.assertPathCompare("a/z", "z/z")
 
311
        self.assertPathCompare("a/c/z", "a/d/e")
 
312
 
 
313
    def test_path_prefix_sorting(self):
 
314
        """Doing a sort on path prefix should match our sample data."""
 
315
        original_paths = [
 
316
            'a',
 
317
            'a/b',
 
318
            'a/b/c',
 
319
            'b',
 
320
            'b/c',
 
321
            'd',
 
322
            'd/e',
 
323
            'd/e/f',
 
324
            'd/f',
 
325
            'd/g',
 
326
            'g',
 
327
            ]
 
328
 
 
329
        dir_sorted_paths = [
 
330
            'a',
 
331
            'b',
 
332
            'd',
 
333
            'g',
 
334
            'a/b',
 
335
            'a/b/c',
 
336
            'b/c',
 
337
            'd/e',
 
338
            'd/f',
 
339
            'd/g',
 
340
            'd/e/f',
 
341
            ]
 
342
 
 
343
        self.assertEqual(
 
344
            dir_sorted_paths,
 
345
            sorted(original_paths, key=osutils.path_prefix_key))
 
346
        # using the comparison routine shoudl work too:
 
347
        self.assertEqual(
 
348
            dir_sorted_paths,
 
349
            sorted(original_paths, cmp=osutils.compare_paths_prefix_order))
 
350
 
 
351
 
 
352
class TestTerminalEncoding(TestCase):
 
353
    """Test the auto-detection of proper terminal encoding."""
 
354
 
 
355
    def setUp(self):
 
356
        self._stdout = sys.stdout
 
357
        self._stderr = sys.stderr
 
358
        self._stdin = sys.stdin
 
359
        self._user_encoding = bzrlib.user_encoding
 
360
 
 
361
        self.addCleanup(self._reset)
 
362
 
 
363
        sys.stdout = StringIOWrapper()
 
364
        sys.stdout.encoding = 'stdout_encoding'
 
365
        sys.stderr = StringIOWrapper()
 
366
        sys.stderr.encoding = 'stderr_encoding'
 
367
        sys.stdin = StringIOWrapper()
 
368
        sys.stdin.encoding = 'stdin_encoding'
 
369
        bzrlib.user_encoding = 'user_encoding'
 
370
 
 
371
    def _reset(self):
 
372
        sys.stdout = self._stdout
 
373
        sys.stderr = self._stderr
 
374
        sys.stdin = self._stdin
 
375
        bzrlib.user_encoding = self._user_encoding
 
376
 
 
377
    def test_get_terminal_encoding(self):
 
378
        # first preference is stdout encoding
 
379
        self.assertEqual('stdout_encoding', osutils.get_terminal_encoding())
 
380
 
 
381
        sys.stdout.encoding = None
 
382
        # if sys.stdout is None, fall back to sys.stdin
 
383
        self.assertEqual('stdin_encoding', osutils.get_terminal_encoding())
 
384
 
 
385
        sys.stdin.encoding = None
 
386
        # and in the worst case, use bzrlib.user_encoding
 
387
        self.assertEqual('user_encoding', osutils.get_terminal_encoding())
 
388