Merge lp:~sinzui/bzr-gtk/handle-patch into lp:bzr-gtk

Proposed by Curtis Hovey
Status: Merged
Approved by: Curtis Hovey
Approved revision: 776
Merged at revision: 774
Proposed branch: lp:~sinzui/bzr-gtk/handle-patch
Merge into: lp:bzr-gtk
Diff against target: 221 lines (+112/-14)
4 files modified
bzr-handle-patch (+7/-4)
diff.py (+14/-10)
tests/test_commit.py (+31/-0)
tests/test_diff.py (+60/-0)
To merge this branch: bzr merge lp:~sinzui/bzr-gtk/handle-patch
Reviewer Review Type Date Requested Status
Jelmer Vernooij (community) Approve
Review via email: mp+91514@code.launchpad.net

Description of the change

Update bzr-handle-patch to gtk3.

    Launchpad bug: https://bugs.launchpad.net/bugs/926315
    Pre-implementation: no one

bzr-handle-patch has import and type errors because it is using Gtk3,
but is calling gtk2 API.

$./bzr-handle-patch ../handle-patch/_test.patch
...
  File "...gtk/diff.py", line 473, in _get_button_bar
    hbox.pack_start(merge_button, expand=False, fill=True)
  File "/usr/lib/python2.7/dist-packages/gi/types.py", line 43, in function
    return info.invoke(*args, **kwargs)
TypeError: pack_start() takes exactly 5 non-keyword arguments (2 given)

--------------------------------------------------------------------

RULES

    * bzr-handle-patch already imports Gtk, but its two calls gtk (the
      identifier does not exist)
    * Fix the hbox.pack_start

QA

    * from the top of the tree run the following command to send
      a mock patch to bzr-handle-patch
echo "=== added file '_test.txt'
--- _test.txt 1970-01-01 00:00:00 +0000
+++ _test.txt 2012-02-03 20:00:34 +0000
@@ -0,0 +1,1 @@
+hello" | BZR_PLUGINS_AT='gtk@.' ./bzr-handle-patch -
    * Verify you are presented the diff window.

IMPLEMENTATION

Fixed the pack_start args. Added tests for the calls to construct the UI.
Updated the base class to be testible. I replaced repeated calls to show()
with show_all() which is more efficient.
  diff.py
  tests/test_diff.py

Fixed the import/calls to Gtk. Added a smoke test to verify the script
starts.
    bzr-handle-patch
    tests/test_commit.py

