Skip to content

Commit df11202

Browse files
germa89akaszynski
andauthored
Fix load file in local mode (#962)
* Fixing local behaviour. * Fixing local behaviour. * Adding unit tests. * Improving unit tests * Removing coverage and adding missing decorator. * Fixing test * Removing _local overwriting. * Update tests/test_math.py Co-authored-by: Alex Kaszynski <[email protected]>
1 parent 0ef42b6 commit df11202

File tree

2 files changed

+66
-4
lines changed

2 files changed

+66
-4
lines changed

src/ansys/mapdl/core/math.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -524,18 +524,37 @@ def _load_file(self, fname):
524524
525525
"""
526526
if self._mapdl._local: # pragma: no cover
527-
if not os.path.exists(fname):
528-
raise FileNotFoundError(f"The file {fname} could not be found.")
527+
base_fname = os.path.basename(fname)
528+
if not os.path.exists(fname) and base_fname not in self._mapdl.list_files():
529+
raise FileNotFoundError(
530+
f"The file {fname} could not be found in the Python working directory ('{os.getcwd()}')"
531+
f"nor in the MAPDL working directory ('{self._mapdl.directory}')."
532+
)
533+
534+
elif os.path.exists(fname) and base_fname in self._mapdl.list_files():
535+
warn(
536+
f"The file '{base_fname} is present in both, the python working directory ('{os.getcwd()}')"
537+
"and in the MAPDL working directory ('{self._mapdl.directory}'). "
538+
"Using the one in the MAPDL directory.\n"
539+
"If you prefer to use the file in the Python directory, you can use `mapdl.upload` before this command to upload it."
540+
)
541+
542+
elif os.path.exists(fname) and base_fname not in self._mapdl.list_files():
543+
self._mapdl.upload(fname)
544+
545+
elif not os.path.exists(fname) and base_fname in self._mapdl.list_files():
546+
pass
547+
529548
else:
530549
if not os.path.exists(fname) and fname not in self._mapdl.list_files():
531550
raise FileNotFoundError(
532551
f"The file {fname} could not be found in the local client or remote working directory."
533552
)
534553
if os.path.exists(fname):
535554
self._mapdl.upload(fname)
536-
fname = os.path.basename(fname)
537555

538-
return fname
556+
# Simplifying name for MAPDL reads it.
557+
return os.path.basename(fname)
539558

540559
def stiff(self, dtype=np.double, fname="file.full", asarray=False):
541560
"""Load the stiffness matrix from a full file.

tests/test_math.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,21 @@
88
from scipy import sparse
99

1010
from ansys.mapdl.core.errors import ANSYSDataTypeError
11+
from ansys.mapdl.core.launcher import get_start_instance
1112
import ansys.mapdl.core.math as apdl_math
13+
from ansys.mapdl.core.misc import random_string
1214

1315
# skip entire module unless HAS_GRPC
1416
pytestmark = pytest.mark.skip_grpc
1517

18+
skip_in_cloud = pytest.mark.skipif(
19+
not get_start_instance(),
20+
reason="""
21+
Must be able to launch MAPDL locally. Remote execution does not allow for
22+
directory creation.
23+
""",
24+
)
25+
1626

1727
@pytest.fixture(scope="module")
1828
def mm(mapdl):
@@ -522,6 +532,39 @@ def test_repr(mm):
522532
assert mm._status == repr(mm)
523533

524534

535+
def test__load_file(mm, tmpdir): # pragma: no cover
536+
# generating dummy file
537+
# mm._mapdl._local = True # Uncomment to test locally.
538+
if not mm._mapdl._local:
539+
return True
540+
541+
fname_ = random_string() + ".file"
542+
fname = str(tmpdir.mkdir("tmpdir").join(fname_))
543+
544+
## Checking non-exists
545+
with pytest.raises(FileNotFoundError):
546+
assert fname_ == mm._load_file(fname)
547+
548+
with open(fname, "w") as fid:
549+
fid.write("# Dummy")
550+
551+
## Checking case where the file is only in python folder
552+
assert fname_ not in mm._mapdl.list_files()
553+
assert fname_ == mm._load_file(fname)
554+
assert fname_ in mm._mapdl.list_files()
555+
556+
## Checking case where the file is in both.
557+
with pytest.warns():
558+
assert fname_ == mm._load_file(fname)
559+
560+
## Checking the case where the file is only in the MAPDL folder
561+
os.remove(fname)
562+
assert fname_ == mm._load_file(fname)
563+
assert not os.path.exists(fname)
564+
assert fname_ in mm._mapdl.list_files()
565+
mm._mapdl._local = False
566+
567+
525568
def test_status(mm, capsys):
526569
assert mm.status() is None
527570
captured = capsys.readouterr()

0 commit comments

Comments
 (0)