Skip to content

Commit

Permalink
Merge pull request #1106 from JoeRobich/public-api
Browse files Browse the repository at this point in the history
Support AdditionalDocuemnt chagnes
  • Loading branch information
JoeRobich committed Apr 26, 2021
2 parents f2bbb8c + 9a51721 commit 8a53b84
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 3 deletions.
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);
}
}
}
}
}
}
Empty file.
Empty file.
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>

0 comments on commit 8a53b84

Please sign in to comment.