dsccommunity / SqlServerDsc

This module contains DSC resources for deployment and configuration of Microsoft SQL Server.
MIT License
361 stars 227 forks source link

SqlServerDsc: Add ScriptAnalyzer rule to validate that resource loads SMO #1683

Closed johlju closed 3 years ago

johlju commented 3 years ago

In the issue https://github.com/dsccommunity/SqlServerDsc/issues/1680#issuecomment-769723694 there was an issue reported that the resource failed due to the SMO assemblies was not loaded. This was due to that Import-SQLPSModule was not called.

Suggest adding a PSScriptAnalyzer rule that validates that each *-TargetResource function do call Import-SQLPSModule. If a function is not suppose to call Import-SQLPSModule then it would be possible to suppress the analyzer rule for that particular function.

johlju commented 3 years ago

This was added in the working branch add-script-analyzer-rule-for-Import-SQLPSModule and in the folder AnalyzerRules. But the problem is that the CustomRulePath in the Script Analyzer settings file analyzersettings.psd1 does not allow an array of paths:

https://github.com/johlju/SqlServerDsc/blob/cfe91305ec1b403023f9c77171c24a676a23ca85/.vscode/analyzersettings.psd1#L3-L6

When adding above configuration all the custom rules stops working.

So only way to support this is to add it do DscResource.Analysis, or make a Pester test that runs Invoke-ScriptAnalyzer with just this particular custom rule. The former is not a good choice since this rule is limited to SqlServerDsc and would need to made more generic like adding the option to configure it through the Script Analyzer settings file (not sure how to do that). So the only viable option right now is to create a QA test that runs this custom rule.

johlju commented 3 years ago

I have it working with multiple paths in the Script Analyzer settings file. The problem was that the rules need to be exported by a module of in a module script file. So this works:

@{
    CustomRulePath      = @(
        '.\output\RequiredModules\DscResource.AnalyzerRules'
        '.\source\AnalyzerRules\SqlServerDsc.AnalyzerRules.psm1'
    )

    IncludeRules        = @(
        # DSC Resource Kit style guideline rules.
        ...

        # Additional rules
        ...

        'Measure-*'
    )
}