Skip to content

Commit

Permalink
RecentFiles: Check for missing file in workflow dir
Browse files Browse the repository at this point in the history
When opening saved workflow and file widget detects missing file, check if the file is placed in the same directory as workflow is saved.
  • Loading branch information
VesnaT committed Jul 25, 2018
1 parent 459044b commit 3928461
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 7 deletions.
27 changes: 26 additions & 1 deletion Orange/widgets/data/tests/test_owfile.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Test methods with long descriptive names can omit docstrings
# pylint: disable=missing-docstring
from os import path, remove
from os import path, remove, getcwd
from unittest.mock import Mock, patch
import pickle
import tempfile
Expand All @@ -27,6 +27,7 @@
from Orange.widgets.utils.domaineditor import ComboDelegate, VarTypeDelegate, VarTableModel

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


class FailedSheetsFormat(FileFormat):
Expand Down Expand Up @@ -440,3 +441,27 @@ def test_adds_origin(self):
attrs = data1.domain["image"].attributes
self.assertIn("origin", attrs)
self.assertIn("origin1", attrs["origin"])

@patch("Orange.widgets.widget.OWWidget.workflowEnv",
Mock(return_value={"basedir": getcwd()}))
def test_open_moved_workflow(self):
"""Test opening workflow that has been moved to another location
(i.e. sent by email), considering data file is stored in the same
directory as the workflow.
"""
temp_file = tempfile.NamedTemporaryFile(dir=getcwd(), delete=False)
file_name = temp_file.name
temp_file.close()
base_name = path.basename(file_name)
try:
recent_path = RecentPath(
path.join("temp/datasets", base_name), "",
path.join("datasets", base_name)
)
stored_settings = {"recent_paths": [recent_path]}
w = self.create_widget(OWFile, stored_settings=stored_settings)
w.load_data()
self.assertEqual(w.file_combo.count(), 1)
self.assertFalse(w.Error.file_not_found.is_shown())
finally:
remove(file_name)
15 changes: 9 additions & 6 deletions Orange/widgets/utils/filedialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,14 +240,17 @@ def search(self, searchpaths):
def resolve(self, searchpaths):
if self.prefix is None and os.path.exists(self.abspath):
return self
elif self.prefix is not None:
else:
for prefix, base in searchpaths:
if self.prefix == prefix:
path = None
if self.prefix and self.prefix == prefix:
path = os.path.join(base, self.relpath)
if os.path.exists(path):
return RecentPath(
os.path.normpath(path), self.prefix, self.relpath,
file_format=self.file_format)
elif not self.prefix and prefix == "basedir":
path = os.path.join(base, self.basename)
if path and os.path.exists(path):
return RecentPath(
os.path.normpath(path), self.prefix, self.relpath,
file_format=self.file_format)
return None

@property
Expand Down
21 changes: 21 additions & 0 deletions Orange/widgets/utils/tests/test_filedialogs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import os
import unittest
from tempfile import NamedTemporaryFile

from Orange.widgets.utils.filedialogs import RecentPath


class TestRecentPath(unittest.TestCase):
def test_resolve(self):
file = NamedTemporaryFile(dir=os.getcwd(), delete=False)
file_name = file.name
base_name = os.path.basename(file_name)
try:
recent_path = RecentPath(
os.path.join("temp/datasets", base_name), "",
os.path.join("datasets", base_name)
)
search_paths = [("basedir", os.getcwd())]
self.assertIsNotNone(recent_path.resolve(search_paths))
finally:
os.remove(file_name)

0 comments on commit 3928461

Please sign in to comment.