/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/tests/test__dirstate_helpers.py

  • Committer: Martin
  • Date: 2017-06-11 01:12:29 UTC
  • mto: This revision was merged to the branch mainline in revision 6685.
  • Revision ID: gzlist@googlemail.com-20170611011229-somdjbalby8m7vlw
Make _chunks_to_lines pass for Python 3

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007-2010 Canonical Ltd
 
1
# Copyright (C) 2007-2011 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
20
20
import os
21
21
import time
22
22
 
23
 
from bzrlib import (
 
23
from .. import (
24
24
    dirstate,
25
25
    errors,
26
26
    osutils,
27
27
    tests,
 
28
    _dirstate_helpers_py,
28
29
    )
29
 
from bzrlib.tests import (
 
30
from . import (
30
31
    test_dirstate,
31
 
    test_osutils,
32
 
    )
33
 
 
34
 
try:
35
 
    from bzrlib import _dirstate_helpers_pyx
36
 
    has_dirstate_helpers_pyx = True
37
 
except ImportError:
38
 
    has_dirstate_helpers_pyx = False
39
 
 
40
 
 
41
 
compiled_dirstate_helpers_feature = tests.ModuleAvailableFeature(
42
 
                                'bzrlib._dirstate_helpers_pyx')
43
 
 
44
 
 
45
 
def load_tests(basic_tests, module, loader):
46
 
    # FIXME: we should also parametrize against SHA1Provider !
47
 
    suite = loader.suiteClass()
48
 
    remaining_tests = basic_tests
49
 
 
50
 
    dir_reader_scenarios = test_osutils.dir_reader_scenarios()
51
 
 
52
 
    ue_scenarios = [('dirstate_Python',
53
 
                     {'update_entry': dirstate.py_update_entry})]
54
 
    if compiled_dirstate_helpers_feature.available():
55
 
        update_entry = compiled_dirstate_helpers_feature.module.update_entry
56
 
        pyrex_scenario = ('dirstate_Pyrex', {'update_entry': update_entry})
57
 
        ue_scenarios.append(pyrex_scenario)
58
 
    process_entry_tests, remaining_tests = tests.split_suite_by_condition(
59
 
        remaining_tests, tests.condition_isinstance(TestUpdateEntry))
60
 
    tests.multiply_tests(process_entry_tests,
61
 
                         tests.multiply_scenarios(dir_reader_scenarios,
62
 
                                                  ue_scenarios),
63
 
                         suite)
64
 
 
65
 
    pe_scenarios = [('dirstate_Python',
66
 
                     {'_process_entry': dirstate.ProcessEntryPython})]
67
 
    if compiled_dirstate_helpers_feature.available():
68
 
        process_entry = compiled_dirstate_helpers_feature.module.ProcessEntryC
69
 
        pyrex_scenario = ('dirstate_Pyrex', {'_process_entry': process_entry})
70
 
        pe_scenarios.append(pyrex_scenario)
71
 
    process_entry_tests, remaining_tests = tests.split_suite_by_condition(
72
 
        remaining_tests, tests.condition_isinstance(TestProcessEntry))
73
 
    tests.multiply_tests(process_entry_tests,
74
 
                         tests.multiply_scenarios(dir_reader_scenarios,
75
 
                                                  pe_scenarios),
76
 
                         suite)
77
 
 
78
 
    dir_reader_tests, remaining_tests = tests.split_suite_by_condition(
79
 
        remaining_tests, tests.condition_isinstance(
80
 
            test_dirstate.TestCaseWithDirState))
81
 
    tests.multiply_tests(dir_reader_tests, dir_reader_scenarios, suite)
82
 
    suite.addTest(remaining_tests)
83
 
 
84
 
    return suite
 
32
    )
 
33
from .test_osutils import dir_reader_scenarios
 
34
from .scenarios import (
 
35
    load_tests_apply_scenarios,
 
36
    multiply_scenarios,
 
37
    )
 
38
from . import (
 
39
    features,
 
40
    )
 
41
 
 
42
 
 
43
load_tests = load_tests_apply_scenarios
 
44
 
 
45
 
 
46
compiled_dirstate_helpers_feature = features.ModuleAvailableFeature(
 
47
    'breezy._dirstate_helpers_pyx')
 
48
 
 
49
 
 
50
# FIXME: we should also parametrize against SHA1Provider !
 
51
 
 
52
ue_scenarios = [('dirstate_Python',
 
53
    {'update_entry': dirstate.py_update_entry})]
 
54
if compiled_dirstate_helpers_feature.available():
 
55
    update_entry = compiled_dirstate_helpers_feature.module.update_entry
 
56
    ue_scenarios.append(('dirstate_Pyrex', {'update_entry': update_entry}))
 
57
 
 
58
pe_scenarios = [('dirstate_Python',
 
59
    {'_process_entry': dirstate.ProcessEntryPython})]
 
60
if compiled_dirstate_helpers_feature.available():
 
61
    process_entry = compiled_dirstate_helpers_feature.module.ProcessEntryC
 
62
    pe_scenarios.append(('dirstate_Pyrex', {'_process_entry': process_entry}))
 
63
 
 
64
helper_scenarios = [('dirstate_Python', {'helpers': _dirstate_helpers_py})]
 
65
if compiled_dirstate_helpers_feature.available():
 
66
    helper_scenarios.append(('dirstate_Pyrex',
 
67
        {'helpers': compiled_dirstate_helpers_feature.module}))
85
68
 
86
69
 
87
70
class TestBisectPathMixin(object):
243
226
    """Run all Bisect Path tests against _bisect_path_left."""
244
227
 
245
228
    def get_bisect_path(self):
246
 
        from bzrlib._dirstate_helpers_py import _bisect_path_left
 
229
        from breezy._dirstate_helpers_py import _bisect_path_left
247
230
        return _bisect_path_left
248
231
 
249
232
    def get_bisect(self):
256
239
    _test_needs_features = [compiled_dirstate_helpers_feature]
257
240
 
258
241
    def get_bisect_path(self):
259
 
        from bzrlib._dirstate_helpers_pyx import _bisect_path_left
 
242
        from breezy._dirstate_helpers_pyx import _bisect_path_left
260
243
        return _bisect_path_left
261
244
 
262
245
 
264
247
    """Run all Bisect Path tests against _bisect_path_right"""
265
248
 
266
249
    def get_bisect_path(self):
267
 
        from bzrlib._dirstate_helpers_py import _bisect_path_right
 
250
        from breezy._dirstate_helpers_py import _bisect_path_right
268
251
        return _bisect_path_right
269
252
 
270
253
    def get_bisect(self):
277
260
    _test_needs_features = [compiled_dirstate_helpers_feature]
278
261
 
279
262
    def get_bisect_path(self):
280
 
        from bzrlib._dirstate_helpers_pyx import _bisect_path_right
 
263
        from breezy._dirstate_helpers_pyx import _bisect_path_right
281
264
        return _bisect_path_right
282
265
 
283
266
 
295
278
 
296
279
    def get_bisect_dirblock(self):
297
280
        """Return an implementation of bisect_dirblock"""
298
 
        from bzrlib._dirstate_helpers_py import bisect_dirblock
 
281
        from breezy._dirstate_helpers_py import bisect_dirblock
299
282
        return bisect_dirblock
300
283
 
301
284
    def assertBisect(self, dirblocks, split_dirblocks, path, *args, **kwargs):
389
372
    _test_needs_features = [compiled_dirstate_helpers_feature]
390
373
 
391
374
    def get_bisect_dirblock(self):
392
 
        from bzrlib._dirstate_helpers_pyx import bisect_dirblock
 
375
        from breezy._dirstate_helpers_pyx import bisect_dirblock
393
376
        return bisect_dirblock
394
377
 
395
378
 
396
 
class TestCmpByDirs(tests.TestCase):
397
 
    """Test an implementation of cmp_by_dirs()
 
379
class TestLtByDirs(tests.TestCase):
 
380
    """Test an implementation of lt_by_dirs()
398
381
 
399
 
    cmp_by_dirs() compares 2 paths by their directory sections, rather than as
 
382
    lt_by_dirs() compares 2 paths by their directory sections, rather than as
400
383
    plain strings.
401
384
 
402
 
    Child test cases can override ``get_cmp_by_dirs`` to test a specific
 
385
    Child test cases can override ``get_lt_by_dirs`` to test a specific
403
386
    implementation.
404
387
    """
405
388
 
406
 
    def get_cmp_by_dirs(self):
407
 
        """Get a specific implementation of cmp_by_dirs."""
408
 
        from bzrlib._dirstate_helpers_py import cmp_by_dirs
409
 
        return cmp_by_dirs
 
389
    def get_lt_by_dirs(self):
 
390
        """Get a specific implementation of lt_by_dirs."""
 
391
        from breezy._dirstate_helpers_py import lt_by_dirs
 
392
        return lt_by_dirs
410
393
 
411
394
    def assertCmpByDirs(self, expected, str1, str2):
412
395
        """Compare the two strings, in both directions.
416
399
        :param str1: string to compare
417
400
        :param str2: string to compare
418
401
        """
419
 
        cmp_by_dirs = self.get_cmp_by_dirs()
 
402
        lt_by_dirs = self.get_lt_by_dirs()
420
403
        if expected == 0:
421
404
            self.assertEqual(str1, str2)
422
 
            self.assertEqual(0, cmp_by_dirs(str1, str2))
423
 
            self.assertEqual(0, cmp_by_dirs(str2, str1))
 
405
            self.assertFalse(lt_by_dirs(str1, str2))
 
406
            self.assertFalse(lt_by_dirs(str2, str1))
424
407
        elif expected > 0:
425
 
            self.assertPositive(cmp_by_dirs(str1, str2))
426
 
            self.assertNegative(cmp_by_dirs(str2, str1))
 
408
            self.assertFalse(lt_by_dirs(str1, str2))
 
409
            self.assertTrue(lt_by_dirs(str2, str1))
427
410
        else:
428
 
            self.assertNegative(cmp_by_dirs(str1, str2))
429
 
            self.assertPositive(cmp_by_dirs(str2, str1))
 
411
            self.assertTrue(lt_by_dirs(str1, str2))
 
412
            self.assertFalse(lt_by_dirs(str2, str1))
430
413
 
431
414
    def test_cmp_empty(self):
432
415
        """Compare against the empty string."""
492
475
        self.assertCmpByDirs(-1, 'ab/cd', 'ab-cd')
493
476
 
494
477
    def test_cmp_unicode_not_allowed(self):
495
 
        cmp_by_dirs = self.get_cmp_by_dirs()
496
 
        self.assertRaises(TypeError, cmp_by_dirs, u'Unicode', 'str')
497
 
        self.assertRaises(TypeError, cmp_by_dirs, 'str', u'Unicode')
498
 
        self.assertRaises(TypeError, cmp_by_dirs, u'Unicode', u'Unicode')
 
478
        lt_by_dirs = self.get_lt_by_dirs()
 
479
        self.assertRaises(TypeError, lt_by_dirs, u'Unicode', 'str')
 
480
        self.assertRaises(TypeError, lt_by_dirs, 'str', u'Unicode')
 
481
        self.assertRaises(TypeError, lt_by_dirs, u'Unicode', u'Unicode')
499
482
 
500
483
    def test_cmp_non_ascii(self):
501
484
        self.assertCmpByDirs(-1, '\xc2\xb5', '\xc3\xa5') # u'\xb5', u'\xe5'
505
488
        self.assertCmpByDirs(-1, 'b/a', 'b/\xc2\xb5') # u'b/a', u'b/\xb5'
506
489
 
507
490
 
508
 
class TestCompiledCmpByDirs(TestCmpByDirs):
509
 
    """Test the pyrex implementation of cmp_by_dirs"""
 
491
class TestCompiledLtByDirs(TestLtByDirs):
 
492
    """Test the pyrex implementation of lt_by_dirs"""
510
493
 
511
494
    _test_needs_features = [compiled_dirstate_helpers_feature]
512
495
 
513
 
    def get_cmp_by_dirs(self):
514
 
        from bzrlib._dirstate_helpers_pyx import cmp_by_dirs
515
 
        return cmp_by_dirs
516
 
 
517
 
 
518
 
class TestCmpPathByDirblock(tests.TestCase):
519
 
    """Test an implementation of _cmp_path_by_dirblock()
520
 
 
521
 
    _cmp_path_by_dirblock() compares two paths using the sort order used by
 
496
    def get_lt_by_dirs(self):
 
497
        from breezy._dirstate_helpers_pyx import lt_by_dirs
 
498
        return lt_by_dirs
 
499
 
 
500
 
 
501
class TestLtPathByDirblock(tests.TestCase):
 
502
    """Test an implementation of _lt_path_by_dirblock()
 
503
 
 
504
    _lt_path_by_dirblock() compares two paths using the sort order used by
522
505
    DirState. All paths in the same directory are sorted together.
523
506
 
524
 
    Child test cases can override ``get_cmp_path_by_dirblock`` to test a specific
 
507
    Child test cases can override ``get_lt_path_by_dirblock`` to test a specific
525
508
    implementation.
526
509
    """
527
510
 
528
 
    def get_cmp_path_by_dirblock(self):
529
 
        """Get a specific implementation of _cmp_path_by_dirblock."""
530
 
        from bzrlib._dirstate_helpers_py import _cmp_path_by_dirblock
531
 
        return _cmp_path_by_dirblock
 
511
    def get_lt_path_by_dirblock(self):
 
512
        """Get a specific implementation of _lt_path_by_dirblock."""
 
513
        from breezy._dirstate_helpers_py import _lt_path_by_dirblock
 
514
        return _lt_path_by_dirblock
532
515
 
533
 
    def assertCmpPathByDirblock(self, paths):
 
516
    def assertLtPathByDirblock(self, paths):
534
517
        """Compare all paths and make sure they evaluate to the correct order.
535
518
 
536
519
        This does N^2 comparisons. It is assumed that ``paths`` is properly
544
527
            return dirname.split('/'), basename
545
528
        self.assertEqual(sorted(paths, key=_key), paths)
546
529
 
547
 
        cmp_path_by_dirblock = self.get_cmp_path_by_dirblock()
 
530
        lt_path_by_dirblock = self.get_lt_path_by_dirblock()
548
531
        for idx1, path1 in enumerate(paths):
549
532
            for idx2, path2 in enumerate(paths):
550
 
                cmp_val = cmp_path_by_dirblock(path1, path2)
551
 
                if idx1 < idx2:
552
 
                    self.assertTrue(cmp_val < 0,
553
 
                        '%s did not state that %r came before %r, cmp=%s'
554
 
                        % (cmp_path_by_dirblock.__name__,
555
 
                           path1, path2, cmp_val))
556
 
                elif idx1 > idx2:
557
 
                    self.assertTrue(cmp_val > 0,
558
 
                        '%s did not state that %r came after %r, cmp=%s'
559
 
                        % (cmp_path_by_dirblock.__name__,
560
 
                           path1, path2, cmp_val))
561
 
                else: # idx1 == idx2
562
 
                    self.assertTrue(cmp_val == 0,
563
 
                        '%s did not state that %r == %r, cmp=%s'
564
 
                        % (cmp_path_by_dirblock.__name__,
565
 
                           path1, path2, cmp_val))
 
533
                lt_result = lt_path_by_dirblock(path1, path2)
 
534
                self.assertEqual(idx1 < idx2, lt_result,
 
535
                        '%s did not state that %r < %r, lt=%s'
 
536
                        % (lt_path_by_dirblock.__name__,
 
537
                           path1, path2, lt_result))
566
538
 
567
539
    def test_cmp_simple_paths(self):
568
540
        """Compare against the empty string."""
569
 
        self.assertCmpPathByDirblock(['', 'a', 'ab', 'abc', 'a/b/c', 'b/d/e'])
570
 
        self.assertCmpPathByDirblock(['kl', 'ab/cd', 'ab/ef', 'gh/ij'])
 
541
        self.assertLtPathByDirblock(['', 'a', 'ab', 'abc', 'a/b/c', 'b/d/e'])
 
542
        self.assertLtPathByDirblock(['kl', 'ab/cd', 'ab/ef', 'gh/ij'])
571
543
 
572
544
    def test_tricky_paths(self):
573
 
        self.assertCmpPathByDirblock([
 
545
        self.assertLtPathByDirblock([
574
546
            # Contents of ''
575
547
            '', 'a', 'a-a', 'a=a', 'b',
576
548
            # Contents of 'a'
598
570
            # Contents of 'b',
599
571
            'b/a', 'b/b',
600
572
            ])
601
 
        self.assertCmpPathByDirblock([
 
573
        self.assertLtPathByDirblock([
602
574
                 # content of '/'
603
575
                 '', 'a', 'a-a', 'a-z', 'a=a', 'a=z',
604
576
                 # content of 'a/'
629
601
                ])
630
602
 
631
603
    def test_unicode_not_allowed(self):
632
 
        cmp_path_by_dirblock = self.get_cmp_path_by_dirblock()
633
 
        self.assertRaises(TypeError, cmp_path_by_dirblock, u'Uni', 'str')
634
 
        self.assertRaises(TypeError, cmp_path_by_dirblock, 'str', u'Uni')
635
 
        self.assertRaises(TypeError, cmp_path_by_dirblock, u'Uni', u'Uni')
636
 
        self.assertRaises(TypeError, cmp_path_by_dirblock, u'x/Uni', 'x/str')
637
 
        self.assertRaises(TypeError, cmp_path_by_dirblock, 'x/str', u'x/Uni')
638
 
        self.assertRaises(TypeError, cmp_path_by_dirblock, u'x/Uni', u'x/Uni')
 
604
        lt_path_by_dirblock = self.get_lt_path_by_dirblock()
 
605
        self.assertRaises(TypeError, lt_path_by_dirblock, u'Uni', 'str')
 
606
        self.assertRaises(TypeError, lt_path_by_dirblock, 'str', u'Uni')
 
607
        self.assertRaises(TypeError, lt_path_by_dirblock, u'Uni', u'Uni')
 
608
        self.assertRaises(TypeError, lt_path_by_dirblock, u'x/Uni', 'x/str')
 
609
        self.assertRaises(TypeError, lt_path_by_dirblock, 'x/str', u'x/Uni')
 
610
        self.assertRaises(TypeError, lt_path_by_dirblock, u'x/Uni', u'x/Uni')
639
611
 
640
612
    def test_nonascii(self):
641
 
        self.assertCmpPathByDirblock([
 
613
        self.assertLtPathByDirblock([
642
614
            # content of '/'
643
615
            '', 'a', '\xc2\xb5', '\xc3\xa5',
644
616
            # content of 'a'
656
628
            ])
657
629
 
658
630
 
659
 
class TestCompiledCmpPathByDirblock(TestCmpPathByDirblock):
660
 
    """Test the pyrex implementation of _cmp_path_by_dirblock"""
 
631
class TestCompiledLtPathByDirblock(TestLtPathByDirblock):
 
632
    """Test the pyrex implementation of _lt_path_by_dirblock"""
661
633
 
662
634
    _test_needs_features = [compiled_dirstate_helpers_feature]
663
635
 
664
 
    def get_cmp_by_dirs(self):
665
 
        from bzrlib._dirstate_helpers_pyx import _cmp_path_by_dirblock
666
 
        return _cmp_path_by_dirblock
 
636
    def get_lt_path_by_dirblock(self):
 
637
        from breezy._dirstate_helpers_pyx import _lt_path_by_dirblock
 
638
        return _lt_path_by_dirblock
667
639
 
668
640
 
669
641
class TestMemRChr(tests.TestCase):
672
644
    _test_needs_features = [compiled_dirstate_helpers_feature]
673
645
 
674
646
    def assertMemRChr(self, expected, s, c):
675
 
        from bzrlib._dirstate_helpers_pyx import _py_memrchr
 
647
        from breezy._dirstate_helpers_pyx import _py_memrchr
676
648
        self.assertEqual(expected, _py_memrchr(s, c))
677
649
 
678
650
    def test_missing(self):
719
691
    implementation.
720
692
    """
721
693
 
 
694
    # inherits scenarios from test_dirstate
 
695
 
722
696
    def get_read_dirblocks(self):
723
 
        from bzrlib._dirstate_helpers_py import _read_dirblocks
 
697
        from breezy._dirstate_helpers_py import _read_dirblocks
724
698
        return _read_dirblocks
725
699
 
726
700
    def test_smoketest(self):
737
711
 
738
712
    def test_trailing_garbage(self):
739
713
        tree, state, expected = self.create_basic_dirstate()
740
 
        # On Linux, we can write extra data as long as we haven't read yet, but
 
714
        # On Unix, we can write extra data as long as we haven't read yet, but
741
715
        # on Win32, if you've opened the file with FILE_SHARE_READ, trying to
742
716
        # open it in append mode will fail.
743
717
        state.unlock()
760
734
    _test_needs_features = [compiled_dirstate_helpers_feature]
761
735
 
762
736
    def get_read_dirblocks(self):
763
 
        from bzrlib._dirstate_helpers_pyx import _read_dirblocks
 
737
        from breezy._dirstate_helpers_pyx import _read_dirblocks
764
738
        return _read_dirblocks
765
739
 
766
740
 
774
748
 
775
749
    def test_bisect_dirblock(self):
776
750
        if compiled_dirstate_helpers_feature.available():
777
 
            from bzrlib._dirstate_helpers_pyx import bisect_dirblock
 
751
            from breezy._dirstate_helpers_pyx import bisect_dirblock
778
752
        else:
779
 
            from bzrlib._dirstate_helpers_py import bisect_dirblock
 
753
            from breezy._dirstate_helpers_py import bisect_dirblock
780
754
        self.assertIs(bisect_dirblock, dirstate.bisect_dirblock)
781
755
 
782
756
    def test__bisect_path_left(self):
783
757
        if compiled_dirstate_helpers_feature.available():
784
 
            from bzrlib._dirstate_helpers_pyx import _bisect_path_left
 
758
            from breezy._dirstate_helpers_pyx import _bisect_path_left
785
759
        else:
786
 
            from bzrlib._dirstate_helpers_py import _bisect_path_left
 
760
            from breezy._dirstate_helpers_py import _bisect_path_left
787
761
        self.assertIs(_bisect_path_left, dirstate._bisect_path_left)
788
762
 
789
763
    def test__bisect_path_right(self):
790
764
        if compiled_dirstate_helpers_feature.available():
791
 
            from bzrlib._dirstate_helpers_pyx import _bisect_path_right
 
765
            from breezy._dirstate_helpers_pyx import _bisect_path_right
792
766
        else:
793
 
            from bzrlib._dirstate_helpers_py import _bisect_path_right
 
767
            from breezy._dirstate_helpers_py import _bisect_path_right
794
768
        self.assertIs(_bisect_path_right, dirstate._bisect_path_right)
795
769
 
796
 
    def test_cmp_by_dirs(self):
 
770
    def test_lt_by_dirs(self):
797
771
        if compiled_dirstate_helpers_feature.available():
798
 
            from bzrlib._dirstate_helpers_pyx import cmp_by_dirs
 
772
            from breezy._dirstate_helpers_pyx import lt_by_dirs
799
773
        else:
800
 
            from bzrlib._dirstate_helpers_py import cmp_by_dirs
801
 
        self.assertIs(cmp_by_dirs, dirstate.cmp_by_dirs)
 
774
            from breezy._dirstate_helpers_py import lt_by_dirs
 
775
        self.assertIs(lt_by_dirs, dirstate.lt_by_dirs)
802
776
 
803
777
    def test__read_dirblocks(self):
804
778
        if compiled_dirstate_helpers_feature.available():
805
 
            from bzrlib._dirstate_helpers_pyx import _read_dirblocks
 
779
            from breezy._dirstate_helpers_pyx import _read_dirblocks
806
780
        else:
807
 
            from bzrlib._dirstate_helpers_py import _read_dirblocks
 
781
            from breezy._dirstate_helpers_py import _read_dirblocks
808
782
        self.assertIs(_read_dirblocks, dirstate._read_dirblocks)
809
783
 
810
784
    def test_update_entry(self):
811
785
        if compiled_dirstate_helpers_feature.available():
812
 
            from bzrlib._dirstate_helpers_pyx import update_entry
 
786
            from breezy._dirstate_helpers_pyx import update_entry
813
787
        else:
814
 
            from bzrlib.dirstate import update_entry
 
788
            from breezy.dirstate import update_entry
815
789
        self.assertIs(update_entry, dirstate.update_entry)
816
790
 
817
791
    def test_process_entry(self):
818
792
        if compiled_dirstate_helpers_feature.available():
819
 
            from bzrlib._dirstate_helpers_pyx import ProcessEntryC
 
793
            from breezy._dirstate_helpers_pyx import ProcessEntryC
820
794
            self.assertIs(ProcessEntryC, dirstate._process_entry)
821
795
        else:
822
 
            from bzrlib.dirstate import ProcessEntryPython
 
796
            from breezy.dirstate import ProcessEntryPython
823
797
            self.assertIs(ProcessEntryPython, dirstate._process_entry)
824
798
 
825
799
 
826
800
class TestUpdateEntry(test_dirstate.TestCaseWithDirState):
827
801
    """Test the DirState.update_entry functions"""
828
802
 
 
803
    scenarios = multiply_scenarios(
 
804
        dir_reader_scenarios(), ue_scenarios)
 
805
 
829
806
    # Set by load_tests
830
807
    update_entry = None
831
808
 
843
820
 
844
821
    def test_observed_sha1_cachable(self):
845
822
        state, entry = self.get_state_with_a()
 
823
        state.save()
846
824
        atime = time.time() - 10
847
825
        self.build_tree(['a'])
848
 
        statvalue = os.lstat('a')
849
 
        statvalue = test_dirstate._FakeStat(statvalue.st_size, atime, atime,
850
 
            statvalue.st_dev, statvalue.st_ino, statvalue.st_mode)
 
826
        statvalue = test_dirstate._FakeStat.from_stat(os.lstat('a'))
 
827
        statvalue.st_mtime = statvalue.st_ctime = atime
 
828
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
 
829
                         state._dirblock_state)
