Skip to content

Commit ba951fa

Browse files
authored
Merge pull request #6735 from ales-erjavec/test-owcsvimport-cleanup
[FIX] test_owcsvimport: Workaround for segfault in tests on macos
2 parents 194572b + febf466 commit ba951fa

File tree

1 file changed

+53
-40
lines changed

1 file changed

+53
-40
lines changed

Orange/widgets/data/tests/test_owcsvimport.py

Lines changed: 53 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,34 @@ def test_browse_prefix_parent(self):
283283
mb.assert_called()
284284
self.assertIsNone(widget.current_item())
285285

286+
@staticmethod
287+
@contextmanager
288+
def activate_recent_and_get_dialog(
289+
widget: OWCSVFileImport, recent_index: int = 0
290+
) -> QFileDialog:
291+
"""
292+
Activate the recent item (which MUST be missing on FS) and
293+
yield the QFileDialog which is shown.
294+
"""
295+
browse_dialog = widget._browse_dialog
296+
with mock.patch.object(widget, "_browse_dialog") as r:
297+
dlg = browse_dialog()
298+
# segfaults in tests when using 'sheet' dialog on macos when parent
299+
# is destroyed (before the dialog fully hides - animation)?
300+
dlg.setParent(None)
301+
# calling selectFile when using native (macOS) dialog does not have
302+
# an effect - at least not immediately;
303+
dlg.setOption(QFileDialog.DontUseNativeDialog)
304+
r.return_value = dlg
305+
with mock.patch.object(dlg, "open") as r:
306+
widget.activate_recent(recent_index)
307+
r.assert_called()
308+
try:
309+
yield dlg
310+
finally:
311+
dlg.deleteLater()
312+
return
313+
286314
def test_browse_for_missing(self):
287315
missing = os.path.dirname(__file__) + "/this file does not exist.csv"
288316
widget = self.create_widget(
@@ -292,19 +320,14 @@ def test_browse_for_missing(self):
292320
]
293321
}
294322
)
295-
widget.activate_recent(0)
296-
dlg = widget.findChild(QFileDialog)
297-
assert dlg is not None
298-
# calling selectFile when using native (macOS) dialog does not have
299-
# an effect - at least not immediately;
300-
dlg.setOption(QFileDialog.DontUseNativeDialog)
301-
dlg.selectFile(self.data_regions_path)
302-
dlg.accept()
303-
cur = widget.current_item()
304-
self.assertTrue(samepath(self.data_regions_path, cur.path()))
305-
self.assertEqual(
306-
self.data_regions_options.as_dict(), cur.options().as_dict()
307-
)
323+
with self.activate_recent_and_get_dialog(widget) as dlg:
324+
dlg.selectFile(self.data_regions_path)
325+
dlg.accept()
326+
cur = widget.current_item()
327+
self.assertTrue(samepath(self.data_regions_path, cur.path()))
328+
self.assertEqual(
329+
self.data_regions_options.as_dict(), cur.options().as_dict()
330+
)
308331

309332
def test_browse_for_missing_prefixed(self):
310333
path = self.data_regions_path
@@ -318,21 +341,16 @@ def test_browse_for_missing_prefixed(self):
318341
},
319342
env={"basedir": basedir}
320343
)
321-
widget.activate_recent(0)
322-
dlg = widget.findChild(QFileDialog)
323-
assert dlg is not None
324-
# calling selectFile when using native (macOS) dialog does not have
325-
# an effect - at least not immediately;
326-
dlg.setOption(QFileDialog.DontUseNativeDialog)
327-
dlg.selectFile(path)
328-
dlg.accept()
329-
cur = widget.current_item()
330-
self.assertTrue(samepath(path, cur.path()))
331-
self.assertEqual(
332-
cur.varPath(), PathItem.VarPath("basedir", "data-regions.tab"))
333-
self.assertEqual(
334-
self.data_regions_options.as_dict(), cur.options().as_dict()
335-
)
344+
with self.activate_recent_and_get_dialog(widget) as dlg:
345+
dlg.selectFile(path)
346+
dlg.accept()
347+
cur = widget.current_item()
348+
self.assertTrue(samepath(path, cur.path()))
349+
self.assertEqual(
350+
cur.varPath(), PathItem.VarPath("basedir", "data-regions.tab"))
351+
self.assertEqual(
352+
self.data_regions_options.as_dict(), cur.options().as_dict()
353+
)
336354

337355
def test_browse_for_missing_prefixed_parent(self):
338356
path = self.data_regions_path
@@ -348,18 +366,13 @@ def test_browse_for_missing_prefixed_parent(self):
348366
env={"basedir": basedir}
349367
)
350368
mb = widget._path_must_be_relative_mb = mock.Mock()
351-
widget.activate_recent(0)
352-
dlg = widget.findChild(QFileDialog)
353-
assert dlg is not None
354-
# calling selectFile when using native (macOS) dialog does not have
355-
# an effect - at least not immediately;
356-
dlg.setOption(QFileDialog.DontUseNativeDialog)
357-
dlg.selectFile(path)
358-
dlg.accept()
359-
mb.assert_called()
360-
cur = widget.current_item()
361-
self.assertEqual(item[0], cur.varPath())
362-
self.assertEqual(item[1].as_dict(), cur.options().as_dict())
369+
with self.activate_recent_and_get_dialog(widget) as dlg:
370+
dlg.selectFile(path)
371+
dlg.accept()
372+
mb.assert_called()
373+
cur = widget.current_item()
374+
self.assertEqual(item[0], cur.varPath())
375+
self.assertEqual(item[1].as_dict(), cur.options().as_dict())
363376

364377
def test_long_data(self):
365378
with tempfile.TemporaryDirectory() as tmp:

0 commit comments

Comments
 (0)