From f249ae43c51393bc8deb4f6ec16e125a849b67dc Mon Sep 17 00:00:00 2001 From: Shenglong Li Date: Fri, 4 Apr 2025 12:33:57 -0700 Subject: [PATCH] Make sure to check user_binary_from_path --- .../cli/command_modules/resource/_bicep.py | 12 +++++--- .../cli/command_modules/resource/custom.py | 26 ++++++++--------- .../tests/latest/test_resource_bicep.py | 28 ++++++++++++++++++- .../tests/latest/test_resource_custom.py | 12 ++++---- 4 files changed, 54 insertions(+), 24 deletions(-) diff --git a/src/azure-cli/azure/cli/command_modules/resource/_bicep.py b/src/azure-cli/azure/cli/command_modules/resource/_bicep.py index bbea23a7908..ba007b06fd3 100644 --- a/src/azure-cli/azure/cli/command_modules/resource/_bicep.py +++ b/src/azure-cli/azure/cli/command_modules/resource/_bicep.py @@ -220,10 +220,14 @@ def get_bicep_latest_release_tag(): raise ClientRequestError(f"Error while attempting to retrieve the latest Bicep version: {err}.") -def bicep_version_greater_than_or_equal_to(version): - system = platform.system() - installation_path = _get_bicep_installation_path(system) - installed_version = _get_bicep_installed_version(installation_path) +def bicep_version_greater_than_or_equal_to(cli_ctx, version): + if _use_binary_from_path(cli_ctx): + installed_version = _get_bicep_installed_version("bicep") + else: + system = platform.system() + installation_path = _get_bicep_installation_path(system) + installed_version = _get_bicep_installed_version(installation_path) + parsed_version = semver.VersionInfo.parse(version) return installed_version >= parsed_version diff --git a/src/azure-cli/azure/cli/command_modules/resource/custom.py b/src/azure-cli/azure/cli/command_modules/resource/custom.py index f9fe478724e..ef1cb290cc2 100644 --- a/src/azure-cli/azure/cli/command_modules/resource/custom.py +++ b/src/azure-cli/azure/cli/command_modules/resource/custom.py @@ -1069,11 +1069,11 @@ def _parse_bicepparam_file(cmd, template_file, parameters): ensure_bicep_installation(cmd.cli_ctx, stdout=False) minimum_supported_version_bicepparam_compilation = "0.14.85" - if not bicep_version_greater_than_or_equal_to(minimum_supported_version_bicepparam_compilation): + if not bicep_version_greater_than_or_equal_to(cmd.cli_ctx, minimum_supported_version_bicepparam_compilation): raise ArgumentUsageError(f"Unable to compile .bicepparam file with the current version of Bicep CLI. Please upgrade Bicep CLI to {minimum_supported_version_bicepparam_compilation} or later.") minimum_supported_version_supplemental_param = "0.22.6" - if _get_parameter_count(parameters) > 1 and not bicep_version_greater_than_or_equal_to(minimum_supported_version_supplemental_param): + if _get_parameter_count(parameters) > 1 and not bicep_version_greater_than_or_equal_to(cmd.cli_ctx, minimum_supported_version_supplemental_param): raise ArgumentUsageError(f"Current version of Bicep CLI does not support supplemental parameters with .bicepparam file. Please upgrade Bicep CLI to {minimum_supported_version_supplemental_param} or later.") bicepparam_file = _get_bicepparam_file_path(parameters) @@ -4681,9 +4681,9 @@ def format_bicep_file(cmd, file, stdout=None, outdir=None, outfile=None, newline minimum_supported_version = "0.12.1" kebab_case_params_supported_version = "0.26.54" - if bicep_version_greater_than_or_equal_to(minimum_supported_version): + if bicep_version_greater_than_or_equal_to(cmd.cli_ctx, minimum_supported_version): args = ["format", file] - use_kebab_case_params = bicep_version_greater_than_or_equal_to(kebab_case_params_supported_version) + use_kebab_case_params = bicep_version_greater_than_or_equal_to(cmd.cli_ctx, kebab_case_params_supported_version) newline_kind = newline_kind or newline # Auto is no longer supported by Bicep formatter v2. Use LF as default. @@ -4719,26 +4719,26 @@ def publish_bicep_file(cmd, file, target, documentationUri=None, documentation_u minimum_supported_version = "0.4.1008" kebab_case_param_supported_version = "0.26.54" - if bicep_version_greater_than_or_equal_to(minimum_supported_version): + if bicep_version_greater_than_or_equal_to(cmd.cli_ctx, minimum_supported_version): args = ["publish", file, "--target", target] - use_kebab_case_params = bicep_version_greater_than_or_equal_to(kebab_case_param_supported_version) + use_kebab_case_params = bicep_version_greater_than_or_equal_to(cmd.cli_ctx, kebab_case_param_supported_version) documentation_uri = documentation_uri or documentationUri if documentation_uri: minimum_supported_version_for_documentationUri_parameter = "0.14.46" - if bicep_version_greater_than_or_equal_to(minimum_supported_version_for_documentationUri_parameter): + if bicep_version_greater_than_or_equal_to(cmd.cli_ctx, minimum_supported_version_for_documentationUri_parameter): args += ["--documentation-uri" if use_kebab_case_params else "--documentationUri", documentation_uri] else: logger.error("az bicep publish with --documentationUri/-d parameter could not be executed with the current version of Bicep CLI. Please upgrade Bicep CLI to v%s or later.", minimum_supported_version_for_documentationUri_parameter) if with_source: minimum_supported_version_for_publish_with_source = "0.23.1" - if bicep_version_greater_than_or_equal_to(minimum_supported_version_for_publish_with_source): + if bicep_version_greater_than_or_equal_to(cmd.cli_ctx, minimum_supported_version_for_publish_with_source): args += ["--with-source"] else: logger.error("az bicep publish with --with-source/-s parameter could not be executed with the current version of Bicep CLI. Please upgrade Bicep CLI to v%s or later.", minimum_supported_version_for_publish_with_source) if force: minimum_supported_version_for_publish_force = "0.17.1" - if bicep_version_greater_than_or_equal_to(minimum_supported_version_for_publish_force): + if bicep_version_greater_than_or_equal_to(cmd.cli_ctx, minimum_supported_version_for_publish_force): args += ["--force"] else: logger.error("az bicep publish with --force parameter could not be executed with the current version of Bicep CLI. Please upgrade Bicep CLI to v%s or later.", minimum_supported_version_for_publish_force) @@ -4752,7 +4752,7 @@ def restore_bicep_file(cmd, file, force=None): ensure_bicep_installation(cmd.cli_ctx) minimum_supported_version = "0.4.1008" - if bicep_version_greater_than_or_equal_to(minimum_supported_version): + if bicep_version_greater_than_or_equal_to(cmd.cli_ctx, minimum_supported_version): args = ["restore", file] if force: args += ["--force"] @@ -4772,7 +4772,7 @@ def decompileparams_bicep_file(cmd, file, bicep_file=None, outdir=None, outfile= ensure_bicep_installation(cmd.cli_ctx) minimum_supported_version = "0.18.4" - if bicep_version_greater_than_or_equal_to(minimum_supported_version): + if bicep_version_greater_than_or_equal_to(cmd.cli_ctx, minimum_supported_version): args = ["decompile-params", file] if bicep_file: args += ["--bicep-file", bicep_file] @@ -4803,7 +4803,7 @@ def generate_params_file(cmd, file, no_restore=None, outdir=None, outfile=None, ensure_bicep_installation(cmd.cli_ctx, stdout=False) minimum_supported_version = "0.7.4" - if bicep_version_greater_than_or_equal_to(minimum_supported_version): + if bicep_version_greater_than_or_equal_to(cmd.cli_ctx, minimum_supported_version): args = ["generate-params", file] if no_restore: args += ["--no-restore"] @@ -4830,7 +4830,7 @@ def lint_bicep_file(cmd, file, no_restore=None, diagnostics_format=None): ensure_bicep_installation(cmd.cli_ctx, stdout=False) minimum_supported_version = "0.7.4" - if bicep_version_greater_than_or_equal_to(minimum_supported_version): + if bicep_version_greater_than_or_equal_to(cmd.cli_ctx, minimum_supported_version): args = ["lint", file] if no_restore: args += ["--no-restore"] diff --git a/src/azure-cli/azure/cli/command_modules/resource/tests/latest/test_resource_bicep.py b/src/azure-cli/azure/cli/command_modules/resource/tests/latest/test_resource_bicep.py index d3858ccf9b5..f5bcf1d7fe3 100644 --- a/src/azure-cli/azure/cli/command_modules/resource/tests/latest/test_resource_bicep.py +++ b/src/azure-cli/azure/cli/command_modules/resource/tests/latest/test_resource_bicep.py @@ -11,6 +11,7 @@ from knack.util import CLIError from azure.cli.command_modules.resource._bicep import ( + bicep_version_greater_than_or_equal_to, ensure_bicep_installation, remove_bicep_installation, run_bicep_command, @@ -273,4 +274,29 @@ def test_get_bicep_download_url_returns_correct_urls(self): self.assertEqual(download_url, "https://downloads.bicep.azure.com/v0.26.54/bicep-win-arm64.exe") with self.assertRaises(CLIError): - _get_bicep_download_url("Made Up", "x64", "v0.26.54") \ No newline at end of file + _get_bicep_download_url("Made Up", "x64", "v0.26.54") + + @mock.patch("azure.cli.command_modules.resource._bicep._run_command") + @mock.patch("azure.cli.command_modules.resource._bicep._use_binary_from_path") + def test_bicep_version_greater_than_or_equal_to_use_binary_from_path(self, use_binary_from_path_mock, run_command_mock): + use_binary_from_path_mock.return_value = True + run_command_mock.return_value = "Bicep CLI version 0.13.1 (e3ac80d678)" + + result = bicep_version_greater_than_or_equal_to(self.cli_ctx, "0.13.1") + + self.assertTrue(result) + run_command_mock.assert_called_once_with("bicep", ["--version"]) + + + @mock.patch("azure.cli.command_modules.resource._bicep._run_command") + @mock.patch("azure.cli.command_modules.resource._bicep._get_bicep_installation_path") + @mock.patch("azure.cli.command_modules.resource._bicep._use_binary_from_path") + def test_bicep_version_greater_than_or_equal_to_use_cli_managed_binary(self, use_binary_from_path_mock, get_bicep_installation_path_mock, run_command_mock): + use_binary_from_path_mock.return_value = False + get_bicep_installation_path_mock.return_value = ".azure/bin/bicep" + run_command_mock.return_value = "Bicep CLI version 0.13.1 (e3ac80d678)" + + result = bicep_version_greater_than_or_equal_to(self.cli_ctx, "0.13.2") + + self.assertFalse(result) + run_command_mock.assert_called_once_with(".azure/bin/bicep", ["--version"]) \ No newline at end of file diff --git a/src/azure-cli/azure/cli/command_modules/resource/tests/latest/test_resource_custom.py b/src/azure-cli/azure/cli/command_modules/resource/tests/latest/test_resource_custom.py index 037482ae310..0ba7e232caf 100644 --- a/src/azure-cli/azure/cli/command_modules/resource/tests/latest/test_resource_custom.py +++ b/src/azure-cli/azure/cli/command_modules/resource/tests/latest/test_resource_custom.py @@ -671,8 +671,8 @@ def test_format_bicep_file(self, mock_print, mock_run_bicep_command, mock_bicep_ # Assert. mock_bicep_version_greater_than_or_equal_to.assert_has_calls([ - mock.call("0.12.1"), - mock.call("0.26.54"), + mock.call(cmd.cli_ctx, "0.12.1"), + mock.call(cmd.cli_ctx, "0.26.54"), ]) mock_run_bicep_command.assert_called_once_with(cmd.cli_ctx, ["format", file_path, "--stdout"]) @@ -689,7 +689,7 @@ def test_publish_withsource(self, mock_run_bicep_command, mock_bicep_version_gre # Assert. mock_bicep_version_greater_than_or_equal_to.assert_has_calls([ - mock.call("0.4.1008"), # Min version for 'bicep publish' + mock.call(cmd.cli_ctx, "0.4.1008"), # Min version for 'bicep publish' ]) mock_run_bicep_command.assert_called_once_with(cmd.cli_ctx, ['publish', file_path, '--target', 'br:contoso.azurecr.io/bicep/mymodule:v1']) @@ -705,9 +705,9 @@ def test_publish_without_source(self, mock_run_bicep_command, mock_bicep_version # Assert. mock_bicep_version_greater_than_or_equal_to.assert_has_calls([ - mock.call("0.4.1008"), # Min version for 'bicep publish' - mock.call('0.26.54'), - mock.call("0.23.1") # Min version for 'bicep publish --with-source' + mock.call(cmd.cli_ctx, "0.4.1008"), # Min version for 'bicep publish' + mock.call(cmd.cli_ctx, '0.26.54'), + mock.call(cmd.cli_ctx, "0.23.1") # Min version for 'bicep publish --with-source' ]) mock_run_bicep_command.assert_called_once_with(cmd.cli_ctx, ['publish', file_path, '--target', 'br:contoso.azurecr.io/bicep/mymodule:v1', '--with-source'])