C#包装程序中的StyleCop无法检测到违规 [英] StyleCop from C# wrapper not detecting violations

查看:79
本文介绍了C#包装程序中的StyleCop无法检测到违规的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 GitHub存储库尝试从CMD运行StyleCop.我下载了GitHub存储库,并使用VS2017将其构建在本地计算机上.我试图运行批处理脚本 这是我到目前为止所做的:

I'm using this GitHub repo to try and run StyleCop from CMD. I downloaded the GitHub repo and built it on my local machine using VS2017. I tried to run the batch script This is what I've done so far:

  • 以ZIP格式下载并提取到文件夹中.
  • 在VS2017中打开解决方案并进行构建(没有错误).

然后我尝试运行'sample.bat'(.. \ source \ StyleCopCmdLine \ bin \ Debug \ sample.bat),该操作执行以下操作(由所有者测试回购的来源):

I then tried to run 'sample.bat' (..\source\StyleCopCmdLine\bin\Debug\sample.bat), which does the following (and was created by the owners of the source to test the repo):

  • 加载源文件 .. \ source \ StyleCopWrapper.Tests \ Testfiles \ FileWith7Errors.cs
  • 加载设置文件 .. \ source \ StyleCopWrapper.Tests \ TestFiles \ AllSettingsEnabled.StyleCop
  • Load source file ..\source\StyleCopWrapper.Tests\Testfiles\FileWith7Errors.cs
  • Load settings file ..\source\StyleCopWrapper.Tests\TestFiles\AllSettingsEnabled.StyleCop

然后,批处理脚本运行StyleCopCmdLine并返回以下内容:

The batch script then runs StyleCopCmdLine and returns the following:

C:\Users\X\Desktop\Test\StyleCopCmdLine- 
master\source\StyleCopCmdLine\bin\Debug>Rem Runs the command line with 
reasonable parameters

C:\Users\X\Desktop\Test\StyleCopCmdLine- 
master\source\StyleCopCmdLine\bin\Debug>StyleCopCmdLine -- 
SourceFiles="..\..\..\StyleCopWrapper.Tests\TestFiles\FileWith7Errors.cs" -- 
s=@"..\..\..\StyleCopWrapper.Tests\TestFiles\AllSettingsEnabled.StyleCop"
StyleCopCmdLine 1.0.0.0
Copyright ©  2017

  -f, --SourceFiles                    Required. The files or folders to scan.
                                       Multiple files or folders can be listed

  -s, --SettingsFile                   Required. The settings to use.

  --AdditionalAddInPaths               (Default: System.String[]) The paths to
                                       rules files. Multiple folders can be
                                       listed

  --ShowOutput                         (Default: False) Show the addin of files
                                       to scan output in the log.

  --CacheResults                       (Default: False) Cache scan results.

  --XmlOutputFile                      (Default: .\stylecop.xml) Xml Output
                                       File.

  --LogFile                            (Default: .\stylecop.log) Log File.

  --ForceFullAnalysis                  (Default: True) Force a full analysis.

  --TreatViolationsErrorsAsWarnings    (Default: True) Treat violation errors
                                       as warnings.

  --MaximumViolationCount              (Default: 1000) Maximum violations
                                       before the scan is stopped.

  -v, --verbose                        (Default: True) Prints the configuration
                                       messages to standard output.

  --help                               Display this help screen.


SourceFiles: ..\..\..\StyleCopWrapper.Tests\TestFiles\FileWith7Errors.cs
SettingsFile: 
@..\..\..\StyleCopWrapper.Tests\TestFiles\AllSettingsEnabled.StyleCop
MaximumViolationCount: 1000
ShowOutput: False
CacheResults: False
ForceFullAnalysis: True
XmlOutputFile: .\stylecop.xml
LogFile: .\stylecop.log
TreatViolationsErrorsAsWarnings: True
AdditionalAddInPaths: C:\Program Files (x86)\StyleCop 4.7


No violations encountered
Succeeded [True]
Violation count [0]

C:\Users\X\Desktop\Test\StyleCopCmdLine-master\source\StyleCopCmdLine\bin\Debug>pause

如您所见,该脚本未返回任何违规,但是当我打开StyleCopCmdLine解决方案时,导航至文件"FileWith7Errors",右键单击并单击运行StyleCop",它将返回7个违规.

As you can see, the script returns no violations, yet when I open the StyleCopCmdLine solution, navigate to the file 'FileWith7Errors', right-click and click 'Run StyleCop', it comes back with 7 violations.

我还尝试创建自己的使用StyleCopConsole的StyleCop模块(来自官方StyleCop存储库),但是当我在任何有违规行为的文件上运行它时(我确保选择了正确的设置文件),我永远不会违反任何规则...

I've also tried to create my own StyleCop module that uses the StyleCopConsole (from the official StyleCop repo), but when I run it on any file with violations (I make sure I have the right settings file selected), I never get any violations...

删除 C:\ Program Files(x86)\ StyleCop 4.7 会得到完全相同的结果.

Deleting the C:\Program Files (x86)\StyleCop 4.7 gives exactly the same results.

我尝试过的其他存储库,或者出现错误或相同的问题:

Other repos I've tried and either get an error, or the same issue:

  • StyleCopCLI
  • StyleCopCmd
  • StyleCop-Runner
  • StyleCopConsole

为什么到目前为止我尝试过的所有方法都不能复制简单的'RightClick-> Run StyleCop'?

Why do none of the methods I have tried so far replicate the simple 'RightClick->Run StyleCop'?

对于某些背景,我试图从CMD或PowerShell运行StyleCop分析,以便可以在TFSBuild 2015中将其作为构建步骤来运行.

For some background, I'm trying to run StyleCop analysis from CMD or PowerShell so that I can run it as a build step in TFSBuild 2015.

编辑-已在多台具有相同问题的计算机上进行了测试.

EDIT - This has been tested on multiple machines with the same issue.

编辑2 -将所有源代码文件加载到StyleCop分析器中,然后开始分析之后,分析似乎运行了约<<0.1秒-显然不是正确,所有源代码文件的分析应该花费比这更长的时间.我的想法是,在StyleCop DLL内某处捕获了一个错误,但未记录该错误,然后分析完成,但是它仍然返回"true",表示分析成功,即使分析显然未正确运行.

EDIT 2 - After I load in all the source code files into the StyleCop analyzer, and then start the analysis, the analysis appears to run for ~<0.1 seconds - which obviously isn't right, analysis of all the source code files should take longer than this. My thoughts are that somewhere inside the StyleCop DLL an error is being caught, but not logged, then the analysis finishes, however it does still returns 'true' indicating that the analysis was successful, even though it obviously wasn't run properly...

推荐答案

因此,事实证明它实际上未正确运行分析的原因是我使用的插件路径.

So it turns out the reason it wasn't actually running the analysis properly was because of the addin paths that I used.

初始化StyleCopConsole类时,它需要以下参数:

When initializing the StyleCopConsole class, it requires the following arguments:

StyleCopConsole(string settings, 
                bool writeResultsCache, 
                string outputFile, 
                ICollection<string> addInPaths, 
                bool loadFromDefaultPath);

之前,我将'addInPaths'定义为null,或者显式传递了两个StyleCop CSharp DLL路径.解决方法是改为将外接程序路径定义为在系统上安装StyleCop:

Before, I was either defining 'addInPaths' as null, or passing in the two StyleCop CSharp DLL paths explicitly. The fix was to instead define the addin path as the install of StyleCop on the system:

var addInPath = @"C:\Program Files (x86)\StyleCop 4.7";

然后,StyleCopConsole将本身获取所有必需的DLL. 现在可以使用这些设置运行分析

Then the StyleCopConsole would get all the necessary DLLs itself. The analysis now runs with those settings

这篇关于C#包装程序中的StyleCop无法检测到违规的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