public async Task InitializeAsync() { var logger = new TestLogger(); try { // Restore the Analyzer packages that have been added to `for_analyzer_formatter/analyzer_project/analyzer_project.csproj` var exitCode = await DotNetHelper.PerformRestore(s_analyzerProjectFilePath, TestOutputHelper); Assert.Equal(0, exitCode); // Load the analyzer_project into a MSBuildWorkspace. var workspacePath = Path.Combine(TestProjectsPathHelper.GetProjectsDirectory(), s_analyzerProjectFilePath); MSBuildRegistrar.RegisterInstance(); var analyzerWorkspace = await MSBuildWorkspaceLoader.LoadAsync(workspacePath, WorkspaceType.Project, binaryLogPath : null, logWorkspaceWarnings : true, logger, CancellationToken.None); TestOutputHelper.WriteLine(logger.GetLog()); // From this project we can get valid AnalyzerReferences to add to our test project. _analyzerReferencesProject = analyzerWorkspace.CurrentSolution.Projects.Single(); } catch { TestOutputHelper.WriteLine(logger.GetLog()); throw; } }
internal async Task <string> TestFormatWorkspaceAsync( string workspaceFilePath, string[] include, string[] exclude, bool includeGenerated, int expectedExitCode, int expectedFilesFormatted, int expectedFileCount, FixCategory fixCategory = FixCategory.Whitespace, DiagnosticSeverity codeStyleSeverity = DiagnosticSeverity.Error, DiagnosticSeverity analyzerSeverity = DiagnosticSeverity.Error) { var workspacePath = Path.GetFullPath(workspaceFilePath); WorkspaceType workspaceType; if (Directory.Exists(workspacePath)) { workspaceType = WorkspaceType.Folder; } else { workspaceType = workspacePath.EndsWith(".sln") ? WorkspaceType.Solution : WorkspaceType.Project; } var logger = new TestLogger(); var msBuildPath = MSBuildRegistrar.RegisterInstance(logger); logger.LogDebug(Resources.The_dotnet_runtime_version_is_0, Program.GetRuntimeVersion()); logger.LogTrace(Resources.Using_msbuildexe_located_in_0, msBuildPath); var fileMatcher = SourceFileMatcher.CreateMatcher(include, exclude); var formatOptions = new FormatOptions( workspacePath, workspaceType, LogLevel.Trace, fixCategory, codeStyleSeverity, analyzerSeverity, saveFormattedFiles: false, changesAreErrors: false, fileMatcher, reportPath: string.Empty, includeGenerated); var formatResult = await CodeFormatter.FormatWorkspaceAsync(formatOptions, logger, CancellationToken.None); var log = logger.GetLog(); _output.WriteLine(log); Assert.Equal(expectedExitCode, formatResult.ExitCode); Assert.Equal(expectedFilesFormatted, formatResult.FilesFormatted); Assert.Equal(expectedFileCount, formatResult.FileCount); return(log); }
private static async Task AssertProjectLoadsCleanlyAsync(string projectFilePath, ILogger logger, string[] ignoredDiagnostics) { var binaryLogPath = Path.ChangeExtension(projectFilePath, ".binlog"); MSBuildRegistrar.RegisterInstance(); using var workspace = (MSBuildWorkspace) await MSBuildWorkspaceLoader.LoadAsync(projectFilePath, WorkspaceType.Project, binaryLogPath, logWorkspaceWarnings : true, logger, CancellationToken.None); Assert.Empty(workspace.Diagnostics); var project = workspace.CurrentSolution.Projects.Single(); var compilation = await project.GetCompilationAsync(); // Unnecessary using directives are reported with a severty of Hidden var diagnostics = compilation.GetDiagnostics() .Where(diagnostic => diagnostic.Severity > DiagnosticSeverity.Hidden && ignoredDiagnostics?.Contains(diagnostic.Id) != true); Assert.Empty(diagnostics); }
internal async Task <string> TestFormatWorkspaceAsync( string workspaceFilePath, string[] include, string[] exclude, bool includeGenerated, int expectedExitCode, int expectedFilesFormatted, int expectedFileCount, FixCategory fixCategory = FixCategory.Whitespace, DiagnosticSeverity codeStyleSeverity = DiagnosticSeverity.Error, DiagnosticSeverity analyzerSeverity = DiagnosticSeverity.Error, string[] diagnostics = null, bool noRestore = false, bool saveFormattedFiles = false) { var currentDirectory = Environment.CurrentDirectory; Environment.CurrentDirectory = TestProjectsPathHelper.GetProjectsDirectory(); var workspacePath = Path.GetFullPath(workspaceFilePath); WorkspaceType workspaceType; if (Directory.Exists(workspacePath)) { workspaceType = WorkspaceType.Folder; } else { workspaceType = workspacePath.EndsWith("proj") ? WorkspaceType.Project : WorkspaceType.Solution; } var logger = new TestLogger(); var msBuildPath = MSBuildRegistrar.RegisterInstance(); logger.LogTrace(Resources.Using_msbuildexe_located_in_0, msBuildPath); var fileMatcher = SourceFileMatcher.CreateMatcher(include, exclude); var formatOptions = new FormatOptions( workspacePath, workspaceType, noRestore, LogLevel.Trace, fixCategory, codeStyleSeverity, analyzerSeverity, diagnostics?.ToImmutableHashSet() ?? ImmutableHashSet <string> .Empty, ExcludeDiagnostics: ImmutableHashSet <string> .Empty, saveFormattedFiles, ChangesAreErrors: false, fileMatcher, ReportPath: string.Empty, IncludeGeneratedFiles: includeGenerated, BinaryLogPath: null); var formatResult = await CodeFormatter.FormatWorkspaceAsync(formatOptions, logger, CancellationToken.None); Environment.CurrentDirectory = currentDirectory; var log = logger.GetLog(); try { Assert.Equal(expectedExitCode, formatResult.ExitCode); Assert.Equal(expectedFilesFormatted, formatResult.FilesFormatted); Assert.Equal(expectedFileCount, formatResult.FileCount); } catch { _output.WriteLine(log); throw; } return(log); }