To post a comment you must log in.
Revision history for this message
Jelmer Vernooij (jelmer) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'bzr-handle-patch'
2--- bzr-handle-patch 2011-07-31 16:50:29 +0000
3+++ bzr-handle-patch 2012-02-03 23:12:18 +0000
4@@ -2,10 +2,9 @@
5
6 from bzrlib import errors, merge_directive
7 from bzrlib.plugin import load_plugins
8+load_plugins()
9 from bzrlib.plugins.gtk.commands import open_display
10
11-load_plugins()
12-
13 import sys
14
15 if len(sys.argv) < 2:
16@@ -32,9 +31,13 @@
17 window = controller.window
18 window.show()
19 Gtk = open_display()
20- window.connect("destroy", gtk.main_quit)
21+ window.connect("destroy", Gtk.main_quit)
22 except Exception, e:
23 from bzrlib.plugins.gtk.dialog import error_dialog
24 error_dialog('Error', str(e))
25 raise
26-gtk.main()
27+
28+if len(sys.argv) == 3 and sys.argv[2] == 'test':
29+ sys.exit(0)
30+
31+Gtk.main()
32
33=== modified file 'diff.py'
34--- diff.py 2012-01-23 21:47:33 +0000
35+++ diff.py 2012-02-03 23:12:18 +0000
36@@ -435,10 +435,12 @@
37 differences between two revisions on a branch.
38 """
39
40+ SHOW_WIDGETS = True
41+
42 def __init__(self, parent=None, operations=None):
43 super(DiffWindow, self).__init__(parent=parent)
44 self.set_border_width(0)
45- self.set_title("bzrk diff")
46+ self.set_title("bzr diff")
47
48 # Use two thirds of the screen by default
49 screen = self.get_screen()
50@@ -452,10 +454,12 @@
51 """Construct the window contents."""
52 self.vbox = Gtk.VBox()
53 self.add(self.vbox)
54- self.vbox.show()
55+ if self.SHOW_WIDGETS:
56+ self.vbox.show()
57 self.diff = DiffWidget()
58 self.vbox.pack_end(self.diff, True, True, 0)
59- self.diff.show_all()
60+ if self.SHOW_WIDGETS:
61+ self.diff.show_all()
62 # Build after DiffWidget to connect signals
63 menubar = self._get_menu_bar()
64 self.vbox.pack_start(menubar, False, False, 0)
65@@ -471,13 +475,11 @@
66 mb_view_wrapsource = Gtk.CheckMenuItem.new_with_mnemonic(
67 _i18n("Wrap _Long Lines"))
68 mb_view_wrapsource.connect('activate', self.diff._on_wraplines_toggled)
69- mb_view_wrapsource.show()
70 mb_view_menu.append(mb_view_wrapsource)
71- mb_view.show()
72 mb_view.set_submenu(mb_view_menu)
73- mb_view.show()
74 menubar.append(mb_view)
75- menubar.show()
76+ if self.SHOW_WIDGETS:
77+ menubar.show_all()
78 return menubar
79
80 def _get_button_bar(self, operations):
81@@ -491,11 +493,13 @@
82 hbox.set_layout(Gtk.ButtonBoxStyle.START)
83 for title, method in operations:
84 merge_button = Gtk.Button(title)
85- merge_button.show()
86+ if self.SHOW_WIDGETS:
87+ merge_button.show()
88 merge_button.set_relief(Gtk.ReliefStyle.NONE)
89 merge_button.connect("clicked", method)
90- hbox.pack_start(merge_button, expand=False, fill=True)
91- hbox.show()
92+ hbox.pack_start(merge_button, False, True, 0)
93+ if self.SHOW_WIDGETS:
94+ hbox.show()
95 return hbox
96
97 def _get_merge_target(self):
98
99=== modified file 'tests/test_commit.py'
100--- tests/test_commit.py 2012-01-23 16:52:40 +0000
101+++ tests/test_commit.py 2012-02-03 23:12:18 +0000
102@@ -17,6 +17,8 @@
103 """Test the Commit functionality."""
104
105 import os
106+import subprocess
107+from tempfile import NamedTemporaryFile
108
109 from gi.repository import Gtk
110
111@@ -1296,3 +1298,32 @@
112 self.assertEquals(u'', self._get_commit_message())
113 self.assertEquals(u'de',
114 self._get_file_commit_messages())
115+
116+
117+class BzrHandlePatchTestCase(tests.TestCase):
118+
119+ def setUp(self):
120+ top = os.path.abspath(os.path.join(
121+ os.path.dirname(__file__), os.pardir))
122+ self.script = os.path.join(top, 'bzr-handle-patch')
123+ self.env = dict(os.environ)
124+ self.env['BZR_PLUGINS_AT'] = 'gtk@%s' %top
125+ self.patch = NamedTemporaryFile()
126+ self.patch.write('\n'.join([
127+ "=== added file '_test.txt'",
128+ "--- _test.txt 1970-01-01 00:00:00 +0000",
129+ "+++ _test.txt 2012-02-03 20:00:34 +0000",
130+ "@@ -0,0 +1,1 @@",
131+ "+hello",
132+ ]))
133+ self.patch.flush()
134+ super(BzrHandlePatchTestCase, self).setUp()
135+
136+ def test_smoketest(self):
137+ # This is a smoke test to verify the process starts.
138+ bzr_notify = subprocess.Popen(
139+ [self.script, self.patch.name, 'test'],
140+ stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=self.env)
141+ stdout, stderr = bzr_notify.communicate()
142+ self.assertEqual('', stdout)
143+ self.assertEqual('', stderr)
144
145=== modified file 'tests/test_diff.py'
146--- tests/test_diff.py 2012-01-23 18:10:44 +0000
147+++ tests/test_diff.py 2012-02-03 23:12:18 +0000
148@@ -35,6 +35,7 @@
149 DiffController,
150 DiffView,
151 DiffWidget,
152+ DiffWindow,
153 iter_changes_to_status,
154 MergeDirectiveController,
155 )
156@@ -131,6 +132,65 @@
157 self.assertFalse(widget.diff_view.show_diff.called)
158
159
160+class FakeDiffWindow(DiffWindow):
161+
162+ SHOW_WIDGETS = False
163+
164+
165+class DiffWindowTestCase(tests.TestCaseWithTransport):
166+
167+ def test_init(self):
168+ window = DiffWindow()
169+ self.assertEqual('bzr diff', window.props.title)
170+ self.assertEqual(0, window.props.border_width)
171+
172+ def test_init_construct_without_operations(self):
173+ window = DiffWindow()
174+ widgets = window.vbox.get_children()
175+ self.assertEqual(2, len(widgets))
176+ self.assertIsInstance(widgets[0], Gtk.MenuBar)
177+ self.assertIsInstance(widgets[1], DiffWidget)
178+
179+ def test_init_construct_with_operations(self):
180+ method = MockMethod()
181+ window = DiffWindow(operations=[('title', method)])
182+ widgets = window.vbox.get_children()
183+ self.assertEqual(3, len(widgets))
184+ self.assertIsInstance(widgets[0], Gtk.MenuBar)
185+ self.assertIsInstance(widgets[1], Gtk.HButtonBox)
186+ self.assertIsInstance(widgets[2], DiffWidget)
187+
188+ def test_get_menu_bar(self):
189+ window = DiffWindow()
190+ menu_bar = window._get_menu_bar()
191+ self.assertIsNot(None, menu_bar)
192+ menus = menu_bar.get_children()
193+ self.assertEqual(1, len(menus))
194+ self.assertEqual('_View', menus[0].props.label)
195+ sub_menu = menus[0].get_submenu()
196+ self.assertIsNot(None, sub_menu)
197+ items = sub_menu.get_children()
198+ self.assertEqual(1, len(items))
199+ menus[0].get_submenu().get_children()[0].props.label
200+ self.assertEqual('Wrap _Long Lines', items[0].props.label)
201+
202+ def test_get_button_bar_with_none(self):
203+ window = DiffWindow()
204+ self.assertIs(None, window._get_button_bar(None))
205+
206+ def test_get_button_bar_with_operations(self):
207+ window = DiffWindow()
208+ method = MockMethod()
209+ button_bar = window._get_button_bar([('title', method)])
210+ self.assertIsNot(None, button_bar)
211+ buttons = button_bar.get_children()
212+ self.assertEqual(1, len(buttons))
213+ self.assertEqual('title', buttons[0].props.label)
214+ buttons[0].emit('clicked')
215+ self.assertIs(True, method.called)
216+
217+
218+
219 class MockDiffWidget(object):
220
221 def set_diff_text_sections(self, sections):

Subscribers

People subscribed via source and target branches

to all changes: