Skip to content

Linter stylecop for tests#857

Closed
igormcoelho wants to merge 32 commits intoneo-project:masterfrom
igormcoelho:linter_3x
Closed

Linter stylecop for tests#857
igormcoelho wants to merge 32 commits intoneo-project:masterfrom
igormcoelho:linter_3x

Conversation

@igormcoelho
Copy link
Copy Markdown
Contributor

@igormcoelho igormcoelho commented Jun 24, 2019

I think a great step for us is to adopt a linter and code formatting "cop" to help us maintain good quality code.
I tested StyleCop and it looks like a good alternative. It has many possibilities to configure it, according to project needs.

I just put it with some default template, but we can work on it (on tests folder) before putting it on the main project.

PS: we can give some claps to this guy here: https://medium.com/@michaelparkerdev/linting-c-in-2019-stylecop-sonar-resharper-and-roslyn-73e88af57ebd

@igormcoelho
Copy link
Copy Markdown
Contributor Author

igormcoelho commented Jun 24, 2019

Current output is:

       "/opt/neo/neo.UnitTests/neo.UnitTests.csproj" (VSTest target) (1:7) ->
       "/opt/neo/neo.UnitTests/neo.UnitTests.csproj" (default target) (1:8) ->
       (CoreCompile target) -> 
TRUNCATED BY SHARGON
    276 Warning(s)
    0 Error(s)

@igormcoelho
Copy link
Copy Markdown
Contributor Author

@erikzhang to disable some warning, just put the code (and message explaining comment) under "None" section. Step by step we can improve these rules.

One interesting case is:
UT_InteropPrices.cs(15,20): warning SA1312: Variable 'SyscallSystemRuntimeCheckWitnessHash' should begin with lower-case letter [/opt/neo/neo.UnitTests/neo.UnitTests.csproj]

I think it's right, we could define local variables starting with lower case letters. There are other possibilities too.

@igormcoelho
Copy link
Copy Markdown
Contributor Author

igormcoelho commented Jun 24, 2019

This can be right too: UT_ProtocolHandlerMailbox.cs(147,44): warning SA1012: Opening brace should be preceded by a space. [/opt/neo/neo.UnitTests/neo.UnitTests.csproj]

This line is: uut.ShallDrop(msg, new object[]{ msg }).Should().Be(false);. Perhaps it should indeed be better formatted.

Also agreee with SA1005: //handshaking This should start with an empty space... not direct text.

Also SA1003: for (int i=1; i<10; i++), it should include spaces before equals sign int i = 1.

Do you have a local code formatter @shargon? Could try to format them to "standard", to see which warnings still continue?

@shargon
Copy link
Copy Markdown
Member

shargon commented Jun 24, 2019

We could remove

Severity	Code	Description	Project	File	Line	Suppression State
Warning	SA1503	Braces should not be omitted	neo.UnitTests	C:\Sources\Neo\neo\neo.UnitTests\TestDataCache.cs	39	

and this

Severity	Code	Description	Project	File	Line	Suppression State
Warning	SA1512	Single-line comments should not be followed by blank line	neo.UnitTests	C:\Sources\Neo\neo\neo.UnitTests\UT_GasToken.cs	98	N/A

more than 1 thousand in visual studio ...

@shargon
Copy link
Copy Markdown
Member

shargon commented Jun 24, 2019

Warning	SA1413	Use trailing comma in multi-line initializers	neo.UnitTests	C:\Sources\Neo\neo\neo.UnitTests\TestUtils.cs	76	N/A

This have no sense

@igormcoelho
Copy link
Copy Markdown
Contributor Author

igormcoelho commented Jun 24, 2019

@shargon
SA1413 and SA1512 are disabled already
<Rule Id="SA1512" Action="None" /> <!-- Single-line comments should not be followed by blank line -->

Here I have "only" 200 warnings... if you have 1000, looks like the StyleCop.ruleset file is being ignored. This happened to me earlier today. Please try to adjust the neo.UnitTests.csproj file, to see warnings go down to 200...

  <ItemGroup>
    <!-- <PackageReference Include="StyleCop.MSBuild" Version="6.1.0" /> -->
    <PackageReference Include="StyleCop.Analyzers" Version="1.1.118" />
    <AdditionalFiles Include="stylecop.json" Link="stylecop.json" />
  </ItemGroup>

  <PropertyGroup>
     <CodeAnalysisRuleSet>$(SolutionDir)StyleCop.ruleset</CodeAnalysisRuleSet>
  </PropertyGroup>

Perhaps removing the SolutionDir here? I can try to adjust, but it's working already for us (pure dotnet builders). It is important that it takes effect for you visual studio guys.

