summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTodd Leonhardt <tleonhardt@gmail.com>2017-06-28 16:28:47 -0400
committerTodd Leonhardt <tleonhardt@gmail.com>2017-06-28 16:28:47 -0400
commit1931ce6013fff1804cf1658759eff80a64fb131a (patch)
tree24a81d52705e4054a77e070d4e26a962c3f77b9d
parent6ea7fc0587ba1645ee4bdd4c610480e55c4a7d96 (diff)
downloadcmd2-git-1931ce6013fff1804cf1658759eff80a64fb131a.tar.gz
Simplified implementation of do__relative_load
Also removed default argument for _relative_load and load
-rwxr-xr-xcmd2.py32
-rw-r--r--examples/scripts/nested.txt3
-rw-r--r--tests/relative_multiple.txt1
-rw-r--r--tests/script.txt1
-rw-r--r--tests/scripts/one_down.txt1
-rw-r--r--tests/test_cmd2.py9
6 files changed, 24 insertions, 23 deletions
diff --git a/cmd2.py b/cmd2.py
index 1c62d675..9a445d98 100755
--- a/cmd2.py
+++ b/cmd2.py
@@ -1759,10 +1759,10 @@ Edited files are run on close if the ``autorun_on_edit`` settable parameter is T
self.perror('Error saving {}'.format(fname))
raise
- def do__relative_load(self, arg=None):
+ def do__relative_load(self, file_path):
"""Runs commands in script file that is encoded as either ASCII or UTF-8 text.
- Usage: _relative_load [file_path]
+ Usage: _relative_load <file_path>
optional argument:
file_path a file path pointing to a script
@@ -1774,29 +1774,31 @@ relative to the already-running script's directory.
NOTE: This command is intended to only be used within text file scripts.
"""
- if arg:
- arg = arg.split(None, 1)
- targetname, args = arg[0], (arg[1:] or [''])[0]
- targetname = os.path.join(self._current_script_dir or '', targetname)
- self.do_load('%s %s' % (targetname, args))
+ # If arg is None or arg is an empty string this is an error
+ if not file_path:
+ self.perror('_relative_load command requires a file path:\n', traceback_war=False)
+ return
- def do_load(self, file_path=None):
+ file_path = file_path.strip()
+ # NOTE: Relative path is an absolute path, it is just relative to the current script directory
+ relative_path = os.path.join(self._current_script_dir or '', file_path)
+ self.do_load(relative_path)
+
+ def do_load(self, file_path):
"""Runs commands in script file that is encoded as either ASCII or UTF-8 text.
- Usage: load [file_path]
+ Usage: load <file_path>
* file_path - a file path pointing to a script
Script should contain one command per line, just like command would be typed in console.
"""
- # If arg is None or arg is an empty string, use the default filename
+ # If arg is None or arg is an empty string this is an error
if not file_path:
- targetname = self.default_file_name
- else:
- file_path = file_path.split(None, 1)
- targetname, args = file_path[0], (file_path[1:] or [''])[0].strip()
+ self.perror('load command requires a file path:\n', traceback_war=False)
+ return
- expanded_path = os.path.expanduser(targetname)
+ expanded_path = os.path.abspath(os.path.expanduser(file_path.strip()))
try:
target = open(expanded_path)
except IOError as e:
diff --git a/examples/scripts/nested.txt b/examples/scripts/nested.txt
new file mode 100644
index 00000000..9ec26160
--- /dev/null
+++ b/examples/scripts/nested.txt
@@ -0,0 +1,3 @@
+!echo "Doing a relative load"
+_relative_load script.txt
+
diff --git a/tests/relative_multiple.txt b/tests/relative_multiple.txt
new file mode 100644
index 00000000..bbd11739
--- /dev/null
+++ b/tests/relative_multiple.txt
@@ -0,0 +1 @@
+_relative_load scripts/one_down.txt
diff --git a/tests/script.txt b/tests/script.txt
index 1e18262a..4dfe9677 100644
--- a/tests/script.txt
+++ b/tests/script.txt
@@ -1,2 +1 @@
-help
help history
diff --git a/tests/scripts/one_down.txt b/tests/scripts/one_down.txt
new file mode 100644
index 00000000..b87ff844
--- /dev/null
+++ b/tests/scripts/one_down.txt
@@ -0,0 +1 @@
+_relative_load ../script.txt
diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py
index ba83e426..92fdaa14 100644
--- a/tests/test_cmd2.py
+++ b/tests/test_cmd2.py
@@ -283,18 +283,13 @@ load {}
assert out == expected
-def test_base_load_default_file(base_app, capsys):
- # TODO: Make sure to remove the 'command.txt' file in case it exists
-
+def test_base_load_with_empty_args(base_app, capsys):
# The way the load command works, we can't directly capture its stdout or stderr
run_cmd(base_app, 'load')
out, err = capsys.readouterr()
# The default file 'command.txt' doesn't exist, so we should get an error message
- expected = normalize("""ERROR: Problem accessing script from command.txt:
-[Errno 2] No such file or directory: 'command.txt'
-To enable full traceback, run the following command: 'set debug true'
-""")
+ expected = normalize("""ERROR: load command requires a file path:\n""")
assert normalize(str(err)) == expected