forked from MetOffice/fab
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
124 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
############################################################################## | ||
# (c) Crown copyright Met Office. All rights reserved. | ||
# For further details please refer to the file COPYRIGHT | ||
# which you should have received as part of this distribution | ||
############################################################################## | ||
|
||
"""This file contains a base class for shells. This can be used to execute | ||
other scripts. | ||
""" | ||
|
||
from pathlib import Path | ||
from typing import List, Union | ||
|
||
from fab.tools.category import Category | ||
from fab.tools.tool import Tool | ||
|
||
|
||
class Shell(Tool): | ||
'''A simple wrapper that runs a shell script. There seems to be no | ||
consistent way to simply check if a shell is working - not only support | ||
a version command (e.g. sh and dash don't). Instead, availability | ||
is tested by running a simple 'echo' command. | ||
:name: the path to the script to run. | ||
''' | ||
def __init__(self, name: str): | ||
super().__init__(name=name, exec_name=name, | ||
availability_option=["-c", "echo hello"], | ||
category=Category.SHELL) | ||
|
||
def exec(self, command: Union[str, List[Union[Path, str]]]) -> str: | ||
'''Executes the specified command. | ||
:param command: the command and potential parameters to execute. | ||
:returns: stdout of the result. | ||
''' | ||
if isinstance(command, str): | ||
params = ["-c", command] | ||
else: | ||
params = ["-c"] | ||
params.extend(command) | ||
return super().run(additional_parameters=params, | ||
capture_output=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
############################################################################## | ||
# (c) Crown copyright Met Office. All rights reserved. | ||
# For further details please refer to the file COPYRIGHT | ||
# which you should have received as part of this distribution | ||
############################################################################## | ||
|
||
'''Tests the shell implementation. | ||
''' | ||
|
||
from unittest import mock | ||
|
||
from fab.tools import Category, Shell | ||
|
||
|
||
def test_shell_constructor(): | ||
'''Test the Shell constructor.''' | ||
bash = Shell("bash") | ||
assert bash.category == Category.SHELL | ||
assert bash.name == "bash" | ||
assert bash.exec_name == "bash" | ||
|
||
|
||
def test_shell_check_available(): | ||
'''Tests the is_available functionality.''' | ||
bash = Shell("bash") | ||
mock_result = mock.Mock(returncode=0) | ||
with mock.patch('fab.tools.tool.subprocess.run', | ||
return_value=mock_result) as tool_run: | ||
assert bash.check_available() | ||
tool_run.assert_called_once_with( | ||
["bash", "-c", "echo hello"], capture_output=True, env=None, | ||
cwd=None, check=False) | ||
|
||
# Test behaviour if a runtime error happens: | ||
with mock.patch("fab.tools.tool.Tool.run", | ||
side_effect=RuntimeError("")) as tool_run: | ||
assert not bash.check_available() | ||
|
||
|
||
def test_shell_exec_single_arg(): | ||
'''Test running a shell script without additional parameters.''' | ||
bash = Shell("ksh") | ||
mock_result = mock.Mock(returncode=0) | ||
with mock.patch('fab.tools.tool.subprocess.run', | ||
return_value=mock_result) as tool_run: | ||
bash.exec("echo") | ||
tool_run.assert_called_with(['ksh', '-c', 'echo'], | ||
capture_output=True, env=None, cwd=None, | ||
check=False) | ||
|
||
|
||
def test_shell_exec_multiple_args(): | ||
'''Test running a shell script with parameters.''' | ||
bash = Shell("ksh") | ||
mock_result = mock.Mock(returncode=0) | ||
with mock.patch('fab.tools.tool.subprocess.run', | ||
return_value=mock_result) as tool_run: | ||
bash.exec(["some", "shell", "function"]) | ||
tool_run.assert_called_with(['ksh', '-c', 'some', 'shell', 'function'], | ||
capture_output=True, env=None, cwd=None, | ||
check=False) |