@neo-project neo-project deleted a comment from codecov-io Jun 24, 2019
@shargon
Copy link
Copy Markdown
Member

shargon commented Jun 24, 2019

Done

@igormcoelho
Copy link
Copy Markdown
Contributor Author

Working fine for me Shargon, is it working locally now?
This is my output:

    276 Warning(s)
    0 Error(s)

@neo-project neo-project deleted a comment from codecov-io Jun 24, 2019
</ItemGroup>

<ItemGroup>
<!-- <PackageReference Include="StyleCop.MSBuild" Version="6.1.0" /> -->
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove this line

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

@igormcoelho
Copy link
Copy Markdown
Contributor Author

igormcoelho commented Jun 24, 2019

@shargon let's vote on a list of what's necessary, and what's not. Some sampling here (list codes is ordered. Edit the comment here.):

IMPORTANT UT_StorageItem.cs(59,23): warning SA1003: Operator '=' should be followed by whitespace. [/opt/neo/neo.UnitTests/neo.UnitTests.csproj]
DISABLED: UT_Consensus.cs(49,15): warning SA1005: Single line comment should begin with a space. [/opt/neo/neo.UnitTests/neo.UnitTests.csproj]
UT_Consensus.cs(51,52): warning SA1009: Closing parenthesis should not be followed by a space. [/opt/neo/neo.UnitTests/neo.UnitTests.csproj]
TestUtils.cs(31,41): warning SA1011: Closing square bracket should be followed by a space. [/opt/neo/neo.UnitTests/neo.UnitTests.csproj]
TestUtils.cs(31,42): warning SA1012: Opening brace should be preceded by a space. [/opt/neo/neo.UnitTests/neo.UnitTests.csproj]
IMPORTANT UT_Consensus.cs(316,91): warning SA1106: Code should not contain empty statements [/opt/neo/neo.UnitTests/neo.UnitTests.csproj]
IMPORTANT UUT_Consensus.cs(316,91): warning SA1107: Code should not contain multiple statements on one line [/opt/neo/neo.UnitTests/neo.UnitTests.csproj]
UT_Consensus.cs(92,38): warning SA1111: Closing parenthesis should be on line of last parameter [/opt/neo/neo.UnitTests/neo.UnitTests.csproj]
DISABLED UT_ConsensusServiceMailbox.cs(16,44): warning SA1025: Code should not contain multiple whitespace characters in a row. [/opt/neo/neo.UnitTests/neo.UnitTests.csproj]
DISABLED Extensions/NativeContractExtensions.cs(5,1): warning SA1208: Using directive for 'System' should appear before directive for 'Neo.VM' [/opt/neo/neo.UnitTests/neo.UnitTests.csproj]
UT_ConsensusServiceMailbox.cs(8,1): warning SA1210: Using directives should be ordered alphabetically by the namespaces. [/opt/neo/neo.UnitTests/neo.UnitTests.csproj]
IMPORTANT UT_MemoryPool.cs(140,22): warning SA1300: Element 'verifyTransactionsSortedDescending' should begin with an uppercase letter [/opt/neo/neo.UnitTests/neo.UnitTests.csproj]
TestBlockchain.cs(11,34): warning SA1306: Field 'TheNeoSystem' should begin with lower-case letter [/opt/neo/neo.UnitTests/neo.UnitTests.csproj]
DISABLED UT_UIntBenchmarks.cs(11,27): warning SA1310: Field 'MAX_TESTS' should not contain an underscore [/opt/neo/neo.UnitTests/neo.UnitTests.csproj]
UT_InteropPrices.cs(31,20): warning SA1312: Variable 'SyscallSystemStorageGetHash' should begin with lower-case letter [/opt/neo/neo.UnitTests/neo.UnitTests.csproj]
DISABLED UT_RemoteNodeMailbox.cs(22,27): warning SA1400: Element 'uut' should declare an access modifier [/opt/neo/neo.UnitTests/neo.UnitTests.csproj]
TestUtils.cs(31,42): warning SA1500: Braces for multi-line statements should not share line [/opt/neo/neo.UnitTests/neo.UnitTests.csproj]
DISABLED UT_UIntBenchmarks.cs(324,21): warning SA1503: Braces should not be omitted [/opt/neo/neo.UnitTests/neo.UnitTests.csproj]
DISABLED UT_ConcatenatedIterator.cs(8,1): warning SA1505: An opening brace should not be followed by a blank line. [/opt/neo/neo.UnitTests/neo.UnitTests.csproj]
DISABLED UT_MemoryPool.cs(82,1): warning SA1507: Code should not contain multiple blank lines in a row [/opt/neo/neo.UnitTests/neo.UnitTests.csproj]
DISABLED UT_StorageKey.cs(117,5): warning SA1508: A closing brace should not be preceded by a blank line. [/opt/neo/neo.UnitTests/neo.UnitTests.csproj]

