/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz

« back to all changes in this revision

Viewing changes to tests/test_commit.py

  • Committer: Vincent Ladeuil
  • Date: 2009-05-28 12:58:49 UTC
  • mto: This revision was merged to the branch mainline in revision 640.
  • Revision ID: v.ladeuil+lp@free.fr-20090528125849-9w4bhdp7cyzjokmh
Fix a leaking dialog windows appearing during the test suite.

* tests/test_commit.py:
(TestReusingSavedCommitMessages._get_commit_dialog): Ensure we
never prompt the user.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007 John Arbash Meinel <john@arbash-meinel.com>
 
1
# Copyright (C) 2007, 2008 John Arbash Meinel <john@arbash-meinel.com>
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
23
23
from bzrlib import (
24
24
    tests,
25
25
    revision,
 
26
    uncommit,
26
27
    )
27
28
from bzrlib.util import bencode
28
29
 
644
645
                            'message':'message\nfor b_dir\n'},
645
646
                          ]), dlg._get_specific_files())
646
647
 
647
 
 
648
 
class TestCommitDialog_Commit(tests.TestCaseWithTransport):
649
 
    """Tests on the actual 'commit' button being pushed."""
 
648
    def test_specific_files_sanitizes_messages(self):
 
649
        tree = self.make_branch_and_tree('tree')
 
650
        tree.branch.get_config().set_user_option('per_file_commits', 'true')
 
651
        self.build_tree(['tree/a_file', 'tree/b_dir/'])
 
652
        tree.add(['a_file', 'b_dir'], ['1a-id', '0b-id'])
 
653
 
 
654
        dlg = commit.CommitDialog(tree)
 
655
        dlg._commit_selected_radio.set_active(True)
 
656
        self.assertEqual((['a_file', 'b_dir'], []), dlg._get_specific_files())
 
657
 
 
658
        dlg._treeview_files.set_cursor((1,))
 
659
        dlg._set_file_commit_message('Test\r\nmessage\rfor a_file\n')
 
660
        dlg._treeview_files.set_cursor((2,))
 
661
        dlg._set_file_commit_message('message\r\nfor\nb_dir\r')
 
662
 
 
663
        self.assertEqual((['a_file', 'b_dir'],
 
664
                          [{'path':'a_file', 'file_id':'1a-id',
 
665
                            'message':'Test\nmessage\nfor a_file\n'},
 
666
                           {'path':'b_dir', 'file_id':'0b-id',
 
667
                            'message':'message\nfor\nb_dir\n'},
 
668
                          ]), dlg._get_specific_files())
 
669
 
 
670
 
 
671
class QuestionHelpers(object):
650
672
 
651
673
    def _set_question_yes(self, dlg):
652
674
        """Set the dialog to answer YES to any questions."""
653
675
        self.questions = []
654
 
        def _question_yes(*args):
 
676
        def _question_yes(*args, **kwargs):
655
677
            self.questions.append(args)
656
678
            self.questions.append('YES')
657
679
            return gtk.RESPONSE_YES
660
682
    def _set_question_no(self, dlg):
661
683
        """Set the dialog to answer NO to any questions."""
662
684
        self.questions = []
663
 
        def _question_no(*args):
 
685
        def _question_no(*args, **kwargs):
664
686
            self.questions.append(args)
665
687
            self.questions.append('NO')
666
688
            return gtk.RESPONSE_NO
667
689
        dlg._question_dialog = _question_no
668
690
 
 
691
 
 
692
class TestCommitDialog_Commit(tests.TestCaseWithTransport, QuestionHelpers):
 
693
    """Tests on the actual 'commit' button being pushed."""
 
694
 
669
695
    def test_bound_commit_local(self):
670
696
        tree = self.make_branch_and_tree('tree')
671
697
        self.build_tree(['tree/a'])
687
713
        self.assertEqual(last_rev, dlg.committed_revision_id)
688
714
        self.assertEqual(rev_id1, tree.branch.last_revision())
689
715
 
 
716
    def test_commit_global_sanitizes_message(self):
 
717
        tree = self.make_branch_and_tree('tree')
 
718
        self.build_tree(['tree/a'])
 
719
        tree.add(['a'], ['a-id'])
 
720
        rev_id1 = tree.commit('one')
 
721
 
 
722
        self.build_tree(['tree/b'])
 
723
        tree.add(['b'], ['b-id'])
 
724
        dlg = commit.CommitDialog(tree)
 
725
        # With the check box set, it should only effect the local branch
 
726
        dlg._set_global_commit_message('Commit\r\nmessage\rfoo\n')
 
727
        dlg._do_commit()
 
728
        rev = tree.branch.repository.get_revision(tree.last_revision())
 
