Skip to content

Commit

Permalink
Python support: make sure that we ask to open a valid and existing fi…
Browse files Browse the repository at this point in the history
…le (#2178).
  • Loading branch information
agarny committed Dec 10, 2019
1 parent c744514 commit 3881439
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
12 changes: 11 additions & 1 deletion src/misc/openfile.cpp.inl
Expand Up @@ -9,7 +9,9 @@ QString cliOpenFile(const QString &pFileName, File::Type pType,
// Make sure that modes are available and that the file exists

#ifdef GUI_SUPPORT
if ((mModeTabs->count() == 0) || !QFile::exists(pFileName)) {
if ( (mModeTabs->count() == 0)
|| !QFileInfo(pFileName).isFile()
|| !QFile::exists(pFileName)) {
// Let the user know about us not being able to open the file, but only
// if we are not starting OpenCOR, i.e. only if our main window is
// visible
Expand All @@ -23,6 +25,12 @@ QString cliOpenFile(const QString &pFileName, File::Type pType,
pFileName));
}

return QObject::tr("'%1' could not be opened.").arg(pUrl.isEmpty()?
QDir::toNativeSeparators(pFileName):
pFileName);
}
#else
if (!QFileInfo(pFileName).isFile() || !QFile::exists(pFileName)) {
return QObject::tr("'%1' could not be opened.").arg(pUrl.isEmpty()?
QDir::toNativeSeparators(pFileName):
pFileName);
Expand Down Expand Up @@ -51,6 +59,8 @@ QString cliOpenFile(const QString &pFileName, File::Type pType,
return QObject::tr("'%1' could not be opened.").arg(pUrl.isEmpty()?
QDir::toNativeSeparators(pFileName):
pFileName);
// Note: we should never reach this point (since we test for the file
// existence above), but better be safe than sorry...
}

// Create a new tab, insert it just after the current tab, set the full name
Expand Down
10 changes: 8 additions & 2 deletions src/plugins/support/PythonSupport/tests/data/basictests.py
Expand Up @@ -16,9 +16,15 @@ def open_simulation(file_name_or_url):
if file_name_or_url.startswith('https://'):
return oc.open_simulation(file_name_or_url)
else:
os.chdir(os.path.dirname(__file__) + '/../../../../../..')
original_directory = os.getcwd()

return oc.open_simulation('models/tests/' + file_name_or_url)
os.chdir(os.path.dirname(__file__) + '/../../../../../../models/tests/')

simulation = oc.open_simulation(file_name_or_url)

os.chdir(original_directory)

return simulation


def print_values(data):
Expand Down
7 changes: 6 additions & 1 deletion src/plugins/support/PythonSupport/tests/data/edgetests.out
@@ -1,8 +1,13 @@
---------------------------------------
No file name or URL provided
---------------------------------------
- Open simulation
- OSError("'' could not be opened.")
---------------------------------------
Unknown local file
---------------------------------------
- Open simulation
- OSError("'models/tests/unknown' could not be opened.")
- OSError("'unknown' could not be opened.")

---------------------------------------
Unknown remote file
Expand Down
7 changes: 7 additions & 0 deletions src/plugins/support/PythonSupport/tests/data/edgetests.py
Expand Up @@ -6,6 +6,13 @@
from basictests import *

if __name__ == '__main__':
# Test for no file name or URL provided

try:
test_simulation('No file name or URL provided', '')
except Exception as e:
print(' - %s' % repr(e))

# Test for an unknown local/remote file

try:
Expand Down

0 comments on commit 3881439

Please sign in to comment.