Skip to content

Commit

Permalink
Functions to include file's content as string or entire file as base64 (
Browse files Browse the repository at this point in the history
Azure#2501)

* Load Functions

LoadTextContent function loads text file content as string.
LoadFileAsBase64 function loads entire binary file as a base64 string

In tests:
IFileResolver needed to be added into Compilation - in multiple tests I
added a single class to avoid using new calls over and over (for
majority of tests it's not used anyway)

* UTF-8 BOM/no-BOM tests

* Fix Hover Handler

* Fix unit test

* Remove unnecessary gitattributes entry

* Remove micro character from asset files

* Update baselines

* Remove micro sign

* minor fixes
  • Loading branch information
miqm committed Jun 30, 2021
1 parent 2509507 commit 6d0eeaf
Show file tree
Hide file tree
Showing 164 changed files with 9,057 additions and 2,660 deletions.
10 changes: 6 additions & 4 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
*.bicep -text
*.ts text eol=lf
*.cs text eol=lf
*.sh text eol=lf
*.bicep -text
*.ts text eol=lf
*.cs text eol=lf
*.sh text eol=lf
/src/Bicep.Core.Samples/Files/**/Assets/**/*.txt -text

2 changes: 1 addition & 1 deletion src/Bicep.Cli.IntegrationTests/ProgramTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ public void LockedOutputFileShouldProduceExpectedError()

private static IEnumerable<string> GetAllDiagnostics(string bicepFilePath)
{
var syntaxTreeGrouping = SyntaxTreeGroupingBuilder.Build(new FileResolver(), new Workspace(), PathHelper.FilePathToFileUrl(bicepFilePath));
var syntaxTreeGrouping = SyntaxTreeGroupingBuilder.Build(BicepTestConstants.FileResolver, new Workspace(), PathHelper.FilePathToFileUrl(bicepFilePath));
var compilation = new Compilation(TestTypeHelper.CreateEmptyProvider(), syntaxTreeGrouping);

var output = new List<string>();
Expand Down
2 changes: 1 addition & 1 deletion src/Bicep.Cli/CommandLine/ArgumentParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ private static string GetVersionString()
var versionSplit = ThisAssembly.AssemblyInformationalVersion.Split('+');

// <major>.<minor>.<patch> (<commmithash>)
return $"{versionSplit[0]} ({(versionSplit.Length > 1 ? versionSplit[1] : "custom")})";
return $"{versionSplit[0]} ({(versionSplit.Length > 1 ? versionSplit[1] : "0000000000")})";
}

public static void PrintVersion(TextWriter writer)
Expand Down
16 changes: 10 additions & 6 deletions src/Bicep.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ private static bool LogDiagnosticsAndCheckSuccess(IDiagnosticLogger logger, Comp

private void BuildToFile(IDiagnosticLogger logger, string bicepPath, string outputPath)
{
var syntaxTreeGrouping = SyntaxTreeGroupingBuilder.Build(new FileResolver(), new Workspace(), PathHelper.FilePathToFileUrl(bicepPath));
var fileResolver = new FileResolver();
var syntaxTreeGrouping = SyntaxTreeGroupingBuilder.Build(fileResolver, new Workspace(), PathHelper.FilePathToFileUrl(bicepPath));
var compilation = new Compilation(resourceTypeProvider, syntaxTreeGrouping);

var success = LogDiagnosticsAndCheckSuccess(logger, compilation);
Expand All @@ -183,7 +184,8 @@ private void BuildToStdout(IDiagnosticLogger logger, string bicepPath)
Formatting = Formatting.Indented
};

var syntaxTreeGrouping = SyntaxTreeGroupingBuilder.Build(new FileResolver(), new Workspace(), PathHelper.FilePathToFileUrl(bicepPath));
var fileResolver = new FileResolver();
var syntaxTreeGrouping = SyntaxTreeGroupingBuilder.Build(fileResolver, new Workspace(), PathHelper.FilePathToFileUrl(bicepPath));
var compilation = new Compilation(resourceTypeProvider, syntaxTreeGrouping);

var success = LogDiagnosticsAndCheckSuccess(logger, compilation);
Expand All @@ -201,16 +203,17 @@ private int DecompileToFile(IDiagnosticLogger logger, string jsonPath, string ou
{
var jsonUri = PathHelper.FilePathToFileUrl(jsonPath);
var bicepUri = PathHelper.FilePathToFileUrl(outputPath);
var fileResolver = new FileResolver();

var (entrypointUri, filesToSave) = TemplateDecompiler.DecompileFileWithModules(resourceTypeProvider, new FileResolver(), jsonUri, bicepUri);
var (entrypointUri, filesToSave) = TemplateDecompiler.DecompileFileWithModules(resourceTypeProvider, fileResolver, jsonUri, bicepUri);
var workspace = new Workspace();
foreach (var (fileUri, bicepOutput) in filesToSave)
{
File.WriteAllText(fileUri.LocalPath, bicepOutput);
workspace.UpsertSyntaxTrees(SyntaxTree.Create(fileUri, bicepOutput).AsEnumerable());
}

var syntaxTreeGrouping = SyntaxTreeGroupingBuilder.Build(new FileResolver(), workspace, entrypointUri);
var syntaxTreeGrouping = SyntaxTreeGroupingBuilder.Build(fileResolver, workspace, entrypointUri);
var compilation = new Compilation(resourceTypeProvider, syntaxTreeGrouping);

return LogDiagnosticsAndCheckSuccess(logger, compilation) ? 0 : 1;
Expand All @@ -228,16 +231,17 @@ private int DecompileToStdout(IDiagnosticLogger logger, string jsonPath)
{
var jsonUri = PathHelper.FilePathToFileUrl(jsonPath);
var bicepUri = PathHelper.ChangeToBicepExtension(jsonUri);
var fileResolver = new FileResolver();

var (entrypointUri, filesToSave) = TemplateDecompiler.DecompileFileWithModules(resourceTypeProvider, new FileResolver(), jsonUri, bicepUri);
var (entrypointUri, filesToSave) = TemplateDecompiler.DecompileFileWithModules(resourceTypeProvider, fileResolver, jsonUri, bicepUri);
var workspace = new Workspace();
foreach (var (fileUri, bicepOutput) in filesToSave)
{
this.outputWriter.Write(bicepOutput);
workspace.UpsertSyntaxTrees(SyntaxTree.Create(fileUri, bicepOutput).AsEnumerable());
}

var syntaxTreeGrouping = SyntaxTreeGroupingBuilder.Build(new FileResolver(), workspace, entrypointUri);
var syntaxTreeGrouping = SyntaxTreeGroupingBuilder.Build(fileResolver, workspace, entrypointUri);
var compilation = new Compilation(resourceTypeProvider, syntaxTreeGrouping);

return LogDiagnosticsAndCheckSuccess(logger, compilation) ? 0 : 1;
Expand Down
7 changes: 5 additions & 2 deletions src/Bicep.Core.IntegrationTests/DecoratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
using System.Collections.Generic;
using System.Linq;
using Bicep.Core.Diagnostics;
using Bicep.Core.FileSystem;
using Bicep.Core.Semantics;
using Bicep.Core.UnitTests;
using Bicep.Core.UnitTests.Assertions;
using Bicep.Core.UnitTests.Utils;
using Bicep.Core.Analyzers.Linter.Rules;
using FluentAssertions;
using FluentAssertions.Execution;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

namespace Bicep.Core.IntegrationTests
{
Expand Down Expand Up @@ -77,7 +80,7 @@ public void ParameterDecorator_AttachedToOtherKindsOfDeclarations_CannotBeUsedAs
",
};

var compilation = new Compilation(TestTypeHelper.CreateEmptyProvider(), SyntaxTreeGroupingFactory.CreateForFiles(files, mainUri));
var compilation = new Compilation(TestTypeHelper.CreateEmptyProvider(), SyntaxTreeGroupingFactory.CreateForFiles(files, mainUri, BicepTestConstants.FileResolver));
var diagnosticsByFile = compilation.GetAllDiagnosticsBySyntaxTree().ToDictionary(kvp => kvp.Key.FileUri, kvp => kvp.Value);
var success = diagnosticsByFile.Values.SelectMany(x => x).All(d => d.Level != DiagnosticLevel.Error);

Expand Down Expand Up @@ -158,7 +161,7 @@ public void NonDecoratorFunction_AttachedToDeclaration_CannotBeUsedAsDecorator()
",
};

var compilation = new Compilation(TestTypeHelper.CreateEmptyProvider(), SyntaxTreeGroupingFactory.CreateForFiles(files, mainUri));
var compilation = new Compilation(TestTypeHelper.CreateEmptyProvider(), SyntaxTreeGroupingFactory.CreateForFiles(files, mainUri, BicepTestConstants.FileResolver));
var diagnosticsByFile = compilation.GetAllDiagnosticsBySyntaxTree().ToDictionary(kvp => kvp.Key.FileUri, kvp => kvp.Value);
var success = diagnosticsByFile.Values.SelectMany(x => x).All(d => d.Level != DiagnosticLevel.Error);

Expand Down
9 changes: 5 additions & 4 deletions src/Bicep.Core.IntegrationTests/Emit/TemplateEmitterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Bicep.Core.Workspaces;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

Expand All @@ -37,7 +38,7 @@ public void ValidBicep_TemplateEmiterShouldProduceExpectedTemplate(DataSet dataS
var compiledFilePath = FileHelper.GetResultFilePath(this.TestContext, Path.Combine(dataSet.Name, DataSet.TestFileMainCompiled));

// emitting the template should be successful
var result = this.EmitTemplate(SyntaxTreeGroupingBuilder.Build(new FileResolver(), new Workspace(), PathHelper.FilePathToFileUrl(bicepFilePath)), compiledFilePath, BicepTestConstants.DevAssemblyFileVersion);
var result = this.EmitTemplate(SyntaxTreeGroupingBuilder.Build(BicepTestConstants.FileResolver, new Workspace(), PathHelper.FilePathToFileUrl(bicepFilePath)), compiledFilePath, BicepTestConstants.DevAssemblyFileVersion);
result.Diagnostics.Should().NotHaveErrors();
result.Status.Should().Be(EmitStatus.Succeeded);

Expand All @@ -53,7 +54,7 @@ public void ValidBicep_TemplateEmiterShouldProduceExpectedTemplate(DataSet dataS
[TestMethod]
public void TemplateEmitter_output_should_not_include_UTF8_BOM()
{
var syntaxTreeGrouping = SyntaxTreeGroupingFactory.CreateFromText("");
var syntaxTreeGrouping = SyntaxTreeGroupingFactory.CreateFromText("", BicepTestConstants.FileResolver);
var compiledFilePath = FileHelper.GetResultFilePath(this.TestContext, "main.json");

// emitting the template should be successful
Expand All @@ -78,7 +79,7 @@ public void ValidBicepTextWriter_TemplateEmiterShouldProduceExpectedTemplate(Dat
MemoryStream memoryStream = new MemoryStream();

// emitting the template should be successful
var result = this.EmitTemplate(SyntaxTreeGroupingBuilder.Build(new FileResolver(), new Workspace(), PathHelper.FilePathToFileUrl(bicepFilePath)), memoryStream, BicepTestConstants.DevAssemblyFileVersion);
var result = this.EmitTemplate(SyntaxTreeGroupingBuilder.Build(BicepTestConstants.FileResolver, new Workspace(), PathHelper.FilePathToFileUrl(bicepFilePath)), memoryStream, BicepTestConstants.DevAssemblyFileVersion);
result.Diagnostics.Should().NotHaveErrors();
result.Status.Should().Be(EmitStatus.Succeeded);

Expand All @@ -103,7 +104,7 @@ public void InvalidBicep_TemplateEmiterShouldNotProduceAnyTemplate(DataSet dataS
string filePath = FileHelper.GetResultFilePath(this.TestContext, $"{dataSet.Name}_Compiled_Original.json");

// emitting the template should fail
var result = this.EmitTemplate(SyntaxTreeGroupingBuilder.Build(new FileResolver(), new Workspace(), PathHelper.FilePathToFileUrl(bicepFilePath)), filePath, BicepTestConstants.DevAssemblyFileVersion);
var result = this.EmitTemplate(SyntaxTreeGroupingBuilder.Build(BicepTestConstants.FileResolver, new Workspace(), PathHelper.FilePathToFileUrl(bicepFilePath)), filePath, BicepTestConstants.DevAssemblyFileVersion);
result.Diagnostics.Should().NotBeEmpty();
result.Status.Should().Be(EmitStatus.Failed);
}
Expand Down
20 changes: 10 additions & 10 deletions src/Bicep.Core.IntegrationTests/ModuleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void Modules_can_be_compiled_successfully()
};


var compilation = new Compilation(TestTypeHelper.CreateEmptyProvider(), SyntaxTreeGroupingFactory.CreateForFiles(files, mainUri));
var compilation = new Compilation(TestTypeHelper.CreateEmptyProvider(), SyntaxTreeGroupingFactory.CreateForFiles(files, mainUri, BicepTestConstants.FileResolver));

var (success, diagnosticsByFile) = GetSuccessAndDiagnosticsByFile(compilation);
diagnosticsByFile.Values.SelectMany(x => x).Should().BeEmpty();
Expand Down Expand Up @@ -103,7 +103,7 @@ public void Module_self_cycle_is_detected_correctly()
};


var compilation = new Compilation(TestTypeHelper.CreateEmptyProvider(), SyntaxTreeGroupingFactory.CreateForFiles(files, mainUri));
var compilation = new Compilation(TestTypeHelper.CreateEmptyProvider(), SyntaxTreeGroupingFactory.CreateForFiles(files, mainUri, BicepTestConstants.FileResolver));

var (success, diagnosticsByFile) = GetSuccessAndDiagnosticsByFile(compilation);
diagnosticsByFile[mainUri].Should().HaveDiagnostics(new[] {
Expand Down Expand Up @@ -158,7 +158,7 @@ public void Module_cycles_are_detected_correctly()
};


var compilation = new Compilation(TestTypeHelper.CreateEmptyProvider(), SyntaxTreeGroupingFactory.CreateForFiles(files, mainUri));
var compilation = new Compilation(TestTypeHelper.CreateEmptyProvider(), SyntaxTreeGroupingFactory.CreateForFiles(files, mainUri, BicepTestConstants.FileResolver));

var (success, diagnosticsByFile) = GetSuccessAndDiagnosticsByFile(compilation);
diagnosticsByFile[mainUri].Should().HaveDiagnostics(new[] {
Expand Down Expand Up @@ -219,13 +219,13 @@ public void Module_should_include_diagnostic_if_module_file_cannot_be_resolved()

var mockFileResolver = Repository.Create<IFileResolver>();
SetupFileReaderMock(mockFileResolver, mainFileUri, mainFileContents, null);
mockFileResolver.Setup(x => x.TryResolveModulePath(mainFileUri, "modulea.bicep")).Returns((Uri?)null);
mockFileResolver.Setup(x => x.TryResolveFilePath(mainFileUri, "modulea.bicep")).Returns((Uri?)null);

var compilation = new Compilation(TestTypeHelper.CreateEmptyProvider(), SyntaxTreeGroupingBuilder.Build(mockFileResolver.Object, new Workspace(), mainFileUri));

var (success, diagnosticsByFile) = GetSuccessAndDiagnosticsByFile(compilation);
diagnosticsByFile[mainFileUri].Should().HaveDiagnostics(new[] {
("BCP093", DiagnosticLevel.Error, "Module path \"modulea.bicep\" could not be resolved relative to \"/path/to/main.bicep\"."),
("BCP093", DiagnosticLevel.Error, "File path \"modulea.bicep\" could not be resolved relative to \"/path/to/main.bicep\"."),
});
}

Expand Down Expand Up @@ -294,7 +294,7 @@ public void Modules_should_have_metadata()
};


var compilation = new Compilation(TestTypeHelper.CreateEmptyProvider(), SyntaxTreeGroupingFactory.CreateForFiles(files, mainUri));
var compilation = new Compilation(TestTypeHelper.CreateEmptyProvider(), SyntaxTreeGroupingFactory.CreateForFiles(files, mainUri, BicepTestConstants.FileResolver));

var (success, diagnosticsByFile) = GetSuccessAndDiagnosticsByFile(compilation);
diagnosticsByFile.Values.SelectMany(x => x).Should().BeEmpty();
Expand Down Expand Up @@ -322,22 +322,22 @@ public void Modules_should_have_metadata()
{
[moduleAUri] = files[moduleAUri]
},
moduleAUri)), moduleATemplateHash);
moduleAUri, BicepTestConstants.FileResolver)), moduleATemplateHash);

ModuleTemplateHashValidator(
new Compilation(TestTypeHelper.CreateEmptyProvider(), SyntaxTreeGroupingFactory.CreateForFiles(new Dictionary<Uri, string>
{
[moduleBUri] = files[moduleBUri],
[moduleCUri] = files[moduleCUri]
},
moduleBUri)), moduleBTemplateHash);
moduleBUri, BicepTestConstants.FileResolver)), moduleBTemplateHash);

ModuleTemplateHashValidator(
new Compilation(TestTypeHelper.CreateEmptyProvider(), SyntaxTreeGroupingFactory.CreateForFiles(new Dictionary<Uri, string>
{
[moduleCUri] = files[moduleCUri]
},
moduleCUri)), moduleCTemplateHash);
moduleCUri, BicepTestConstants.FileResolver)), moduleCTemplateHash);
}

[TestMethod]
Expand All @@ -361,7 +361,7 @@ public void Module_should_include_diagnostic_if_module_file_cannot_be_loaded()
var mockFileResolver = Repository.Create<IFileResolver>();
SetupFileReaderMock(mockFileResolver, mainUri, mainFileContents, null);
SetupFileReaderMock(mockFileResolver, moduleAUri, null, x => x.ErrorOccurredReadingFile("Mock read failure!"));
mockFileResolver.Setup(x => x.TryResolveModulePath(mainUri, "modulea.bicep")).Returns(moduleAUri);
mockFileResolver.Setup(x => x.TryResolveFilePath(mainUri, "modulea.bicep")).Returns(moduleAUri);

var compilation = new Compilation(TestTypeHelper.CreateEmptyProvider(), SyntaxTreeGroupingBuilder.Build(mockFileResolver.Object, new Workspace(), mainUri));

Expand Down
Loading

0 comments on commit 6d0eeaf

Please sign in to comment.