-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #211 from earloc/fix/quality-gate
fix: quality-gate issues
- Loading branch information
Showing
5 changed files
with
362 additions
and
301 deletions.
There are no files selected for viewing
50 changes: 25 additions & 25 deletions
50
src/TypealizR.Tests/CLI.Tests/Abstractions/FileStorage.Tests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,25 @@ | ||
using FluentAssertions; | ||
using TypealizR.CLI.Abstractions; | ||
|
||
namespace TypealizR.Tests.CLI.Tests.Abstractions; | ||
public class FileStorage_Tests | ||
{ | ||
|
||
[Fact] | ||
public async Task CanAdd_File_With_Content() | ||
{ | ||
var sut = new FileStorage(); | ||
|
||
var fileName = $"{Guid.NewGuid()}.txt"; | ||
File.Exists(fileName).Should().BeFalse(); | ||
|
||
var expected = Guid.NewGuid().ToString(); | ||
await sut.AddAsync(fileName, expected); | ||
|
||
File.Exists(fileName).Should().BeTrue(); | ||
|
||
var actual = File.ReadAllText(fileName); | ||
actual.Should().Be(expected); | ||
} | ||
|
||
} | ||
using FluentAssertions; | ||
using TypealizR.CLI.Abstractions; | ||
|
||
namespace TypealizR.Tests.CLI.Tests.Abstractions; | ||
public class FileStorage_Tests | ||
{ | ||
|
||
[Fact] | ||
public async Task CanAdd_File_With_Content() | ||
{ | ||
var sut = new FileStorage(); | ||
|
||
var fileName = $"{Guid.NewGuid()}.txt"; | ||
File.Exists(fileName).Should().BeFalse(); | ||
|
||
var expected = Guid.NewGuid().ToString(); | ||
await sut.AddAsync(fileName, expected); | ||
|
||
File.Exists(fileName).Should().BeTrue(); | ||
|
||
var actual = await File.ReadAllTextAsync(fileName); | ||
actual.Should().Be(expected); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,84 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.IO; | ||
using System.Linq; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using TypealizR.Diagnostics; | ||
|
||
namespace TypealizR.Core; | ||
|
||
public sealed class GeneratorOptions | ||
{ | ||
public const string msBuildProjectDirectory_BuildProperty = "build_property.msbuildprojectdirectory"; | ||
public const string projectDir_BuildProperty = "build_property.projectdir"; | ||
public const string rootNamespace_BuildProperty = "build_property.rootnamespace"; | ||
|
||
public GeneratorOptions(string? projectDirectory, string rootNamespace, IDictionary<string, DiagnosticSeverity> severityConfig) | ||
{ | ||
RootNamespace = rootNamespace; | ||
SeverityConfig = severityConfig; | ||
if (projectDirectory is not null) | ||
{ | ||
ProjectDirectory = new DirectoryInfo(projectDirectory); | ||
} | ||
} | ||
|
||
public DirectoryInfo? ProjectDirectory { get; } | ||
public string RootNamespace { get; } | ||
public IDictionary<string, DiagnosticSeverity> SeverityConfig { get; } | ||
|
||
public static GeneratorOptions From(AnalyzerConfigOptions options) | ||
{ | ||
if (options is null) | ||
{ | ||
throw new ArgumentNullException(nameof(options)); | ||
} | ||
|
||
if (!options.TryGetValue(msBuildProjectDirectory_BuildProperty, out var projectDirectory)) | ||
{ | ||
options.TryGetValue(projectDir_BuildProperty, out projectDirectory); | ||
} | ||
|
||
options.TryGetValue(rootNamespace_BuildProperty, out var rootNamespace); | ||
|
||
var severityConfig = ReadSeverityConfig(options); | ||
|
||
return new( | ||
projectDirectory: projectDirectory, | ||
rootNamespace: rootNamespace ?? "", | ||
severityConfig: severityConfig | ||
); | ||
} | ||
|
||
[SuppressMessage("Globalization", "CA1308:Normalize strings to uppercase", Justification = "uppercase would not be valid")] | ||
private static IDictionary<string, DiagnosticSeverity> ReadSeverityConfig(AnalyzerConfigOptions options) | ||
{ | ||
var severityConfig = new Dictionary<string, DiagnosticSeverity>(); | ||
|
||
var availableDiagnostics = Enum.GetValues(typeof(DiagnosticsId)) | ||
.OfType<DiagnosticsId>() | ||
.Select(x => x.ToString() | ||
); | ||
|
||
foreach (var diagnostic in availableDiagnostics) | ||
{ | ||
var key = $"dotnet_diagnostic_{diagnostic.ToLowerInvariant()}_severity"; | ||
|
||
if (options.TryGetValue(key, out var rawValue)) | ||
{ | ||
if (Enum.TryParse<DiagnosticSeverity>(rawValue, true, out var severity)) | ||
{ | ||
severityConfig[diagnostic] = severity; | ||
} | ||
else | ||
{ | ||
throw new InvalidOperationException($"'{key}' has invalid value '{rawValue}'"); | ||
} | ||
} | ||
} | ||
|
||
return severityConfig; | ||
} | ||
} | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.IO; | ||
using System.Linq; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using TypealizR.Diagnostics; | ||
|
||
namespace TypealizR.Core; | ||
|
||
public sealed class GeneratorOptions | ||
{ | ||
public const string msBuildProjectDirectory_BuildProperty = "build_property.msbuildprojectdirectory"; | ||
public const string projectDir_BuildProperty = "build_property.projectdir"; | ||
public const string rootNamespace_BuildProperty = "build_property.rootnamespace"; | ||
|
||
public GeneratorOptions(string? projectDirectory, string rootNamespace, IDictionary<string, DiagnosticSeverity> severityConfig) | ||
{ | ||
RootNamespace = rootNamespace; | ||
SeverityConfig = severityConfig; | ||
if (projectDirectory is not null) | ||
{ | ||
ProjectDirectory = new DirectoryInfo(projectDirectory); | ||
} | ||
} | ||
|
||
public DirectoryInfo? ProjectDirectory { get; } | ||
public string RootNamespace { get; } | ||
public IDictionary<string, DiagnosticSeverity> SeverityConfig { get; } | ||
|
||
public static GeneratorOptions From(AnalyzerConfigOptions options) | ||
{ | ||
if (options is null) | ||
{ | ||
throw new ArgumentNullException(nameof(options)); | ||
} | ||
|
||
if (!options.TryGetValue(msBuildProjectDirectory_BuildProperty, out var projectDirectory)) | ||
{ | ||
options.TryGetValue(projectDir_BuildProperty, out projectDirectory); | ||
} | ||
|
||
options.TryGetValue(rootNamespace_BuildProperty, out var rootNamespace); | ||
|
||
var severityConfig = ReadSeverityConfig(options); | ||
|
||
return new( | ||
projectDirectory: projectDirectory, | ||
rootNamespace: rootNamespace ?? "", | ||
severityConfig: severityConfig | ||
); | ||
} | ||
|
||
[SuppressMessage("Globalization", "CA1308:Normalize strings to uppercase", Justification = "uppercase would not be valid")] | ||
private static Dictionary<string, DiagnosticSeverity> ReadSeverityConfig(AnalyzerConfigOptions options) | ||
{ | ||
var severityConfig = new Dictionary<string, DiagnosticSeverity>(); | ||
|
||
var availableDiagnostics = Enum.GetValues(typeof(DiagnosticsId)) | ||
.OfType<DiagnosticsId>() | ||
.Select(x => x.ToString() | ||
); | ||
|
||
foreach (var diagnostic in availableDiagnostics) | ||
{ | ||
var key = $"dotnet_diagnostic_{diagnostic.ToLowerInvariant()}_severity"; | ||
|
||
if (options.TryGetValue(key, out var rawValue)) | ||
{ | ||
if (Enum.TryParse<DiagnosticSeverity>(rawValue, true, out var severity)) | ||
{ | ||
severityConfig[diagnostic] = severity; | ||
} | ||
else | ||
{ | ||
throw new InvalidOperationException($"'{key}' has invalid value '{rawValue}'"); | ||
} | ||
} | ||
} | ||
|
||
return severityConfig; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,66 @@ | ||
using System;using System.Linq;using Microsoft.CodeAnalysis;using Microsoft.CodeAnalysis.CSharp;using Microsoft.CodeAnalysis.Text;using TypealizR.Extensions;namespace TypealizR.Core;internal class MemberName{ | ||
using System; | ||
using System.Linq; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.Text; | ||
using TypealizR.Extensions; | ||
|
||
namespace TypealizR.Core; | ||
internal class MemberName | ||
{ | ||
//refactor me | ||
private readonly string name; private string? nameOverride; public MemberName(string raw) { var value = new string(raw.SkipWhile(x => !x.IsValidInIdentifier(true)).ToArray()); | ||
private readonly string name; | ||
private string? nameOverride; | ||
internal static readonly char[] separator = new[] { ' ' }; | ||
|
||
public MemberName(string raw) | ||
{ | ||
var value = new string(raw.SkipWhile(x => !x.IsValidInIdentifier(true)).ToArray()); | ||
|
||
value = value.RemoveAndReplaceDuplicatesOf(" ", "@"); | ||
value = value.Replace(".", ""); | ||
|
||
value = new string( | ||
value | ||
.Trim('_') | ||
.Select((x, i) => x.IsValidInIdentifier(i == 0) ? x : ' ') | ||
.ToArray() | ||
); | ||
|
||
value = string.Join(" ", | ||
value.Split(separator, StringSplitOptions.RemoveEmptyEntries) | ||
.Where(x => !string.IsNullOrEmpty(x)) | ||
) | ||
.Replace("___", "__") | ||
.ReplaceInvalidForMemberNameWith('_'); | ||
|
||
name = value.Trim('_'); | ||
} | ||
|
||
public static implicit operator string(MemberName that) => that.nameOverride ?? that.name; | ||
public override string ToString() => nameOverride ?? name; | ||
|
||
internal bool IsValidMethodName() | ||
{ | ||
var method = SourceText.From($$""" | ||
|
||
namespace ProbingSpace; | ||
|
||
public class Programm { | ||
static void main() { | ||
} | ||
|
||
void {{name}} () {} | ||
} | ||
"""); | ||
var syntax = CSharpSyntaxTree.ParseText(method); | ||
var compilation = CSharpCompilation.Create("probe").AddSyntaxTrees(syntax); | ||
var diagnostics = compilation.GetDiagnostics(); | ||
|
||
var error = diagnostics.FirstOrDefault(x => x.Id == "CS1519"); | ||
|
||
return error == null; | ||
} | ||
|
||
value = value.RemoveAndReplaceDuplicatesOf(" ", "@"); value = value.Replace(".", ""); value = new string( value .Trim('_') .Select((x, i) => x.IsValidInIdentifier(i == 0) ? x : ' ') .ToArray() ); value = string.Join(" ", value.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries) .Where(x => !string.IsNullOrEmpty(x)) ) .Replace("___", "__") .ReplaceInvalidForMemberNameWith('_'); name = value.Trim('_'); } public static implicit operator string(MemberName that) => that.nameOverride ?? that.name; public override string ToString() => nameOverride ?? name; internal bool IsValidMethodName() { var method = SourceText.From($$""" namespace ProbingSpace; public class Programm { static void main() { } void {{name}} () {} }"""); var syntax = CSharpSyntaxTree.ParseText(method); var compilation = CSharpCompilation.Create("probe").AddSyntaxTrees(syntax); var diagnostics = compilation.GetDiagnostics(); var error = diagnostics.FirstOrDefault(x => x.Id == "CS1519"); return error == null; } internal void MakeCompilable() => nameOverride = $"_{name}";} | ||
internal void MakeCompilable() => nameOverride = $"_{name}"; | ||
} |
Oops, something went wrong.