From 49b0eb97a2d9d7dc0d46519c785e13a6b6b02f44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Gawe=C5=82?= <40299500+bartlomiejgawel@users.noreply.github.com> Date: Mon, 2 Jan 2023 14:44:38 +0100 Subject: [PATCH] fix: remove duplicate projects from output (#65) --- .../Infrastructure/OutputFormatterExecutor.cs | 5 +++- .../Formatters/FormatterExecutorTests.cs | 29 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/dotnet-affected/Infrastructure/OutputFormatterExecutor.cs b/src/dotnet-affected/Infrastructure/OutputFormatterExecutor.cs index cbae9fd..221e039 100644 --- a/src/dotnet-affected/Infrastructure/OutputFormatterExecutor.cs +++ b/src/dotnet-affected/Infrastructure/OutputFormatterExecutor.cs @@ -32,7 +32,10 @@ public async Task Execute(IEnumerable projects, var formatterDictionary = formatters .ToDictionary(t => t, FindFormatter); - var allProjects = projects.ToList(); + var allProjects = projects + .GroupBy(project => project.FilePath) + .Select(group => group.First()) + .ToList(); foreach (var (type, formatter) in formatterDictionary) { diff --git a/test/dotnet-affected.Tests/Formatters/FormatterExecutorTests.cs b/test/dotnet-affected.Tests/Formatters/FormatterExecutorTests.cs index 3861b36..8dfab3a 100644 --- a/test/dotnet-affected.Tests/Formatters/FormatterExecutorTests.cs +++ b/test/dotnet-affected.Tests/Formatters/FormatterExecutorTests.cs @@ -1,6 +1,7 @@ using DotnetAffected.Testing.Utils; using Microsoft.Extensions.DependencyInjection; using System.IO; +using System.Linq; using System.Threading.Tasks; using Xunit; @@ -36,5 +37,33 @@ await executor.Execute(projects, new[] Assert.Contains(msBuildProject.FullPath, outputContents); } + + [Fact] + public async Task Should_write_deduplicated_projects() + { + // Arrange + const string formatterType = "text"; + const string projectName = "InventoryManagement"; + const string outputFileName = "affected"; + var msBuildProject = this.Repository.CreateCsProject(projectName); + var executor = this.ServiceProvider.GetRequiredService(); + var projects = new[] + { + new ProjectInfo("DuplicatedTestProject", msBuildProject.FullPath), + new ProjectInfo("DuplicatedTestProject", msBuildProject.FullPath), + }; + + // Act + await executor.Execute(projects, new[] + { + formatterType + }, Repository.Path, outputFileName, false, true); + + // Assert + var outputPath = Path.Combine(Repository.Path, $"{outputFileName}.txt"); + var outputContents = await File.ReadAllLinesAsync(outputPath); + Assert.Single(outputContents); + Assert.Equal(msBuildProject.FullPath, outputContents.First()); + } } }