851
830
        state._observed_sha1(entry, "foo", statvalue)
852
831
        self.assertEqual('foo', entry[1][0][1])
853
832
        packed_stat = dirstate.pack_stat(statvalue)
854
833
        self.assertEqual(packed_stat, entry[1][0][4])
 
834
        self.assertEqual(dirstate.DirState.IN_MEMORY_HASH_MODIFIED,
 
835
                         state._dirblock_state)
855
836
 
856
837
    def test_observed_sha1_not_cachable(self):
857
838
        state, entry = self.get_state_with_a()
 
839
        state.save()
858
840
        oldval = entry[1][0][1]
859
841
        oldstat = entry[1][0][4]
860
842
        self.build_tree(['a'])
861
843
        statvalue = os.lstat('a')
 
844
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
 
845
                         state._dirblock_state)
862
846
        state._observed_sha1(entry, "foo", statvalue)
863
847
        self.assertEqual(oldval, entry[1][0][1])
864
848
        self.assertEqual(oldstat, entry[1][0][4])
 
849
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
 
850
                         state._dirblock_state)
865
851
 
866
852
    def test_update_entry(self):
867
853
        state, _ = self.get_state_with_a()
892
878
                                          stat_value=stat_value)
893
879
        self.assertEqual(None, link_or_sha1)
