Skip to content

Commit 2c7ba54

Browse files
authored
SqlAgentAlert: Refactor to class-bases resource (#2150)
1 parent 6488425 commit 2c7ba54

23 files changed

+1185
-1296
lines changed

.github/copilot-instructions.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
# AI Instructions for SqlServerDsc
1+
# AI Instructions
22

3-
This file provides AI agent guidance for the SqlServerDsc project. Each
4-
instruction file below targets specific file glob patterns and use cases.
3+
This file provides AI agent guidance for the project. Each instruction file below
4+
targets specific file glob patterns and use cases.
5+
6+
## Build & Test Workflow
7+
- Run in PowerShell, from repository root
8+
- Build before running tests: `.\build.ps1 -Tasks build`
9+
- Always run tests in new PowerShell session: `Invoke-Pester -Path @({test paths}) -Output Detailed`
510

611
## Instructions Overview
712

.github/instructions/SqlServerDsc-guidelines.instructions.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ applyTo: "**"
1111

1212
## Resources
1313
- Database Engine resources: inherit `SqlResourceBase`
14-
- `SqlResourceBase` provides: `InstanceName`, `ServerName`, `Credential`, `Reasons`, `GetServerObject()`
14+
- Add `InstanceName`, `ServerName`, and `Credential` to `$this.ExcludeDscProperties`
15+
- `SqlResourceBase` provides: `InstanceName`, `ServerName`, `Credential`, `Reasons`, `GetServerObject()`
16+
- Constructor: `MyResourceName() : base () { }` (no $PSScriptRoot parameter)
1517

1618
## SQL Server Interaction
1719
- Always prefer SMO over T-SQL
@@ -34,6 +36,7 @@ applyTo: "**"
3436
- Choose the appropriate group number based on the required dependencies
3537

3638
## Unit tests
37-
- When unit test uses SMO types, ensure they are properly stubbed in SMO.cs
38-
- Load stub types from SMO.cs in unit test files, e.g. `Add-Type -Path "$PSScriptRoot/../Stubs/SMO.cs"`
39-
- After changing SMO stub types, run tests in a new PowerShell session for changes to take effect.
39+
- When unit test tests classes or commands that contain SMO types, e.g. `[Microsoft.SqlServer.Management.Smo.*]`
40+
- Ensure they are properly stubbed in SMO.cs
41+
- Load SMO stub types from SMO.cs in unit test files, e.g. `Add-Type -Path "$PSScriptRoot/../Stubs/SMO.cs"`
42+
- After changing SMO stub types, run tests in a new PowerShell session for changes to take effect.

.github/instructions/dsc-community-style-guidelines-changelog.instructions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ applyTo: "CHANGELOG.md"
88
- Always update the Unreleased section in CHANGELOG.md
99
- Use Keep a Changelog format
1010
- Describe notable changes briefly, ≤2 items per change type
11-
- Reference issues using format [#<issue_number>](https://github.com/<owner>/<repo>/issues/<issue_number>)
11+
- Reference issues using format [issue #<issue_number>](https://github.com/<owner>/<repo>/issues/<issue_number>)
1212
- No empty lines between list items in same section
1313
- Do not add item if there are already an existing item for the same change

.github/instructions/dsc-community-style-guidelines-class-resource.instructions.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ applyTo: "source/[cC]lasses/**/*.ps1"
1212
- Decoration: `[DscResource(RunAsCredential = 'Optional')]` (replace with `'Mandatory'` if required)
1313
- Inheritance: Must inherit `ResourceBase` (part of module DscResource.Base)
1414
- `$this.localizedData` hashtable auto-populated by `ResourceBase` from localization file
15+
- value-type properties: Use `[Nullable[{FullTypeName}]]` (e.g., `[Nullable[System.Int32]]`)
1516

1617
## Required constructor
1718

1819
```powershell
1920
MyResourceName () : base ($PSScriptRoot)
2021
{
21-
# Property names where state cannot be enforced, e.g Ensure
22+
# Property names where state cannot be enforced, e.g. IsSingleInstance, Force
2223
$this.ExcludeDscProperties = @()
2324
}
2425
```
@@ -78,3 +79,19 @@ hidden [void] NormalizeProperties([System.Collections.Hashtable] $properties)
7879
# Normalize user-provided properties, $properties contains user assigned values
7980
}
8081
```
82+
83+
## Required comment-based help
84+
85+
Add to .DESCRIPTION section:
86+
- `## Requirements`: List minimum requirements
87+
- `## Known issues`: Critical issues + pattern: `All issues are not listed here, see [all open issues](https://github.com/{owner}/{repo}/issues?q=is%3Aissue+is%3Aopen+in%3Atitle+{ResourceName}).`
88+
89+
## Error Handling
90+
- Use `try/catch` blocks to handle exceptions
91+
- Do not use `throw` for terminating errors, use `New-*Exception` commands:
92+
- [`New‑InvalidDataException`](https://github.com/dsccommunity/DscResource.Common/wiki/New%E2%80%91InvalidDataException)
93+
- [`New-ArgumentException`](https://github.com/dsccommunity/DscResource.Common/wiki/New%E2%80%91ArgumentException)
94+
- [`New-InvalidOperationException`](https://github.com/dsccommunity/DscResource.Common/wiki/New%E2%80%91InvalidOperationException)
95+
- [`New-ObjectNotFoundException`](https://github.com/dsccommunity/DscResource.Common/wiki/New%E2%80%91ObjectNotFoundException)
96+
- [`New-InvalidResultException`](https://github.com/dsccommunity/DscResource.Common/wiki/New%E2%80%91InvalidResultException)
97+
- [`New-NotImplementedException`](https://github.com/dsccommunity/DscResource.Common/wiki/New%E2%80%91NotImplementedException)

.github/instructions/dsc-community-style-guidelines-integration-tests.instructions.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ BeforeDiscovery {
4545
}
4646
4747
BeforeAll {
48-
$script:dscModuleName = 'SqlServerDsc'
48+
$script:moduleName = '{MyModuleName}'
4949
50-
Import-Module -Name $script:dscModuleName -Force -ErrorAction 'Stop'
50+
Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop'
5151
}
5252
```

.github/instructions/dsc-community-style-guidelines-localization.instructions.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ applyTo: "source/**/*.ps1"
1111
- Assume `$script:localizedData` is available
1212

1313
## String Files
14-
- Commands/functions: `source/en-US/SqlServerDsc.strings.psd1`
14+
- Commands/functions: `source/en-US/{MyModuleName}.strings.psd1`
1515
- Class resources: `source/en-US/{ResourceClassName}.strings.psd1`
1616

1717
## Key Naming Patterns
18-
- Format: `Verb_FunctionName_Action` (underscore separators), e.g. `Get_SqlDscDatabase_ConnectingToDatabase`
18+
- Format: `Verb_FunctionName_Action` (underscore separators), e.g. `Get_Database_ConnectingToDatabase`
1919

2020
## String Format
2121
```powershell

.github/instructions/dsc-community-style-guidelines-pester.instructions.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,36 @@ applyTo: "**/*.[Tt]ests.ps1"
99
- All public commands, private functions and classes must have unit tests
1010
- All public commands and class-based resources must have integration tests
1111
- Use Pester v5 syntax only
12-
- One `Describe` block per file matching the tested entity name
1312
- Test code only inside `Describe` blocks
1413
- Assertions only in `It` blocks
1514
- Never test verbose messages, debug messages or parameter binding behavior
1615
- Pass all mandatory parameters to avoid prompts
1716

17+
## Naming
18+
- One `Describe` block per file matching the tested entity name
19+
- `Context` descriptions start with 'When'
20+
- `It` descriptions start with 'Should', must not contain 'when'
21+
- Mock variables prefix: 'mock'
22+
1823
## Structure & Scope
1924
- Public commands: Never use `InModuleScope` (unless retrieving localized strings)
2025
- Private functions/class resources: Always use `InModuleScope`
26+
- Each class method = separate `Context` block
2127
- Each scenario = separate `Context` block
2228
- Use nested `Context` blocks for complex scenarios
2329
- Mocking in `BeforeAll` (`BeforeEach` only when required)
2430
- Setup/teardown in `BeforeAll`,`BeforeEach`/`AfterAll`,`AfterEach` close to usage
2531

2632
## Syntax Rules
2733
- PascalCase: `Describe`, `Context`, `It`, `Should`, `BeforeAll`, `BeforeEach`, `AfterAll`, `AfterEach`
28-
- `Context` descriptions start with 'When'
29-
- `It` descriptions start with 'Should', must not contain 'when'
30-
- Mock variables prefix: 'mock'
3134
- Prefer `-BeTrue`/`-BeFalse` over `-Be $true`/`-Be $false`
35+
- Never use `Assert-MockCalled`, use `Should -Invoke` instead
3236
- No `Should -Not -Throw` - invoke commands directly
3337
- Never add an empty `-MockWith` block
3438
- Omit `-MockWith` when returning `$null`
3539
- Set `$PSDefaultParameterValues` for `Mock:ModuleName`, `Should:ModuleName`, `InModuleScope:ModuleName`
3640
- Omit `-ModuleName` parameter on Pester commands
41+
- Never use `Mock` inside `InModuleScope`-block
3742

3843
## File Organization
3944
- Class resources: `tests/Unit/Classes/{Name}.Tests.ps1`

.github/instructions/dsc-community-style-guidelines-powershell.instructions.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ applyTo: "**/*.ps?(m|d)1"
4949
### Hashtables
5050

5151
- Empty: `@{}`
52-
- Multi-line: each property on separate line with proper indentation
52+
- Each property on separate line with proper indentation
5353
- Properties: Use PascalCase
5454

5555
### Comments
@@ -79,14 +79,18 @@ applyTo: "**/*.ps?(m|d)1"
7979
- For state-changing functions, use `SupportsShouldProcess`
8080
- Place ShouldProcess check immediately before each state-change
8181
- `$PSCmdlet.ShouldProcess` must use required pattern
82-
- Use `$PSCmdlet.ThrowTerminatingError()` for terminating errors, use relevant error category
83-
- Use `Write-Error` for non-terminating errors, use relevant error category
84-
- Use `Write-Warning` for warnings
85-
- Use `Write-Debug` for debugging information
86-
- Use `Write-Verbose` for actionable information
87-
- Use `Write-Information` for informational messages.
8882
- Never use backtick as line continuation in production code.
8983

84+
## Output streams
85+
86+
- Never output sensitive data/secrets
87+
- Use `Write-Debug` for: Internal diagnostics; Variable values/traces; Developer-focused details
88+
- Use `Write-Verbose` for: High-level execution flow only; User-actionable information
89+
- Use `Write-Information` for: User-facing status updates; Important operational messages; Non-error state changes
90+
- Use `Write-Warning` for: Non-fatal issues requiring attention; Deprecated functionality usage; Configuration problems that don't block execution
91+
- Use `$PSCmdlet.ThrowTerminatingError()` for terminating errors (except for classes), use relevant error category
92+
- Use `Write-Error` for non-terminating errors, use relevant error category
93+
9094
## ShouldProcess Required Pattern
9195

9296
- Ensure `$descriptionMessage` explains what will happen

.github/instructions/dsc-community-style-guidelines-unit-tests.instructions.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ BeforeDiscovery {
4242
}
4343
4444
BeforeAll {
45-
$script:dscModuleName = 'SqlServerDsc'
45+
$script:moduleName = '{MyModuleName}'
4646
47-
Import-Module -Name $script:dscModuleName -Force -ErrorAction 'Stop'
47+
Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop'
4848
49-
$PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:dscModuleName
50-
$PSDefaultParameterValues['Mock:ModuleName'] = $script:dscModuleName
51-
$PSDefaultParameterValues['Should:ModuleName'] = $script:dscModuleName
49+
$PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:moduleName
50+
$PSDefaultParameterValues['Mock:ModuleName'] = $script:moduleName
51+
$PSDefaultParameterValues['Should:ModuleName'] = $script:moduleName
5252
}
5353
5454
AfterAll {
@@ -57,7 +57,7 @@ AfterAll {
5757
$PSDefaultParameterValues.Remove('Should:ModuleName')
5858
5959
# Unload the module being tested so that it doesn't impact any other tests.
60-
Get-Module -Name $script:dscModuleName -All | Remove-Module -Force
60+
Get-Module -Name $script:moduleName -All | Remove-Module -Force
6161
}
6262
```
6363

.github/instructions/dsc-community-style-guidelines.instructions.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@ applyTo: "**"
1111
- **Resource**: DSC class-based resource
1212

1313
## Build & Test Workflow
14-
- Run project scripts in PowerShell from repository root
15-
- Build after source changes: `.\build.ps1 -Tasks build`
16-
- Test workflow: Build → `Invoke-Pester -Path @('<test paths>') -Output Detailed`
17-
- New session required after class changes
14+
- Run in PowerShell, from repository root
15+
- Build before running tests: `.\build.ps1 -Tasks build`
16+
- Always run tests in new PowerShell session: `Invoke-Pester -Path @({test paths}) -Output Detailed`
1817

1918
## File Organization
2019
- Public commands: `source/Public/{CommandName}.ps1`

0 commit comments

Comments
 (0)