示例#1
0
 public void Deconstruct(
     out string workspaceFilePath,
     out WorkspaceType workspaceType,
     out LogLevel logLevel,
     out FixCategory fixCategory,
     out DiagnosticSeverity codeStyleSeverity,
     out DiagnosticSeverity analyzerSeverity,
     out bool saveFormattedFiles,
     out bool changesAreErrors,
     out SourceFileMatcher fileMatcher,
     out string?reportPath,
     out bool includeGeneratedFiles)
 {
     workspaceFilePath     = WorkspaceFilePath;
     workspaceType         = WorkspaceType;
     logLevel              = LogLevel;
     fixCategory           = FixCategory;
     codeStyleSeverity     = CodeStyleSeverity;
     analyzerSeverity      = AnalyzerSeverity;
     saveFormattedFiles    = SaveFormattedFiles;
     changesAreErrors      = ChangesAreErrors;
     fileMatcher           = FileMatcher;
     reportPath            = ReportPath;
     includeGeneratedFiles = IncludeGeneratedFiles;
 }
示例#2
0
        private protected async Task <SourceText> AssertCodeChangedAsync(
            string testCode,
            string expectedCode,
            string editorConfig,
            Encoding?encoding       = null,
            FixCategory fixCategory = FixCategory.Whitespace,
            IEnumerable <AnalyzerReference>?analyzerReferences = null,
            DiagnosticSeverity codeStyleSeverity = DiagnosticSeverity.Error,
            DiagnosticSeverity analyzerSeverity  = DiagnosticSeverity.Error,
            string[]?diagnostics = null)
        {
            var(formattedText, _, logger) = await ApplyFormatterAsync(testCode, editorConfig, encoding, fixCategory, analyzerReferences, codeStyleSeverity, analyzerSeverity, diagnostics);

            try
            {
                Assert.Equal(expectedCode, formattedText.ToString());
            }
            catch
            {
                TestOutputHelper?.WriteLine(logger.GetLog());
                throw;
            }

            return(formattedText);
        }
示例#3
0
        private protected async Task <SourceText> AssertNoReportedFileChangesAsync(
            string code,
            string editorConfig,
            Encoding?encoding       = null,
            FixCategory fixCategory = FixCategory.Whitespace,
            IEnumerable <AnalyzerReference>?analyzerReferences = null,
            DiagnosticSeverity codeStyleSeverity = DiagnosticSeverity.Error,
            DiagnosticSeverity analyzerSeverity  = DiagnosticSeverity.Error,
            string[]?diagnostics = null)
        {
            var(formattedText, formattedFiles, logger) = await ApplyFormatterAsync(code, editorConfig, encoding, fixCategory, analyzerReferences, codeStyleSeverity, analyzerSeverity, diagnostics);

            try
            {
                // Ensure the code is unchanged
                Assert.Equal(code, formattedText.ToString());

                // Ensure no non-fixable diagnostics were reported
                Assert.Empty(formattedFiles);
            }
            catch
            {
                TestOutputHelper?.WriteLine(logger.GetLog());
                throw;
            }

            return(formattedText);
        }
示例#4
0
 public FormatOptions(
     string workspaceFilePath,
     WorkspaceType workspaceType,
     LogLevel logLevel,
     FixCategory fixCategory,
     DiagnosticSeverity codeStyleSeverity,
     DiagnosticSeverity analyzerSeverity,
     bool saveFormattedFiles,
     bool changesAreErrors,
     SourceFileMatcher fileMatcher,
     string?reportPath,
     bool includeGeneratedFiles)
 {
     WorkspaceFilePath     = workspaceFilePath;
     WorkspaceType         = workspaceType;
     LogLevel              = logLevel;
     FixCategory           = fixCategory;
     CodeStyleSeverity     = codeStyleSeverity;
     AnalyzerSeverity      = analyzerSeverity;
     SaveFormattedFiles    = saveFormattedFiles;
     ChangesAreErrors      = changesAreErrors;
     FileMatcher           = fileMatcher;
     ReportPath            = reportPath;
     IncludeGeneratedFiles = includeGeneratedFiles;
 }
示例#5
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)
        {
            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);
        }
示例#6
0
 private protected Task <SourceText> AssertCodeUnchangedAsync(
     string code,
     IReadOnlyDictionary <string, string> editorConfig,
     Encoding encoding       = null,
     FixCategory fixCategory = FixCategory.Whitespace,
     DiagnosticSeverity codeStyleSeverity = DiagnosticSeverity.Error,
     DiagnosticSeverity analyzerSeverity  = DiagnosticSeverity.Error)
 {
     return(AssertCodeChangedAsync(code, code, ToEditorConfig(editorConfig), encoding, fixCategory, codeStyleSeverity, analyzerSeverity));
 }
