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;
            }
        }
Esempio n. 2
0
 private static Task <Workspace?> OpenMSBuildWorkspaceAsync(
     string solutionOrProjectPath,
     WorkspaceType workspaceType,
     bool createBinaryLog,
     bool logWorkspaceWarnings,
     ILogger logger,
     CancellationToken cancellationToken)
 {
     return(MSBuildWorkspaceLoader.LoadAsync(solutionOrProjectPath, workspaceType, createBinaryLog, logWorkspaceWarnings, logger, cancellationToken));
 }
Esempio n. 3
0
        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);
        }
Esempio n. 4
0
        private static async Task <Workspace?> OpenMSBuildWorkspaceAsync(
            string solutionOrProjectPath,
            WorkspaceType workspaceType,
            bool noRestore,
            bool requiresSemantics,
            string?binaryLogPath,
            bool logWorkspaceWarnings,
            ILogger logger,
            CancellationToken cancellationToken)
        {
            if (requiresSemantics &&
                !noRestore &&
                await DotNetHelper.PerformRestoreAsync(solutionOrProjectPath, logger) != 0)
            {
                throw new Exception("Restore operation failed.");
            }

            return(await MSBuildWorkspaceLoader.LoadAsync(solutionOrProjectPath, workspaceType, binaryLogPath, logWorkspaceWarnings, logger, cancellationToken));
        }