894
880
 
895
 
        # The dirblock entry should not have cached the file's sha1 (too new)
 
881
        # The dirblock entry should not have computed or cached the file's
 
882
        # sha1, but it did update the files' st_size. However, this is not
 
883
        # worth writing a dirstate file for, so we leave the state UNMODIFIED
896
884
        self.assertEqual(('f', '', 14, False, dirstate.DirState.NULLSTAT),
897
885
                         entry[1][0])
898
 
        self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
 
886
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
899
887
                         state._dirblock_state)
900
888
        mode = stat_value.st_mode
901
889
        self.assertEqual([('is_exec', mode, False)], state._log)
904
892
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
905
893
                         state._dirblock_state)
906
894
 
907
 
        # If we do it again right away, we don't know if the file has changed
908
 
        # so we will re-read the file. Roll the clock back so the file is
909
 
        # guaranteed to look too new.
 
895
        # Roll the clock back so the file is guaranteed to look too new. We
 
896
        # should still not compute the sha1.
910
897
        state.adjust_time(-10)
911
898
        del state._log[:]
912
899
 
914
901
                                          stat_value=stat_value)
915
902
        self.assertEqual([('is_exec', mode, False)], state._log)
916
903
        self.assertEqual(None, link_or_sha1)
917
 
        self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
 