729
        self.assertEqual('Commit\nmessage\nfoo\n', rev.message)
 
730
 
690
731
    def test_bound_commit_both(self):
691
732
        tree = self.make_branch_and_tree('tree')
692
733
        self.build_tree(['tree/a'])
708
749
        self.assertEqual(last_rev, dlg.committed_revision_id)
709
750
        self.assertEqual(last_rev, tree.branch.last_revision())
710
751
 
711
 
    def test_commit_no_message(self):
 
752
    def test_commit_empty_message(self):
712
753
        tree = self.make_branch_and_tree('tree')
713
754
        self.build_tree(['tree/a', 'tree/b'])
714
755
        tree.add(['a'], ['a-id'])
1030
1071
                          {'path':u'\u03a9', 'file_id':'omega-id',
1031
1072
                           'message':u'\u03a9 is the end of all things.\n'},
1032
1073
                         ], file_info_decoded)
 
1074
 
 
1075
 
 
1076
class TestSanitizeMessage(tests.TestCase):
 
1077
 
 
1078
    def assertSanitize(self, expected, original):
 
1079
        self.assertEqual(expected,
 
1080
                         commit._sanitize_and_decode_message(original))
 
1081
 
 
1082
    def test_untouched(self):
 
1083
        self.assertSanitize('foo\nbar\nbaz\n', 'foo\nbar\nbaz\n')
 
1084
 
 
1085
    def test_converts_cr_to_lf(self):
 
1086
        self.assertSanitize('foo\nbar\nbaz\n', 'foo\rbar\rbaz\r')
 
1087
 
 
1088
    def test_converts_crlf_to_lf(self):
 
1089
        self.assertSanitize('foo\nbar\nbaz\n', 'foo\r\nbar\r\nbaz\r\n')
 
1090
 
 
1091
    def test_converts_mixed_to_lf(self):
 
1092
        self.assertSanitize('foo\nbar\nbaz\n', 'foo\r\nbar\rbaz\n')
 
1093
 
 
1094
 
 
1095
class TestSavedCommitMessages(tests.TestCaseWithTransport):
 
1096
 
 
1097
    def _get_file_info_dict(self, rank):
 
1098
        file_info = [dict(path='a', file_id='a-id', message='a msg %d' % rank),
 
1099
                     dict(path='b', file_id='b-id', message='b msg %d' % rank)]
 
1100
        return file_info
 
1101
 
 
1102
    def _get_file_info_revprops(self, rank):
 
1103
        file_info_prop = self._get_file_info_dict(rank)
 
1104
        return {'file-info': bencode.bencode(file_info_prop).decode('UTF-8')}
 
1105
 
 
1106
    def _get_commit_message(self):
 
1107
        return self.config.get_user_option('gtk_global_commit_message')
 
1108
 
 
1109
    def _get_file_commit_messages(self):
 
1110
        return self.config.get_user_option('gtk_file_commit_messages')
 
1111
 
 
1112
 
 
1113
class TestUncommitHook(TestSavedCommitMessages):
 
1114
 
 
1115
    def setUp(self):
 
1116
        super(TestUncommitHook, self).setUp()
 
1117
        self.tree = self.make_branch_and_tree('tree')
 
1118
        self.config = self.tree.branch.get_config()
 
1119
        self.build_tree(['tree/a', 'tree/b'])
 
1120
        self.tree.add(['a'], ['a-id'])
 
1121
        self.tree.add(['b'], ['b-id'])
 
1122
        rev1 = self.tree.commit('one', revprops=self._get_file_info_revprops(1))
 
1123
        rev2 = self.tree.commit('two', revprops=self._get_file_info_revprops(2))
 
1124
        rev3 = self.tree.commit('three',
 
1125
                                revprops=self._get_file_info_revprops(3))
 
1126
 
 
1127
    def test_uncommit_one_by_one(self):
 
1128
        uncommit.uncommit(self.tree.branch, tree=self.tree)
 
1129
        self.assertEquals(u'three', self._get_commit_message())
 
1130
        self.assertEquals(u'd4:a-id7:a msg 34:b-id7:b msg 3e',
 
1131
                          self._get_file_commit_messages())
 
1132
 
 
1133
        uncommit.uncommit(self.tree.branch, tree=self.tree)
 
1134
        self.assertEquals(u'two\n******\nthree', self._get_commit_message())
 
1135
        self.assertEquals(u'd4:a-id22:a msg 2\n******\na msg 3'
 
1136
                          '4:b-id22:b msg 2\n******\nb msg 3e',
 
1137
                          self._get_file_commit_messages())
 
1138
 
 
1139
        uncommit.uncommit(self.tree.branch, tree=self.tree)
 
