[pygobject] tests: Make tests run without Gtk/Gdk installed



commit 7bc20fe2d1225bebb2f4ec1faabf6a70e4e5a4e7
Author: Mikhail Fludkov <misha pexip com>
Date:   Thu Feb 8 12:49:01 2018 +0100

    tests: Make tests run without Gtk/Gdk installed
    
    Python evaluates the code inside @unittest.skipIf decorator during
    import time. Because Gtk is not installed it crashes while evaluating
    'Gtk._version' where Gtk=None.

 tests/test_ossig.py         | 10 ++++++---
 tests/test_overrides_gdk.py | 17 +++++++--------
 tests/test_overrides_gtk.py | 50 +++++++++++++++++++++++----------------------
 3 files changed, 42 insertions(+), 35 deletions(-)
---
diff --git a/tests/test_ossig.py b/tests/test_ossig.py
index bf218b82..0b6eea65 100644
--- a/tests/test_ossig.py
+++ b/tests/test_ossig.py
@@ -20,7 +20,11 @@ import unittest
 import threading
 from contextlib import contextmanager
 
-from gi.repository import Gtk, Gio, GLib
+try:
+    from gi.repository import Gtk
+except ImportError:
+    Gtk = None
+from gi.repository import Gio, GLib
 from gi._ossighelper import wakeup_on_signal, register_sigint_fallback
 
 
@@ -83,7 +87,7 @@ class TestOverridesWakeupOnAlarm(unittest.TestCase):
             app.connect("activate", lambda *args: None)
             app.run()
 
-    @unittest.skipIf(os.name == "nt", "not on Windows")
+    @unittest.skipIf(Gtk is None or os.name == "nt", "not on Windows")
     def test_gtk_main(self):
         signal.signal(signal.SIGALRM, lambda *args: Gtk.main_quit())
         GLib.idle_add(signal.setitimer, signal.ITIMER_REAL, 0.001)
@@ -91,7 +95,7 @@ class TestOverridesWakeupOnAlarm(unittest.TestCase):
         with self._run_with_timeout(2000, Gtk.main_quit):
             Gtk.main()
 
-    @unittest.skipIf(os.name == "nt", "not on Windows")
+    @unittest.skipIf(Gtk is None or os.name == "nt", "not on Windows")
     def test_gtk_dialog_run(self):
         w = Gtk.Window()
         d = Gtk.Dialog(transient_for=w)
diff --git a/tests/test_overrides_gdk.py b/tests/test_overrides_gdk.py
index 0974d35c..14527375 100644
--- a/tests/test_overrides_gdk.py
+++ b/tests/test_overrides_gdk.py
@@ -10,9 +10,10 @@ from gi import PyGIDeprecationWarning
 
 try:
     from gi.repository import Gdk, GdkPixbuf, Gtk
-    Gdk  # pyflakes
+    Gdk_version = Gdk._version
 except ImportError:
     Gdk = None
+    Gdk_version = None
 
 from helper import capture_glib_deprecation_warnings
 
@@ -21,7 +22,7 @@ from helper import capture_glib_deprecation_warnings
 class TestGdk(unittest.TestCase):
 
     @unittest.skipIf(sys.platform == "darwin" or os.name == "nt", "crashes")
-    @unittest.skipIf(Gdk._version == "4.0", "not in gdk4")
+    @unittest.skipIf(Gdk_version == "4.0", "not in gdk4")
     def test_constructor(self):
         attribute = Gdk.WindowAttr()
         attribute.window_type = Gdk.WindowType.CHILD
@@ -30,7 +31,7 @@ class TestGdk(unittest.TestCase):
         window = Gdk.Window(None, attribute, attributes_mask)
         self.assertEqual(window.get_window_type(), Gdk.WindowType.CHILD)
 
-    @unittest.skipIf(Gdk._version == "4.0", "not in gdk4")
+    @unittest.skipIf(Gdk_version == "4.0", "not in gdk4")
     def test_color(self):
         color = Gdk.Color(100, 200, 300)
         self.assertEqual(color.red, 100)
