diff --git a/src/ByReplace.Test/Analyzers/AnalyzerRunnerTest.cs b/src/ByReplace.Test/Analyzers/AnalyzerRunnerTest.cs new file mode 100644 index 0000000..38223ce --- /dev/null +++ b/src/ByReplace.Test/Analyzers/AnalyzerRunnerTest.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ByReplace.Test.Analyzers; + +internal class AnalyzerRunnerTest +{ +} diff --git a/src/ByReplace.Test/Analyzers/AnalyzersAndFixersTest.cs b/src/ByReplace.Test/Analyzers/AnalyzersAndFixersTest.cs new file mode 100644 index 0000000..7cf481f --- /dev/null +++ b/src/ByReplace.Test/Analyzers/AnalyzersAndFixersTest.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ByReplace.Test.Analyzers; + +internal class AnalyzersAndFixersTest +{ +} diff --git a/src/ByReplace.Test/Analyzers/AnalyzersTest.cs b/src/ByReplace.Test/Analyzers/AnalyzersTest.cs new file mode 100644 index 0000000..d106711 --- /dev/null +++ b/src/ByReplace.Test/Analyzers/AnalyzersTest.cs @@ -0,0 +1,69 @@ +using ByReplace.Analyzers; +using ByReplace.Builders; +using ByReplace.Models; +using ByReplace.Printers; +using ByReplace.Test.Common.FolderMock; +using Moq; +using Xunit; + +namespace ByReplace.Test.Analyzers; + +public class AnalyzersTest +{ + private readonly PathCompilationSyntax _pathCompilationSyntax; + private readonly BrConfiguration _brConfiguration; + private readonly Mock _printMock; + + public AnalyzersTest() + { + var levelOne = FolderSyntax + .FolderDeclaration("OneLevel") + .AddMenbers(FileSyntax.FileDeclaration("FileOne.cs", "ITest = new Test()")); + + var SecondOne = FolderSyntax.FolderDeclaration("SecondLevel") + .AddParent(levelOne) + .AddMenbers(FileSyntax.FileDeclaration("FileSecond.cs", "ITest2 = new Test()")); + + _pathCompilationSyntax = PathFactory + .Compile(nameof(AnalyzersTest)) + .AddFolders(SecondOne) + .CreateThreeFolder(); + + _brConfiguration = BrConfigurationBuilder + .Instantiate() + .SetPath($"./{_pathCompilationSyntax.InternalIdentifier}") + .SetConfigPath($"./{_pathCompilationSyntax.InternalIdentifier}") + .Build(); + + _printMock = new Mock(); + } + + [Fact] + public void LoadThreeFiles_MapAllSourceThreeOfDirectory_ShouldReturnSourceFileThree() + { + // Arrange + var analyzer = new Analyzer(_brConfiguration, _printMock.Object); + + // Act + var directoryNodes = analyzer.LoadThreeFiles(); + + // Assert + Assert.Equal(3, directoryNodes.Count); + Assert.Single(directoryNodes[0].Files); + Assert.Single(directoryNodes[1].Files); + Assert.Single(directoryNodes[2].Files); + } + + [Fact] + public void LoadThreeFiles_WhenPrintLogInformation_ShouldValidateLogWasCalled() + { + // Arrange + var analyzer = new Analyzer(_brConfiguration, _printMock.Object); + + // Act + var directoryNodes = analyzer.LoadThreeFiles(); + + // Assert + _printMock.Verify(x => x.Information("Identifying folder three files."), Times.Once); + } +} diff --git a/src/ByReplace.Test/Analyzers/DocumentFixTest.cs b/src/ByReplace.Test/Analyzers/DocumentFixTest.cs new file mode 100644 index 0000000..458c27c --- /dev/null +++ b/src/ByReplace.Test/Analyzers/DocumentFixTest.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ByReplace.Test.Analyzers; + +internal class DocumentFixTest +{ +} diff --git a/src/ByReplace.Test/ByReplace.Test.csproj b/src/ByReplace.Test/ByReplace.Test.csproj index 999785f..4e6cabd 100644 --- a/src/ByReplace.Test/ByReplace.Test.csproj +++ b/src/ByReplace.Test/ByReplace.Test.csproj @@ -1,7 +1,7 @@ - + - net6.0 + net8.0 enable enable @@ -13,9 +13,12 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - - - + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive all @@ -25,4 +28,18 @@ + + + + + + + + + + + Always + + + diff --git a/src/ByReplace.Test/Common/ConfigMock/ConfigFactory.cs b/src/ByReplace.Test/Common/ConfigMock/ConfigFactory.cs new file mode 100644 index 0000000..43e8aa3 --- /dev/null +++ b/src/ByReplace.Test/Common/ConfigMock/ConfigFactory.cs @@ -0,0 +1,6 @@ +namespace ByReplace.Test.Common.ConfigMock; + +public class ConfigFactory +{ + +} \ No newline at end of file diff --git a/src/ByReplace.Test/Common/FolderMock/FileSyntax.cs b/src/ByReplace.Test/Common/FolderMock/FileSyntax.cs new file mode 100644 index 0000000..cead8e9 --- /dev/null +++ b/src/ByReplace.Test/Common/FolderMock/FileSyntax.cs @@ -0,0 +1,30 @@ +namespace ByReplace.Test.Common.FolderMock; + +internal sealed class FileSyntax +{ + public FileSyntax(string name) + { + Name = name; + } + + public FileSyntax(string name, string content) + { + Name = name; + Content = content; + } + + public string Name { get; } + public string Content { get; } + + public string Extension => System.IO.Path.GetExtension(Name); + + public static FileSyntax FileDeclaration(string name) + { + return new FileSyntax(name); + } + + public static FileSyntax FileDeclaration(string name, string content) + { + return new FileSyntax(name, content); + } +} diff --git a/src/ByReplace.Test/Common/FolderMock/FolderSyntax.cs b/src/ByReplace.Test/Common/FolderMock/FolderSyntax.cs new file mode 100644 index 0000000..bb4645f --- /dev/null +++ b/src/ByReplace.Test/Common/FolderMock/FolderSyntax.cs @@ -0,0 +1,57 @@ +namespace ByReplace.Test.Common.FolderMock; + +internal sealed class FolderSyntax +{ + public FolderSyntax(string name) + { + Name = name; + Files = new List(); + } + + public FolderSyntax(string name, FolderSyntax parent) + { + Name = name; + Parent = parent; + Files = new List(); + } + + public FolderSyntax(string name, FolderSyntax parent, List files) + { + Name = name; + Parent = parent; + Files = files; + } + + public FolderSyntax Parent { get; private set; } + + public string Name { get; private set; } + + public List Files { get; private set; } + + public static FolderSyntax FolderDeclaration(string name) + { + return new FolderSyntax(name); + } + + public static FolderSyntax FolderDeclaration(string name, FolderSyntax parent) + { + return new FolderSyntax(name, parent); + } + + public static FolderSyntax FolderDeclaration(string name, FolderSyntax parent, List files) + { + return new FolderSyntax(name, parent, files); + } + + public FolderSyntax AddMenbers(FileSyntax fileSyntax) + { + this.Files.Add(fileSyntax); + return this; + } + + public FolderSyntax AddParent(FolderSyntax parent) + { + this.Parent = parent; + return this; + } +} diff --git a/src/ByReplace.Test/Common/FolderMock/PathCompilationSyntax.cs b/src/ByReplace.Test/Common/FolderMock/PathCompilationSyntax.cs new file mode 100644 index 0000000..4fda21d --- /dev/null +++ b/src/ByReplace.Test/Common/FolderMock/PathCompilationSyntax.cs @@ -0,0 +1,82 @@ +using System.Runtime.InteropServices; +namespace ByReplace.Test.Common.FolderMock; + +internal sealed class PathCompilationSyntax +{ + public List Folders = new List(); + + public string InternalIdentifier { get; private set; } + + public PathCompilationSyntax() + { + InternalIdentifier = Guid.NewGuid().ToString(); + } + + public PathCompilationSyntax(string testCase) + { + InternalIdentifier = $"{testCase}_{Guid.NewGuid()}"; + } + + public PathCompilationSyntax AddFolders(params FolderSyntax[] foldersSyntax) + { + this.Folders.AddRange(foldersSyntax); + + return this; + } + + public PathCompilationSyntax AddFolder(FolderSyntax folderSyntax) + { + this.Folders.Add(folderSyntax); + + return this; + } + + public PathCompilationSyntax AddFolder(string name) + { + this.Folders.Add(new FolderSyntax(name)); + + return this; + } + + public PathCompilationSyntax CreateThreeFolder() + { + foreach (ref var folder in CollectionsMarshal.AsSpan(Folders)) + { + CreateThreeFolder(folder); + } + + //TODO: Código temporário até o ConfigFactory ser implementado. + File.Copy("./brconfig.json", $"./{InternalIdentifier}/brconfig.json", true); + + return this; + } + + public PathCompilationSyntax CreateThreeFolder(FolderSyntax folderSyntax) + { + if (folderSyntax is null) + { + return this; + } + + if (folderSyntax.Parent is not null) + { + CreateThreeFolder(folderSyntax.Parent); + } + + var dirPath = folderSyntax.Parent is not null + ? $"./{InternalIdentifier}/{folderSyntax.Parent.Name}/{folderSyntax.Name}" + : $"./{InternalIdentifier}/{folderSyntax.Name}"; + + if (!Directory.Exists(dirPath)) + { + Directory.CreateDirectory(dirPath); + } + + foreach (ref var file in CollectionsMarshal.AsSpan(folderSyntax.Files)) + { + File.WriteAllText($"{dirPath}/{file.Name}", file.Content); + } + + return this; + } +} diff --git a/src/ByReplace.Test/Common/FolderMock/PathFactory.cs b/src/ByReplace.Test/Common/FolderMock/PathFactory.cs new file mode 100644 index 0000000..39c8349 --- /dev/null +++ b/src/ByReplace.Test/Common/FolderMock/PathFactory.cs @@ -0,0 +1,7 @@ +namespace ByReplace.Test.Common.FolderMock; + +internal sealed class PathFactory +{ + public static PathCompilationSyntax Compile() => new PathCompilationSyntax(); + public static PathCompilationSyntax Compile(string testCase) => new PathCompilationSyntax(testCase); +} diff --git a/src/ByReplace.Test/brconfig.json b/src/ByReplace.Test/brconfig.json new file mode 100644 index 0000000..e3d9a00 --- /dev/null +++ b/src/ByReplace.Test/brconfig.json @@ -0,0 +1,16 @@ +{ + "Path": "C:\\Users\\iel_1\\Documents\\TestLieu", + "SkipDirectories": [ "bin", ".vs", "object", "git" ], + "Rules": [ + { + "Name": "RemoveServiceBus", + "Description": "Substitui a implementação da mensageria para AwsEventBroker", + "Skip": [ "Startup.cs", "Program.cs", "**/Controllers/*" ], + "Extensions": [ ".cs", ".json" ], + "Replacement": { + "Old": [ "this._eventBus", "this.eventBus" ], + "New": "this._awsEventBroker" + } + } + ] +} diff --git a/src/ByReplace/Analyzers/Analyzer.cs b/src/ByReplace/Analyzers/Analyzer.cs index 2678198..5d91ebd 100644 --- a/src/ByReplace/Analyzers/Analyzer.cs +++ b/src/ByReplace/Analyzers/Analyzer.cs @@ -1,4 +1,6 @@ -namespace ByReplace.Analyzers; +[assembly: InternalsVisibleTo("ByReplace.Test")] + +namespace ByReplace.Analyzers; internal class Analyzer { @@ -13,7 +15,7 @@ public Analyzer(BrConfiguration brConfiguration, IPrint print) internal ImmutableList LoadThreeFiles() { - print.Information($"Identifying folder three files."); + print.Information("Identifying folder three files."); DirectoryThree directoryThree = new DirectoryThree(brConfiguration.Path); directoryThree.MapThreeSources(); diff --git a/src/ByReplace/Analyzers/AnalyzerRunner.cs b/src/ByReplace/Analyzers/AnalyzerRunner.cs index 2393f5d..4ccaa98 100644 --- a/src/ByReplace/Analyzers/AnalyzerRunner.cs +++ b/src/ByReplace/Analyzers/AnalyzerRunner.cs @@ -1,4 +1,6 @@ -namespace ByReplace.Analyzers; +[assembly: InternalsVisibleTo("ByReplace.Test")] + +namespace ByReplace.Analyzers; internal class AnalyzerRunner { @@ -13,7 +15,7 @@ internal AnalyzerRunner(BrConfiguration brConfiguration, IPrint print) internal AnalyzersAndFixers RunAnalysis(ImmutableList directoryThree, Analyses diagnostic) { - print.Information($"Identifying folder three files."); + print.Information("Identifying rules that has matches."); AnalyzersAndFixers analyzersAndFixers = new AnalyzersAndFixers(this.print); diff --git a/src/ByReplace/Analyzers/AnalyzersAndFixers.cs b/src/ByReplace/Analyzers/AnalyzersAndFixers.cs index 292e810..b4aa2ee 100644 --- a/src/ByReplace/Analyzers/AnalyzersAndFixers.cs +++ b/src/ByReplace/Analyzers/AnalyzersAndFixers.cs @@ -1,4 +1,6 @@ -namespace ByReplace.Analyzers; +[assembly: InternalsVisibleTo("ByReplace.Test")] + +namespace ByReplace.Analyzers; internal class AnalyzersAndFixers : Dictionary> { diff --git a/src/ByReplace/Analyzers/DocumentFix.cs b/src/ByReplace/Analyzers/DocumentFix.cs index 652eab7..3b319e8 100644 --- a/src/ByReplace/Analyzers/DocumentFix.cs +++ b/src/ByReplace/Analyzers/DocumentFix.cs @@ -1,4 +1,6 @@ -namespace ByReplace.Analyzers; +[assembly: InternalsVisibleTo("ByReplace.Test")] + +namespace ByReplace.Analyzers; internal class DocumentFix { diff --git a/src/ByReplace/Builders/BrConfigurationBuilder.cs b/src/ByReplace/Builders/BrConfigurationBuilder.cs index d42b4f1..6e2febf 100644 --- a/src/ByReplace/Builders/BrConfigurationBuilder.cs +++ b/src/ByReplace/Builders/BrConfigurationBuilder.cs @@ -48,8 +48,6 @@ public BrConfiguration Build() .Where(r => r.Name.Equals(_rule, StringComparison.InvariantCultureIgnoreCase)) .FirstOrDefault(); - //RuleNotFoundException.ThrowIfNull(rule, $"Rule {_rule} was not found in brconfig file."); - // dado um arquivo com várias regras, esse código vai fazer o usuário escolher apenas uma // regra para ser aplicada individualmente. configuration.SetOnlyOneRule(rule); diff --git a/src/ByReplace/ByReplace.csproj b/src/ByReplace/ByReplace.csproj index 1477211..af17f02 100644 --- a/src/ByReplace/ByReplace.csproj +++ b/src/ByReplace/ByReplace.csproj @@ -2,7 +2,7 @@ Exe - net6.0;net7.0;net8.0; + net8.0 enable ByReplace https://github.com/Daniel-iel/ByReplace @@ -34,23 +34,19 @@ - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - - - + + - + all runtime; build; native; contentfiles; analyzers; buildtransitive - - - - + + + + diff --git a/src/ByReplace/Mappers/DirectoryThree.cs b/src/ByReplace/Mappers/DirectoryThree.cs index e20bb76..4ad8ab5 100644 --- a/src/ByReplace/Mappers/DirectoryThree.cs +++ b/src/ByReplace/Mappers/DirectoryThree.cs @@ -24,12 +24,12 @@ public void MapThreeSources() { List nodes = new List(); - MapThreeSubSources(_path, ref nodes); + MapThreeSubFolders(_path, ref nodes); Nodes = nodes; } - private void MapThreeSubSources(string dir, ref List nodes) + private void MapThreeSubFolders(string dir, ref List nodes) { DirectoryInfo directoryInfo = new DirectoryInfo(dir); @@ -50,7 +50,7 @@ private void MapThreeSubSources(string dir, ref List nodes) // SubFolders foreach (string subDirectory in Directory.GetDirectories(dir)) { - MapThreeSubSources(subDirectory, ref nodes); + MapThreeSubFolders(subDirectory, ref nodes); } } } \ No newline at end of file diff --git a/src/ByReplace/Matches/SkipMatch.cs b/src/ByReplace/Matches/SkipMatch.cs index 01a06f3..26969c3 100644 --- a/src/ByReplace/Matches/SkipMatch.cs +++ b/src/ByReplace/Matches/SkipMatch.cs @@ -29,8 +29,8 @@ private bool SkipFile() private bool SkipDir() { return param.Any(c => - c.StartsWith("**", StringComparison.Ordinal) && - c.EndsWith("*",StringComparison.Ordinal) && - dir.Contains(c)); + c.StartsWith("**", StringComparison.Ordinal) && + c.EndsWith("*",StringComparison.Ordinal) && + dir.Contains(c)); } } diff --git a/src/ByReplace/Program.cs b/src/ByReplace/Program.cs index d92204c..088bc99 100644 --- a/src/ByReplace/Program.cs +++ b/src/ByReplace/Program.cs @@ -5,14 +5,19 @@ using ByReplace.Commands.TimerFinish; using Cocona.Builder; -CoconaAppBuilder builder = CoconaApp.CreateBuilder( - new[] - { - "apply", - "rules", - @"-p C:\Projetos\Daniel-iel\ByReplace\samples", - @"-f C:\Projetos\Daniel-iel\ByReplace\src\ByReplace" - }); +#if DEBUG + CoconaAppBuilder builder = CoconaApp.CreateBuilder( + new[] + { + "apply", + "rules", + @"-p C:\Projetos\Daniel-iel\ByReplace\samples", + @"-f C:\Projetos\Daniel-iel\ByReplace\src\ByReplace" + }); +#else + CoconaAppBuilder builder = CoconaApp.CreateBuilder(); +#endif + builder.Services.AddScoped(); CoconaApp app = builder.Build();