Skip to content

Commit 678e057

Browse files
jgd10akaszynski
andauthored
Added support for CWD to contain spaces (#643)
* Added support for CWD to contain spaces the cwd command accepts directory paths with spaces when enclosed in a pair of single quotes/apostrophes. However, it seems that it does NOT support directory path's that otherwise contain single quotes. I have added a note to this effect to the docstring. Otherwise this commit enables the supplication of directory paths with spaces in as well as two tests for this. The methods could (and perhaps should) be extended to other path-related commands, but I'm not sure which these are, and would welcome input from other people. Otherwise, this commit should fix issue #641. * fixed blank line problem * ignore path tests on cloud Co-authored-by: Alex Kaszynski <[email protected]>
1 parent 6c95168 commit 678e057

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

ansys/mapdl/core/_commands/session/run_controls.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ def config(self, lab="", value="", **kwargs):
113113
def cwd(self, dirpath="", **kwargs):
114114
"""Changes the current working directory.
115115
116+
``dirpath`` must not contain any singular quotations/apostrophes.
117+
These are not supported in APDL.
118+
116119
APDL Command: /CWD
117120
118121
Parameters
@@ -124,9 +127,11 @@ def cwd(self, dirpath="", **kwargs):
124127
-----
125128
After issuing the /CWD command, all new files opened with no default
126129
directory specified (via the FILE, /COPY, or RESUME commands, for
127-
example) default to the new DIRPATH directory.
130+
example) default to the new ``dirpath`` directory.
128131
"""
129-
command = "/CWD,%s" % (str(dirpath))
132+
if not (dirpath.startswith('\'') and dirpath.endswith('\'')) and "'" in dirpath:
133+
raise RuntimeError('The CWD command does not accept paths that contain singular quotes "'"")
134+
command = f"/CWD,'{dirpath}'"
130135
return self.run(command, **kwargs)
131136

132137
def filname(self, fname="", key="", **kwargs):

tests/conftest.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from pathlib import Path
2+
from collections import namedtuple
23
import os
34
import time
45

@@ -19,6 +20,9 @@
1920
# Necessary for CI plotting
2021
pyvista.OFF_SCREEN = True
2122

23+
SpacedPaths = namedtuple('SpacedPaths', ['path_without_spaces', 'path_with_spaces',
24+
'path_with_single_quote'])
25+
2226

2327
# Check if MAPDL is installed
2428
# NOTE: checks in this order to get the newest installed version
@@ -240,6 +244,14 @@ def mapdl(request, tmpdir_factory):
240244
assert not check_pid(pid)
241245

242246

247+
@pytest.fixture
248+
def path_tests(tmpdir):
249+
p1 = tmpdir.mkdir("./temp/")
250+
p2 = tmpdir.mkdir("./t e m p/")
251+
p3 = tmpdir.mkdir("./temp'")
252+
return SpacedPaths(str(p1), str(p2), str(p3))
253+
254+
243255
@pytest.fixture(scope="function")
244256
def cleared(mapdl):
245257
mapdl.finish(mute=True)

tests/test_mapdl.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020

2121
skip_in_cloud = pytest.mark.skipif(
2222
not get_start_instance(),
23-
reason="Must be able to launch MAPDL locally. Cloud does not allow create folders."
23+
reason="""
24+
Must be able to launch MAPDL locally. Remote execution does not allow for
25+
directory creation.
26+
"""
2427
)
2528

2629

@@ -819,3 +822,22 @@ def test_inval_commands_silent(mapdl, tmpdir, cleared):
819822
assert not mapdl.run("parm = 'asdf'") # assert it is not empty
820823

821824
mapdl._run('/gopr') # getting settings back
825+
826+
827+
@skip_in_cloud
828+
def test_path_without_spaces(mapdl, path_tests):
829+
resp = mapdl.cwd(path_tests.path_without_spaces)
830+
assert 'WARNING' not in resp
831+
832+
833+
@skip_in_cloud
834+
def test_path_with_spaces(mapdl, path_tests):
835+
resp = mapdl.cwd(path_tests.path_with_spaces)
836+
assert 'WARNING' not in resp
837+
838+
839+
@skip_in_cloud
840+
def test_path_with_single_quote(mapdl, path_tests):
841+
with pytest.raises(RuntimeError):
842+
resp = mapdl.cwd(path_tests.path_with_single_quote)
843+
assert 'WARNING' not in resp

0 commit comments

Comments
 (0)