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;
            }
        }
        /// <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);
Beispiel #3
0
        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)
        {
            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.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,
                noRestore,
                LogLevel.Trace,
                fixCategory,
                codeStyleSeverity,
                analyzerSeverity,
                diagnostics?.ToImmutableHashSet() ?? ImmutableHashSet <string> .Empty,
                saveFormattedFiles: false,
                changesAreErrors: false,
                fileMatcher,
                reportPath: string.Empty,
                includeGenerated);
            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);
        }