示例#7
0
 private protected Task <SourceText> AssertNoReportedFileChangesAsync(
     string code,
     IReadOnlyDictionary <string, string> editorConfig,
     Encoding?encoding       = null,
     FixCategory fixCategory = FixCategory.Whitespace,
     IEnumerable <AnalyzerReference>?analyzerReferences = null,
     DiagnosticSeverity codeStyleSeverity = DiagnosticSeverity.Error,
     DiagnosticSeverity analyzerSeverity  = DiagnosticSeverity.Error)
 {
     return(AssertNoReportedFileChangesAsync(code, ToEditorConfig(editorConfig), encoding, fixCategory, analyzerReferences, codeStyleSeverity, analyzerSeverity));
 }
示例#8
0
 private protected Task <SourceText> AssertCodeChangedAsync(
     string testCode,
     string expectedCode,
     IReadOnlyDictionary <string, string> editorConfig,
     Encoding?encoding       = null,
     FixCategory fixCategory = FixCategory.Whitespace,
     IEnumerable <AnalyzerReference>?analyzerReferences = null,
     DiagnosticSeverity codeStyleSeverity = DiagnosticSeverity.Error,
     DiagnosticSeverity analyzerSeverity  = DiagnosticSeverity.Error,
     string[]?diagnostics = null)
 {
     return(AssertCodeChangedAsync(testCode, expectedCode, ToEditorConfig(editorConfig), encoding, fixCategory, analyzerReferences, codeStyleSeverity, analyzerSeverity, diagnostics));
 }
示例#9
0
 public AnalyzerFormatter(
     string name,
     FixCategory category,
     IAnalyzerInformationProvider informationProvider,
     IAnalyzerRunner runner,
     ICodeFixApplier applier)
 {
     _name                = name;
     Category             = category;
     _informationProvider = informationProvider;
     _runner              = runner;
     _applier             = applier;
 }
示例#10
0
 public AnalyzerFormatter(
     string name,
     FixCategory category,
     bool includeCompilerDiagnostics,
     IAnalyzerInformationProvider informationProvider,
     IAnalyzerRunner runner,
     ICodeFixApplier applier)
 {
     _name    = name;
     Category = category;
     _includeCompilerDiagnostics = includeCompilerDiagnostics;
     _informationProvider        = informationProvider;
     _runner  = runner;
     _applier = applier;
 }
示例#11
0
        private protected async Task <(SourceText FormattedText, List <FormattedFile> FormattedFiles, TestLogger Logger)> ApplyFormatterAsync(
            string code,
            string editorConfig,
            Encoding?encoding       = null,
            FixCategory fixCategory = FixCategory.Whitespace,
            IEnumerable <AnalyzerReference>?analyzerReferences = null,
            DiagnosticSeverity codeStyleSeverity = DiagnosticSeverity.Error,
            DiagnosticSeverity analyzerSeverity  = DiagnosticSeverity.Error)
        {
            var text = SourceText.From(code, encoding ?? Encoding.UTF8);

            TestState.Sources.Add(text);

            var solution = await GetSolutionAsync(TestState.Sources.ToArray(), TestState.AdditionalFiles.ToArray(), TestState.AdditionalReferences.ToArray(), editorConfig, analyzerReferences);

            var project  = solution.Projects.Single();
            var document = project.Documents.Single();

            var fileMatcher   = SourceFileMatcher.CreateMatcher(new[] { document.FilePath ! }, exclude: Array.Empty <string>());
示例#12
0
        private protected async Task <SourceText> AssertCodeChangedAsync(
            string testCode,
            string expectedCode,
            string editorConfig,
            Encoding encoding       = null,
            FixCategory fixCategory = FixCategory.Whitespace,
            DiagnosticSeverity codeStyleSeverity = DiagnosticSeverity.Error,
            DiagnosticSeverity analyzerSeverity  = DiagnosticSeverity.Error)
        {
            var text = SourceText.From(testCode, encoding ?? Encoding.UTF8);

            TestState.Sources.Add(text);

            var solution = GetSolution(TestState.Sources.ToArray(), TestState.AdditionalFiles.ToArray(), TestState.AdditionalReferences.ToArray(), editorConfig);
            var project  = solution.Projects.Single();
            var document = project.Documents.Single();

            var fileMatcher   = SourceFileMatcher.CreateMatcher(new[] { document.FilePath }, exclude: Array.Empty <string>());
            var formatOptions = new FormatOptions(
                workspaceFilePath: project.FilePath,
                workspaceType: WorkspaceType.Folder,
                logLevel: LogLevel.Trace,
                fixCategory,
                codeStyleSeverity,
                analyzerSeverity,
                saveFormattedFiles: true,
                changesAreErrors: false,
                fileMatcher,
                reportPath: string.Empty,
                includeGeneratedFiles: false);

            var pathsToFormat = GetOnlyFileToFormat(solution);

            var formattedSolution = await Formatter.FormatAsync(solution, pathsToFormat, formatOptions, Logger, new List <FormattedFile>(), default);

            var formattedDocument = GetOnlyDocument(formattedSolution);
            var formattedText     = await formattedDocument.GetTextAsync();

            Assert.Equal(expectedCode, formattedText.ToString());

            return(formattedText);
        }
        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);
        }