Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support AdditionalDocuemnt chagnes #1106

Merged
merged 1 commit into from Apr 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Analyzers/Extensions.cs
Expand Up @@ -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<T>(this Type type, [NotNullWhen(returnValue: true)] out T? instance) where T : class
{
Expand Down
87 changes: 85 additions & 2 deletions tests/CodeFormatterTests.cs
Expand Up @@ -504,6 +504,39 @@ public async Task FilesFormattedInAnalyzersSolution_WhenFixingAnalyzerErrors()
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<string> TestFormatWorkspaceAsync(
string workspaceFilePath,
string[] include,
Expand All @@ -516,7 +549,8 @@ public async Task FilesFormattedInAnalyzersSolution_WhenFixingAnalyzerErrors()
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();
Expand Down Expand Up @@ -551,7 +585,7 @@ public async Task FilesFormattedInAnalyzersSolution_WhenFixingAnalyzerErrors()
codeStyleSeverity,
analyzerSeverity,
diagnostics?.ToImmutableHashSet() ?? ImmutableHashSet<string>.Empty,
saveFormattedFiles: false,
saveFormattedFiles,
changesAreErrors: false,
fileMatcher,
reportPath: string.Empty,
Expand All @@ -575,5 +609,54 @@ public async Task FilesFormattedInAnalyzersSolution_WhenFixingAnalyzerErrors()

return log;
}

/// <summary>
/// Copies the specified folder to the temp folder and returns the path.
/// </summary>
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);
}
}
}
}
}
}
Expand Up @@ -4,4 +4,11 @@
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.PublicApiAnalyzers" Version="3.3.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>

</ItemGroup>
</Project>