1919 _get_bicep_download_url ,
2020 _bicep_version_check_file_path ,
2121)
22- from azure .cli .core .azclierror import InvalidTemplateError
22+ from azure .cli .command_modules .resource .custom import (
23+ run_bicep_cli_passthrough ,
24+ snapshot_bicep_file ,
25+ )
26+ from azure .cli .core .azclierror import InvalidArgumentValueError , InvalidTemplateError
2327from azure .cli .core .mock import DummyCli
2428
2529
@@ -302,4 +306,128 @@ def test_bicep_version_greater_than_or_equal_to_use_cli_managed_binary(self, use
302306 result = bicep_version_greater_than_or_equal_to (self .cli_ctx , "0.13.2" )
303307
304308 self .assertFalse (result )
305- run_command_mock .assert_called_once_with (".azure/bin/bicep" , ["--version" ])
309+ run_command_mock .assert_called_once_with (".azure/bin/bicep" , ["--version" ])
310+
311+
312+ class TestBicepSnapshot (unittest .TestCase ):
313+ def setUp (self ):
314+ self .cli_ctx = DummyCli (random_config_dir = True )
315+ self .cmd = mock .Mock ()
316+ self .cmd .cli_ctx = self .cli_ctx
317+
318+ @mock .patch ("azure.cli.command_modules.resource.custom.run_bicep_command" )
319+ @mock .patch ("azure.cli.command_modules.resource.custom.bicep_version_greater_than_or_equal_to" )
320+ @mock .patch ("azure.cli.command_modules.resource.custom.ensure_bicep_installation" )
321+ def test_snapshot_bicep_file_passes_minimum_args (
322+ self , ensure_bicep_installation_mock , bicep_version_check_mock , run_bicep_command_mock
323+ ):
324+ bicep_version_check_mock .return_value = True
325+ run_bicep_command_mock .return_value = ""
326+
327+ snapshot_bicep_file (self .cmd , "main.bicepparam" )
328+
329+ ensure_bicep_installation_mock .assert_called_once_with (self .cli_ctx , stdout = False )
330+ bicep_version_check_mock .assert_called_once_with (self .cli_ctx , "0.41.2" )
331+ run_bicep_command_mock .assert_called_once_with (self .cli_ctx , ["snapshot" , "main.bicepparam" ])
332+
333+ @mock .patch ("azure.cli.command_modules.resource.custom.run_bicep_command" )
334+ @mock .patch ("azure.cli.command_modules.resource.custom.bicep_version_greater_than_or_equal_to" )
335+ @mock .patch ("azure.cli.command_modules.resource.custom.ensure_bicep_installation" )
336+ def test_snapshot_bicep_file_passes_all_optional_args (
337+ self , ensure_bicep_installation_mock , bicep_version_check_mock , run_bicep_command_mock
338+ ):
339+ bicep_version_check_mock .return_value = True
340+ run_bicep_command_mock .return_value = ""
341+
342+ snapshot_bicep_file (
343+ self .cmd ,
344+ "main.bicepparam" ,
345+ mode = "Validate" ,
346+ tenant_id = "tenant-id" ,
347+ subscription_id = "sub-id" ,
348+ management_group_id = "mg-id" ,
349+ location = "westus" ,
350+ resource_group = "myRg" ,
351+ deployment_name = "myDeployment" ,
352+ )
353+
354+ run_bicep_command_mock .assert_called_once_with (
355+ self .cli_ctx ,
356+ [
357+ "snapshot" ,
358+ "main.bicepparam" ,
359+ "--mode" , "Validate" ,
360+ "--tenant-id" , "tenant-id" ,
361+ "--subscription-id" , "sub-id" ,
362+ "--management-group-id" , "mg-id" ,
363+ "--location" , "westus" ,
364+ "--resource-group" , "myRg" ,
365+ "--deployment-name" , "myDeployment" ,
366+ ],
367+ )
368+
369+ @mock .patch ("azure.cli.command_modules.resource.custom.run_bicep_command" )
370+ @mock .patch ("azure.cli.command_modules.resource.custom.bicep_version_greater_than_or_equal_to" )
371+ @mock .patch ("azure.cli.command_modules.resource.custom.ensure_bicep_installation" )
372+ def test_snapshot_bicep_file_errors_when_bicep_too_old (
373+ self , ensure_bicep_installation_mock , bicep_version_check_mock , run_bicep_command_mock
374+ ):
375+ from azure .cli .core .azclierror import ValidationError
376+
377+ bicep_version_check_mock .return_value = False
378+
379+ with self .assertRaisesRegex (ValidationError , "az bicep snapshot.*0\\ .41\\ .2" ):
380+ snapshot_bicep_file (self .cmd , "main.bicepparam" )
381+
382+ run_bicep_command_mock .assert_not_called ()
383+
384+
385+ class TestBicepRun (unittest .TestCase ):
386+ def setUp (self ):
387+ self .cli_ctx = DummyCli (random_config_dir = True )
388+ self .cmd = mock .Mock ()
389+ self .cmd .cli_ctx = self .cli_ctx
390+
391+ @mock .patch ("azure.cli.command_modules.resource.custom.run_bicep_command" )
392+ @mock .patch ("azure.cli.command_modules.resource.custom.ensure_bicep_installation" )
393+ def test_run_bicep_cli_passthrough_forwards_split_args (self , ensure_bicep_installation_mock , run_bicep_command_mock ):
394+ run_bicep_command_mock .return_value = ""
395+
396+ run_bicep_cli_passthrough (self .cmd , "build main.bicep --stdout" )
397+
398+ ensure_bicep_installation_mock .assert_called_once_with (self .cli_ctx , stdout = False )
399+ run_bicep_command_mock .assert_called_once_with (
400+ self .cli_ctx , ["build" , "main.bicep" , "--stdout" ]
401+ )
402+
403+ @mock .patch ("azure.cli.command_modules.resource.custom.run_bicep_command" )
404+ @mock .patch ("azure.cli.command_modules.resource.custom.ensure_bicep_installation" )
405+ def test_run_bicep_cli_passthrough_preserves_quoted_args (self , ensure_bicep_installation_mock , run_bicep_command_mock ):
406+ run_bicep_command_mock .return_value = ""
407+
408+ run_bicep_cli_passthrough (self .cmd , 'build "path with spaces/main.bicep"' )
409+
410+ run_bicep_command_mock .assert_called_once_with (
411+ self .cli_ctx , ["build" , "path with spaces/main.bicep" ]
412+ )
413+
414+ @mock .patch ("azure.cli.command_modules.resource.custom.run_bicep_command" )
415+ @mock .patch ("azure.cli.command_modules.resource.custom.ensure_bicep_installation" )
416+ def test_run_bicep_cli_passthrough_preserves_windows_path_backslashes (self , ensure_bicep_installation_mock , run_bicep_command_mock ):
417+ run_bicep_command_mock .return_value = ""
418+
419+ # Windows paths use backslashes which collide with POSIX shell escape semantics.
420+ # The passthrough must preserve them so the Bicep CLI receives a valid path.
421+ run_bicep_cli_passthrough (self .cmd , r"build D:\azure-cli\samples\main.bicep --stdout" )
422+
423+ run_bicep_command_mock .assert_called_once_with (
424+ self .cli_ctx , ["build" , r"D:\azure-cli\samples\main.bicep" , "--stdout" ]
425+ )
426+
427+ @mock .patch ("azure.cli.command_modules.resource.custom.run_bicep_command" )
428+ @mock .patch ("azure.cli.command_modules.resource.custom.ensure_bicep_installation" )
429+ def test_run_bicep_cli_passthrough_raises_when_command_empty (self , ensure_bicep_installation_mock , run_bicep_command_mock ):
430+ with self .assertRaisesRegex (InvalidArgumentValueError , "--command must not be empty." ):
431+ run_bicep_cli_passthrough (self .cmd , " " )
432+
433+ run_bicep_command_mock .assert_not_called ()
0 commit comments