904
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
918
905
                         state._dirblock_state)
919
906
        self.assertEqual(('f', '', 14, False, dirstate.DirState.NULLSTAT),
920
907
                         entry[1][0])
930
917
        self.assertEqual([('is_exec', mode, False)], state._log)
931
918
        self.assertEqual(('f', '', 14, False, dirstate.DirState.NULLSTAT),
932
919
                         entry[1][0])
 
920
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
 
921
                         state._dirblock_state)
933
922
 
934
923
        # If the file is no longer new, and the clock has been moved forward
935
924
        # sufficiently, it will cache the sha.
960
949
 
961
950
    def test_update_entry_symlink(self):
962
951
        """Update entry should read symlinks."""
963
 
        self.requireFeature(tests.SymlinkFeature)
 
952
        self.requireFeature(features.SymlinkFeature)
964
953
        state, entry = self.get_state_with_a()
965
954
        state.save()
966
955
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
977
966
        # Dirblock is not updated (the link is too new)
978
967
        self.assertEqual([('l', '', 6, False, dirstate.DirState.NULLSTAT)],
979
968
                         entry[1])
980
 
        self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
 
969
        # The file entry turned into a symlink, that is considered
 
970
        # HASH modified worthy.
 
971
        self.assertEqual(dirstate.DirState.IN_MEMORY_HASH_MODIFIED,
981
972
                         state._dirblock_state)