Anything you agree? Some I agree, ordering on includes for example is a good practice on many languages. Space-related stuff we can cut I think... and closing brackets stuff too.

@neo-project neo-project deleted a comment from codecov-io Jun 24, 2019
@igormcoelho
Copy link
Copy Markdown
Contributor Author

@shargon zero warnings now \o/

I disable most of the "crazy" rules, and also those that were "too strict". I kept many rules, mostly those related to variable naming, which is very important to us (start following a minimal standard).

@erikzhang Please review the changes I did on the unit tests code, all of them were done following a standard (nothing from my head). If you think any change was not necessary, just mark it, and we try to find which rule that caused it (so we blacklist it).

vncoelho
vncoelho previously approved these changes Jun 24, 2019
Copy link
Copy Markdown
Member

@vncoelho vncoelho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amazing job, @igormcoelho and @shargon!

@erikzhang
Copy link
Copy Markdown
Member

Is this only effective for unit testing?

@igormcoelho
Copy link
Copy Markdown
Contributor Author

Is this only effective for unit testing?

Nope, just warming up here.. next step is the whole project.

@erikzhang
Copy link
Copy Markdown
Member

I need to see how it will affect the whole project, then we can discuss which rule set to be enabled or disabled.

@igormcoelho
Copy link
Copy Markdown
Contributor Author

Ok, it's reasonable. I'll put here the real numbers.

@igormcoelho
Copy link
Copy Markdown
Contributor Author

igormcoelho commented Jun 25, 2019

      (CoreCompile target) -> 
         Cryptography/ECC/ECPoint.cs(81,13): warning SA1120: Comments should contain text [/opt/neo/neo/neo.csproj]
         Cryptography/ECC/ECPoint.cs(84,13): warning SA1120: Comments should contain text [/opt/neo/neo/neo.csproj]
         Cryptography/RIPEMD160Managed.cs(19,9): warning SA1120: Comments should contain text [/opt/neo/neo/neo.csproj]
TRUNCATED BY SHARGON
         SmartContract/InteropService.NEO.cs(473,83): warning SA1312: Variable '_interface2' should begin with lower-case letter [/opt/neo/neo/neo.csproj]

    521 Warning(s)
    0 Error(s)

@igormcoelho
Copy link
Copy Markdown
Contributor Author

igormcoelho commented Jun 25, 2019

A quick analysis:

  • too many 'unnecessary" (but perhaps necessary) parenthesis on RIPEMD managed... better disable SA1119: Statement should not use unnecessary parenthesis
  • We should disable SA1120: Comments should contain text, it's useful sometimes not having text
  • This one is fixable: SA1206: The 'static' modifier should appear before 'unsafe'
  • this could be disabled SA1519: Braces should not be omitted from multi-line child statement

The rest is upper-case vs lower-case situations... I think we should adjust to some standard Erik. Even if not perfect, it's easier to organize things.

Example: _interface2 to interface2 (on SmartContract/InteropService.NEO.cs).
And the opposite: leveldb_writebatch_put -> Leveldb_writebatch_put (or CamelCase LeveldbWriteBatchPut)

@erikzhang
Copy link
Copy Markdown
Member

Let's merge other important PRs first.

@codecov-io
Copy link
Copy Markdown

codecov-io commented Jun 25, 2019

Codecov Report

Merging #857 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #857   +/-   ##
=======================================
  Coverage   43.57%   43.57%           
=======================================
  Files         177      177           
  Lines       12547    12547           
=======================================
  Hits         5467     5467           
  Misses       7080     7080

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 31bee40...f38be82. Read the comment docs.

@shargon
Copy link
Copy Markdown
Member

shargon commented Jun 25, 2019

@igormcoelho please don't spam with huge outputs xD

@vncoelho
Copy link
Copy Markdown
Member

@erikzhang, maybe it is better to decide this here first, this PR changes many files and will easily become hard to solve the conflicts...aheuahueaea

Is there anything bad in this styles or warning?

@igormcoelho
Copy link
Copy Markdown
Contributor Author

I think Erik wants to do the whole project at once... when we have some "free" time, on other PR, we advance on this one... right now, conflicts are not impossible, but may become harder. Not very important too, we can re-open if necessary.

@igormcoelho
Copy link
Copy Markdown
Contributor Author

Focusing on main PR. We can revive this in the future, if necessary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants