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

Registry module tool improvements #8191

Merged
merged 3 commits into from
Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
using Bicep.Core.Json;
using Bicep.RegistryModuleTool.Exceptions;
using Bicep.RegistryModuleTool.ModuleFiles;
using Bicep.RegistryModuleTool.ModuleFileValidators;
using Bicep.RegistryModuleTool.ModuleValidators;
using Bicep.RegistryModuleTool.TestFixtures.MockFactories;
using FluentAssertions;
using Json.More;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO.Abstractions.TestingHelpers;

namespace Bicep.RegistryModuleTool.UnitTests.ModuleFileValidators
namespace Bicep.RegistryModuleTool.UnitTests.ModuleValidators
{
[TestClass]
public class DescriptionsValidatorTests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using Bicep.RegistryModuleTool.Exceptions;
using Bicep.RegistryModuleTool.ModuleFiles;
using Bicep.RegistryModuleTool.ModuleFileValidators;
using Bicep.RegistryModuleTool.ModuleValidators;
using Bicep.RegistryModuleTool.TestFixtures.MockFactories;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
Expand All @@ -13,7 +13,7 @@
using System.Text.RegularExpressions;
using static FluentAssertions.FluentActions;

namespace Bicep.RegistryModuleTool.UnitTests.ModuleFileValidators
namespace Bicep.RegistryModuleTool.UnitTests.ModuleValidators
{
[TestClass]
public class DiffValidatorTests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using Bicep.Core.Json;
using Bicep.RegistryModuleTool.Exceptions;
using Bicep.RegistryModuleTool.ModuleFiles;
using Bicep.RegistryModuleTool.ModuleFileValidators;
using Bicep.RegistryModuleTool.ModuleValidators;
using Bicep.RegistryModuleTool.TestFixtures.MockFactories;
using FluentAssertions;
using Json.More;
Expand All @@ -14,7 +14,7 @@
using System.Collections.Generic;
using System.IO.Abstractions.TestingHelpers;

namespace Bicep.RegistryModuleTool.UnitTests.ModuleFileValidators
namespace Bicep.RegistryModuleTool.UnitTests.ModuleValidators
{
[TestClass]
public class JsonSchemaValidatorTests
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Bicep.RegistryModuleTool.Exceptions;
using Bicep.RegistryModuleTool.ModuleValidators;
using Bicep.RegistryModuleTool.TestFixtures.MockFactories;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO.Abstractions.TestingHelpers;
using static FluentAssertions.FluentActions;

namespace Bicep.RegistryModuleTool.UnitTests.ModuleValidators
{
[TestClass]
public class ModulePathValidatorTests
{
[TestMethod]
public void ValidateModulePath_InvalidPath_Throws()
{
const string currentDirectory = "/foo/bar";
var fileSystem = new MockFileSystem();

fileSystem.AddDirectory(currentDirectory);
fileSystem.Directory.SetCurrentDirectory(fileSystem.Path.GetFullPath(currentDirectory));

Invoking(() => ModulePathValidator.ValidateModulePath(fileSystem))
.Should()
.Throw<InvalidModuleException>()
.WithMessage(@"Could not find the ""modules"" folder in the path ""C:\foo\bar"".");
shenglol marked this conversation as resolved.
Show resolved Hide resolved
}

[TestMethod]
public void ValidateModulePath_ValidPath_DoesNotThrow()
{
var fileSystem = MockFileSystemFactory.CreateFileSystemWithEmptyFolder();

Invoking(() => ModulePathValidator.ValidateModulePath(fileSystem))
.Should()
.NotThrow();
}
}
}
14 changes: 14 additions & 0 deletions src/Bicep.RegistryModuleTool/Commands/GenerateCommand.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Bicep.RegistryModuleTool.Exceptions;
using Bicep.RegistryModuleTool.Extensions;
using Bicep.RegistryModuleTool.ModuleFiles;
using Bicep.RegistryModuleTool.ModuleValidators;
using Bicep.RegistryModuleTool.Proxies;
using Microsoft.Extensions.Logging;
using System;
Expand Down Expand Up @@ -33,6 +36,17 @@ public CommandHandler(IEnvironmentProxy environmentProxy, IProcessProxy processP

protected override int Invoke(InvocationContext context)
{
try
{
ModulePathValidator.ValidateModulePath(this.FileSystem);
}
catch (InvalidModuleException exception)
{
context.Console.WriteError(exception.Message);

return 1;
}

// Read or create main Bicep file.
this.Logger.LogInformation("Ensuring {MainBicepFile} exists...", "main Bicep file");
var mainBicepFile = MainBicepFile.EnsureInFileSystem(this.FileSystem);
Expand Down
66 changes: 3 additions & 63 deletions src/Bicep.RegistryModuleTool/Commands/ValidateCommand.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Bicep.Core.Exceptions;
using Bicep.RegistryModuleTool.Exceptions;
using Bicep.RegistryModuleTool.Extensions;
using Bicep.RegistryModuleTool.ModuleFiles;
using Bicep.RegistryModuleTool.ModuleFileValidators;
using Bicep.RegistryModuleTool.ModuleValidators;
using Bicep.RegistryModuleTool.Proxies;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.IO.Abstractions;
using System.Linq;
using System.Security;

namespace Bicep.RegistryModuleTool.Commands
{
Expand Down Expand Up @@ -43,7 +39,7 @@ protected override int Invoke(InvocationContext context)
var valid = true;

this.Logger.LogInformation("Validating module path...");
valid &= Validate(context.Console, () => ValidateModulePath(this.FileSystem));
valid &= Validate(context.Console, () => ModulePathValidator.ValidateModulePath(this.FileSystem));

this.Logger.LogInformation("Validating main Bicep file...");

Expand Down Expand Up @@ -78,54 +74,6 @@ protected override int Invoke(InvocationContext context)
return valid ? 0 : 1;
}

private static void ValidateModulePath(IFileSystem fileSystem)
{
var directoryPath = fileSystem.Directory.GetCurrentDirectory();
var modulePath = GetModulePath(fileSystem, directoryPath);

if (modulePath.Count(x => x == fileSystem.Path.DirectorySeparatorChar) != 1)
{
string modulePathFormat = $"<module-folder>{fileSystem.Path.DirectorySeparatorChar}<module-name>";

throw new InvalidModuleException($"The module path \"{modulePath}\" in the path \"{directoryPath}\" is invalid. The module path must be in the format of \"{modulePathFormat}\".");
}

if (modulePath.Any(char.IsUpper))
{
throw new InvalidModuleException($"The module path \"{modulePath}\" in the path \"{directoryPath}\" is invalid. All characters in the module path must be in lowercase.");
}
}

private static string GetModulePath(IFileSystem fileSystem, string directoryPath)
{
var directoryInfo = fileSystem.DirectoryInfo.FromDirectoryName(directoryPath);
var directoryStack = new Stack<string>();

try
{

while (directoryInfo is not null && !directoryInfo.Name.Equals("modules", StringComparison.OrdinalIgnoreCase))
{
directoryStack.Push(directoryInfo.Name);

directoryInfo = directoryInfo.Parent;
}

if (directoryInfo is null)
{
throw new InvalidModuleException($"Could not find the \"modules\" folder in the path \"{directoryPath}\".");
}
}
catch (SecurityException exception)
{
throw new BicepException(exception.Message, exception);
}

var modulePath = string.Join(fileSystem.Path.DirectorySeparatorChar, directoryStack.ToArray());

return modulePath;
}

private static bool Validate(IConsole console, Action validateAction)
{
try
Expand All @@ -134,15 +82,7 @@ private static bool Validate(IConsole console, Action validateAction)
}
catch (InvalidModuleException exception)
{
// Normalize the error message to make it always end with a new line.
var normalizedErrorMessage = exception.Message.ReplaceLineEndings();

if (!normalizedErrorMessage.EndsWith(Environment.NewLine))
{
normalizedErrorMessage = $"{normalizedErrorMessage}{Environment.NewLine}";
}

console.WriteError(normalizedErrorMessage);
console.WriteError(exception.Message);

return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,21 @@ namespace Bicep.RegistryModuleTool.Exceptions
public class InvalidModuleException : Exception
{
public InvalidModuleException(string message, Exception? innerException = null)
: base(message, innerException)
: base(NormalizeLineEndings(message), innerException)
{
}

public static string NormalizeLineEndings(string message)
{
// Normalize the message to make it always end with a new line.
var normalizedMessage = message.ReplaceLineEndings();

if (!normalizedMessage.EndsWith(Environment.NewLine))
{
normalizedMessage = $"{normalizedMessage}{Environment.NewLine}";
}

return normalizedMessage;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using Bicep.Core.Extensions;
using Bicep.Core.Json;
using Bicep.RegistryModuleTool.Extensions;
using Bicep.RegistryModuleTool.ModuleFileValidators;
using Bicep.RegistryModuleTool.ModuleValidators;
using Bicep.RegistryModuleTool.Proxies;
using System;
using System.Collections.Generic;
Expand Down
2 changes: 1 addition & 1 deletion src/Bicep.RegistryModuleTool/ModuleFiles/MainBicepFile.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Bicep.RegistryModuleTool.ModuleFileValidators;
using Bicep.RegistryModuleTool.ModuleValidators;
using System.IO;
using System.IO.Abstractions;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Bicep.RegistryModuleTool.ModuleFileValidators;
using Bicep.RegistryModuleTool.ModuleValidators;
using System.IO;
using System.IO.Abstractions;

Expand Down
2 changes: 1 addition & 1 deletion src/Bicep.RegistryModuleTool/ModuleFiles/MetadataFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using Bicep.Core.Exceptions;
using Bicep.Core.Extensions;
using Bicep.Core.Json;
using Bicep.RegistryModuleTool.ModuleFileValidators;
using Bicep.RegistryModuleTool.ModuleValidators;
using System.Collections.Generic;
using System.IO;
using System.IO.Abstractions;
Expand Down
2 changes: 1 addition & 1 deletion src/Bicep.RegistryModuleTool/ModuleFiles/ModuleFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT License.

using Bicep.Core.Extensions;
using Bicep.RegistryModuleTool.ModuleFileValidators;
using Bicep.RegistryModuleTool.ModuleValidators;
using System.Collections.Generic;
using System.Linq;

Expand Down
2 changes: 1 addition & 1 deletion src/Bicep.RegistryModuleTool/ModuleFiles/ReadmeFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT License.

using Bicep.RegistryModuleTool.Extensions;
using Bicep.RegistryModuleTool.ModuleFileValidators;
using Bicep.RegistryModuleTool.ModuleValidators;
using Markdig;
using Markdig.Syntax;
using Markdig.Syntax.Inlines;
Expand Down
2 changes: 1 addition & 1 deletion src/Bicep.RegistryModuleTool/ModuleFiles/VersionFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using Bicep.Core.Exceptions;
using Bicep.Core.Extensions;
using Bicep.Core.Json;
using Bicep.RegistryModuleTool.ModuleFileValidators;
using Bicep.RegistryModuleTool.ModuleValidators;
using System.Collections.Generic;
using System.IO;
using System.IO.Abstractions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using System.Linq;
using System.Text;

namespace Bicep.RegistryModuleTool.ModuleFileValidators
namespace Bicep.RegistryModuleTool.ModuleValidators
{
public sealed class DescriptionsValidator : IModuleFileValidator
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using System.IO;
using System.IO.Abstractions;

namespace Bicep.RegistryModuleTool.ModuleFileValidators
namespace Bicep.RegistryModuleTool.ModuleValidators
{
public sealed class DiffValidator : IModuleFileValidator
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using Bicep.RegistryModuleTool.ModuleFiles;
using System;

namespace Bicep.RegistryModuleTool.ModuleFileValidators
namespace Bicep.RegistryModuleTool.ModuleValidators
{
public interface IModuleFileValidator
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
using System.Text;
using System.Text.Json;

namespace Bicep.RegistryModuleTool.ModuleFileValidators
namespace Bicep.RegistryModuleTool.ModuleValidators
{
public sealed class JsonSchemaValidator : IModuleFileValidator
{
Expand Down
Loading