Skip to content

Commit

Permalink
Add more analyzer rules (#1757)
Browse files Browse the repository at this point in the history
  • Loading branch information
johlju authored Jun 14, 2022
1 parent 344ac44 commit c71947e
Show file tree
Hide file tree
Showing 49 changed files with 248 additions and 150 deletions.
22 changes: 20 additions & 2 deletions .vscode/analyzersettings.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
CustomRulePath = @(
'./output/RequiredModules/DscResource.AnalyzerRules'
'./tests/QA/AnalyzerRules/SqlServerDsc.AnalyzerRules.psm1'
'./output/RequiredModules/Indented.ScriptAnalyzerRules'
)
IncludeDefaultRules = $true
IncludeRules = @(
# DSC Resource Kit style guideline rules.
# DSC Community style guideline rules from the module ScriptAnalyzer.
'PSAvoidDefaultValueForMandatoryParameter',
'PSAvoidDefaultValueSwitchParameter',
'PSAvoidInvokingEmptyMembers',
Expand Down Expand Up @@ -41,15 +42,32 @@
'PSUseDeclaredVarsMoreThanAssignments',
'PSUsePSCredentialType',

# Additional rules
# Additional rules from the module ScriptAnalyzer
'PSUseConsistentWhitespace',
'UseCorrectCasing',
'PSPlaceOpenBrace',
'PSPlaceCloseBrace',
'AlignAssignmentStatement',
'AvoidUsingDoubleQuotesForConstantString',

# Rules from the modules DscResource.AnalyzerRules and SqlServerDsc.AnalyzerRules
'Measure-*'

# Rules from the module Indented.ScriptAnalyzerRules
'AvoidCreatingObjectsFromAnEmptyString'
'AvoidDashCharacters'
'AvoidEmptyNamedBlocks'
'AvoidFilter'
'AvoidHelpMessage'
'AvoidNestedFunctions'
'AvoidNewObjectToCreatePSObject'
'AvoidParameterAttributeDefaultValues'
'AvoidProcessWithoutPipeline'
'AvoidSmartQuotes'
'AvoidThrowOutsideOfTry'
'AvoidWriteErrorStop'
'AvoidWriteOutput'
'UseSyntacticallyCorrectExamples'
)

Rules = @{
Expand Down
3 changes: 2 additions & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"recommendations": [
"davidanson.vscode-markdownlint",
"pspester.pester-test",
"ms-vscode.powershell"
"ms-vscode.powershell",
"streetsidesoftware.code-spell-checker"
]
}
55 changes: 54 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
"files.associations": {
"*.ps1xml": "xml"
},
"cSpell.dictionaries": [
"powershell"
],
"cSpell.words": [
"COMPANYNAME",
"ICONURI",
Expand All @@ -35,7 +38,57 @@
"notin",
"pscmdlet",
"steppable",
"SSRS"
"SSRS",
"Addnode",
"AppVeyor",
"CHANGELOG",
"IsUpdateable",
"PSPKI",
"AgtSvcStartupType",
"SqlTempdbFileGrowth",
"SqlTempdbLogFileGrowth",
"hashtable",
"hashtables",
"dsccommunity",
"SqlTempdbFileCount",
"SqlTempdbFileSize",
"SqlTempdbLogFileSize",
"Maxime",
"Daniou",
"Reist",
"Reilingh",
"Sarti",
"RSSQLServer",
"RSSQLInstanceName",
"DSCFCISqlBuild",
"DSCSQLBuildEncrypted",
"IsHadrEnabled",
"xSQLAOGroupJoin",
"xSQLAOGroupEnsure",
"Test-SPDSCObjectHasProperty",
"DynamicAlloc",
"GetxPDTVariable"
],
"cSpell.ignorePaths": [
".git"
],
"cSpell.ignoreRegExpList": [
"/HKCU:/g",
"/analyzersettings\\.psd1/g",
"/SQLENGINE/g",
"//norestart/g",
"/@Zuldans/g",
"/@kungfu71186/g",
"/@pshamus/g",
"/@mdaniou/g",
"/@adamrushuk/g",
"/@randomnote1/g",
"/@NReilingh/g",
"/@chasewilson/g",
"/@prkelly/g",
"/@glennsarti/g",
"/tempdb/g",
"/sqlcmd\\.exe/g"
],
"[markdown]": {
"files.trimTrailingWhitespace": true,
Expand Down
46 changes: 27 additions & 19 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- SqlServerDsc
- Added recommended VS Code extensions.
- Added settings for VS Code extension _Pester Test Adapter_.
- Added new Script Analyzer rules from the module _Indented.ScriptAnalyzerRules_
to help development and review process. The rules that did not contradict
the existing DSC Community rules and style guideline were added.
- Added the Visual Studio Code extension _Code Spell Checker_ to the list
of recommended Visual Studio Code extensions.
- CommonTestHelper
- `Import-SqlModuleStub`
- Added the optional parameter **PasThru** that, if used, will return the
Expand Down Expand Up @@ -110,12 +115,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Only update values for the properties that are actually enforced by the
configuration.
- SqlAGDatabase
- Added StatementTimeout optional parameter with default value of 600 seconds (10 mins) to SqlAGDatabase to fix Issue#1743
Users will be able to specify the backup and restore timeout with it.
- Added StatementTimeout optional parameter with default value of 600 seconds
(10 mins) to SqlAGDatabase to fix issue #1743. Users will be able to specify
the backup and restore timeout with it.
- SqlDatabaseUser
- `Test-TargetResource` returns true if the `IsUpdateable` property of the database is `$false` to resolve Issue#1748.
- `Test-TargetResource` returns true if the `IsUpdateable` property of the
database is `$false` to resolve issue #1748.
- SqlDatabaseRole
- `Test-TargetResource` returns true if the `IsUpdateable` property of the database is `$false` to resolve Issue#1750.
- `Test-TargetResource` returns true if the `IsUpdateable` property of the
database is `$false` to resolve issue #1750.

### Fixed

Expand Down Expand Up @@ -200,7 +208,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Updated pipelines files to latest from Sampler project.
- Updated GitHub issue templates.
- Remove pipeline jobs `Test_Integration_SQL2016`, `Test_Integration_SQL2017`,
and `Test_Integration_SQL2019` and raplaced with a single job
and `Test_Integration_SQL2019` and replaced with a single job
`Test_Integration` ([issue #1713](https://github.com/dsccommunity/SqlServerDsc/issues/1713)).
- Update HQRM tests to run on the VM image `windows-2022`.
- Update unit tests to run on the VM image `windows-2022`.
Expand Down Expand Up @@ -977,7 +985,7 @@ in a future release.
for the test of `Set-TargetResource`.
- SqlServerRole
- Add support for nested role membership ([issue #1452](https://github.com/dsccommunity/SqlServerDsc/issues/1452))
- Removed use of case-sensitive Contains() function when evalutating role membership.
- Removed use of case-sensitive Contains() function when evaluating role membership.
([issue #1153](https://github.com/dsccommunity/SqlServerDsc/issues/1153))
- Refactored mocks and unit tests to increase performance. ([issue #979](https://github.com/dsccommunity/SqlServerDsc/issues/979))

Expand Down Expand Up @@ -1117,8 +1125,8 @@ in a future release.
- Fix typo in test it block.
- Changes to SqlDatabaseRole
- BREAKING CHANGE: Refactored to enable creation/deletion of the database role
itself as well as management of the role members. *Note that the resource no
longer adds database users.* ([issue #845](https://github.com/dsccommunity/SqlServerDsc/issues/845),
itself as well as management of the role members. _Note that the resource no
longer adds database users._ ([issue #845](https://github.com/dsccommunity/SqlServerDsc/issues/845),
[issue #847](https://github.com/dsccommunity/SqlServerDsc/issues/847),
[issue #1252](https://github.com/dsccommunity/SqlServerDsc/issues/1252),
[issue #1339](https://github.com/dsccommunity/SqlServerDsc/issues/1339)).
Expand Down Expand Up @@ -1191,13 +1199,13 @@ in a future release.
- Fixed one of the error handling to use localization, and made the
error message more descriptive when the Set-TargetResource function
calls the Test-TargetResource function to verify the desired
state. *This was done prior to adding full en-US localization.*
state. _This was done prior to adding full en-US localization_.
- Fixed ([issue #1258](https://github.com/dsccommunity/SqlServerDsc/issues/1258)).
When initializing Reporting Services, there is no need to execute `InitializeReportServer`
CIM method, since executing `SetDatabaseConnection` CIM method initializes
Reporting Services.
- [issue #864](https://github.com/dsccommunity/SqlServerDsc/issues/864) SqlRs
can now initialise SSRS 2017 instances
can now initialize SSRS 2017 instances
- Changes to SqlServerLogin
- Added en-US localization ([issue #615](https://github.com/dsccommunity/SqlServerDsc/issues/615)).
- Added unit tests to improved code coverage.
Expand Down Expand Up @@ -1289,15 +1297,15 @@ in a future release.
- Refactored unit tests for the functions Test-TargetResource and
Set-TargetResource to improve testing speed.
- Modified the Test-TargetResource and Set-TargetResource to not be
case-sensitive when comparing feature names. *This was handled
case-sensitive when comparing feature names. _This was handled
correctly in real-world scenarios, but failed when running the unit
tests (and testing casing).*
tests (and testing casing)._
- Changes to SqlAGDatabase
- Fix MatchDatabaseOwner to check for CONTROL SERVER, IMPERSONATE LOGIN, or
CONTROL LOGIN permission in addition to IMPERSONATE ANY LOGIN.
- Update and fix MatchDatabaseOwner help text.
- Changes to SqlAG
- Updated documentation on the behaviour of defaults as they only apply when
- Updated documentation on the behavior of defaults as they only apply when
creating a group.
- Changes to SqlAGReplica
- AvailabilityMode, BackupPriority, and FailoverMode defaults only apply when
Expand Down Expand Up @@ -1364,7 +1372,7 @@ in a future release.
- Updated the integration test to stop the named instance while installing
the other instances to mitigate
[issue #1260](https://github.com/dsccommunity/SqlServerDsc/issues/1260).
- Add parameters to configure the Tempdb files during the installation of
- Add parameters to configure the 'tempdb' files during the installation of
the instance. The new parameters are SqlTempdbFileCount, SqlTempdbFileSize,
SqlTempdbFileGrowth, SqlTempdbLogFileSize and SqlTempdbLogFileGrowth
([issue #1167](https://github.com/dsccommunity/SqlServerDsc/issues/1167)).
Expand Down Expand Up @@ -1429,7 +1437,7 @@ in a future release.
SqlServer module).
- Changes to SqlAg, SqlAGDatabase, and SqlAGReplica examples
- Included configuration for SqlAlwaysOnService to enable
HADR on each node to avoid confusion
High Availability Disaster Recovery on each node to avoid confusion
([issue #1182](https://github.com/dsccommunity/SqlServerDsc/issues/1182)).
- Changes to SqlServerDatabaseMail
- Minor update to Ensure parameter description in the README.md.
Expand Down Expand Up @@ -2365,7 +2373,7 @@ in a future release.
- When TcpDynamicPorts is set to '0' the Test-TargetResource function will no
longer fail each time (issue #564).
- Changes to xSQLServerRSConfig
- Replaced sqlcmd.exe usages with Invoke-Sqlcmd calls (issue #567).
- Replaced sqlcmd.exe usages with Invoke-SqlCmd calls (issue #567).
- Changes to xSQLServerDatabasePermission
- Fixed code style, updated README.md and removed *-SqlDatabasePermission functions
from xSQLServerHelper.psm1.
Expand Down Expand Up @@ -2767,7 +2775,7 @@ in a future release.
- Get-TargetResource now works with Get-DscConfiguration.
- Fixes in xSQLServerRole
- Updated Ensure parameter to 'Present' default value.
- Renamed helper functions *-SqlServerRole* to *-SqlServerRoleMember*.
- Renamed helper functions _-SqlServerRole_ to _-SqlServerRoleMember_.
- Changes to xSQLAlias
- Add UseDynamicTcpPort parameter for option "Dynamically determine port".
- Change Get-WmiObject to Get-CimInstance in Resource and associated pester file.
Expand Down Expand Up @@ -2873,8 +2881,8 @@ in a future release.
- Get-SQLPSInstance
- Get-SQLAlwaysOnEndpoint
- modified functions
- New-TerminatingError - *added optional parameter `InnerException` to be able
to give the user more information in the returned message*
- New-TerminatingError - _added optional parameter `InnerException` to be able
to give the user more information in the returned message_

## [1.7.0.0] - 2016-06-29

Expand Down
30 changes: 30 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,36 @@ supported versions.
Those SQL Server products that are still supported can be listed at the
[Microsoft life cycle site](https://support.microsoft.com/en-us/lifecycle/search?alpha=SQL%20Server).

## Script Analyzer rules

There are several Script Analyzer rules to help with the development and review
process. Rules come from the modules **ScriptAnalyzer**, **DscResource.AnalyzerRules**,
**Indented.ScriptAnalyzerRules**, and **SqlServerDsc.AnalyzerRules**.

Some rules (but not all) are allowed to be overridden with a justification.

This is an example how to override a rule from the module **SqlServerDsc.AnalyzerRules**.

```powershell
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Connect-Sql is called when Get-TargetResource is called')]
param ()
```

This is an example how to override a rule from the module **ScriptAnalyzer**.

```powershell
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidGlobalVars', '', Justification='Because $global:DSCMachineStatus is used to trigger a Restart, either by force or when there are pending changes')]
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '', Justification='Because $global:DSCMachineStatus is only set, never used (by design of Desired State Configuration)')]
param ()
```

This is an example how to override a rule from the module **Indented.ScriptAnalyzerRules**.

```powershell
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('AvoidThrowOutsideOfTry', '', Justification='Because the code throws based on an prior expression')]
param ()
```

## Design patterns

### Credentials that does not have password
Expand Down
45 changes: 24 additions & 21 deletions RequiredModules.psd1
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
@{
PSDependOptions = @{
PSDependOptions = @{
AddToPath = $true
Target = 'output\RequiredModules'
Parameters = @{
Repository = 'PSGallery'
}
}

InvokeBuild = 'latest'
PSScriptAnalyzer = 'latest'
Pester = 'latest'
Plaster = 'latest'
ModuleBuilder = 'latest'
ChangelogManagement = 'latest'
Sampler = 'latest'
'Sampler.GitHubTasks' = 'latest'
MarkdownLinkCheck = 'latest'
'DscResource.Common' = 'latest'
'DscResource.Test' = 'latest'
'DscResource.AnalyzerRules' = 'latest'
xDscResourceDesigner = 'latest'
'DscResource.DocGenerator' = 'latest'
InvokeBuild = 'latest'
PSScriptAnalyzer = 'latest'
Pester = 'latest'
Plaster = 'latest'
ModuleBuilder = 'latest'
ChangelogManagement = 'latest'
Sampler = 'latest'
'Sampler.GitHubTasks' = 'latest'
MarkdownLinkCheck = 'latest'
'DscResource.Common' = 'latest'
'DscResource.Test' = 'latest'
xDscResourceDesigner = 'latest'
'DscResource.DocGenerator' = 'latest'

# Analyzer rules
'DscResource.AnalyzerRules' = 'latest'
'Indented.ScriptAnalyzerRules' = 'latest'

# Dependency for integration tests
LoopbackAdapter = 'latest'
PSPKI = 'latest'
LoopbackAdapter = 'latest'
PSPKI = 'latest'

# Prerequisites modules needed for examples or integration tests
PSDscResources = '2.12.0.0'
StorageDsc = '4.9.0.0'
NetworkingDsc = '7.4.0.0'
PowerShellGet = '2.1.2'
PSDscResources = '2.12.0.0'
StorageDsc = '4.9.0.0'
NetworkingDsc = '7.4.0.0'
PowerShellGet = '2.1.2'
}
3 changes: 1 addition & 2 deletions source/DSCResources/DSC_SqlAG/DSC_SqlAG.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ function Set-TargetResource
#>
function Test-TargetResource
{
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Connect-Sql is called when Get-TargetResource is called')]
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Connect-Sql is called when Get-TargetResource is called')]
[CmdletBinding()]
[OutputType([System.Boolean])]
param
Expand Down Expand Up @@ -819,4 +819,3 @@ function Update-AvailabilityGroup
$ErrorActionPreference = $originalErrorActionPreference
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ function Get-TargetResource
#>
function Set-TargetResource
{
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('AvoidThrowOutsideOfTry', '', Justification='Because the code throws based on an prior expression')]
[CmdletBinding()]
param
(
Expand Down
Loading

0 comments on commit c71947e

Please sign in to comment.