From 0a5c927a3210b2e77eae5e49359ea7433056a97e Mon Sep 17 00:00:00 2001 From: Jonathon Marolf Date: Mon, 22 Jun 2020 15:19:20 -0700 Subject: [PATCH 1/2] add string localization --- src/Analyzers/AnalyzerFormatter.cs | 8 +-- src/Analyzers/SolutionCodeFixApplier.cs | 6 +- src/CodeFormatter.cs | 4 +- src/Resources.resx | 85 ++++++++++++++++--------- src/xlf/Resources.cs.xlf | 45 +++++++++++++ src/xlf/Resources.de.xlf | 45 +++++++++++++ src/xlf/Resources.es.xlf | 45 +++++++++++++ src/xlf/Resources.fr.xlf | 45 +++++++++++++ src/xlf/Resources.it.xlf | 45 +++++++++++++ src/xlf/Resources.ja.xlf | 45 +++++++++++++ src/xlf/Resources.ko.xlf | 45 +++++++++++++ src/xlf/Resources.pl.xlf | 45 +++++++++++++ src/xlf/Resources.pt-BR.xlf | 45 +++++++++++++ src/xlf/Resources.ru.xlf | 45 +++++++++++++ src/xlf/Resources.tr.xlf | 45 +++++++++++++ src/xlf/Resources.zh-Hans.xlf | 45 +++++++++++++ src/xlf/Resources.zh-Hant.xlf | 45 +++++++++++++ 17 files changed, 650 insertions(+), 38 deletions(-) diff --git a/src/Analyzers/AnalyzerFormatter.cs b/src/Analyzers/AnalyzerFormatter.cs index ffabcba3a4..da3a8cb397 100644 --- a/src/Analyzers/AnalyzerFormatter.cs +++ b/src/Analyzers/AnalyzerFormatter.cs @@ -48,12 +48,12 @@ internal class AnalyzerFormatter : ICodeFormatter } var analysisStopwatch = Stopwatch.StartNew(); - logger.LogTrace($"Running {_name} analysis."); + logger.LogTrace(Resources.Running_0_analysis, _name); var formattablePaths = formattableDocuments.Select(id => solution.GetDocument(id)!.FilePath) .OfType().ToImmutableHashSet(); - logger.LogTrace("Determining diagnostics..."); + logger.LogTrace(Resources.Determining_diagnostics); var allAnalyzers = analyzersAndFixers.Select(pair => pair.Analyzer).ToImmutableArray(); var projectAnalyzers = await _finder.FilterBySeverityAsync(solution.Projects, allAnalyzers, formattablePaths, formatOptions, cancellationToken).ConfigureAwait(false); @@ -63,14 +63,14 @@ internal class AnalyzerFormatter : ICodeFormatter var projectDiagnosticsMS = analysisStopwatch.ElapsedMilliseconds; logger.LogTrace(Resources.Complete_in_0_ms, projectDiagnosticsMS); - logger.LogTrace("Fixing diagnostics..."); + logger.LogTrace(Resources.Fixing_diagnostics); solution = await FixDiagnosticsAsync(solution, analyzersAndFixers, projectDiagnostics, formattablePaths, logger, cancellationToken).ConfigureAwait(false); var fixDiagnosticsMS = analysisStopwatch.ElapsedMilliseconds - projectDiagnosticsMS; logger.LogTrace(Resources.Complete_in_0_ms, fixDiagnosticsMS); - logger.LogTrace("Analysis complete in {0}ms.", analysisStopwatch.ElapsedMilliseconds); + logger.LogTrace(Resources.Analysis_complete_in_0ms_, analysisStopwatch.ElapsedMilliseconds); return solution; } diff --git a/src/Analyzers/SolutionCodeFixApplier.cs b/src/Analyzers/SolutionCodeFixApplier.cs index f8a960cf4c..316d4a347e 100644 --- a/src/Analyzers/SolutionCodeFixApplier.cs +++ b/src/Analyzers/SolutionCodeFixApplier.cs @@ -26,14 +26,14 @@ internal class SolutionCodeFixApplier : ICodeFixApplier var fixAllProvider = codeFix.GetFixAllProvider(); if (fixAllProvider?.GetSupportedFixAllScopes()?.Contains(FixAllScope.Solution) != true) { - logger.LogWarning($"Unable to fix {diagnosticId}. Code fix {codeFix.GetType().Name} doesn't support Fix All in Solution."); + logger.LogWarning(Resources.Unable_to_fix_0_Code_fix_1_doesnt_support_Fix_All_in_Solution, diagnosticId, codeFix.GetType().Name); return solution; } var project = solution.Projects.FirstOrDefault(); if (project == null) { - throw new InvalidOperationException($"Solution {solution} has no projects"); + throw new InvalidOperationException(string.Format(Resources.Solution_0_has__no_projects, solution)); } var fixAllContext = new FixAllContext( @@ -56,7 +56,7 @@ internal class SolutionCodeFixApplier : ICodeFixApplier } catch (Exception ex) { - logger.LogWarning($"Failed to apply code fix {codeFix?.GetType().Name} for {diagnosticId}: {ex.Message}"); + logger.LogWarning(Resources.Failed_to_apply_code_fix_0_for_1_2, codeFix?.GetType().Name, diagnosticId, ex.Message); return solution; } } diff --git a/src/CodeFormatter.cs b/src/CodeFormatter.cs index a186a8af53..673ba300e1 100644 --- a/src/CodeFormatter.cs +++ b/src/CodeFormatter.cs @@ -29,8 +29,8 @@ internal static class CodeFormatter new EndOfLineFormatter(), new CharsetFormatter(), new ImportsFormatter(), - new AnalyzerFormatter("Code Style", new RoslynCodeStyleAnalyzerFinder(), new AnalyzerRunner(), new SolutionCodeFixApplier()), - new AnalyzerFormatter("Analyzer Reference", new AnalyzerReferenceAnalyzerFinder(), new AnalyzerRunner(), new SolutionCodeFixApplier()), + new AnalyzerFormatter(Resources.Code_Style, new RoslynCodeStyleAnalyzerFinder(), new AnalyzerRunner(), new SolutionCodeFixApplier()), + new AnalyzerFormatter(Resources.Analyzer_Reference, new AnalyzerReferenceAnalyzerFinder(), new AnalyzerRunner(), new SolutionCodeFixApplier()), }.ToImmutableArray(); public static async Task FormatWorkspaceAsync( diff --git a/src/Resources.resx b/src/Resources.resx index 14a834e49b..18954f9c5a 100644 --- a/src/Resources.resx +++ b/src/Resources.resx @@ -1,17 +1,17 @@  - @@ -243,7 +243,7 @@ The `--workspace` option is deprecated. Use the `<project>` argument instead. - + The `--folder` option is deprecated for specifying the path. Pass the `--folder` option but specify the path with the project argument instead. @@ -255,10 +255,37 @@ Fix imports ordering. - + Unable to organize imports for '{0}'. The document is too complex. Run code style analyzers and apply fixes. + + Analyzer Reference + + + Code Style + + + Analysis complete in {0}ms. + + + Determining diagnostics... + + + Failed to apply code fix {0} for {1}: {2} + + + Fixing diagnostics... + + + Running {0} analysis. + + + Solution {0} has no projects + + + Unable to fix {0}. Code fix {1} doesn't support Fix All in Solution. + \ No newline at end of file diff --git a/src/xlf/Resources.cs.xlf b/src/xlf/Resources.cs.xlf index 120a6d2afd..2051bdab39 100644 --- a/src/xlf/Resources.cs.xlf +++ b/src/xlf/Resources.cs.xlf @@ -17,6 +17,16 @@ Accepts a file path, which if provided, will produce a json report in the given directory. + + Analysis complete in {0}ms. + Analysis complete in {0}ms. + + + + Analyzer Reference + Analyzer Reference + + Both a MSBuild project file and solution file found in '{0}'. Specify which to use with the --workspace option. {0} obsahuje jak soubor projektu MSBuild, tak soubor řešení. Určete, který soubor chcete použít, pomocí parametru --workspace. @@ -37,6 +47,11 @@ Cannot specify both project argument and workspace option. + + Code Style + Code Style + + Complete in {0}ms. Complete in {0}ms. @@ -52,11 +67,21 @@ {0} nejde formátovat. Formátovat jde v současné době jenom projekty v jazycích C# a Visual Basic. + + Determining diagnostics... + Determining diagnostics... + + Determining formattable files. Determining formattable files. + + Failed to apply code fix {0} for {1}: {2} + Failed to apply code fix {0} for {1}: {2} + + Failed to save formatting changes. Nepodařilo se uložit změny formátování. @@ -87,6 +112,11 @@ Fix whitespace formatting. + + Fixing diagnostics... + Fixing diagnostics... + + Format complete in {0}ms. Format complete in {0}ms. @@ -142,6 +172,11 @@ Run code style analyzers and apply fixes. + + Running {0} analysis. + Running {0} analysis. + + Running formatters. Running formatters. @@ -157,6 +192,11 @@ Přeskočí se odkazovaný projekt {0}. + + Solution {0} has no projects + Solution {0} has no projects + + The dotnet CLI version is '{0}'. The dotnet CLI version is '{0}'. @@ -207,6 +247,11 @@ The `--workspace` option is deprecated. Use the `<project>` argument instead. + + Unable to fix {0}. Code fix {1} doesn't support Fix All in Solution. + Unable to fix {0}. Code fix {1} doesn't support Fix All in Solution. + + Unable to locate MSBuild. Ensure the .NET SDK was installed with the official installer. Unable to locate MSBuild. Ensure the .NET SDK was installed with the official installer. diff --git a/src/xlf/Resources.de.xlf b/src/xlf/Resources.de.xlf index ac6b07c594..b6b2284980 100644 --- a/src/xlf/Resources.de.xlf +++ b/src/xlf/Resources.de.xlf @@ -17,6 +17,16 @@ Accepts a file path, which if provided, will produce a json report in the given directory. + + Analysis complete in {0}ms. + Analysis complete in {0}ms. + + + + Analyzer Reference + Analyzer Reference + + Both a MSBuild project file and solution file found in '{0}'. Specify which to use with the --workspace option. In "{0}" wurden eine MSBuild-Projektdatei und eine Projektmappe gefunden. Geben Sie mit der Option "--workspace" an, welche verwendet werden soll. @@ -37,6 +47,11 @@ Cannot specify both project argument and workspace option. + + Code Style + Code Style + + Complete in {0}ms. Complete in {0}ms. @@ -52,11 +67,21 @@ "{0}" konnte nicht formatiert werden. Derzeit wird die Formatierung nur für C#- und Visual Basic-Projekte unterstützt. + + Determining diagnostics... + Determining diagnostics... + + Determining formattable files. Determining formattable files. + + Failed to apply code fix {0} for {1}: {2} + Failed to apply code fix {0} for {1}: {2} + + Failed to save formatting changes. Fehler beim Speichern von Formatierungsänderungen. @@ -87,6 +112,11 @@ Fix whitespace formatting. + + Fixing diagnostics... + Fixing diagnostics... + + Format complete in {0}ms. Format complete in {0}ms. @@ -142,6 +172,11 @@ Run code style analyzers and apply fixes. + + Running {0} analysis. + Running {0} analysis. + + Running formatters. Running formatters. @@ -157,6 +192,11 @@ Überspringen von referenziertem Projekt "{0}". + + Solution {0} has no projects + Solution {0} has no projects + + The dotnet CLI version is '{0}'. The dotnet CLI version is '{0}'. @@ -207,6 +247,11 @@ The `--workspace` option is deprecated. Use the `<project>` argument instead. + + Unable to fix {0}. Code fix {1} doesn't support Fix All in Solution. + Unable to fix {0}. Code fix {1} doesn't support Fix All in Solution. + + Unable to locate MSBuild. Ensure the .NET SDK was installed with the official installer. Unable to locate MSBuild. Ensure the .NET SDK was installed with the official installer. diff --git a/src/xlf/Resources.es.xlf b/src/xlf/Resources.es.xlf index 1a888586b8..87f3c6f4ba 100644 --- a/src/xlf/Resources.es.xlf +++ b/src/xlf/Resources.es.xlf @@ -17,6 +17,16 @@ Accepts a file path, which if provided, will produce a json report in the given directory. + + Analysis complete in {0}ms. + Analysis complete in {0}ms. + + + + Analyzer Reference + Analyzer Reference + + Both a MSBuild project file and solution file found in '{0}'. Specify which to use with the --workspace option. Se encontró un archivo de proyecto y un archivo de solución MSBuild en "{0}". Especifique cuál debe usarse con la opción --workspace. @@ -37,6 +47,11 @@ Cannot specify both project argument and workspace option. + + Code Style + Code Style + + Complete in {0}ms. Complete in {0}ms. @@ -52,11 +67,21 @@ No se pudo dar formato a "{0}". El formato admite solo proyectos de C# y Visual Basic en este momento. + + Determining diagnostics... + Determining diagnostics... + + Determining formattable files. Determining formattable files. + + Failed to apply code fix {0} for {1}: {2} + Failed to apply code fix {0} for {1}: {2} + + Failed to save formatting changes. Error al guardar cambios de formato. @@ -87,6 +112,11 @@ Fix whitespace formatting. + + Fixing diagnostics... + Fixing diagnostics... + + Format complete in {0}ms. Format complete in {0}ms. @@ -142,6 +172,11 @@ Run code style analyzers and apply fixes. + + Running {0} analysis. + Running {0} analysis. + + Running formatters. Running formatters. @@ -157,6 +192,11 @@ Omitiendo projecto al que se hace referencia "{0}". + + Solution {0} has no projects + Solution {0} has no projects + + The dotnet CLI version is '{0}'. The dotnet CLI version is '{0}'. @@ -207,6 +247,11 @@ The `--workspace` option is deprecated. Use the `<project>` argument instead. + + Unable to fix {0}. Code fix {1} doesn't support Fix All in Solution. + Unable to fix {0}. Code fix {1} doesn't support Fix All in Solution. + + Unable to locate MSBuild. Ensure the .NET SDK was installed with the official installer. Unable to locate MSBuild. Ensure the .NET SDK was installed with the official installer. diff --git a/src/xlf/Resources.fr.xlf b/src/xlf/Resources.fr.xlf index 1f3dd39d6c..f92284f72c 100644 --- a/src/xlf/Resources.fr.xlf +++ b/src/xlf/Resources.fr.xlf @@ -17,6 +17,16 @@ Accepts a file path, which if provided, will produce a json report in the given directory. + + Analysis complete in {0}ms. + Analysis complete in {0}ms. + + + + Analyzer Reference + Analyzer Reference + + Both a MSBuild project file and solution file found in '{0}'. Specify which to use with the --workspace option. Un fichier projet et un fichier solution MSBuild ont été trouvés dans '{0}'. Spécifiez celui à utiliser avec l'option --workspace. @@ -37,6 +47,11 @@ Cannot specify both project argument and workspace option. + + Code Style + Code Style + + Complete in {0}ms. Complete in {0}ms. @@ -52,11 +67,21 @@ Impossible de mettre en forme '{0}'. L'opération Mettre en forme prend uniquement en charge les projets C# et Visual Basic pour le moment. + + Determining diagnostics... + Determining diagnostics... + + Determining formattable files. Determining formattable files. + + Failed to apply code fix {0} for {1}: {2} + Failed to apply code fix {0} for {1}: {2} + + Failed to save formatting changes. L'enregistrement des changements de mise en forme a échoué. @@ -87,6 +112,11 @@ Fix whitespace formatting. + + Fixing diagnostics... + Fixing diagnostics... + + Format complete in {0}ms. Format complete in {0}ms. @@ -142,6 +172,11 @@ Run code style analyzers and apply fixes. + + Running {0} analysis. + Running {0} analysis. + + Running formatters. Running formatters. @@ -157,6 +192,11 @@ Saut du projet référencé '{0}'. + + Solution {0} has no projects + Solution {0} has no projects + + The dotnet CLI version is '{0}'. The dotnet CLI version is '{0}'. @@ -207,6 +247,11 @@ The `--workspace` option is deprecated. Use the `<project>` argument instead. + + Unable to fix {0}. Code fix {1} doesn't support Fix All in Solution. + Unable to fix {0}. Code fix {1} doesn't support Fix All in Solution. + + Unable to locate MSBuild. Ensure the .NET SDK was installed with the official installer. Unable to locate MSBuild. Ensure the .NET SDK was installed with the official installer. diff --git a/src/xlf/Resources.it.xlf b/src/xlf/Resources.it.xlf index ecf5ef2c34..f0752d5af9 100644 --- a/src/xlf/Resources.it.xlf +++ b/src/xlf/Resources.it.xlf @@ -17,6 +17,16 @@ Accepts a file path, which if provided, will produce a json report in the given directory. + + Analysis complete in {0}ms. + Analysis complete in {0}ms. + + + + Analyzer Reference + Analyzer Reference + + Both a MSBuild project file and solution file found in '{0}'. Specify which to use with the --workspace option. In '{0}' sono stati trovati sia un file di progetto che un file di soluzione MSBuild. Per specificare quello desiderato, usare l'opzione --workspace. @@ -37,6 +47,11 @@ Cannot specify both project argument and workspace option. + + Code Style + Code Style + + Complete in {0}ms. Complete in {0}ms. @@ -52,11 +67,21 @@ Non è stato possibile formattare '{0}'. Il formato supporta attualmente solo progetti C# e Visual Basic. + + Determining diagnostics... + Determining diagnostics... + + Determining formattable files. Determining formattable files. + + Failed to apply code fix {0} for {1}: {2} + Failed to apply code fix {0} for {1}: {2} + + Failed to save formatting changes. Non è stato possibile salvare le modifiche di formattazione. @@ -87,6 +112,11 @@ Fix whitespace formatting. + + Fixing diagnostics... + Fixing diagnostics... + + Format complete in {0}ms. Format complete in {0}ms. @@ -142,6 +172,11 @@ Run code style analyzers and apply fixes. + + Running {0} analysis. + Running {0} analysis. + + Running formatters. Running formatters. @@ -157,6 +192,11 @@ Il progetto di riferimento '{0}' verrà ignorato. + + Solution {0} has no projects + Solution {0} has no projects + + The dotnet CLI version is '{0}'. The dotnet CLI version is '{0}'. @@ -207,6 +247,11 @@ The `--workspace` option is deprecated. Use the `<project>` argument instead. + + Unable to fix {0}. Code fix {1} doesn't support Fix All in Solution. + Unable to fix {0}. Code fix {1} doesn't support Fix All in Solution. + + Unable to locate MSBuild. Ensure the .NET SDK was installed with the official installer. Unable to locate MSBuild. Ensure the .NET SDK was installed with the official installer. diff --git a/src/xlf/Resources.ja.xlf b/src/xlf/Resources.ja.xlf index cce6cfbd54..5d7fad18a4 100644 --- a/src/xlf/Resources.ja.xlf +++ b/src/xlf/Resources.ja.xlf @@ -17,6 +17,16 @@ Accepts a file path, which if provided, will produce a json report in the given directory. + + Analysis complete in {0}ms. + Analysis complete in {0}ms. + + + + Analyzer Reference + Analyzer Reference + + Both a MSBuild project file and solution file found in '{0}'. Specify which to use with the --workspace option. MSBuild プロジェクト ファイルとソリューション ファイルが '{0}' で見つかりました。使用するファイルを --workspace オプションで指定してください。 @@ -37,6 +47,11 @@ Cannot specify both project argument and workspace option. + + Code Style + Code Style + + Complete in {0}ms. Complete in {0}ms. @@ -52,11 +67,21 @@ '{0}' をフォーマットできませんでした。フォーマットは、C# および Visual Basic のプロジェクトでのみサポートされています。 + + Determining diagnostics... + Determining diagnostics... + + Determining formattable files. Determining formattable files. + + Failed to apply code fix {0} for {1}: {2} + Failed to apply code fix {0} for {1}: {2} + + Failed to save formatting changes. 書式変更を保存できませんでした。 @@ -87,6 +112,11 @@ Fix whitespace formatting. + + Fixing diagnostics... + Fixing diagnostics... + + Format complete in {0}ms. Format complete in {0}ms. @@ -142,6 +172,11 @@ Run code style analyzers and apply fixes. + + Running {0} analysis. + Running {0} analysis. + + Running formatters. Running formatters. @@ -157,6 +192,11 @@ 参照プロジェクト '{0}' をスキップしています。 + + Solution {0} has no projects + Solution {0} has no projects + + The dotnet CLI version is '{0}'. The dotnet CLI version is '{0}'. @@ -207,6 +247,11 @@ The `--workspace` option is deprecated. Use the `<project>` argument instead. + + Unable to fix {0}. Code fix {1} doesn't support Fix All in Solution. + Unable to fix {0}. Code fix {1} doesn't support Fix All in Solution. + + Unable to locate MSBuild. Ensure the .NET SDK was installed with the official installer. Unable to locate MSBuild. Ensure the .NET SDK was installed with the official installer. diff --git a/src/xlf/Resources.ko.xlf b/src/xlf/Resources.ko.xlf index cad257abe2..712ced7a50 100644 --- a/src/xlf/Resources.ko.xlf +++ b/src/xlf/Resources.ko.xlf @@ -17,6 +17,16 @@ Accepts a file path, which if provided, will produce a json report in the given directory. + + Analysis complete in {0}ms. + Analysis complete in {0}ms. + + + + Analyzer Reference + Analyzer Reference + + Both a MSBuild project file and solution file found in '{0}'. Specify which to use with the --workspace option. '{0}'에 MSBuild 프로젝트 파일 및 솔루션 파일이 모두 있습니다. --workspace 옵션에 사용할 파일을 지정하세요. @@ -37,6 +47,11 @@ Cannot specify both project argument and workspace option. + + Code Style + Code Style + + Complete in {0}ms. Complete in {0}ms. @@ -52,11 +67,21 @@ '{0}'의 서식을 지정할 수 없습니다. 서식 지정은 현재 C# 및 Visual Basic 프로젝트만 지원합니다. + + Determining diagnostics... + Determining diagnostics... + + Determining formattable files. Determining formattable files. + + Failed to apply code fix {0} for {1}: {2} + Failed to apply code fix {0} for {1}: {2} + + Failed to save formatting changes. 서식 변경 내용을 저장하지 못했습니다. @@ -87,6 +112,11 @@ Fix whitespace formatting. + + Fixing diagnostics... + Fixing diagnostics... + + Format complete in {0}ms. Format complete in {0}ms. @@ -142,6 +172,11 @@ Run code style analyzers and apply fixes. + + Running {0} analysis. + Running {0} analysis. + + Running formatters. Running formatters. @@ -157,6 +192,11 @@ 참조된 프로젝트 '{0}'을(를) 건너뜁니다. + + Solution {0} has no projects + Solution {0} has no projects + + The dotnet CLI version is '{0}'. The dotnet CLI version is '{0}'. @@ -207,6 +247,11 @@ The `--workspace` option is deprecated. Use the `<project>` argument instead. + + Unable to fix {0}. Code fix {1} doesn't support Fix All in Solution. + Unable to fix {0}. Code fix {1} doesn't support Fix All in Solution. + + Unable to locate MSBuild. Ensure the .NET SDK was installed with the official installer. Unable to locate MSBuild. Ensure the .NET SDK was installed with the official installer. diff --git a/src/xlf/Resources.pl.xlf b/src/xlf/Resources.pl.xlf index 636c3c4617..dfec45a515 100644 --- a/src/xlf/Resources.pl.xlf +++ b/src/xlf/Resources.pl.xlf @@ -17,6 +17,16 @@ Accepts a file path, which if provided, will produce a json report in the given directory. + + Analysis complete in {0}ms. + Analysis complete in {0}ms. + + + + Analyzer Reference + Analyzer Reference + + Both a MSBuild project file and solution file found in '{0}'. Specify which to use with the --workspace option. W elemencie „{0}” znaleziono zarówno plik rozwiązania, jak i plik projektu MSBuild. Określ plik do użycia za pomocą opcji --workspace. @@ -37,6 +47,11 @@ Cannot specify both project argument and workspace option. + + Code Style + Code Style + + Complete in {0}ms. Complete in {0}ms. @@ -52,11 +67,21 @@ Nie można sformatować elementu „{0}”. Obecnie format obsługuje tylko projekty C# i Visual Basic. + + Determining diagnostics... + Determining diagnostics... + + Determining formattable files. Determining formattable files. + + Failed to apply code fix {0} for {1}: {2} + Failed to apply code fix {0} for {1}: {2} + + Failed to save formatting changes. Nie można zapisać zmian formatowania. @@ -87,6 +112,11 @@ Fix whitespace formatting. + + Fixing diagnostics... + Fixing diagnostics... + + Format complete in {0}ms. Format complete in {0}ms. @@ -142,6 +172,11 @@ Run code style analyzers and apply fixes. + + Running {0} analysis. + Running {0} analysis. + + Running formatters. Running formatters. @@ -157,6 +192,11 @@ Pomijanie przywoływanego projektu „{0}”. + + Solution {0} has no projects + Solution {0} has no projects + + The dotnet CLI version is '{0}'. The dotnet CLI version is '{0}'. @@ -207,6 +247,11 @@ The `--workspace` option is deprecated. Use the `<project>` argument instead. + + Unable to fix {0}. Code fix {1} doesn't support Fix All in Solution. + Unable to fix {0}. Code fix {1} doesn't support Fix All in Solution. + + Unable to locate MSBuild. Ensure the .NET SDK was installed with the official installer. Unable to locate MSBuild. Ensure the .NET SDK was installed with the official installer. diff --git a/src/xlf/Resources.pt-BR.xlf b/src/xlf/Resources.pt-BR.xlf index c91388097a..d0cf59e42b 100644 --- a/src/xlf/Resources.pt-BR.xlf +++ b/src/xlf/Resources.pt-BR.xlf @@ -17,6 +17,16 @@ Accepts a file path, which if provided, will produce a json report in the given directory. + + Analysis complete in {0}ms. + Analysis complete in {0}ms. + + + + Analyzer Reference + Analyzer Reference + + Both a MSBuild project file and solution file found in '{0}'. Specify which to use with the --workspace option. Foram encontrados um arquivo de solução e um arquivo de projeto MSBuild em '{0}'. Especifique qual usar com a opção --espaço de trabalho. @@ -37,6 +47,11 @@ Cannot specify both project argument and workspace option. + + Code Style + Code Style + + Complete in {0}ms. Complete in {0}ms. @@ -52,11 +67,21 @@ Não foi possível formatar '{0}'. O formato atualmente suporta apenas projetos do Visual Basic e C#. + + Determining diagnostics... + Determining diagnostics... + + Determining formattable files. Determining formattable files. + + Failed to apply code fix {0} for {1}: {2} + Failed to apply code fix {0} for {1}: {2} + + Failed to save formatting changes. Falha ao salvar alterações de formatação. @@ -87,6 +112,11 @@ Fix whitespace formatting. + + Fixing diagnostics... + Fixing diagnostics... + + Format complete in {0}ms. Format complete in {0}ms. @@ -142,6 +172,11 @@ Run code style analyzers and apply fixes. + + Running {0} analysis. + Running {0} analysis. + + Running formatters. Running formatters. @@ -157,6 +192,11 @@ Ignorando o projeto referenciado '{0}'. + + Solution {0} has no projects + Solution {0} has no projects + + The dotnet CLI version is '{0}'. The dotnet CLI version is '{0}'. @@ -207,6 +247,11 @@ The `--workspace` option is deprecated. Use the `<project>` argument instead. + + Unable to fix {0}. Code fix {1} doesn't support Fix All in Solution. + Unable to fix {0}. Code fix {1} doesn't support Fix All in Solution. + + Unable to locate MSBuild. Ensure the .NET SDK was installed with the official installer. Unable to locate MSBuild. Ensure the .NET SDK was installed with the official installer. diff --git a/src/xlf/Resources.ru.xlf b/src/xlf/Resources.ru.xlf index db622fd2ed..3aa559f191 100644 --- a/src/xlf/Resources.ru.xlf +++ b/src/xlf/Resources.ru.xlf @@ -17,6 +17,16 @@ Accepts a file path, which if provided, will produce a json report in the given directory. + + Analysis complete in {0}ms. + Analysis complete in {0}ms. + + + + Analyzer Reference + Analyzer Reference + + Both a MSBuild project file and solution file found in '{0}'. Specify which to use with the --workspace option. В "{0}" обнаружены как файл проекта, так и файл решения MSBuild. Укажите используемый файл с помощью параметра --workspace. @@ -37,6 +47,11 @@ Cannot specify both project argument and workspace option. + + Code Style + Code Style + + Complete in {0}ms. Complete in {0}ms. @@ -52,11 +67,21 @@ Не удалось отформатировать "{0}". Форматирование сейчас поддерживается только для проектов C# и Visual Basic. + + Determining diagnostics... + Determining diagnostics... + + Determining formattable files. Determining formattable files. + + Failed to apply code fix {0} for {1}: {2} + Failed to apply code fix {0} for {1}: {2} + + Failed to save formatting changes. Не удалось сохранить изменения форматирования. @@ -87,6 +112,11 @@ Fix whitespace formatting. + + Fixing diagnostics... + Fixing diagnostics... + + Format complete in {0}ms. Format complete in {0}ms. @@ -142,6 +172,11 @@ Run code style analyzers and apply fixes. + + Running {0} analysis. + Running {0} analysis. + + Running formatters. Running formatters. @@ -157,6 +192,11 @@ Пропуск указанного проекта "{0}". + + Solution {0} has no projects + Solution {0} has no projects + + The dotnet CLI version is '{0}'. The dotnet CLI version is '{0}'. @@ -207,6 +247,11 @@ The `--workspace` option is deprecated. Use the `<project>` argument instead. + + Unable to fix {0}. Code fix {1} doesn't support Fix All in Solution. + Unable to fix {0}. Code fix {1} doesn't support Fix All in Solution. + + Unable to locate MSBuild. Ensure the .NET SDK was installed with the official installer. Unable to locate MSBuild. Ensure the .NET SDK was installed with the official installer. diff --git a/src/xlf/Resources.tr.xlf b/src/xlf/Resources.tr.xlf index 8f8afdd3a0..a3a9b05b50 100644 --- a/src/xlf/Resources.tr.xlf +++ b/src/xlf/Resources.tr.xlf @@ -17,6 +17,16 @@ Accepts a file path, which if provided, will produce a json report in the given directory. + + Analysis complete in {0}ms. + Analysis complete in {0}ms. + + + + Analyzer Reference + Analyzer Reference + + Both a MSBuild project file and solution file found in '{0}'. Specify which to use with the --workspace option. Hem bir MSBuild proje dosyası ve çözüm dosyası '{0}' içinde bulundu. Hangi--çalışma alanı seçeneği ile kullanmak için belirtin. @@ -37,6 +47,11 @@ Cannot specify both project argument and workspace option. + + Code Style + Code Style + + Complete in {0}ms. Complete in {0}ms. @@ -52,11 +67,21 @@ '{0}' biçiminde değil. Biçimi şu anda yalnızca C# ve Visual Basic projeleri desteklemektedir. + + Determining diagnostics... + Determining diagnostics... + + Determining formattable files. Determining formattable files. + + Failed to apply code fix {0} for {1}: {2} + Failed to apply code fix {0} for {1}: {2} + + Failed to save formatting changes. Biçimlendirme değişiklikleri kaydedilemedi. @@ -87,6 +112,11 @@ Fix whitespace formatting. + + Fixing diagnostics... + Fixing diagnostics... + + Format complete in {0}ms. Format complete in {0}ms. @@ -142,6 +172,11 @@ Run code style analyzers and apply fixes. + + Running {0} analysis. + Running {0} analysis. + + Running formatters. Running formatters. @@ -157,6 +192,11 @@ Atlama projesi '{0}' başvuru. + + Solution {0} has no projects + Solution {0} has no projects + + The dotnet CLI version is '{0}'. The dotnet CLI version is '{0}'. @@ -207,6 +247,11 @@ The `--workspace` option is deprecated. Use the `<project>` argument instead. + + Unable to fix {0}. Code fix {1} doesn't support Fix All in Solution. + Unable to fix {0}. Code fix {1} doesn't support Fix All in Solution. + + Unable to locate MSBuild. Ensure the .NET SDK was installed with the official installer. Unable to locate MSBuild. Ensure the .NET SDK was installed with the official installer. diff --git a/src/xlf/Resources.zh-Hans.xlf b/src/xlf/Resources.zh-Hans.xlf index 3c196cefd5..0fb9804162 100644 --- a/src/xlf/Resources.zh-Hans.xlf +++ b/src/xlf/Resources.zh-Hans.xlf @@ -17,6 +17,16 @@ Accepts a file path, which if provided, will produce a json report in the given directory. + + Analysis complete in {0}ms. + Analysis complete in {0}ms. + + + + Analyzer Reference + Analyzer Reference + + Both a MSBuild project file and solution file found in '{0}'. Specify which to use with the --workspace option. 在“{0}”中同时找到 MSBuild 项目文件和解决方案文件。请指定要将哪一个文件用于 --workspace 选项。 @@ -37,6 +47,11 @@ Cannot specify both project argument and workspace option. + + Code Style + Code Style + + Complete in {0}ms. Complete in {0}ms. @@ -52,11 +67,21 @@ 无法设置“{0}”的格式。该格式当前仅支持 C# 和 Visual Basic 项目。 + + Determining diagnostics... + Determining diagnostics... + + Determining formattable files. Determining formattable files. + + Failed to apply code fix {0} for {1}: {2} + Failed to apply code fix {0} for {1}: {2} + + Failed to save formatting changes. 未能保存格式更改。 @@ -87,6 +112,11 @@ Fix whitespace formatting. + + Fixing diagnostics... + Fixing diagnostics... + + Format complete in {0}ms. Format complete in {0}ms. @@ -142,6 +172,11 @@ Run code style analyzers and apply fixes. + + Running {0} analysis. + Running {0} analysis. + + Running formatters. Running formatters. @@ -157,6 +192,11 @@ 正在跳过引用的项目“{0}”。 + + Solution {0} has no projects + Solution {0} has no projects + + The dotnet CLI version is '{0}'. The dotnet CLI version is '{0}'. @@ -207,6 +247,11 @@ The `--workspace` option is deprecated. Use the `<project>` argument instead. + + Unable to fix {0}. Code fix {1} doesn't support Fix All in Solution. + Unable to fix {0}. Code fix {1} doesn't support Fix All in Solution. + + Unable to locate MSBuild. Ensure the .NET SDK was installed with the official installer. Unable to locate MSBuild. Ensure the .NET SDK was installed with the official installer. diff --git a/src/xlf/Resources.zh-Hant.xlf b/src/xlf/Resources.zh-Hant.xlf index 5df45261a4..574df84741 100644 --- a/src/xlf/Resources.zh-Hant.xlf +++ b/src/xlf/Resources.zh-Hant.xlf @@ -17,6 +17,16 @@ Accepts a file path, which if provided, will produce a json report in the given directory. + + Analysis complete in {0}ms. + Analysis complete in {0}ms. + + + + Analyzer Reference + Analyzer Reference + + Both a MSBuild project file and solution file found in '{0}'. Specify which to use with the --workspace option. 在 '{0}' 中同時找到 MSBuild 專案檔和解決方案檔。請指定要搭配 --workspace 選項使用的檔案。 @@ -37,6 +47,11 @@ Cannot specify both project argument and workspace option. + + Code Style + Code Style + + Complete in {0}ms. Complete in {0}ms. @@ -52,11 +67,21 @@ 無法將 '{0}' 格式化。格式化目前只支援 C# 和 Visual Basic 專案。 + + Determining diagnostics... + Determining diagnostics... + + Determining formattable files. Determining formattable files. + + Failed to apply code fix {0} for {1}: {2} + Failed to apply code fix {0} for {1}: {2} + + Failed to save formatting changes. 無法儲存格式化變更。 @@ -87,6 +112,11 @@ Fix whitespace formatting. + + Fixing diagnostics... + Fixing diagnostics... + + Format complete in {0}ms. Format complete in {0}ms. @@ -142,6 +172,11 @@ Run code style analyzers and apply fixes. + + Running {0} analysis. + Running {0} analysis. + + Running formatters. Running formatters. @@ -157,6 +192,11 @@ 跳過參考的專案 '{0}’。 + + Solution {0} has no projects + Solution {0} has no projects + + The dotnet CLI version is '{0}'. The dotnet CLI version is '{0}'. @@ -207,6 +247,11 @@ The `--workspace` option is deprecated. Use the `<project>` argument instead. + + Unable to fix {0}. Code fix {1} doesn't support Fix All in Solution. + Unable to fix {0}. Code fix {1} doesn't support Fix All in Solution. + + Unable to locate MSBuild. Ensure the .NET SDK was installed with the official installer. Unable to locate MSBuild. Ensure the .NET SDK was installed with the official installer. From adf693c45dbc69c9dbbb2a3b3e889c8e9e5ce278 Mon Sep 17 00:00:00 2001 From: Jonathon Marolf Date: Tue, 23 Jun 2020 07:57:39 -0700 Subject: [PATCH 2/2] add analyzer reflection tests --- src/Analyzers/AnalyzerFinderHelpers.cs | 6 +- .../AnalyzerReferenceAnalyzerFinder.cs | 2 +- .../RoslynCodeStyleAnalyzerFinder.cs | 2 +- tests/Analyzers/AnalyzerAssemblyGenerator.cs | 112 ++++++++++++++ tests/Analyzers/FilterDiagnosticsTests.cs | 83 +++++++++++ .../Analyzers/LoadAnalyzersAndFixersTests.cs | 141 ++++++++++++++++++ tests/dotnet-format.UnitTests.csproj | 3 + 7 files changed, 343 insertions(+), 6 deletions(-) create mode 100644 tests/Analyzers/AnalyzerAssemblyGenerator.cs create mode 100644 tests/Analyzers/FilterDiagnosticsTests.cs create mode 100644 tests/Analyzers/LoadAnalyzersAndFixersTests.cs diff --git a/src/Analyzers/AnalyzerFinderHelpers.cs b/src/Analyzers/AnalyzerFinderHelpers.cs index 21ab8eaa09..cb069816c0 100644 --- a/src/Analyzers/AnalyzerFinderHelpers.cs +++ b/src/Analyzers/AnalyzerFinderHelpers.cs @@ -6,17 +6,15 @@ using System.Reflection; using System.Threading; using System.Threading.Tasks; + using Microsoft.CodeAnalysis.CodeFixes; using Microsoft.CodeAnalysis.Diagnostics; -using Microsoft.Extensions.Logging; namespace Microsoft.CodeAnalysis.Tools.Analyzers { internal static class AnalyzerFinderHelpers { - public static ImmutableArray<(DiagnosticAnalyzer Analyzer, CodeFixProvider? Fixer)> LoadAnalyzersAndFixers( - IEnumerable assemblies, - ILogger logger) + public static ImmutableArray<(DiagnosticAnalyzer Analyzer, CodeFixProvider? Fixer)> LoadAnalyzersAndFixers(IEnumerable assemblies) { var types = assemblies .SelectMany(assembly => assembly.GetTypes() diff --git a/src/Analyzers/AnalyzerReferenceAnalyzerFinder.cs b/src/Analyzers/AnalyzerReferenceAnalyzerFinder.cs index 7e4e053f0a..3d376abb1e 100644 --- a/src/Analyzers/AnalyzerReferenceAnalyzerFinder.cs +++ b/src/Analyzers/AnalyzerReferenceAnalyzerFinder.cs @@ -30,7 +30,7 @@ internal class AnalyzerReferenceAnalyzerFinder : IAnalyzerFinder .Distinct() .Select(path => Assembly.LoadFrom(path)); - return AnalyzerFinderHelpers.LoadAnalyzersAndFixers(assemblies, logger); + return AnalyzerFinderHelpers.LoadAnalyzersAndFixers(assemblies); } public Task>> FilterBySeverityAsync( diff --git a/src/Analyzers/RoslynCodeStyleAnalyzerFinder.cs b/src/Analyzers/RoslynCodeStyleAnalyzerFinder.cs index fea651ba5f..40463d9022 100644 --- a/src/Analyzers/RoslynCodeStyleAnalyzerFinder.cs +++ b/src/Analyzers/RoslynCodeStyleAnalyzerFinder.cs @@ -36,7 +36,7 @@ internal class RoslynCodeStyleAnalyzerFinder : IAnalyzerFinder _featuresVisualBasicPath }.Select(path => Assembly.LoadFrom(path)); - return AnalyzerFinderHelpers.LoadAnalyzersAndFixers(assemblies, logger); + return AnalyzerFinderHelpers.LoadAnalyzersAndFixers(assemblies); } public Task>> FilterBySeverityAsync( diff --git a/tests/Analyzers/AnalyzerAssemblyGenerator.cs b/tests/Analyzers/AnalyzerAssemblyGenerator.cs new file mode 100644 index 0000000000..d0ae19429c --- /dev/null +++ b/tests/Analyzers/AnalyzerAssemblyGenerator.cs @@ -0,0 +1,112 @@ +// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Collections.Generic; +using System.Collections.Immutable; +using System.Composition; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Threading; +using System.Threading.Tasks; + +using Microsoft.CodeAnalysis.CodeFixes; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.Diagnostics; +using Microsoft.CodeAnalysis.Testing; + +namespace Microsoft.CodeAnalysis.Tools.Tests.Analyzers +{ + public static class AnalyzerAssemblyGenerator + { + public static SyntaxTree GenerateCodeFix(string typeName, string diagnosticId) + { + var codefix = $@" +using System; +using System.Collections.Immutable; +using System.Composition; +using System.Threading.Tasks; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CodeFixes; +using Microsoft.CodeAnalysis.Diagnostics; +[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof({typeName})), Shared] +public class {typeName} : CodeFixProvider +{{ + public const string DiagnosticId = ""{diagnosticId}""; + + public sealed override ImmutableArray FixableDiagnosticIds + => ImmutableArray.Create(DiagnosticId); + + public sealed override FixAllProvider GetFixAllProvider() + {{ + return WellKnownFixAllProviders.BatchFixer; + }} + + public sealed override Task RegisterCodeFixesAsync(CodeFixContext context) + {{ + throw new NotImplementedException(); + }} +}}"; + return CSharpSyntaxTree.ParseText(codefix); + } + + public static SyntaxTree GenerateAnalyzerCode(string typeName, string diagnosticId) + { + var analyzer = $@" +using System.Collections.Immutable; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Diagnostics; +[DiagnosticAnalyzer(LanguageNames.CSharp)] +public class {typeName} : DiagnosticAnalyzer +{{ + public const string DiagnosticId = ""{diagnosticId}""; + internal static readonly LocalizableString Title = ""{typeName} Title""; + internal static readonly LocalizableString MessageFormat = ""{typeName} '{{0}}'""; + internal const string Category = ""{typeName} Category""; + internal static DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Warning, true); + public override ImmutableArray SupportedDiagnostics => ImmutableArray.Create(Rule); + public override void Initialize(AnalysisContext context) + {{ + }} +}}"; + return CSharpSyntaxTree.ParseText(analyzer); + } + + public static async Task GenerateAssemblyAsync(params SyntaxTree[] trees) + { + var assemblyName = Guid.NewGuid().ToString(); + var references = new List() + { + MetadataReference.CreateFromFile(typeof(ImmutableArray).Assembly.Location), + MetadataReference.CreateFromFile(typeof(SharedAttribute).Assembly.Location), + MetadataReference.CreateFromFile(typeof(CSharpCompilation).Assembly.Location), + MetadataReference.CreateFromFile(typeof(DiagnosticAnalyzer).Assembly.Location), + MetadataReference.CreateFromFile(typeof(CodeFixProvider).Assembly.Location), + }; + + var netstandardMetaDataReferences = await ReferenceAssemblies.NetStandard.NetStandard20.ResolveAsync(LanguageNames.CSharp, CancellationToken.None); + references.AddRange(netstandardMetaDataReferences); + var compilation = CSharpCompilation.Create(assemblyName, trees, references, + options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); + using (var ms = new MemoryStream()) + { + var result = compilation.Emit(ms); + if (!result.Success) + { + var failures = result.Diagnostics.Where(diagnostic => + diagnostic.IsWarningAsError || + diagnostic.Severity == DiagnosticSeverity.Error) + .Select(diagnostic => $"{diagnostic.Id}: {diagnostic.GetMessage()}"); + + throw new Exception(string.Join(Environment.NewLine, failures)); + } + else + { + ms.Seek(0, SeekOrigin.Begin); + var assembly = Assembly.Load(ms.ToArray()); + return assembly; + } + } + } + } +} diff --git a/tests/Analyzers/FilterDiagnosticsTests.cs b/tests/Analyzers/FilterDiagnosticsTests.cs new file mode 100644 index 0000000000..65c4ae573e --- /dev/null +++ b/tests/Analyzers/FilterDiagnosticsTests.cs @@ -0,0 +1,83 @@ +using System; +using System.Collections.Generic; +using System.Collections.Immutable; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +using Microsoft.CodeAnalysis.Diagnostics; +using Microsoft.CodeAnalysis.Testing; +using Microsoft.CodeAnalysis.Text; +using Microsoft.CodeAnalysis.Tools.Analyzers; +using Microsoft.CodeAnalysis.Tools.Formatters; +using Microsoft.CodeAnalysis.Tools.Tests.Formatters; + +using Xunit; + +namespace Microsoft.CodeAnalysis.Tools.Tests.Analyzers +{ + using static AnalyzerAssemblyGenerator; + + public class FilterDiagnosticsTests : CSharpFormatterTests + { + [Fact] + public async Task TestFilterWarning() + { + var projects = GetProjects(); + var allAnalyzers = await GetAnalyzersAsync(); + var formattablePaths = ImmutableHashSet.Create(projects.First().Documents.First().FilePath); + var minimumSeverity = DiagnosticSeverity.Warning; + var result = await AnalyzerFinderHelpers.FilterBySeverityAsync(projects, + allAnalyzers, + formattablePaths, + minimumSeverity, + CancellationToken.None); + var (_, analyzers) = Assert.Single(result); + Assert.Single(analyzers); + } + + [Fact] + public async Task TestFilterError() + { + var projects = GetProjects(); + var allAnalyzers = await GetAnalyzersAsync(); + var formattablePaths = ImmutableHashSet.Create(projects.First().Documents.First().FilePath); + var minimumSeverity = DiagnosticSeverity.Error; + var result = await AnalyzerFinderHelpers.FilterBySeverityAsync(projects, + allAnalyzers, + formattablePaths, + minimumSeverity, + CancellationToken.None); + var (_, analyzers) = Assert.Single(result); + Assert.Empty(analyzers); + } + + private async Task> GetAnalyzersAsync() + { + var assemblies = new[] + { + await GenerateAssemblyAsync( + GenerateAnalyzerCode("DiagnosticAnalyzer1", "DiagnosticAnalyzerId"), + GenerateCodeFix("CodeFixProvider1", "DiagnosticAnalyzerId")) + }; + + var analyzersAndFixers = AnalyzerFinderHelpers.LoadAnalyzersAndFixers(assemblies); + return ImmutableArray.Create(analyzersAndFixers[0].Analyzer); + } + + private IEnumerable GetProjects() + { + var text = SourceText.From(""); + TestState.Sources.Add(text); + + var solution = GetSolution(TestState.Sources.ToArray(), + TestState.AdditionalFiles.ToArray(), + TestState.AdditionalReferences.ToArray(), + "root = true"); + return solution.Projects; + } + + private protected override ICodeFormatter Formatter { get; } + } +} diff --git a/tests/Analyzers/LoadAnalyzersAndFixersTests.cs b/tests/Analyzers/LoadAnalyzersAndFixersTests.cs new file mode 100644 index 0000000000..e844945853 --- /dev/null +++ b/tests/Analyzers/LoadAnalyzersAndFixersTests.cs @@ -0,0 +1,141 @@ +// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System.Threading.Tasks; + +using Microsoft.CodeAnalysis.CodeFixes; +using Microsoft.CodeAnalysis.Diagnostics; +using Microsoft.CodeAnalysis.Tools.Analyzers; + +using Xunit; + +namespace Microsoft.CodeAnalysis.Tools.Tests.Analyzers +{ + using static AnalyzerAssemblyGenerator; + + public class LoadAnalyzersAndFixersTests + { + [Fact] + public static async Task TestSingleAnalyzerAndFixerAsync() + { + var assemblies = new[] + { + await GenerateAssemblyAsync( + GenerateAnalyzerCode("DiagnosticAnalyzer1", "DiagnosticAnalyzerId"), + GenerateCodeFix("CodeFixProvider1", "DiagnosticAnalyzerId")) + }; + + var analyzersAndFixers = AnalyzerFinderHelpers.LoadAnalyzersAndFixers(assemblies); + var (analyzer, fixer) = Assert.Single(analyzersAndFixers); + var analyzerDiagnosticDescriptor = Assert.Single(analyzer.SupportedDiagnostics); + var fixerDiagnosticId = Assert.Single(fixer.FixableDiagnosticIds); + Assert.Equal(analyzerDiagnosticDescriptor.Id, fixerDiagnosticId); + } + + [Fact] + public static async Task TestMultipleAnalyzersAndFixersAsync() + { + var assemblies = new[] + { + await GenerateAssemblyAsync( + GenerateAnalyzerCode("DiagnosticAnalyzer1", "DiagnosticAnalyzerId1"), + GenerateAnalyzerCode("DiagnosticAnalyzer2", "DiagnosticAnalyzerId2"), + GenerateCodeFix("CodeFixProvider1", "DiagnosticAnalyzerId1"), + GenerateCodeFix("CodeFixProvider2", "DiagnosticAnalyzerId2")) + }; + + var analyzersAndFixers = AnalyzerFinderHelpers.LoadAnalyzersAndFixers(assemblies); + Assert.Equal(2, analyzersAndFixers.Length); + Assert.Collection(analyzersAndFixers, VerifyAnalyzerCodeFixTuple, VerifyAnalyzerCodeFixTuple); + } + + [Fact] + public static async Task TestMultipleAnalyzersAndFixersFromTwoAssembliesAsync() + { + var assemblies = new[] + { + await GenerateAssemblyAsync( + GenerateAnalyzerCode("DiagnosticAnalyzer1", "DiagnosticAnalyzerId1"), + GenerateCodeFix("CodeFixProvider1", "DiagnosticAnalyzerId1")), + await GenerateAssemblyAsync( + GenerateAnalyzerCode("DiagnosticAnalyzer2", "DiagnosticAnalyzerId2"), + GenerateCodeFix("CodeFixProvider2", "DiagnosticAnalyzerId2")), + }; + var analyzersAndFixers = AnalyzerFinderHelpers.LoadAnalyzersAndFixers(assemblies); + Assert.Equal(2, analyzersAndFixers.Length); + Assert.Collection(analyzersAndFixers, VerifyAnalyzerCodeFixTuple, VerifyAnalyzerCodeFixTuple); + } + + [Fact] + public static async Task NonMatchingIdsAsync() + { + var assemblies = new[] + { + await GenerateAssemblyAsync( + GenerateAnalyzerCode("DiagnosticAnalyzer1", "DiagnosticAnalyzerId"), + GenerateCodeFix("CodeFixProvider1", "CodeFixProviderId")) + }; + + var analyzersAndFixers = AnalyzerFinderHelpers.LoadAnalyzersAndFixers(assemblies); + Assert.Empty(analyzersAndFixers); + } + + [Fact] + public static async Task SomeMatchingIdsAsync() + { + var assemblies = new[] + { + await GenerateAssemblyAsync( + GenerateAnalyzerCode("DiagnosticAnalyzer1", "DiagnosticAnalyzerId1"), + GenerateAnalyzerCode("DiagnosticAnalyzer2", "DiagnosticAnalyzerId2"), + GenerateCodeFix("CodeFixProvider1", "DiagnosticAnalyzerId1"), + GenerateCodeFix("CodeFixProvider2", "CodeFixProviderId")) + }; + + var analyzersAndFixers = AnalyzerFinderHelpers.LoadAnalyzersAndFixers(assemblies); + var (analyzer, fixer) = Assert.Single(analyzersAndFixers); + var analyzerDiagnosticDescriptor = Assert.Single(analyzer.SupportedDiagnostics); + var fixerDiagnosticId = Assert.Single(fixer.FixableDiagnosticIds); + Assert.Equal(analyzerDiagnosticDescriptor.Id, fixerDiagnosticId); + } + + [Fact] + public static async Task SingleIdMapstoMultipleFixersAsync() + { + var assemblies = new[] + { + await GenerateAssemblyAsync( + GenerateAnalyzerCode("DiagnosticAnalyzer1", "DiagnosticAnalyzerId1"), + GenerateAnalyzerCode("DiagnosticAnalyzer2", "DiagnosticAnalyzerId1"), + GenerateCodeFix("CodeFixProvider1", "DiagnosticAnalyzerId1"), + GenerateCodeFix("CodeFixProvider2", "CodeFixProviderId")) + }; + + var analyzersAndFixers = AnalyzerFinderHelpers.LoadAnalyzersAndFixers(assemblies); + Assert.Equal(2, analyzersAndFixers.Length); + Assert.Collection(analyzersAndFixers, VerifyAnalyzerCodeFixTuple, VerifyAnalyzerCodeFixTuple); + } + + [Fact] + public static async Task MultipleIdsMaptoSingleFixerAsync() + { + var assemblies = new[] + { + await GenerateAssemblyAsync( + GenerateAnalyzerCode("DiagnosticAnalyzer1", "DiagnosticAnalyzerId1"), + GenerateAnalyzerCode("DiagnosticAnalyzer2", "DiagnosticAnalyzerId1"), + GenerateCodeFix("CodeFixProvider1", "DiagnosticAnalyzerId1")) + }; + + var analyzersAndFixers = AnalyzerFinderHelpers.LoadAnalyzersAndFixers(assemblies); + Assert.Equal(2, analyzersAndFixers.Length); + Assert.Collection(analyzersAndFixers, VerifyAnalyzerCodeFixTuple, VerifyAnalyzerCodeFixTuple); + } + + private static void VerifyAnalyzerCodeFixTuple((DiagnosticAnalyzer Analyzer, CodeFixProvider Fixer) tuple) + { + var analyzerDiagnosticDescriptor = Assert.Single(tuple.Analyzer.SupportedDiagnostics); + var fixerDiagnosticId = Assert.Single(tuple.Fixer.FixableDiagnosticIds); + Assert.Equal(analyzerDiagnosticDescriptor.Id, fixerDiagnosticId); + } + } +} diff --git a/tests/dotnet-format.UnitTests.csproj b/tests/dotnet-format.UnitTests.csproj index d002e8609c..0c3a8f02fd 100644 --- a/tests/dotnet-format.UnitTests.csproj +++ b/tests/dotnet-format.UnitTests.csproj @@ -11,8 +11,11 @@ + + +