1140
        self.assertEquals(u'one\n******\ntwo\n******\nthree',
 
1141
                          self._get_commit_message())
 
1142
        self.assertEquals(u'd4:a-id37:a msg 1\n******\na msg 2\n******\na msg 3'
 
1143
                          '4:b-id37:b msg 1\n******\nb msg 2\n******\nb msg 3e',
 
1144
                          self._get_file_commit_messages())
 
1145
 
 
1146
    def test_uncommit_all_at_once(self):
 
1147
        uncommit.uncommit(self.tree.branch, tree=self.tree, revno=1)
 
1148
        self.assertEquals(u'one\n******\ntwo\n******\nthree',
 
1149
                          self._get_commit_message())
 
1150
        self.assertEquals(u'd4:a-id37:a msg 1\n******\na msg 2\n******\na msg 3'
 
1151
                          '4:b-id37:b msg 1\n******\nb msg 2\n******\nb msg 3e',
 
1152
                          self._get_file_commit_messages())
 
1153
 
 
1154
 
 
1155
class TestReusingSavedCommitMessages(TestSavedCommitMessages, QuestionHelpers):
 
1156
 
 
1157
    def setUp(self):
 
1158
        super(TestReusingSavedCommitMessages, self).setUp()
 
1159
        self.tree = self.make_branch_and_tree('tree')
 
1160
        self.config = self.tree.branch.get_config()
 
1161
        self.config.set_user_option('per_file_commits', 'true')
 
1162
        self.build_tree(['tree/a', 'tree/b'])
 
1163
        self.tree.add(['a'], ['a-id'])
 
1164
        self.tree.add(['b'], ['b-id'])
 
1165
        rev1 = self.tree.commit('one', revprops=self._get_file_info_revprops(1))
 
1166
        rev2 = self.tree.commit('two', revprops=self._get_file_info_revprops(2))
 
1167
        uncommit.uncommit(self.tree.branch, tree=self.tree)
 
1168
        self.build_tree_contents([('tree/a', 'new a content\n'),
 
1169
                                  ('tree/b', 'new b content'),])
 
1170
 
 
1171
    def _get_commit_dialog(self, tree):
 
1172
        # Ensure we will never use a dialog that can actually prompt the user
 
1173
        # during the test suite. Test *can* and *should* override with the
 
1174
        # correct question dialog type.
 
1175
        dlg = commit.CommitDialog(tree)
 
1176
        self._set_question_no(dlg)
 
1177
        return dlg
 
1178
 
 
1179
    def test_setup_saved_messages(self):
 
1180
        # Check the initial setup
 
1181
        self.assertEquals(u'two', self._get_commit_message())
 
1182
        self.assertEquals(u'd4:a-id7:a msg 24:b-id7:b msg 2e',
 
1183
                          self._get_file_commit_messages())
 
1184
 
 
1185
    def test_messages_are_reloaded(self):
 
1186
        dlg = self._get_commit_dialog(self.tree)
 
1187
        self.assertEquals(u'two', dlg._get_global_commit_message())
 
1188
        self.assertEquals(([u'a', u'b'],
 
1189
                           [{ 'path': 'a',
 
1190
                             'file_id': 'a-id', 'message': 'a msg 2',},
 
1191
                           {'path': 'b',
 
1192
                            'file_id': 'b-id', 'message': 'b msg 2',}],),
 
1193
                          dlg._get_specific_files())
 
1194
 
 
1195
    def test_messages_are_consumed(self):
 
1196
        dlg = self._get_commit_dialog(self.tree)
 
1197
        dlg._do_commit()
 
1198
        self.assertEquals(u'', self._get_commit_message())
 
1199
        self.assertEquals(u'de', self._get_file_commit_messages())
 
1200
 
 
1201
    def test_messages_are_saved_on_cancel_if_required(self):
 
1202
        dlg = self._get_commit_dialog(self.tree)
 
1203
        self._set_question_yes(dlg) # Save messages
 
1204
        dlg._do_cancel()
 
1205
        self.assertEquals(u'two', self._get_commit_message())
 
1206
        self.assertEquals(u'd4:a-id7:a msg 24:b-id7:b msg 2e',
 
1207
                          self._get_file_commit_messages())
 
1208
 
 
1209
    def test_messages_are_cleared_on_cancel_if_required(self):
 
1210
        dlg = self._get_commit_dialog(self.tree)
 
1211
        self._set_question_no(dlg) # Don't save messages
 
1212
        dlg._do_cancel()
 
1213
        self.assertEquals(u'', self._get_commit_message())
 
1214
        self.assertEquals(u'de',
 
1215
                          self._get_file_commit_messages())