Skip to content

Commit e485117

Browse files
authored
[ARM] az bicep lint: Add new command to lint a bicep file (#27378)
1 parent c635e02 commit e485117

File tree

6 files changed

+78
-0
lines changed

6 files changed

+78
-0
lines changed

src/azure-cli/azure/cli/command_modules/resource/_help.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3101,6 +3101,18 @@
31013101
text: az bicep generate-params --file {bicep_file} --output-format {output_format} --include-params {include_params}
31023102
"""
31033103

3104+
helps['bicep lint'] = """
3105+
type: command
3106+
short-summary: Lint a Bicep file.
3107+
examples:
3108+
- name: Lint a Bicep file.
3109+
text: az bicep lint --file {bicep_file}
3110+
- name: Lint a Bicep file without restoring external modules.
3111+
text: az bicep lint --file {bicep_file} --no-restore
3112+
- name: Lint a Bicep file with specified diagnostics format. Valid values are ( default | sarif ).
3113+
text: az bicep lint --file {bicep_file} --diagnostics-format {diagnostics_format}
3114+
"""
3115+
31043116
helps['resourcemanagement'] = """
31053117
type: group
31063118
short-summary: resourcemanagement CLI command group.

src/azure-cli/azure/cli/command_modules/resource/_params.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,11 @@ def load_arguments(self, _):
863863
c.argument('output_format', help="Set output format. Valid values are ( json | bicepparam ).")
864864
c.argument('include_params', help="Set include params. Valid values are ( all | required-only ).")
865865

866+
with self.argument_context('bicep lint') as c:
867+
c.argument('file', arg_type=bicep_file_type, help="The path to the Bicep module file to lint in the file system.")
868+
c.argument('no_restore', arg_type=bicep_no_restore_type, help="When set, generates the parameters file without restoring external modules.")
869+
c.argument('diagnostics_format', arg_type=get_enum_type(['default', 'sarif']), help="Set diagnostics format.")
870+
866871
with self.argument_context('resourcemanagement private-link create') as c:
867872
c.argument('resource_group', arg_type=resource_group_name_type,
868873
help='The name of the resource group.')

src/azure-cli/azure/cli/command_modules/resource/commands.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,7 @@ def load_command_table(self, _):
616616
g.custom_command('version', 'show_bicep_cli_version')
617617
g.custom_command('list-versions', 'list_bicep_cli_versions')
618618
g.custom_command('generate-params', 'generate_params_file')
619+
g.custom_command('lint', 'lint_bicep_file')
619620

620621
with self.command_group('resourcemanagement private-link', resource_resourcemanagementprivatelink_sdk, resource_type=ResourceType.MGMT_RESOURCE_PRIVATELINKS) as g:
621622
g.custom_command('create', 'create_resourcemanager_privatelink')

src/azure-cli/azure/cli/command_modules/resource/custom.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4456,6 +4456,24 @@ def generate_params_file(cmd, file, no_restore=None, outdir=None, outfile=None,
44564456
logger.error("az bicep generate-params could not be executed with the current version of Bicep CLI. Please upgrade Bicep CLI to v%s or later.", minimum_supported_version)
44574457

44584458

4459+
def lint_bicep_file(cmd, file, no_restore=None, diagnostics_format=None):
4460+
ensure_bicep_installation(cmd.cli_ctx)
4461+
4462+
minimum_supported_version = "0.7.4"
4463+
if bicep_version_greater_than_or_equal_to(minimum_supported_version):
4464+
args = ["lint", file]
4465+
if no_restore:
4466+
args += ["--no-restore"]
4467+
if diagnostics_format:
4468+
args += ["--diagnostics-format", diagnostics_format]
4469+
4470+
output = run_bicep_command(cmd.cli_ctx, args)
4471+
4472+
print(output)
4473+
else:
4474+
logger.error("az bicep lint could not be executed with the current version of Bicep CLI. Please upgrade Bicep CLI to v%s or later.", minimum_supported_version)
4475+
4476+
44594477
def create_resourcemanager_privatelink(
44604478
cmd, resource_group, name, location):
44614479
rcf = _resource_privatelinks_client_factory(cmd.cli_ctx)

src/azure-cli/azure/cli/command_modules/resource/tests/latest/test_resource.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5157,6 +5157,39 @@ def test_bicep_generate_params(self):
51575157
if os.path.exists(params_path):
51585158
os.remove(params_path)
51595159

5160+
class BicepLintTest(LiveScenarioTest):
5161+
def setup(self):
5162+
super().setup()
5163+
self.cmd('az bicep uninstall')
5164+
5165+
def tearDown(self):
5166+
super().tearDown()
5167+
self.cmd('az bicep uninstall')
5168+
5169+
def test_bicep_lint(self):
5170+
curr_dir = os.path.dirname(os.path.realpath(__file__))
5171+
tf = os.path.join(curr_dir, 'sample_params.bicep').replace('\\', '\\\\')
5172+
5173+
self.cmd('az bicep lint -f {tf}')
5174+
5175+
def test_bicep_lint_no_restore(self):
5176+
curr_dir = os.path.dirname(os.path.realpath(__file__))
5177+
tf = os.path.join(curr_dir, 'sample_params.bicep').replace('\\', '\\\\')
5178+
5179+
self.cmd('az bicep lint -f {tf} --no-restore')
5180+
5181+
def test_bicep_lint_diagnostics_format_default(self):
5182+
curr_dir = os.path.dirname(os.path.realpath(__file__))
5183+
tf = os.path.join(curr_dir, 'sample_params.bicep').replace('\\', '\\\\')
5184+
5185+
self.cmd('az bicep lint -f {tf} --diagnostics-format default')
5186+
5187+
def test_bicep_lint_diagnostics_format_sarif(self):
5188+
curr_dir = os.path.dirname(os.path.realpath(__file__))
5189+
tf = os.path.join(curr_dir, 'sample_params.bicep').replace('\\', '\\\\')
5190+
5191+
self.cmd('az bicep lint -f {tf} --diagnostics-format sarif')
5192+
51605193
class BicepInstallationTest(LiveScenarioTest):
51615194
def setup(self):
51625195
super().setup()

src/azure-cli/azure/cli/command_modules/resource/tests/latest/test_resource_custom.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,15 @@ def test_bicep_generate_params_include_params(self):
397397
is_generated_params_file_exists = os.path.exists(json_file)
398398
self.assertTrue(is_generated_params_file_exists)
399399

400+
@live_only()
401+
def test_bicep_lint_defaults(self):
402+
curr_dir = os.path.dirname(os.path.realpath(__file__))
403+
param_file = os.path.join(curr_dir, 'sample_params.bicep').replace('\\', '\\\\')
404+
405+
run_bicep_command(cli_ctx, ["lint", param_file])
406+
407+
self.assertTrue(param_file)
408+
400409
@live_only()
401410
def test_bicep_build_params_defaults(self):
402411
curr_dir = os.path.dirname(os.path.realpath(__file__))

0 commit comments

Comments
 (0)