|
| 1 | +# --------------------------------------------------------- |
| 2 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +# --------------------------------------------------------- |
| 4 | + |
| 5 | + |
| 6 | +import os |
| 7 | +import shlex |
| 8 | +import subprocess |
| 9 | +from unittest.mock import patch |
| 10 | + |
| 11 | +import pytest |
| 12 | + |
| 13 | +from azure.ai.ml._local_endpoints.utilities.commandline_utility import run_cli_command |
| 14 | + |
| 15 | + |
| 16 | +def _expected_command(args): |
| 17 | + return subprocess.list2cmdline(args) if os.name == "nt" else shlex.join(args) |
| 18 | + |
| 19 | + |
| 20 | +@pytest.mark.unittest |
| 21 | +class TestRunCliCommand: |
| 22 | + def test_arguments_are_quoted_before_shell_execution(self): |
| 23 | + # An argument carrying shell metacharacters (e.g. a deployment-derived path component). |
| 24 | + args = ["echo", "x$(touch pwned)"] |
| 25 | + |
| 26 | + with patch( |
| 27 | + "azure.ai.ml._local_endpoints.utilities.commandline_utility.subprocess.check_output", |
| 28 | + return_value=b"", |
| 29 | + ) as mock_check_output: |
| 30 | + run_cli_command(args) |
| 31 | + |
| 32 | + command_to_execute = mock_check_output.call_args[0][0] |
| 33 | + # The raw space-join would let the shell evaluate the $(...) substitution. |
| 34 | + assert command_to_execute != " ".join(args) |
| 35 | + assert command_to_execute == _expected_command(args) |
| 36 | + |
| 37 | + def test_plain_uri_argument_is_unchanged(self): |
| 38 | + uri = "vscode-remote://dev-container+deadbeef/var/azureml-app/onlinescoring" |
| 39 | + args = ["code", "--folder-uri", uri] |
| 40 | + |
| 41 | + with patch( |
| 42 | + "azure.ai.ml._local_endpoints.utilities.commandline_utility.subprocess.check_output", |
| 43 | + return_value=b"", |
| 44 | + ) as mock_check_output: |
| 45 | + run_cli_command(args) |
| 46 | + |
| 47 | + command_to_execute = mock_check_output.call_args[0][0] |
| 48 | + assert command_to_execute == _expected_command(args) |
| 49 | + if os.name != "nt": |
| 50 | + assert command_to_execute == f"code --folder-uri {uri}" |
0 commit comments