982
973
 
983
974
        # Because the stat_value looks new, we should re-read the target
 
975
        del state._log[:]
984
976
        link_or_sha1 = self.update_entry(state, entry, abspath='a',
985
977
                                          stat_value=stat_value)
986
978
        self.assertEqual('target', link_or_sha1)
987
 
        self.assertEqual([('read_link', 'a', ''),
988
 
                          ('read_link', 'a', ''),
989
 
                         ], state._log)
 
979
        self.assertEqual([('read_link', 'a', '')], state._log)
990
980
        self.assertEqual([('l', '', 6, False, dirstate.DirState.NULLSTAT)],
991
981
                         entry[1])
 
982
        state.save()
992
983
        state.adjust_time(+20) # Skip into the future, all files look old
 
984
        del state._log[:]
993
985
        link_or_sha1 = self.update_entry(state, entry, abspath='a',
994
986
                                          stat_value=stat_value)
 
987
        # The symlink stayed a symlink. So while it is new enough to cache, we
 
988
        # don't bother setting the flag, because it is not really worth saving
 
989
        # (when we stat the symlink, we'll have paged in the target.)
 
990
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
 
991
                         state._dirblock_state)
995
992
        self.assertEqual('target', link_or_sha1)
996
993
        # We need to re-read the link because only now can we cache it
