Skip to content

Commit 988e7ec

Browse files
authored
Merge pull request #3064 from VesnaT/recent_path
[ENH] RecentFiles: Check for missing file in workflow dir
2 parents 56fdfc9 + e3ba664 commit 988e7ec

File tree

3 files changed

+57
-7
lines changed

3 files changed

+57
-7
lines changed

Orange/widgets/data/tests/test_owfile.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Test methods with long descriptive names can omit docstrings
22
# pylint: disable=missing-docstring
3-
from os import path, remove
3+
from os import path, remove, getcwd
44
from unittest.mock import Mock, patch
55
import pickle
66
import tempfile
@@ -27,6 +27,7 @@
2727
from Orange.widgets.utils.domaineditor import ComboDelegate, VarTypeDelegate, VarTableModel
2828

2929
TITANIC_PATH = path.join(path.dirname(Orange.__file__), 'datasets', 'titanic.tab')
30+
orig_path_exists = path.exists
3031

3132

3233
class FailedSheetsFormat(FileFormat):
@@ -440,3 +441,27 @@ def test_adds_origin(self):
440441
attrs = data1.domain["image"].attributes
441442
self.assertIn("origin", attrs)
442443
self.assertIn("origin1", attrs["origin"])
444+
445+
@patch("Orange.widgets.widget.OWWidget.workflowEnv",
446+
Mock(return_value={"basedir": getcwd()}))
447+
def test_open_moved_workflow(self):
448+
"""Test opening workflow that has been moved to another location
449+
(i.e. sent by email), considering data file is stored in the same
450+
directory as the workflow.
451+
"""
452+
temp_file = tempfile.NamedTemporaryFile(dir=getcwd(), delete=False)
453+
file_name = temp_file.name
454+
temp_file.close()
455+
base_name = path.basename(file_name)
456+
try:
457+
recent_path = RecentPath(
458+
path.join("temp/datasets", base_name), "",
459+
path.join("datasets", base_name)
460+
)
461+
stored_settings = {"recent_paths": [recent_path]}
462+
w = self.create_widget(OWFile, stored_settings=stored_settings)
463+
w.load_data()
464+
self.assertEqual(w.file_combo.count(), 1)
465+
self.assertFalse(w.Error.file_not_found.is_shown())
466+
finally:
467+
remove(file_name)

Orange/widgets/utils/filedialogs.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -243,14 +243,17 @@ def search(self, searchpaths):
243243
def resolve(self, searchpaths):
244244
if self.prefix is None and os.path.exists(self.abspath):
245245
return self
246-
elif self.prefix is not None:
246+
else:
247247
for prefix, base in searchpaths:
248-
if self.prefix == prefix:
248+
path = None
249+
if self.prefix and self.prefix == prefix:
249250
path = os.path.join(base, self.relpath)
250-
if os.path.exists(path):
251-
return RecentPath(
252-
os.path.normpath(path), self.prefix, self.relpath,
253-
file_format=self.file_format)
251+
elif not self.prefix and prefix == "basedir":
252+
path = os.path.join(base, self.basename)
253+
if path and os.path.exists(path):
254+
return RecentPath(
255+
os.path.normpath(path), self.prefix, self.relpath,
256+
file_format=self.file_format)
254257
return None
255258

256259
@property
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import os
2+
import unittest
3+
from tempfile import NamedTemporaryFile
4+
5+
from Orange.widgets.utils.filedialogs import RecentPath
6+
7+
8+
class TestRecentPath(unittest.TestCase):
9+
def test_resolve(self):
10+
temp_file = NamedTemporaryFile(dir=os.getcwd(), delete=False)
11+
file_name = temp_file.name
12+
temp_file.close()
13+
base_name = os.path.basename(file_name)
14+
try:
15+
recent_path = RecentPath(
16+
os.path.join("temp/datasets", base_name), "",
17+
os.path.join("datasets", base_name)
18+
)
19+
search_paths = [("basedir", os.getcwd())]
20+
self.assertIsNotNone(recent_path.resolve(search_paths))
21+
finally:
22+
os.remove(file_name)

0 commit comments

Comments
 (0)