Merge pull request #408 from YannickJadoul/fix-bashlex_eval-quotes
Fix quotes in command in bashlex_eval
This commit is contained in:
@@ -1,15 +1,14 @@
|
||||
import shlex
|
||||
import subprocess
|
||||
from typing import Callable, Dict, List, NamedTuple, Optional, Sequence
|
||||
|
||||
import bashlex # type: ignore
|
||||
|
||||
# a function that takes a shell command and the environment, and returns the result
|
||||
EnvironmentExecutor = Callable[[str, Dict[str, str]], str]
|
||||
# a function that takes a command and the environment, and returns the result
|
||||
EnvironmentExecutor = Callable[[List[str], Dict[str, str]], str]
|
||||
|
||||
|
||||
def local_environment_executor(command: str, env: Dict[str, str]) -> str:
|
||||
return subprocess.check_output(shlex.split(command), env=env, universal_newlines=True)
|
||||
def local_environment_executor(command: List[str], env: Dict[str, str]) -> str:
|
||||
return subprocess.check_output(command, env=env, universal_newlines=True)
|
||||
|
||||
|
||||
class NodeExecutionContext(NamedTuple):
|
||||
@@ -97,8 +96,7 @@ def evaluate_nodes_as_compound_command(nodes: Sequence[bashlex.ast.node], contex
|
||||
|
||||
|
||||
def evaluate_nodes_as_simple_command(nodes: List[bashlex.ast.node], context: NodeExecutionContext):
|
||||
words = [evaluate_node(part, context=context) for part in nodes]
|
||||
command = ' '.join(words)
|
||||
command = [evaluate_node(part, context=context) for part in nodes]
|
||||
return context.executor(command, context.environment)
|
||||
|
||||
|
||||
|
||||
@@ -180,9 +180,9 @@ class DockerContainer:
|
||||
'import sys, json, os; json.dump(os.environ.copy(), sys.stdout)'
|
||||
], capture_output=True))
|
||||
|
||||
def environment_executor(self, command: str, environment: Dict[str, str]) -> str:
|
||||
def environment_executor(self, command: List[str], environment: Dict[str, str]) -> str:
|
||||
# used as an EnvironmentExecutor to evaluate commands and capture output
|
||||
return self.call(shlex.split(command), env=environment)
|
||||
return self.call(command, env=environment)
|
||||
|
||||
|
||||
def shell_quote(path: PurePath) -> str:
|
||||
|
||||
@@ -40,7 +40,7 @@ def test_inheritance():
|
||||
|
||||
|
||||
def test_shell_eval():
|
||||
environment_recipe = parse_environment('VAR="$(echo "a test" string)"')
|
||||
environment_recipe = parse_environment('VAR="$(echo "a test" string)"')
|
||||
|
||||
env_copy = os.environ.copy()
|
||||
env_copy.pop('VAR', None)
|
||||
@@ -50,8 +50,8 @@ def test_shell_eval():
|
||||
)
|
||||
environment_cmds = environment_recipe.as_shell_commands()
|
||||
|
||||
assert environment_dict['VAR'] == 'a test string'
|
||||
assert environment_cmds == ['export VAR="$(echo "a test" string)"']
|
||||
assert environment_dict['VAR'] == 'a test string'
|
||||
assert environment_cmds == ['export VAR="$(echo "a test" string)"']
|
||||
|
||||
|
||||
def test_shell_eval_and_env():
|
||||
|
||||
Reference in New Issue
Block a user