summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTodd Leonhardt <todd.leonhardt@gmail.com>2017-07-02 12:03:53 -0400
committerGitHub <noreply@github.com>2017-07-02 12:03:53 -0400
commit02f234fc6af3e5c2d1434f1a8d52f808ff795dd4 (patch)
treea4c7489d7d2f96a588b2d262f844038e9414acbd
parent26743a702afa42ff199614def9f7c5eb04308f76 (diff)
parent91fc975ba2bf0414624aa9c263ddf441beadfa3d (diff)
downloadcmd2-git-02f234fc6af3e5c2d1434f1a8d52f808ff795dd4.tar.gz
Merge pull request #163 from python-cmd2/refactor_shell
Refactor shell command
-rw-r--r--CHANGES.md1
-rwxr-xr-xcmd2.py8
-rw-r--r--tests/test_cmd2.py4
3 files changed, 5 insertions, 8 deletions
diff --git a/CHANGES.md b/CHANGES.md
index 82b4a59e..af114bda 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -22,6 +22,7 @@ News
* ``load`` command has better error checking and reporting
* Clipboard copy and paste functionality is now handled by the **pyperclip** module
* NOTE: This adds an additional required 3rd-party dependency
+ * ``shell`` command now supports redirection and piping of output
* Added a lot of unit tests
0.7.3
diff --git a/cmd2.py b/cmd2.py
index b04bf215..87a28cd2 100755
--- a/cmd2.py
+++ b/cmd2.py
@@ -479,9 +479,7 @@ class Cmd(cmd.Cmd):
self.initial_stdout = sys.stdout
self.history = History()
self.pystate = {}
- # noinspection PyUnresolvedReferences
- self.keywords = self.reserved_words + [fname[3:] for fname in dir(self)
- if fname.startswith('do_')]
+ self.keywords = self.reserved_words + [fname[3:] for fname in dir(self) if fname.startswith('do_')]
self.parser_manager = ParserManager(redirector=self.redirector, terminators=self.terminators,
multilineCommands=self.multilineCommands,
legalChars=self.legalChars, commentGrammars=self.commentGrammars,
@@ -1138,12 +1136,12 @@ class Cmd(cmd.Cmd):
Usage: pause [text]"""
sm.input(text + '\n')
- # noinspection PyMethodMayBeStatic
def do_shell(self, command):
"""Execute a command as if at the OS prompt.
Usage: shell <command> [arguments]"""
- os.system(command)
+ proc = subprocess.Popen(command, stdout=self.stdout, stderr=sys.stderr, shell=True)
+ proc.communicate()
def path_complete(self, text, line, begidx, endidx, dir_exe_only=False, dir_only=False):
"""Method called to complete an input line by local file system path completion.
diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py
index 592e6af2..9dd2697b 100644
--- a/tests/test_cmd2.py
+++ b/tests/test_cmd2.py
@@ -103,12 +103,10 @@ To enable full traceback, run the following command: 'set debug true'
def test_base_shell(base_app, monkeypatch):
m = mock.Mock()
- monkeypatch.setattr("os.system", m)
+ monkeypatch.setattr("subprocess.Popen", m)
out = run_cmd(base_app, 'shell echo a')
assert out == []
assert m.called
- m.assert_called_with('echo a')
-
def test_base_py(base_app, capsys):
run_cmd(base_app, 'py qqq=3')