简体   繁体   English

C#包装程序中的StyleCop无法检测到违规

[英]StyleCop from C# wrapper not detecting violations

I'm using this GitHub repo to try and run StyleCop from CMD. 我正在使用 GitHub存储库尝试从CMD运行StyleCop。 I downloaded the GitHub repo and built it on my local machine using VS2017. 我下载了GitHub存储库,并使用VS2017在本地计算机上构建了它。 I tried to run the batch script This is what I've done so far: 我试图运行批处理脚本,这是我到目前为止所做的:

  • Downloaded as ZIP and extracted into a folder. 下载为ZIP并解压缩到一个文件夹中。
  • Opened the solution in VS2017 and built it (no errors). 在VS2017中打开了解决方案并进行了构建(无错误)。

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): 然后,我尝试运行'sample.bat' (.. \\ source \\ StyleCopCmdLine \\ bin \\ Debug \\ sample.bat) ,该操作将执行以下操作(由源所有者创建以测试存储库):

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

The batch script then runs StyleCopCmdLine and returns the following: 然后,批处理脚本运行StyleCopCmdLine并返回以下内容:

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

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

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

Deleting the C:\\Program Files (x86)\\StyleCop 4.7 gives exactly the same results. 删除C:\\ Program Files(x86)\\ StyleCop 4.7可获得完全相同的结果。

Other repos I've tried and either get an error, or the same issue: 我尝试过的其他存储库可能会出现错误或相同的问题:

  • StyleCopCLI StyleCopCLI
  • StyleCopCmd StyleCopCmd
  • StyleCop-Runner StyleCop-Runner
  • StyleCopConsole StyleCopConsole

Why do none of the methods I have tried so far replicate the simple 'RightClick->Run StyleCop' ? 为什么到目前为止我尝试过的所有方法都不能复制简单的“ RightClick-> Run StyleCop”

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. 对于某些背景,我试图从CMD或PowerShell运行StyleCop分析,以便可以将其作为TFSBuild 2015中的构建步骤来运行。

EDIT - This has been tested on multiple machines with the same issue. 编辑 -已在多台具有相同问题的计算机上进行了测试。

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. 编辑2-将所有源代码文件加载到StyleCop分析器中,然后开始分析之后,分析似乎运行了<0.1秒-显然不正确,对所有源代码文件的分析应该比这更长。 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... 我的想法是,在StyleCop DLL内的某个地方捕获了一个错误,但未记录该错误,然后分析完成,但是它仍然返回“ true”,表明分析成功,即使它显然未正确运行。 。

So it turns out the reason it wasn't actually running the analysis properly was because of the addin paths that I used. 因此,事实证明,实际上未正确运行分析的原因是由于我使用了插件路径。

When initializing the StyleCopConsole class, it requires the following arguments: 初始化StyleCopConsole类时,它需要以下参数:

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

Before, I was either defining 'addInPaths' as null, or passing in the two StyleCop CSharp DLL paths explicitly. 之前,我要么将'addInPaths'定义为null,要么显式传递两个StyleCop CSharp DLL路径。 The fix was to instead define the addin path as the install of StyleCop on the system: 解决方法是改为将外接程序路径定义为系统上StyleCop的安装:

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

Then the StyleCopConsole would get all the necessary DLLs itself. 然后,StyleCopConsole将本身获取所有必需的DLL。 The analysis now runs with those settings 现在可以使用这些设置运行分析

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM