From 6055b0df1a2587f59225eb61b48b037ab4ce7fdc Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Mon, 22 Jul 2024 21:24:30 +0200 Subject: [PATCH] Fix some SonarCloud warnings (#1138) * r * hdr * sc * StyleCop.Analyzers --- examples/WireMock.Net.Client/Program.cs | 2 + .../__admin/mappings/1.cs | 2 + .../__admin/mappings/1.cs | 2 + hdr.ps1 | 9 +-- src/Directory.Build.props | 4 ++ .../Handlebars/IHandlebarsContext.cs | 2 +- .../Handlebars/WireMockHandlebarsHelpers.cs | 68 ++++++++++++------- src/WireMock.Net/Util/StringUtils.cs | 8 +-- 8 files changed, 64 insertions(+), 33 deletions(-) diff --git a/examples/WireMock.Net.Client/Program.cs b/examples/WireMock.Net.Client/Program.cs index eb0a9a4d..b885318a 100644 --- a/examples/WireMock.Net.Client/Program.cs +++ b/examples/WireMock.Net.Client/Program.cs @@ -1,3 +1,5 @@ +// Copyright © WireMock.Net + using System; using System.Net.Http.Headers; using System.Text; diff --git a/examples/WireMock.Net.Console.NET5/__admin/mappings/1.cs b/examples/WireMock.Net.Console.NET5/__admin/mappings/1.cs index c8143e09..470713ac 100644 --- a/examples/WireMock.Net.Console.NET5/__admin/mappings/1.cs +++ b/examples/WireMock.Net.Console.NET5/__admin/mappings/1.cs @@ -1 +1,3 @@ +// Copyright © WireMock.Net + // C# Hello \ No newline at end of file diff --git a/examples/WireMock.Net.Console.NET6/__admin/mappings/1.cs b/examples/WireMock.Net.Console.NET6/__admin/mappings/1.cs index c8143e09..470713ac 100644 --- a/examples/WireMock.Net.Console.NET6/__admin/mappings/1.cs +++ b/examples/WireMock.Net.Console.NET6/__admin/mappings/1.cs @@ -1 +1,3 @@ +// Copyright © WireMock.Net + // C# Hello \ No newline at end of file diff --git a/hdr.ps1 b/hdr.ps1 index 49f265db..52fee9ac 100644 --- a/hdr.ps1 +++ b/hdr.ps1 @@ -7,11 +7,12 @@ function Write-Header ($file) { $content = Get-Content $file -Raw # Using -Raw to read the entire file as a single string $filename = Split-Path -Leaf $file - # Check if the file content starts with the auto-generated line - if ($content.TrimStart().StartsWith("// ")) { - Write-Host "Skipping auto-generated file: $filename" + if ($content.TrimStart().StartsWith($header.Trim())) { + # Write-Host "Skipping existing: $filename" + } elseif ($content.TrimStart().StartsWith("// ")) { + # Write-Host "Skipping auto-generated file: $filename" } else { - # If not an auto-generated file, prepend the header + Write-Host "Prepend the header for file: $filename" Set-Content $file $header Add-Content $file $content -NoNewline # Writing back to the file without an extra newline } diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 53beab65..4a1a7346 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -9,6 +9,10 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/src/WireMock.Net/Transformers/Handlebars/IHandlebarsContext.cs b/src/WireMock.Net/Transformers/Handlebars/IHandlebarsContext.cs index d6a5f7e2..8e9b2da6 100644 --- a/src/WireMock.Net/Transformers/Handlebars/IHandlebarsContext.cs +++ b/src/WireMock.Net/Transformers/Handlebars/IHandlebarsContext.cs @@ -4,7 +4,7 @@ using HandlebarsDotNet; namespace WireMock.Transformers.Handlebars; -interface IHandlebarsContext : ITransformerContext +internal interface IHandlebarsContext : ITransformerContext { IHandlebars Handlebars { get; } } \ No newline at end of file diff --git a/src/WireMock.Net/Transformers/Handlebars/WireMockHandlebarsHelpers.cs b/src/WireMock.Net/Transformers/Handlebars/WireMockHandlebarsHelpers.cs index cd78de47..37362bcb 100644 --- a/src/WireMock.Net/Transformers/Handlebars/WireMockHandlebarsHelpers.cs +++ b/src/WireMock.Net/Transformers/Handlebars/WireMockHandlebarsHelpers.cs @@ -1,38 +1,58 @@ // Copyright © WireMock.Net +using System; using System.Collections.Generic; +using System.Diagnostics; using System.IO; -using System.Linq; using System.Reflection; using HandlebarsDotNet; using HandlebarsDotNet.Helpers; using HandlebarsDotNet.Helpers.Helpers; using WireMock.Handlers; -namespace WireMock.Transformers.Handlebars -{ - internal static class WireMockHandlebarsHelpers - { - public static void Register(IHandlebars handlebarsContext, IFileSystemHandler fileSystemHandler) - { - // Register https://github.com/StefH/Handlebars.Net.Helpers - HandlebarsHelpers.Register(handlebarsContext, o => - { - o.CustomHelperPaths = new string[] - { - Directory.GetCurrentDirectory() -#if !NETSTANDARD1_3 - , Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) -#endif - } - .Distinct() - .ToList(); +namespace WireMock.Transformers.Handlebars; - o.CustomHelpers = new Dictionary +internal static class WireMockHandlebarsHelpers +{ + public static void Register(IHandlebars handlebarsContext, IFileSystemHandler fileSystemHandler) + { + // Register https://github.com/StefH/Handlebars.Net.Helpers + HandlebarsHelpers.Register(handlebarsContext, o => + { + var paths = new List + { + Directory.GetCurrentDirectory(), + GetBaseDirectory(), + }; + +#if !NETSTANDARD1_3_OR_GREATER + void Add(string? path, ICollection customHelperPaths) + { + if (!string.IsNullOrEmpty(path)) { - { "File", new FileHelpers(handlebarsContext, fileSystemHandler) } - }; - }); - } + customHelperPaths.Add(path!); + } + } + Add(Path.GetDirectoryName(Assembly.GetEntryAssembly()?.Location), paths); + Add(Path.GetDirectoryName(Assembly.GetCallingAssembly().Location), paths); + Add(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), paths); + Add(Path.GetDirectoryName(Process.GetCurrentProcess().MainModule?.FileName), paths); +#endif + o.CustomHelperPaths = paths; + + o.CustomHelpers = new Dictionary + { + { "File", new FileHelpers(handlebarsContext, fileSystemHandler) } + }; + }); + } + + private static string GetBaseDirectory() + { +#if NETSTANDARD1_3_OR_GREATER || NET6_0_OR_GREATER + return AppContext.BaseDirectory.TrimEnd(Path.DirectorySeparatorChar); +#else + return AppDomain.CurrentDomain.BaseDirectory.TrimEnd(Path.DirectorySeparatorChar); +#endif } } \ No newline at end of file diff --git a/src/WireMock.Net/Util/StringUtils.cs b/src/WireMock.Net/Util/StringUtils.cs index 6b982a51..5816967d 100644 --- a/src/WireMock.Net/Util/StringUtils.cs +++ b/src/WireMock.Net/Util/StringUtils.cs @@ -11,14 +11,14 @@ namespace WireMock.Util; internal static class StringUtils { private static readonly string[] ValidUriSchemes = - { + [ "ftp://", "http://", "https://" - }; + ]; private static readonly Func[] ConversionsFunctions = - { + [ s => bool.TryParse(s, out var result) ? (true, result) : (false, s), s => int.TryParse(s, out var result) ? (true, result) : (false, s), s => long.TryParse(s, out var result) ? (true, result) : (false, s), @@ -36,7 +36,7 @@ internal static class StringUtils return (false, s); } - }; + ]; public static (bool IsConverted, object ConvertedValue) TryConvertToKnownType(string value) {