997
 
        self.assertEqual([('read_link', 'a', ''),
998
 
                          ('read_link', 'a', ''),
999
 
                          ('read_link', 'a', ''),
1000
 
                         ], state._log)
 
994
        self.assertEqual([('read_link', 'a', '')], state._log)
1001
995
        self.assertEqual([('l', 'target', 6, False, packed_stat)],
1002
996
                         entry[1])
1003
997
 
 
998
        del state._log[:]
1004
999
        # Another call won't re-read the link
1005
 
        self.assertEqual([('read_link', 'a', ''),
1006
 
                          ('read_link', 'a', ''),
1007
 
                          ('read_link', 'a', ''),
1008
 
                         ], state._log)
 
1000
        self.assertEqual([], state._log)
1009
1001
        link_or_sha1 = self.update_entry(state, entry, abspath='a',
1010
1002
                                          stat_value=stat_value)
1011
1003
        self.assertEqual('target', link_or_sha1)
1026
1018
        self.build_tree(['a/'])
1027
1019
        state.adjust_time(+20)
1028
1020
        self.assertIs(None, self.do_update_entry(state, entry, 'a'))
 
1021
        # a/ used to be a file, but is now a directory, worth saving
1029
1022
        self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
1030
1023
                         state._dirblock_state)
1031
1024
        state.save()
1032
1025
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
1033
1026
                         state._dirblock_state)
1034
 
        self.assertIs(None, self.do_update_entry(state, entry, 'a'))
 
1027
        # No changes to a/ means not worth saving.
 
1028
        self.assertIs(None, self.do_update_entry(state, entry, 'a'))
 
1029
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
 
1030
                         state._dirblock_state)
 
1031
        # Change the last-modified time for the directory
 
1032
        t = time.time() - 100.0
 
1033
        try:
 
1034
            os.utime('a', (t, t))
 
1035
        except OSError:
 
1036
            # It looks like Win32 + FAT doesn't allow to change times on a dir.
 
1037
            raise tests.TestSkipped("can't update mtime of a dir on FAT")
 
1038
        saved_packed_stat = entry[1][0][-1]
 
1039
        self.assertIs(None, self.do_update_entry(state, entry, 'a'))
 
1040
        # We *do* go ahead and update the information in the dirblocks, but we
 
1041
        # don't bother setting IN_MEMORY_MODIFIED because it is trivial to
 
1042
        # recompute.
 
1043
        self.assertNotEqual(saved_packed_stat, entry[1][0][-1])
1035
1044
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
1036
1045
                         state._dirblock_state)
1037
1046
 
1137
1146
 
1138
1147
    def test_update_file_to_symlink(self):
1139
1148
        """File becomes a symlink"""
1140
 
        self.requireFeature(tests.SymlinkFeature)
 
1149
        self.requireFeature(features.SymlinkFeature)
1141
1150
        state, entry = self.get_state_with_a()
1142
1151
        # The file sha1 won't be cached unless the file is old
1143
1152
        state.adjust_time(+10)
1156
1165
 
1157
1166
    def test_update_dir_to_symlink(self):
1158
1167
        """Directory becomes a symlink"""
1159
 
        self.requireFeature(tests.SymlinkFeature)
 
