Skip to content

Commit

Permalink
HQRM: Add QA test for class-based localization (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-hughes authored Oct 9, 2024
1 parent 3011179 commit d29c880
Show file tree
Hide file tree
Showing 6 changed files with 669 additions and 27 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added private command fixes ([Issue [#126](https://github.com/dsccommunity/DscResource.Test/issues/126)]).
- Public command `Get-ObjectNotFoundRecord`
- Use private function `Get-SystemExceptionRecord`.
- `QA/Localization.builtModule.v5.Tests`
- Added new test for checking the localization strings are correct in the resource and strings.psd1 within the built module.
- `Get-ClassDefinitionAst`
- Added helper function to get all class definition AST from a file.

### Changed

Expand All @@ -34,6 +38,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Removed alias `Get-ObjectNotFoundRecord` and added as it's own public command.
- `PSSAResource.common.v4.Tests`
- Fixed rule suppression by using correct variable.
- `Test-FileContainsClassResource`
- Add additional check for files that may only have a DscProperty within.
- `Test-FileContainsClassResource.Tests`
- Update tests to check the class property declarations are matched.
- `QA/Localization.common.v5.Tests`
- Just check remaining `psm1` files in the built module not covered by `QA/Localization.builtModule.v5.Tests`.

### Fixed

Expand Down
58 changes: 46 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -936,8 +936,8 @@ Invoke-Pester -Container $container -Output Detailed

<!-- markdownlint-disable MD013 - Line length -->
```plaintext
[-ModuleBase] <String> [[-SourcePath] <String>] [[-ExcludeModuleFile] <String[]>]
[[-ExcludeSourceFile] <String[]>] [[-Args] <Object>] [<CommonParameters>]
[-ModuleBase] <String> [[-ExcludeModuleFile] <String[]>] [[-ProjectPath] <String[]>]
[[-Args] <Object>] [<CommonParameters>]
```
<!-- markdownlint-enable MD013 - Line length -->

Expand All @@ -947,22 +947,17 @@ Any path or part of a path that will be excluded from the list of files
gathered by the test from the path specified in the parameter `ModuleBase`.
Default no files will be excluded from the test.

##### ExcludeSourceFile

Any path or part of a path that will be excluded from the list of files
gathered by the test from the path specified in the parameter `SourcePath`.
Default no files will be excluded from the test.

##### ModuleBase

The path to the root of built module, e.g. `./output/FileSystemDsc/1.2.0`.

If using the build task the default value for this parameter will be set
to the value that comes from the pipeline.

##### SourcePath
##### ProjectPath

The path to the source folder of the project, e.g. `./source`.
The path to the root of the project, for example the root of the local
Git repository.

If using the build task the default value for this parameter will be set
to the value that comes from the pipeline.
Expand All @@ -975,9 +970,48 @@ $pathToHQRMTests = Join-Path -Path (Get-Module DscResource.Test).ModuleBase -Chi
$container = New-PesterContainer -Path "$pathToHQRMTests/Localization.common.*.Tests.ps1" -Data @{
ModuleBase = "./output/$dscResourceModuleName/*"
# SourcePath = './source'
# ExcludeModuleFile = @('Modules/DscResource.Common')
# ExcludeSourceFile = @('Examples')
# ProjectPath = '.'
}
Invoke-Pester -Container $container -Output Detailed
```

### Localization.builtModule

#### Parameters

<!-- markdownlint-disable MD013 - Line length -->
```plaintext
[-ModuleBase] <String> [[-ProjectPath] <String[]>]
[[-Args] <Object>] [<CommonParameters>]
```
<!-- markdownlint-enable MD013 - Line length -->

##### ModuleBase

The path to the root of built module, e.g. `./output/FileSystemDsc/1.2.0`.

If using the build task the default value for this parameter will be set
to the value that comes from the pipeline.

##### ProjectPath

The path to the root of the project, for example the root of the local
Git repository.

If using the build task the default value for this parameter will be set
to the value that comes from the pipeline.

#### Example

```powershell
$dscResourceModuleName = 'FileSystemDsc'
$pathToHQRMTests = Join-Path -Path (Get-Module DscResource.Test).ModuleBase -ChildPath 'Tests\QA'
$container = New-PesterContainer -Path "$pathToHQRMTests/Localization.builtModule.*.Tests.ps1" -Data @{
ModuleBase = "./output/$dscResourceModuleName/*"
# ProjectPath = '.'
}
Invoke-Pester -Container $container -Output Detailed
Expand Down
42 changes: 42 additions & 0 deletions source/Private/Get-ClassDefinitionAst.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<#
.SYNOPSIS
Returns the class definition ASTs for a script file.
.PARAMETER FullName
Full path to the script file.
#>
function Get-ClassDefinitionAst
{
param
(
[Parameter(Mandatory = $true)]
[System.String]
$FullName
)

$tokens, $parseErrors = $null

$ast = [System.Management.Automation.Language.Parser]::ParseFile(
$FullName,
[ref] $tokens,
[ref] $parseErrors
)

if ($parseErrors)
{
throw $parseErrors
}

$astFilter = {
param
(
[Parameter()]
[System.Management.Automation.Language.Ast]
$Ast
)

$Ast -is [System.Management.Automation.Language.TypeDefinitionAst]
}

return $ast.FindAll($astFilter, $true)
}
Loading

0 comments on commit d29c880

Please sign in to comment.