Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enforce .bicep extension on Bicep build command #6609

Merged
merged 1 commit into from
Apr 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions src/Bicep.Cli.IntegrationTests/BuildCommandTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Bicep.Core;
using Bicep.Core.Configuration;
using Bicep.Core.FileSystem;
using Bicep.Core.Registry;
Expand All @@ -15,12 +22,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace Bicep.Cli.IntegrationTests
{
Expand All @@ -45,6 +46,21 @@ public async Task Build_ZeroFiles_ShouldFail_WithExpectedErrorMessage()
}
}

[TestMethod]
public async Task Build_NonBicepFiles_ShouldFail_WithExpectedErrorMessage()
{
var (output, error, result) = await Bicep("build", "/dev/zero");

using (new AssertionScope())
{
result.Should().Be(1);
output.Should().BeEmpty();

error.Should().NotBeEmpty();
error.Should().Contain($@"The specified input ""/dev/zero"" was not recognized as a bicep file. Bicep files must use the {LanguageConstants.LanguageFileExtension} extension.");
}
}

// TODO: handle variant linter messaging for each data test
[DataTestMethod]
[DynamicData(nameof(GetValidDataSets), DynamicDataSourceType.Method, DynamicDataDisplayNameDeclaringType = typeof(DataSet), DynamicDataDisplayName = nameof(DataSet.GetDisplayName))]
Expand Down Expand Up @@ -426,4 +442,3 @@ private static IEnumerable<object[]> GetValidDataSetsWithExternalModules() => Da
.ToDynamicTestData();
}
}

52 changes: 32 additions & 20 deletions src/Bicep.Cli/CliResources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/Bicep.Cli/CliResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,8 @@ If you would like to report any issues or inaccurate conversions, please see htt
<value>Unrecognized arguments "{0}" specified. Use "{1} --help" to view available options.</value>
<comment>{0} arguments {1} exe name</comment>
</data>
<data name="UnrecognizedFileExtensionMessage" xml:space="preserve">
<value>The specified input "{0}" was not recognized as a bicep file. Bicep files must use the .bicep extension.</value>
<comment>{0} input file path</comment>
</data>
</root>
10 changes: 9 additions & 1 deletion src/Bicep.Cli/Commands/BuildCommand.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Threading.Tasks;
using Bicep.Cli.Arguments;
using Bicep.Cli.Logging;
using Bicep.Cli.Services;
using Bicep.Core.FileSystem;
using Microsoft.Extensions.Logging;
using System.Threading.Tasks;

namespace Bicep.Cli.Commands
{
Expand Down Expand Up @@ -46,6 +46,12 @@ public async Task<int> RunAsync(BuildArguments args)
logger.LogWarning(CliResources.ResourceTypesDisclaimerMessage);
}

if (!IsBicepFile(inputPath))
{
logger.LogError(CliResources.UnrecognizedFileExtensionMessage, inputPath);
return 1;
}

var compilation = await compilationService.CompileAsync(inputPath, args.NoRestore);

if (diagnosticLogger.ErrorCount < 1)
Expand All @@ -67,5 +73,7 @@ public async Task<int> RunAsync(BuildArguments args)
// return non-zero exit code on errors
return diagnosticLogger.ErrorCount > 0 ? 1 : 0;
}

private bool IsBicepFile(string inputPath) => PathHelper.HasBicepExtension(PathHelper.FilePathToFileUrl(inputPath));
}
}