简体   繁体   中英

StyleCop from C# wrapper not detecting violations

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:

  • Downloaded as ZIP and extracted into a folder.
  • Opened the solution in VS2017 and built it (no errors).

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):

  • Load source file ..\\source\\StyleCopWrapper.Tests\\Testfiles\\FileWith7Errors.cs
  • Load settings file ..\\source\\StyleCopWrapper.Tests\\TestFiles\\AllSettingsEnabled.StyleCop

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

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.

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...

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

Why do none of the methods I have tried so far replicate the simple '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.

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. 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.

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

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. 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";

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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