"Analyzer with Code Fix" 项目模板已损坏

"Analyzer with Code Fix" project template is broken

小问题: 如何在 Visual Studio 2019 中使用 工作 单元测试项目设置 roslyn 代码分析器项目v16.6.2?

几个月前(以及一些 Visual Studio 更新),我尝试使用“代码修复分析器 (.NET Standard)”项目模板设置代码分析器项目。它运行良好,正如所有可用操作方法中所记录的那样。

今天(VS2019 v16.6.2)我想开始一个真正的分析器项目,但不幸的是,更新后的模板似乎已损坏或要发布,还有很多未完成的工作正在进行中。 (一个小问题是包管理器突然似乎无法恢复包,因为它不喜欢 vsix 项目使用与分析器项目相同的程序集名称。)

该模板包含一个单元测试项目。在我试验的早期版本中,这个测试项目包含很多代码,它们充当测试基础设施,使开发人员可以轻松地针对真实代码测试分析器。

所有这些代码现在似乎都集成在许多特定于语言的 nuget 包中。但是这些包裹

这些是显然需要的包引用(省略测试框架包):

这是示例单元测试代码:

using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using Verify = Microsoft.CodeAnalysis.CSharp.CodeFix.Testing.MSTest.CodeFixVerifier<
    Analyzer1.Analyzer1Analyzer,
    Analyzer1.Analyzer1CodeFixProvider>;

namespace Analyzer1.Test
{
    [TestClass]
    public class UnitTest
    {
        //No diagnostics expected to show up
        [TestMethod]
        public async Task TestMethod1()
        {
            var test = @"";

            await Verify.VerifyCSharpDiagnosticAsync(test);
        }

        //...

我通过添加正确的源 url 设法安装了软件包,并将所有引用的软件包更新为最新(预发布)版本。但是无论我怎么尝试,这个模板代码都无法编译,因为 namespace

Microsoft.CodeAnalysis.CSharp.CodeFix

(用于 Verify 的别名声明)找不到。谷歌搜索那个命名空间只让我回到 Microsoft.CodeAnalysis.CSharp.CodeFix.Testing.* 包的 myget.org 站点。


那么,我需要做什么才能为我的代码分析器项目设置一个工作单元测试(最好是 mstest)项目?我也可以使用直接在项目中包含所有帮助程序代码的“旧”版本,而不是使用显然未完成的 nuget 包。


更新:实施 后,模板中的第二次测试失败:

    [TestMethod]
    public async Task TestMethod2()
    {
        var test = @"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace ConsoleApplication1
{
    class TypeName
    {   
    }
}";

        var fixtest = @"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace ConsoleApplication1
{
    class TYPENAME
    {   
    }
}";

        var expected = Verify.Diagnostic("Analyzer1").WithLocation(11, 15).WithArguments("TypeName");
        await Verify.VerifyCodeFixAsync(test, expected, fixtest);
    }
}

声明发现了一个诊断,但none预期(这显然是错误的,测试显然需要一个诊断)。

所以问题仍然存在:如何修复这个测试项目?

我最近 运行 遇到了同样的问题。 感谢您提及更改后的 nuget URL。 在 VS 设置中更改它后,我已经能够通过在此处删除 .CodeFix 部分来编译项目

using Verify = Microsoft.CodeAnalysis.CSharp.CodeFix.Testing.MSTest.CodeFixVerifier<
    Analyzer1.Analyzer1Analyzer,
    Analyzer1.Analyzer1CodeFixProvider>;

然后在第一个单元测试中将 VerifyCSharpDiagnosticAsync 替换为 VerifyAnalyzerAsync,在第二个单元测试中将 VerifyCSharpFixAsync 替换为 VerifyCodeFixAsync

不幸的是,其中一个单元测试尚未运行。对于此问题的任何帮助,我将不胜感激。

我找到了几个具有工作单元测试的存储库:

他们似乎都在使用项目中包含的辅助代码的“手动”方法。虽然他们没有阐明捆绑到“测试版”nugets 中的项目模板和帮助程序代码发生了什么,但至少他们提供了一个工作起点。

更新:微软更新了文档; “构建您的第一个分析器和代码修复”教程现在在 Prerequisites 部分中有一个注释,解释了模板中的一个错误应该在 Visual Studio v16.7 中修复。该说明还提供了在此之前修复生成的项目的步骤。

更新 2:按照说明中的步骤操作。事实上,对于测试源,它们与 AndrewSilver 在他的回答中提供的相同。得到相同的结果,第二次测试失败。哦,回到正题。

更新 3:好的,我想我明白了。简短版本:将以下行添加到分析器的 Initialize 方法中:

context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);

长版

花了一些时间调试库。罪魁祸首似乎是 this line that would declare your expected diagnostic message a subject to exclusion and discard it from the list of expected results, thus failing the test due to mismatch between actual (1) and expected (now 0) diagnostics. Was about to file an issue but decided to check if anything similar already exists. It does, indeed, and it points to another lengthy thread with this solution.

顺便说一下,项目的 README file 提供了更多的用法示例。

Microsoft Visual Studio Community 2019 16.8.3

的快速更新

模板经过以下修改后现在可以使用了:

  • 使用 'Analyzer with Code Fix'
  • 初始化新解决方案
  • 在您的解决方案的根目录中添加一个 NuGet.Config 文件,即运行 以下命令:dotnet new nugetconfig
  • 修改 NugetConfig 文件以添加 roslyn-analyzers MyGet 提要,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <!--To inherit the global NuGet package sources remove the <clear/> line below -->
    <clear />
    <add key="nuget" value="https://api.nuget.org/v3/index.json" />
    <add key="roslyn-analyzers" value="https://dotnet.myget.org/F/roslyn-analyzers/api/v3/index.json" />
  </packageSources>
  
  <activePackageSource>
    <!-- this tells that all of them are active -->
    <add key="All" value="(Aggregate source)" />
  </activePackageSource>
</configuration>
  • 关闭visual studio,删除解决方案目录中的.vs文件夹
  • 运行 nuget 恢复和构建现在应该可以工作了。
  • 可选 运行 Get-Project -all | Update-Package 在包管理器控制台中,因此所有包都更新到最新版本。

roslyn (mstest) 包已移动到不同的包存储库:

您的 nuget.config 应如下所示:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <!-- Only specify feed for Arcade SDK (see https://github.com/Microsoft/msbuild/issues/2982) -->
  <packageSources>
    <clear />
    <add key="dotnet5" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet5/nuget/v3/index.json" />
    <add key="dotnet-eng" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-eng/nuget/v3/index.json" />
    <add key="dotnet-tools" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-tools/nuget/v3/index.json" />
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
  </packageSources>
  <disabledPackageSources>
    <clear />
  </disabledPackageSources>
</configuration>

参见示例https://github.com/dotnet/roslyn-analyzers/blob/master/NuGet.config