Skip to content
This repository has been archived by the owner on Feb 24, 2021. It is now read-only.

Add a test for Egyptian braces Meta.tests.ps1 and a fixer to Meta.Fixers.ps1 #27

Closed
PlagueHO opened this issue Feb 8, 2016 · 4 comments
Labels
enhancement The issue is an enhancement request.

Comments

@PlagueHO
Copy link
Collaborator

PlagueHO commented Feb 8, 2016

There is a StyleGuideline that states that Egyptian Braces shouldn't be used - e.g. code block open braces should appear on a new line.

It would be good if Meta.Tests.ps1 could include a test identified this problem.

Ideally, an automatic fixer for this could also be provided in Meta.Fixers.ps1.

@kwirkykat kwirkykat added the enhancement The issue is an enhancement request. label Aug 3, 2016
johlju added a commit to johlju/DscResource.Tests that referenced this issue Jul 31, 2017
- Add Script Analyzer custom rule to test functions and statements so that
  opening braces are set according to the style guideline (issue PowerShell#27).
johlju added a commit to johlju/DscResource.Tests that referenced this issue Jul 31, 2017
- Add Script Analyzer custom rule to test functions and statements so that
  opening braces are set according to the style guideline (issue PowerShell#27).
johlju added a commit to johlju/DscResource.Tests that referenced this issue Jul 31, 2017
- Add Script Analyzer custom rule to test functions and statements so that
  opening braces are set according to the style guideline (issue PowerShell#27).
johlju added a commit to johlju/DscResource.Tests that referenced this issue Jul 31, 2017
- Add Script Analyzer custom rule to test functions and statements so that
  opening braces are set according to the style guideline (issue PowerShell#27).
@johlju
Copy link
Contributor

johlju commented Jul 31, 2017

@PlagueHO I gave this a try to fix this one with PSSA custom rules. But can't seem to get it to report warnings in AppVeyor. 😞 I can see it being called three times as expected in Appveyor here.
Actually, when adding my code, the parameter custom rule stops working as well. But if I start Invoke-ScriptAnalyzer locally all custom rules works. 🤔

Would you mind take a look at the code? It must be a simple mistake somewhere that I fail to see.

I have a test branch test-dscresource-tests here that should trigger each of the custom rule (all three Measue-* functions). It should trigger on MSFT_xCluster.pms1 as seen here johlju/FailoverClusterDsc@1a63b6d.

And the actually new custom rule is in this branch.

Tested it locally:

PS V:\Source\xFailOverCluster> Invoke-ScriptAnalyzer -Path .\DSCResources\MSFT_xCluster\MSFT_xCluster.psm1 -CustomRulePath ..\DscResource.Tests\DscResource.AnalyzerRules\DscResource.
AnalyzerRules.psm1

RuleName                            Severity     FileName   Line  Message
--------                            --------     --------   ----  -------
PSDSCDscTestsPresent                Information  MSFT_xClus 1     No tests found for resource 'MSFT_xCluster'
                                                 ter.psm1
PSDSCDscExamplesPresent             Information  MSFT_xClus 1     No examples found for resource 'MSFT_xCluster'
                                                 ter.psm1
PSDSCUseVerboseMessageInDSCResource Information  MSFT_xClus 19    There is no call to Write-Verbose in DSC function ‘Get-Targe
                                                 ter.psm1         tResource’. If you are using Write-Verbose in a helper funct
                                                                  ion, suppress this rule application.
DscResource.AnalyzerRules\Measure-F Warning      MSFT_xClus 413   Functions should not have the open brace on the same line as
unctionBlockBraces                               ter.psm1          the function name. See https://github.com/PowerShell/DscRes
                                                                  ources/blob/master/StyleGuidelines.md#one-newline-before-bra
                                                                  ces
DscResource.AnalyzerRules\Measure-F Warning      MSFT_xClus 469   Opening brace on function should be followed by a new line.
unctionBlockBraces                               ter.psm1         See https://github.com/PowerShell/DscResources/blob/master/S
                                                                  tyleGuidelines.md#one-newline-after-opening-brace
DscResource.AnalyzerRules\Measure-F Warning      MSFT_xClus 488   Opening brace on functions should only be followed by one ne
unctionBlockBraces                               ter.psm1         w line. See https://github.com/PowerShell/DscResources/blob/
                                                                  master/StyleGuidelines.md#one-newline-after-opening-brace
DscResource.AnalyzerRules\Measure-P Warning      MSFT_xClus 433   The [Parameter()] attribute must start with an upper case 'P
arameterBlockParameterAttribute                  ter.psm1         '. See https://github.com/PowerShell/DscResources/blob/maste
                                                                  r/StyleGuidelines.md#correct-format-for-parameter-block
DscResource.AnalyzerRules\Measure-P Warning      MSFT_xClus 495   A [Parameter()] attribute must be the first attribute of eac
arameterBlockParameterAttribute                  ter.psm1         h parameter and be on its own line. See https://github.com/P
                                                                  owerShell/DscResources/blob/master/StyleGuidelines.md#correc
                                                                  t-format-for-parameter-block
DscResource.AnalyzerRules\Measure-S Warning      MSFT_xClus 449   Statements should not have the open brace on the same line a
tatementBlockBraces                              ter.psm1         s the statement. See https://github.com/PowerShell/DscResour
                                                                  ces/blob/master/StyleGuidelines.md#one-newline-before-braces
DscResource.AnalyzerRules\Measure-S Warning      MSFT_xClus 459   Opening brace on statements should be followed by a new line
tatementBlockBraces                              ter.psm1         . See https://github.com/PowerShell/DscResources/blob/master
                                                                  /StyleGuidelines.md#one-newline-after-opening-brace
DscResource.AnalyzerRules\Measure-S Warning      MSFT_xClus 463   Opening brace on statements should only be followed by one n
tatementBlockBraces                              ter.psm1         ew line. See https://github.com/PowerShell/DscResources/blob
                                                                  /master/StyleGuidelines.md#one-newline-after-opening-brace

johlju added a commit to johlju/DscResource.Tests that referenced this issue Jul 31, 2017
- Add Script Analyzer custom rule to test functions and statements so that
  opening braces are set according to the style guideline (issue PowerShell#27).
@johlju
Copy link
Contributor

johlju commented Jul 31, 2017

@PlagueHO When stripping code from the first function Measure-FunctionBlockBraces called by Invoke-ScriptAnalyzer the parameter rule is started to work again. It seems the code does something wrong at one point, maybe throws an error which is not shown anywhere. And that cause everything to fail.
Very strange that this works locally though. Investigating further. If you see something obvious I missed, then please let me know. :)

@johlju
Copy link
Contributor

johlju commented Jul 31, 2017

This one is making me scratch my head. 😄

So it seems that the the file locally has '\r\n' in the file, while the cloned one in AppVeyor has '\n'. Making sure the file is the same (replaced to '\n') regardless where it was run made thing to start work. Also, add check so it doesn't try to index into arrays that aren't long enough, that was one bug for other files than the one I tested on, but it was not thrown any errors. Now at the end of the day I realized that we starting Invoke-ScriptAnalyzer with 'SilentlyContinue' so that can explain that it didn't throw any errors.

BUT, for some reason Measure-StatementBlockBraces is reporting every line twice 😞 I have not been able to found out why yet. It works locally, and added the same code inside the Meta.Tests.ps1 just to be able to debug, but it works there as well.

https://ci.appveyor.com/project/johlju/xfailovercluster/build/1.6.216.0#L274

# ---- LINE 449
VERBOSE: if ( $true ) {
VERBOSE: Found: StatementOpeningBraceNotOnSameLine
# ---- LINE 459
VERBOSE: { Write-Verbose 'Hello for the third tme' -Verbose
VERBOSE: Found: StatementOpeningBraceShouldBeFollowedByNewLine
# ---- LINE 463
VERBOSE: 
VERBOSE: Found: StatementOpeningBraceShouldBeFollowedByOnlyOneNewLine

But ScriptAnalyzer is returning double entries in AppVeyor.

MSFT_xCluster.psm1 (Line 449): Statements should not have the open brace on the same line as the statement. See https://github.com/PowerShell/DscResources/blob/master/StyleGuidelines.md#one-newline-before-braces
MSFT_xCluster.psm1 (Line 459): Opening brace on statements should be followed by a new line. See https://github.com/PowerShell/DscResources/blob/master/StyleGuidelines.md#one-newline-after-opening-brace
MSFT_xCluster.psm1 (Line 463): Opening brace on statements should only be followed by one new line. See https://github.com/PowerShell/DscResources/blob/master/StyleGuidelines.md#one-newline-after-opening-brace
MSFT_xCluster.psm1 (Line 449): Statements should not have the open brace on the same line as the statement. See https://github.com/PowerShell/DscResources/blob/master/StyleGuidelines.md#one-newline-before-braces
MSFT_xCluster.psm1 (Line 459): Opening brace on statements should be followed by a new line. See https://github.com/PowerShell/DscResources/blob/master/StyleGuidelines.md#one-newline-after-opening-brace
MSFT_xCluster.psm1 (Line 463): Opening brace on statements should only be followed by one new line. See https://github.com/PowerShell/DscResources/blob/master/StyleGuidelines.md#one-newline-after-opening-brace

Locally, with the exact same version (latest) as AppVeyor it works

RuleName                            Severity     ScriptName Line  Message
--------                            --------     ---------- ----  -------
PSDSCDscTestsPresent                Information  MSFT_xClus 1     No tests found for resource 'MSFT_xCluster'
                                                 ter.psm1
PSDSCDscExamplesPresent             Information  MSFT_xClus 1     No examples found for resource 'MSFT_xCluster'
                                                 ter.psm1
PSDSCUseVerboseMessageInDSCResource Information  MSFT_xClus 19    There is no call to Write-Verbose in DSC function ‘Get-Targe
                                                 ter.psm1         tResource’. If you are using Write-Verbose in a helper funct
                                                                  ion, suppress this rule application.
DscResource.AnalyzerRules\Measure-F Warning      MSFT_xClus 413   Functions should not have the open brace on the same line as
unctionBlockBraces                               ter.psm1          the function name. See https://github.com/PowerShell/DscRes
                                                                  ources/blob/master/StyleGuidelines.md#one-newline-before-bra
                                                                  ces
DscResource.AnalyzerRules\Measure-F Warning      MSFT_xClus 469   Opening brace on function should be followed by a new line.
unctionBlockBraces                               ter.psm1         See https://github.com/PowerShell/DscResources/blob/master/S
                                                                  tyleGuidelines.md#one-newline-after-opening-brace
DscResource.AnalyzerRules\Measure-F Warning      MSFT_xClus 488   Opening brace on functions should only be followed by one ne
unctionBlockBraces                               ter.psm1         w line. See https://github.com/PowerShell/DscResources/blob/
                                                                  master/StyleGuidelines.md#one-newline-after-opening-brace
DscResource.AnalyzerRules\Measure-P Warning      MSFT_xClus 433   The [Parameter()] attribute must start with an upper case 'P
arameterBlockParameterAttribute                  ter.psm1         '. See https://github.com/PowerShell/DscResources/blob/maste
                                                                  r/StyleGuidelines.md#correct-format-for-parameter-block
DscResource.AnalyzerRules\Measure-P Warning      MSFT_xClus 495   A [Parameter()] attribute must be the first attribute of eac
arameterBlockParameterAttribute                  ter.psm1         h parameter and be on its own line. See https://github.com/P
                                                                  owerShell/DscResources/blob/master/StyleGuidelines.md#correc
                                                                  t-format-for-parameter-block
DscResource.AnalyzerRules\Measure-S Warning      MSFT_xClus 449   Statements should not have the open brace on the same line a
tatementBlockBraces                              ter.psm1         s the statement. See https://github.com/PowerShell/DscResour
                                                                  ces/blob/master/StyleGuidelines.md#one-newline-before-braces
DscResource.AnalyzerRules\Measure-S Warning      MSFT_xClus 459   Opening brace on statements should be followed by a new line
tatementBlockBraces                              ter.psm1         . See https://github.com/PowerShell/DscResources/blob/master
                                                                  /StyleGuidelines.md#one-newline-after-opening-brace
DscResource.AnalyzerRules\Measure-S Warning      MSFT_xClus 463   Opening brace on statements should only be followed by one n
tatementBlockBraces                              ter.psm1         ew line. See https://github.com/PowerShell/DscResources/blob
                                                                  /master/StyleGuidelines.md#one-newline-after-opening-brace

@johlju
Copy link
Contributor

johlju commented Aug 2, 2017

Finally! This was me being a newbie with Script Analyzer (and AST). The problem was that it calls the measure function for each script block. I thought that was the entire file because that is how it behaved locally. But in AppVeyor it was called first for the entire file, then for each function statement script block (why it didn't do that locally is still a mystery).
This has been nagging me for days... Finally I can send in a PR for this! 😄

johlju added a commit to johlju/DscResource.Tests that referenced this issue Aug 2, 2017
- Add Script Analyzer custom rule to test functions and statements so that
  opening braces are set according to the style guideline (issue PowerShell#27).
johlju added a commit to johlju/DscResource.Tests that referenced this issue Aug 8, 2017
- Add Script Analyzer custom rule to test functions and statements so that
  opening braces are set according to the style guideline (issue PowerShell#27).
johlju added a commit to johlju/DscResource.Tests that referenced this issue Aug 8, 2017
- Add Script Analyzer custom rule to test functions and statements so that
  opening braces are set according to the style guideline (issue PowerShell#27).
johlju added a commit to johlju/DscResource.Tests that referenced this issue Aug 11, 2017
- Add Script Analyzer custom rule to test functions and statements so that
  opening braces are set according to the style guideline (issue PowerShell#27).
johlju added a commit to johlju/DscResource.Tests that referenced this issue Aug 12, 2017
- Add Script Analyzer custom rule to test functions and statements so that
  opening braces are set according to the style guideline (issue PowerShell#27).
johlju added a commit to johlju/DscResource.Tests that referenced this issue Aug 17, 2017
- Add Script Analyzer custom rule to test functions and statements so that
  opening braces are set according to the style guideline (issue PowerShell#27).
johlju added a commit to johlju/DscResource.Tests that referenced this issue Aug 20, 2017
- Add Script Analyzer custom rule to test functions and statements so that
  opening braces are set according to the style guideline (issue PowerShell#27).
johlju added a commit to johlju/DscResource.Tests that referenced this issue Aug 20, 2017
- Add Script Analyzer custom rule to test functions and statements so that
  opening braces are set according to the style guideline (issue PowerShell#27).
johlju added a commit to johlju/DscResource.Tests that referenced this issue Aug 23, 2017
- Add Script Analyzer custom rule to test functions and statements so that
  opening braces are set according to the style guideline (issue PowerShell#27).
johlju added a commit to johlju/DscResource.Tests that referenced this issue Aug 31, 2017
- Add Script Analyzer custom rule to test functions and statements so that
  opening braces are set according to the style guideline (issue PowerShell#27).
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement The issue is an enhancement request.
Projects
None yet
Development

No branches or pull requests

3 participants