@@ -40,7 +41,7 @@ class TestGdk(unittest.TestCase):
             self.assertEqual(color, Gdk.Color(100, 200, 300))
         self.assertNotEqual(color, Gdk.Color(1, 2, 3))
 
-    @unittest.skipIf(Gdk._version == "4.0", "not in gdk4")
+    @unittest.skipIf(Gdk_version == "4.0", "not in gdk4")
     def test_color_floats(self):
         self.assertEqual(Gdk.Color(13107, 21845, 65535),
                          Gdk.Color.from_floats(0.2, 1.0 / 3.0, 1.0))
@@ -107,7 +108,7 @@ class TestGdk(unittest.TestCase):
         event = Gdk.Event.new(Gdk.EventType.CONFIGURE)
         self.assertTrue("CONFIGURE" in repr(event))
 
-    @unittest.skipIf(Gdk._version == "4.0", "not in gdk4")
+    @unittest.skipIf(Gdk_version == "4.0", "not in gdk4")
     def test_event_structures(self):
         def button_press_cb(button, event):
             self.assertTrue(isinstance(event, Gdk.EventButton))
@@ -131,7 +132,7 @@ class TestGdk(unittest.TestCase):
                                  Gdk.ModifierType.CONTROL_MASK,
                                  Gdk.EventType.BUTTON_PRESS)
 
-    @unittest.skipIf(Gdk._version == "4.0", "not in gdk4")
+    @unittest.skipIf(Gdk_version == "4.0", "not in gdk4")
     def test_cursor(self):
         self.assertEqual(Gdk.Cursor, gi.overrides.Gdk.Cursor)
         with capture_glib_deprecation_warnings():
@@ -178,7 +179,7 @@ class TestGdk(unittest.TestCase):
         self.assertEqual(str(Gdk.ModifierType.RELEASE_MASK | Gdk.ModifierType.META_MASK),
                          '<flags GDK_META_MASK | GDK_RELEASE_MASK of type Gdk.ModifierType>')
 
-    @unittest.skipIf(Gdk._version == "4.0", "not in gdk4")
+    @unittest.skipIf(Gdk_version == "4.0", "not in gdk4")
     def test_color_parse(self):
         with capture_glib_deprecation_warnings():
             c = Gdk.color_parse('#00FF80')
@@ -187,7 +188,7 @@ class TestGdk(unittest.TestCase):
         self.assertEqual(c.blue, 32896)
         self.assertEqual(Gdk.color_parse('bogus'), None)
 
-    @unittest.skipIf(Gdk._version == "4.0", "not in gdk4")
+    @unittest.skipIf(Gdk_version == "4.0", "not in gdk4")
     def test_color_representations(self):
         # __repr__ should generate a string which is parsable when possible
         # http://docs.python.org/2/reference/datamodel.html#object.__repr__
diff --git a/tests/test_overrides_gtk.py b/tests/test_overrides_gtk.py
index f15e717d..338ca72c 100644
--- a/tests/test_overrides_gtk.py
+++ b/tests/test_overrides_gtk.py
@@ -17,8 +17,10 @@ from gi.repository import GLib, GObject
 try:
     from gi.repository import Gtk, GdkPixbuf, Gdk
     PyGTKDeprecationWarning = Gtk.PyGTKDeprecationWarning
+    Gtk_version = Gtk._version
 except ImportError:
     Gtk = None
+    Gtk_version = None
     PyGTKDeprecationWarning = None
     GdkPixbuf = None
     Gdk = None
@@ -76,7 +78,7 @@ class TestGtk(unittest.TestCase):
         labels = [x for x in box]
         self.assertEqual(labels, [label, label2])
 
-    @unittest.skipIf(Gtk._version == "4.0", "not in gtk4")
+    @unittest.skipIf(Gtk_version == "4.0", "not in gtk4")
     def test_actions(self):
         self.assertEqual(Gtk.Action, gi.overrides.Gtk.Action)
         action = Gtk.Action(name="test", label="Test", tooltip="Test Action", stock_id=Gtk.STOCK_COPY)
@@ -93,7 +95,7 @@ class TestGtk(unittest.TestCase):
         self.assertEqual(action.get_stock_id(), Gtk.STOCK_COPY)
         self.assertEqual(action.get_current_value(), 1)
 
-    @unittest.skipIf(Gtk._version == "4.0", "not in gtk4")
+    @unittest.skipIf(Gtk_version == "4.0", "not in gtk4")
     def test_actiongroup(self):
         self.assertEqual(Gtk.ActionGroup, gi.overrides.Gtk.ActionGroup)
 
@@ -135,7 +137,7 @@ class TestGtk(unittest.TestCase):
             expected_results.remove(a)
             action.activate()
 
-    @unittest.skipIf(Gtk._version == "4.0", "not in gtk4")
+    @unittest.skipIf(Gtk_version == "4.0", "not in gtk4")
     def test_uimanager(self):
         self.assertEqual(Gtk.UIManager, gi.overrides.Gtk.UIManager)
         ui = Gtk.UIManager()
@@ -155,7 +157,7 @@ class TestGtk(unittest.TestCase):
         self.assertEqual(ag, groups[-2])
         self.assertEqual(ag2, groups[-1])
 
-    @unittest.skipIf(Gtk._version == "4.0", "not in gtk4")
+    @unittest.skipIf(Gtk_version == "4.0", "not in gtk4")
     def test_uimanager_nonascii(self):
         ui = Gtk.UIManager()
         ui.add_ui_from_string(b'<ui><menubar name="menub\xc3\xa6r1" /></ui>'.decode('UTF-8'))
@@ -198,7 +200,7 @@ class TestGtk(unittest.TestCase):
         self.assertEqual(Gtk.Dialog, gi.overrides.Gtk.Dialog)
         self.assertEqual(Gtk.FileChooserDialog, gi.overrides.Gtk.FileChooserDialog)
         self.assertEqual(Gtk.RecentChooserDialog, gi.overrides.Gtk.RecentChooserDialog)
-        if Gtk._version != "4.0":
+        if Gtk_version != "4.0":
             self.assertEqual(Gtk.ColorSelectionDialog, gi.overrides.Gtk.ColorSelectionDialog)
             self.assertEqual(Gtk.FontSelectionDialog, gi.overrides.Gtk.FontSelectionDialog)
 
@@ -306,7 +308,7 @@ class TestGtk(unittest.TestCase):
         self.assertEqual(dialog.get_property('secondary-text'), '2nd markup')
         self.assertTrue(dialog.get_property('secondary-use-markup'))
 
-    @unittest.skipIf(Gtk._version == "4.0", "not in gtk4")
+    @unittest.skipIf(Gtk_version == "4.0", "not in gtk4")
     def test_color_selection_dialog(self):
         dialog = Gtk.ColorSelectionDialog(title="color selection dialog test")
         self.assertTrue(isinstance(dialog, Gtk.Dialog))
@@ -334,7 +336,7 @@ class TestGtk(unittest.TestCase):
         action = dialog.get_property('action')
         self.assertEqual(Gtk.FileChooserAction.OPEN, action)
 
-    @unittest.skipIf(Gtk._version == "4.0", "not in gtk4")
+    @unittest.skipIf(Gtk_version == "4.0", "not in gtk4")
     def test_font_selection_dialog(self):
         dialog = Gtk.FontSelectionDialog(title="font selection dialog test")
         self.assertTrue(isinstance(dialog, Gtk.Dialog))
@@ -371,7 +373,7 @@ class TestGtk(unittest.TestCase):
         self.assertTrue(isinstance(button, Gtk.Container))
         self.assertTrue(isinstance(button, Gtk.Widget))
 
-        if Gtk._version != "4.0":
+        if Gtk_version != "4.0":
             # Using stock items causes hard warning in devel versions of GTK+.
             with capture_glib_warnings(allow_warnings=True):
                 button = Gtk.Button.new_from_stock(Gtk.STOCK_CLOSE)
@@ -469,7 +471,7 @@ class TestGtk(unittest.TestCase):
         adjustment = Gtk.Adjustment(1, -1, 3, 0, 0, 0, value=2)
         self.adjustment_check(adjustment, value=2, lower=-1, upper=3)
 
-    @unittest.skipIf(Gtk._version == "4.0", "not in gtk4")
+    @unittest.skipIf(Gtk_version == "4.0", "not in gtk4")
     def test_table(self):
         table = Gtk.Table()
         self.assertTrue(isinstance(table, Gtk.Table))
@@ -531,7 +533,7 @@ class TestGtk(unittest.TestCase):
         widget.drag_source_add_uri_targets()
         widget.drag_source_set_icon_name("_About")
         widget.drag_source_set_icon_pixbuf(GdkPixbuf.Pixbuf())
-        if Gtk._version != "4.0":
+        if Gtk_version != "4.0":
             widget.drag_source_set_icon_stock(Gtk.STOCK_ABOUT)
         widget.drag_source_get_target_list()
         widget.drag_source_set_target_list(None)
@@ -540,7 +542,7 @@ class TestGtk(unittest.TestCase):
 
         # these methods cannot be called because they require a valid drag on
         # a real GdkWindow. So we only check that they exist and are callable.
-        if Gtk._version != "4.0":
+        if Gtk_version != "4.0":
             self.assertTrue(hasattr(widget, 'drag_dest_set_proxy'))
         self.assertTrue(hasattr(widget, 'drag_get_data'))
 
@@ -577,7 +579,7 @@ class TestGtk(unittest.TestCase):
         treeview.enable_model_drag_dest(mixed_target_list,
                                         Gdk.DragAction.DEFAULT | Gdk.DragAction.MOVE)
 
-    @unittest.skipIf(Gtk._version == "4.0", "not in gtk4")
+    @unittest.skipIf(Gtk_version == "4.0", "not in gtk4")
     def test_scrollbar(self):
         adjustment = Gtk.Adjustment()
 
@@ -600,7 +602,7 @@ class TestGtk(unittest.TestCase):
         iconview = Gtk.IconView(model=model)
         self.assertEqual(iconview.props.model, model)
 
-    @unittest.skipIf(Gtk._version == "4.0", "not in gtk4")
+    @unittest.skipIf(Gtk_version == "4.0", "not in gtk4")
     def test_toolbutton(self):
         # PyGTK compat
 
@@ -623,7 +625,7 @@ class TestGtk(unittest.TestCase):
         self.assertEqual(button.props.label, 'mylabel')
         self.assertEqual(button.props.icon_widget, icon)
 
-    @unittest.skipIf(Gtk._version == "4.0", "not in gtk4")
+    @unittest.skipIf(Gtk_version == "4.0", "not in gtk4")
     def test_iconset(self):
         Gtk.IconSet()
         pixbuf = GdkPixbuf.Pixbuf()
@@ -639,7 +641,7 @@ class TestGtk(unittest.TestCase):
         self.assertEqual(viewport.props.vadjustment, vadjustment)
         self.assertEqual(viewport.props.hadjustment, hadjustment)
 
-    @unittest.skipIf(Gtk._version == "4.0", "not in gtk4")
+    @unittest.skipIf(Gtk_version == "4.0", "not in gtk4")
     def test_stock_lookup(self):
         stock_item = Gtk.stock_lookup('gtk-ok')
         self.assertEqual(type(stock_item), Gtk.StockItem)
@@ -655,7 +657,7 @@ class TestGtk(unittest.TestCase):
         GLib.idle_add(Gtk.main_quit, 'hello')
         Gtk.main()
 
-    @unittest.skipIf(Gtk._version == "4.0", "not in gtk4")
+    @unittest.skipIf(Gtk_version == "4.0", "not in gtk4")
     def test_widget_render_icon(self):
         button = Gtk.Button(label='OK')
         pixbuf = button.render_icon(Gtk.STOCK_OK, Gtk.IconSize.BUTTON)
@@ -663,7 +665,7 @@ class TestGtk(unittest.TestCase):
 
 
 @unittest.skipUnless(Gtk, 'Gtk not available')
-@unittest.skipIf(Gtk._version == "4.0", "not in gtk4")
+@unittest.skipIf(Gtk_version == "4.0", "not in gtk4")
 class TestWidget(unittest.TestCase):
     def test_style_get_property_gvalue(self):
         button = Gtk.Button()
@@ -2002,7 +2004,7 @@ class TestTextBuffer(unittest.TestCase):
 @unittest.skipUnless(Gtk, 'Gtk not available')
 class TestContainer(unittest.TestCase):
 
-    @unittest.skipIf(Gtk._version == "4.0", "not in gtk4")
+    @unittest.skipIf(Gtk_version == "4.0", "not in gtk4")
     def test_child_set_property(self):
         box = Gtk.Box()
         child = Gtk.Button()
@@ -2014,7 +2016,7 @@ class TestContainer(unittest.TestCase):
         box.child_get_property(child, 'padding', value)
         self.assertEqual(value.get_int(), 42)
 
-    @unittest.skipIf(Gtk._version == "4.0", "not in gtk4")
+    @unittest.skipIf(Gtk_version == "4.0", "not in gtk4")
     def test_child_get_property_gvalue(self):
         box = Gtk.Box()
         child = Gtk.Button()
@@ -2024,7 +2026,7 @@ class TestContainer(unittest.TestCase):
         box.child_get_property(child, 'padding', value)
         self.assertEqual(value.get_int(), 42)
 
-    @unittest.skipIf(Gtk._version == "4.0", "not in gtk4")
+    @unittest.skipIf(Gtk_version == "4.0", "not in gtk4")
     def test_child_get_property_return_with_explicit_gvalue(self):
         box = Gtk.Box()
         child = Gtk.Button()
@@ -2034,7 +2036,7 @@ class TestContainer(unittest.TestCase):
         result = box.child_get_property(child, 'padding', value)
         self.assertEqual(result, 42)
 
-    @unittest.skipIf(Gtk._version == "4.0", "not in gtk4")
+    @unittest.skipIf(Gtk_version == "4.0", "not in gtk4")
     def test_child_get_property_return_with_implicit_gvalue(self):
         box = Gtk.Box()
         child = Gtk.Button()
@@ -2046,14 +2048,14 @@ class TestContainer(unittest.TestCase):
     def test_child_get_property_error(self):
         box = Gtk.Box()
         child = Gtk.Button()
-        if Gtk._version == "4.0":
+        if Gtk_version == "4.0":
             box.pack_start(child, expand=False, fill=True)
         else:
             box.pack_start(child, expand=False, fill=True, padding=42)
         with self.assertRaises(ValueError):
             box.child_get_property(child, 'not-a-valid-child-property')
 
-    @unittest.skipIf(Gtk._version == "4.0", "not in gtk4")
+    @unittest.skipIf(Gtk_version == "4.0", "not in gtk4")
     def test_child_get_and_set(self):
         box = Gtk.Box()
         child = Gtk.Button()
@@ -2070,7 +2072,7 @@ class TestContainer(unittest.TestCase):
         self.assertEqual(fill, False)
         self.assertEqual(padding, 21)
 
-    @unittest.skipIf(Gtk._version != "4.0", "only in gtk4")
+    @unittest.skipIf(Gtk_version != "4.0", "only in gtk4")
     def test_child_get_and_set_gtk4(self):
         # padding got removed in gtk4
         box = Gtk.Box()


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]