From 9a517212185ef614af09b107cbc203866ff7f7b5 Mon Sep 17 00:00:00 2001 From: Joey Robichaud Date: Sun, 25 Apr 2021 19:43:38 -0700 Subject: [PATCH] Support AdditionalDocuemnt chagnes --- src/Analyzers/Extensions.cs | 3 +- tests/CodeFormatterTests.cs | 87 ++++++++++++++++++- .../library/PublicAPI.Shipped.txt | 0 .../library/PublicAPI.Unshipped.txt | 0 .../analyzers_solution/library/library.csproj | 7 ++ 5 files changed, 94 insertions(+), 3 deletions(-) create mode 100644 tests/projects/for_code_formatter/analyzers_solution/library/PublicAPI.Shipped.txt create mode 100644 tests/projects/for_code_formatter/analyzers_solution/library/PublicAPI.Unshipped.txt diff --git a/src/Analyzers/Extensions.cs b/src/Analyzers/Extensions.cs index fe7f7f7f2a..863a431643 100644 --- a/src/Analyzers/Extensions.cs +++ b/src/Analyzers/Extensions.cs @@ -27,7 +27,8 @@ static Extensions() } public static bool Any(this SolutionChanges solutionChanges) - => solutionChanges.GetProjectChanges().Any(x => x.GetChangedDocuments().Any()); + => solutionChanges.GetProjectChanges() + .Any(x => x.GetChangedDocuments().Any() || x.GetChangedAdditionalDocuments().Any()); public static bool TryCreateInstance(this Type type, [NotNullWhen(returnValue: true)] out T? instance) where T : class { diff --git a/tests/CodeFormatterTests.cs b/tests/CodeFormatterTests.cs index 7ef9222666..da287568d6 100644 --- a/tests/CodeFormatterTests.cs +++ b/tests/CodeFormatterTests.cs @@ -504,6 +504,39 @@ await TestFormatWorkspaceAsync( analyzerSeverity: DiagnosticSeverity.Error); } + [MSBuildFact] + public async Task AdditionalDocumentsSavedInAnalyzersSolution_WhenFixingAnalyzerErrors() + { + // Copy solution to temp folder so we can write changes to disk. + var solutionPath = CopyToTempFolder(s_analyzersSolutionPath); + + try + { + // Fix PublicAPI analyzer diagnostics. + await TestFormatWorkspaceAsync( + Path.Combine(solutionPath, "library", "library.csproj"), + include: EmptyFilesList, + exclude: EmptyFilesList, + includeGenerated: false, + expectedExitCode: 0, + expectedFilesFormatted: 1, + expectedFileCount: 3, + fixCategory: FixCategory.Analyzers, + analyzerSeverity: DiagnosticSeverity.Warning, + diagnostics: new[] { "RS0016" }, + saveFormattedFiles: true); + + // Verify that changes were persisted to disk. + var unshippedPublicApi = File.ReadAllText(Path.Combine(solutionPath, "library", "PublicAPI.Unshipped.txt")); + Assert.NotEqual(string.Empty, unshippedPublicApi); + } + finally + { + // Cleanup + Directory.Delete(solutionPath, true); + } + } + internal async Task TestFormatWorkspaceAsync( string workspaceFilePath, string[] include, @@ -516,7 +549,8 @@ internal async Task TestFormatWorkspaceAsync( DiagnosticSeverity codeStyleSeverity = DiagnosticSeverity.Error, DiagnosticSeverity analyzerSeverity = DiagnosticSeverity.Error, string[] diagnostics = null, - bool noRestore = false) + bool noRestore = false, + bool saveFormattedFiles = false) { var currentDirectory = Environment.CurrentDirectory; Environment.CurrentDirectory = TestProjectsPathHelper.GetProjectsDirectory(); @@ -551,7 +585,7 @@ internal async Task TestFormatWorkspaceAsync( codeStyleSeverity, analyzerSeverity, diagnostics?.ToImmutableHashSet() ?? ImmutableHashSet.Empty, - saveFormattedFiles: false, + saveFormattedFiles, changesAreErrors: false, fileMatcher, reportPath: string.Empty, @@ -575,5 +609,54 @@ internal async Task TestFormatWorkspaceAsync( return log; } + + /// + /// Copies the specified folder to the temp folder and returns the path. + /// + private static string CopyToTempFolder(string sourcePath) + { + var fullPath = Path.GetFullPath(sourcePath, TestProjectsPathHelper.GetProjectsDirectory()); + var tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); + + DirectoryCopy(fullPath, tempPath, true); + + return tempPath; + + static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs) + { + // Get the subdirectories for the specified directory. + var dir = new DirectoryInfo(sourceDirName); + + if (!dir.Exists) + { + throw new DirectoryNotFoundException( + "Source directory does not exist or could not be found: " + + sourceDirName); + } + + var dirs = dir.GetDirectories(); + + // If the destination directory doesn't exist, create it. + Directory.CreateDirectory(destDirName); + + // Get the files in the directory and copy them to the new location. + var files = dir.GetFiles(); + foreach (var file in files) + { + var tempPath = Path.Combine(destDirName, file.Name); + file.CopyTo(tempPath, false); + } + + // If copying subdirectories, copy them and their contents to new location. + if (copySubDirs) + { + foreach (var subdir in dirs) + { + var tempPath = Path.Combine(destDirName, subdir.Name); + DirectoryCopy(subdir.FullName, tempPath, copySubDirs); + } + } + } + } } } diff --git a/tests/projects/for_code_formatter/analyzers_solution/library/PublicAPI.Shipped.txt b/tests/projects/for_code_formatter/analyzers_solution/library/PublicAPI.Shipped.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/projects/for_code_formatter/analyzers_solution/library/PublicAPI.Unshipped.txt b/tests/projects/for_code_formatter/analyzers_solution/library/PublicAPI.Unshipped.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/projects/for_code_formatter/analyzers_solution/library/library.csproj b/tests/projects/for_code_formatter/analyzers_solution/library/library.csproj index 9f5c4f4abb..b1e95b303e 100644 --- a/tests/projects/for_code_formatter/analyzers_solution/library/library.csproj +++ b/tests/projects/for_code_formatter/analyzers_solution/library/library.csproj @@ -4,4 +4,11 @@ netstandard2.0 + + + all + runtime; build; native; contentfiles; analyzers + + +