1168
        self.requireFeature(features.SymlinkFeature)
1160
1169
        state, entry = self.get_state_with_a()
1161
1170
        # The symlink target won't be cached if it isn't old
1162
1171
        state.adjust_time(+10)
1166
1175
 
1167
1176
    def test_update_symlink_to_file(self):
1168
1177
        """Symlink becomes a file"""
1169
 
        self.requireFeature(tests.SymlinkFeature)
 
1178
        self.requireFeature(features.SymlinkFeature)
1170
1179
        state, entry = self.get_state_with_a()
1171
1180
        # The symlink and file info won't be cached unless old
1172
1181
        state.adjust_time(+10)
1176
1185
 
1177
1186
    def test_update_symlink_to_dir(self):
1178
1187
        """Symlink becomes a directory"""
1179
 
        self.requireFeature(tests.SymlinkFeature)
 
1188
        self.requireFeature(features.SymlinkFeature)
1180
1189
        state, entry = self.get_state_with_a()
1181
1190
        # The symlink target won't be cached if it isn't old
1182
1191
        state.adjust_time(+10)
1269
1278
 
1270
1279
class TestProcessEntry(test_dirstate.TestCaseWithDirState):
1271
1280
 
 
1281
    scenarios = multiply_scenarios(dir_reader_scenarios(), pe_scenarios)
 
1282
 
1272
1283
    # Set by load_tests
1273
1284
    _process_entry = None
1274
1285
 
1321
1332
        state._sha1_provider = UppercaseSHA1Provider()
1322
1333
        self.assertChangedFileIds(['file-id'], tree)
1323
1334
 
 
1335
 
 
1336
class TestPackStat(tests.TestCase):
 
1337
    """Check packed representaton of stat values is robust on all inputs"""
 
1338
 
 
1339
    scenarios = helper_scenarios
 
1340
 
 
1341
    def pack(self, statlike_tuple):
 
1342
        return self.helpers.pack_stat(os.stat_result(statlike_tuple))
 
1343
 
 
1344
    @staticmethod
 
1345
    def unpack_field(packed_string, stat_field):
 
1346
        return _dirstate_helpers_py._unpack_stat(packed_string)[stat_field]
 
1347
 
 
1348
    def test_result(self):
 
1349
        self.assertEqual("AAAQAAAAABAAAAARAAAAAgAAAAEAAIHk",
 
1350
            self.pack((33252, 1, 2, 0, 0, 0, 4096, 15.5, 16.5, 17.5)))
 
1351
 
 
1352
    def test_giant_inode(self):
 
1353
        packed = self.pack((33252, 0xF80000ABC, 0, 0, 0, 0, 0, 0, 0, 0))
 
1354
        self.assertEqual(0x80000ABC, self.unpack_field(packed, "st_ino"))
 
1355
 
 
1356
    def test_giant_size(self):
 
1357
        packed = self.pack((33252, 0, 0, 0, 0, 0, (1 << 33) + 4096, 0, 0, 0))
 
1358
        self.assertEqual(4096, self.unpack_field(packed, "st_size"))
 
1359
 
 
1360
    def test_fractional_mtime(self):
 
1361
        packed = self.pack((33252, 0, 0, 0, 0, 0, 0, 0, 16.9375, 0))
 
1362
        self.assertEqual(16, self.unpack_field(packed, "st_mtime"))
 
1363
 
 
1364
    def test_ancient_mtime(self):
 
1365
        packed = self.pack((33252, 0, 0, 0, 0, 0, 0, 0, -11644473600.0, 0))
 
1366
        self.assertEqual(1240428288, self.unpack_field(packed, "st_mtime"))
 
1367
 
 
1368
    def test_distant_mtime(self):
 
1369
        packed = self.pack((33252, 0, 0, 0, 0, 0, 0, 0, 64060588800.0, 0))
 
1370
        self.assertEqual(3931046656, self.unpack_field(packed, "st_mtime"))
 
1371
 
 
1372
    def test_fractional_ctime(self):
 
1373
        packed = self.pack((33252, 0, 0, 0, 0, 0, 0, 0, 0, 17.5625))
 
1374
        self.assertEqual(17, self.unpack_field(packed, "st_ctime"))
 
1375
 
 
1376
    def test_ancient_ctime(self):
 
1377
        packed = self.pack((33252, 0, 0, 0, 0, 0, 0, 0, 0, -11644473600.0))
 
1378
        self.assertEqual(1240428288, self.unpack_field(packed, "st_ctime"))
 
1379
 
 
1380
    def test_distant_ctime(self):
 
1381
        packed = self.pack((33252, 0, 0, 0, 0, 0, 0, 0, 0, 64060588800.0))
 
1382
        self.assertEqual(3931046656, self.unpack_field(packed, "st_ctime"))
 
1383
 
 
1384
    def test_negative_dev(self):
 
1385
        packed = self.pack((33252, 0, -0xFFFFFCDE, 0, 0, 0, 0, 0, 0, 0))
 
1386
        self.assertEqual(0x322, self.unpack_field(packed, "st_dev"))