Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding some fixes #428

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
266 changes: 4 additions & 262 deletions .build/tasks/DscResource.Test.build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -44,268 +44,10 @@ param
$BuildInfo = (property BuildInfo @{ })
)

# Synopsis: Making sure the Module meets some quality standard (help, tests)
task Invoke_DscResource_Tests {
# Get the vales for task variables, see https://github.com/gaelcolas/Sampler#task-variables.
. Set-SamplerTaskVariable

$DscTestOutputFolder = Get-SamplerAbsolutePath -Path $DscTestOutputFolder -RelativeTo $OutputDirectory
# Synopsis: Deprecated HQRM task
task DscResource_Tests_Stop_On_Fail {
Write-Warning -Message "THIS TASK IS DEPRECATED! Please use Invoke_HQRM_Tests_Stop_On_Fail from the module DscResource.Test..."

"`tDSC Test Output Folder = '$DscTestOutputFolder'"

$builtDscResourcesFolder = Get-SamplerAbsolutePath -Path 'DSCResources' -RelativeTo $builtModuleBase

"`tBuilt DSC Resource Path = '$builtDscResourcesFolder'"

if (-not (Test-Path -Path $DscTestOutputFolder))
{
Write-Build -Color 'Yellow' -Text "Creating folder $DscTestOutputFolder"

$null = New-Item -Path $DscTestOutputFolder -ItemType 'Directory' -Force -ErrorAction 'Stop'
}

$DscTestScript = $DscTestScript.Where{ -not [System.String]::IsNullOrEmpty($_) }
$DscTestTag = $DscTestTag.Where{ -not [System.String]::IsNullOrEmpty($_) }
$DscTestExcludeTag = $DscTestExcludeTag.Where{ -not [System.String]::IsNullOrEmpty($_) }

# Same parameters for both Pester 4 and Pester 5.
$defaultDscTestParams = @{
PassThru = $true
}

$isPester5 = (Get-Module -Name 'Pester').Version -ge '5.0.0'

if ($isPester5)
{
$defaultDscTestParams['Output'] = 'Detailed'
}
else
{
$defaultDscTestParams['OutputFile'] = $DscTestOutputFullPath
$defaultDscTestParams['OutputFormat'] = 'NUnitXML'
}

Import-Module -Name 'DscResource.Test' -ErrorAction 'Stop'
Import-Module -Name 'Pester' -MinimumVersion 4.0 -ErrorAction 'Stop'

$dscTestCmd = Get-Command -Name Invoke-DscResourceTest

<#
This will build the DscTest* variables (e.g. PesterScript, or
PesterOutputFormat) in this scope that are used in the rest of the code.
It will use values for the variables in the following order:

1. Skip creating the variable if a variable is already available because
it was already set in a passed parameter (Pester*).
2. Use the value from a property in the build.yaml under the key 'Pester:'.
3. Use the default value set previously in the variable $defaultPesterParams.
#>
foreach ($paramName in $dscTestCmd.Parameters.Keys)
{
if (($paramName -eq 'ExcludeTagFilter' -or $paramName -eq 'TagFilter') -and -not $isPester5)
{
$paramName = $paramName -replace 'Filter'
}

$taskParamName = "DscTest$paramName"

$DscTestBuildConfig = $BuildInfo.DscTest

if (-not (Get-Variable -Name $taskParamName -ValueOnly -ErrorAction 'SilentlyContinue') -and ($DscTestBuildConfig))
{
$paramValue = $DscTestBuildConfig.($paramName)

# The Variable is set to '' so we should try to use the Config'd one if exists
if ($paramValue)
{
Write-Build -Color 'DarkGray' -Text "Using $taskParamName from Build Config"

Set-Variable -Name $taskParamName -Value $paramValue
} # or use a default if available
elseif ($defaultDscTestParams.ContainsKey($paramName))
{
Write-Build -Color 'DarkGray' -Text "Using $taskParamName from Defaults"

Set-Variable -Name $taskParamName -Value $defaultDscTestParams.($paramName)
}
}
else
{
Write-Build -Color 'DarkGray' -Text "Using $taskParamName from Build Invocation Parameters"
}
}

"`tTest Scripts = $($DscTestScript -join ', ')"
"`tTags = $($DscTestTag -join ', ')"
"`tExclude Tags = $($DscTestExcludeTag -join ', ')"

$os = Get-OperatingSystemShortName

$psVersion = 'PSv.{0}' -f $PSVersionTable.PSVersion
$DscTestOutputFileFileName = "DscTest_{0}_v{1}.{2}.{3}.xml" -f $ProjectName, $ModuleVersion, $os, $psVersion
$DscTestOutputFullPath = Join-Path -Path $DscTestOutputFolder -ChildPath "$($DscTestOutputFormat)_$DscTestOutputFileFileName"

$dscTestParams = @{
PassThru = $true
}

if ($isPester5)
{
$dscTestParams['Output'] = $DscTestOutput
}
else
{
$dscTestParams['OutputFormat'] = $DscTestOutputFormat
$dscTestParams['OutputFile'] = $DscTestOutputFullPath
}

if ($DscTestModule)
{
$dscTestParams.Add('Module', $DscTestModule)
}
elseif ($DscTestFullyQualifiedModule)
{
$dscTestParams.Add('FullyQualifiedModule', $DscTestFullyQualifiedModule)
}
else
{
$dscTestParams.Add('ProjectPath', $ProjectPath)
}

if ($DscTestExcludeTag.Count -gt 0)
{
$dscTestParams.Add('ExcludeTag', $DscTestExcludeTag)
}

if ($DscTestTag.Count -gt 0)
{
$dscTestParams.Add('Tag', $DscTestTag)
}

# Test folders is specified, override invoke-DscResourceTest internal default
if ($DscTestScript.Count -gt 0)
{
$dscTestParams.Add('Path', @())

Write-Build -Color 'DarkGray' -Text " Adding DscTestScript to params"

foreach ($testFolder in $DscTestScript)
{
if (-not (Split-Path -IsAbsolute $testFolder))
{
$testFolder = Join-Path -Path $ProjectPath -ChildPath $testFolder
}

Write-Build -Color 'DarkGray' -Text " ... $testFolder"

<#
The Absolute path to this folder exists, adding to the list of
DscTest scripts to run.
#>
if (Test-Path -Path $testFolder)
{
if ($isPester5)
{
$dscTestParams.Path += $testFolder
}
else
{
$dscTestParams.Script += $testFolder
}
}
}
}

# Add all DscTest* variables in current scope into the $dscTestParams hashtable.
foreach ($paramName in $DscTestCmd.Parameters.keys)
{
$paramValueFromScope = (Get-Variable -Name "DscTest$paramName" -ValueOnly -ErrorAction 'SilentlyContinue')

if (-not $dscTestParams.ContainsKey($paramName) -and $paramValueFromScope)
{
$dscTestParams.Add($paramName, $paramValueFromScope)
}
}

Write-Verbose -Message ($dscTestParams | ConvertTo-Json)

$script:testResults = Invoke-DscResourceTest @dscTestParams

$DscTestResultObjectCliXml = Join-Path -Path $DscTestOutputFolder -ChildPath "DscTestObject_$DscTestOutputFileFileName"

$null = $script:testResults | Export-CliXml -Path $DscTestResultObjectCliXml -Force
}

# Synopsis: This task ensures the build job fails if the test aren't successful.
task Fail_Build_If_DscResource_Tests_Failed {
"Asserting that no test failed"
""

# Get the vales for task variables, see https://github.com/gaelcolas/Sampler#task-variables.
. Set-SamplerTaskVariable

$DscTestOutputFolder = Get-SamplerAbsolutePath -Path $DscTestOutputFolder -RelativeTo $OutputDirectory

$os = Get-OperatingSystemShortName

$builtDscResourcesFolder = Get-SamplerAbsolutePath -Path 'DSCResources' -RelativeTo $builtModuleBase

"`tBuilt DSC Resource Path = '$builtDscResourcesFolder'"

$psVersion = 'PSv.{0}' -f $PSVersionTable.PSVersion
$DscTestOutputFileFileName = "DscTest_{0}_v{1}.{2}.{3}.xml" -f $ProjectName, $ModuleVersion, $os, $psVersion
$DscTestResultObjectClixml = Join-Path -Path $DscTestOutputFolder -ChildPath "DscTestObject_$DscTestOutputFileFileName"

"`tDscTest Output Object = $DscTestResultObjectClixml"

if (-not (Test-Path -Path $DscTestResultObjectClixml))
{
throw "No command were tested. $DscTestResultObjectClixml not found"
}
else
{
$DscTestObject = Import-Clixml -Path $DscTestResultObjectClixml -ErrorAction 'Stop'

Assert-Build -Condition ($DscTestObject.FailedCount -eq 0) -Message ('Failed {0} tests. Aborting Build' -f $DscTestObject.FailedCount)
}
throw "THIS TASK IS DEPRECATED! Please use Invoke_HQRM_Tests_Stop_On_Fail from the module DscResource.Test..."
}

# Synopsis: Uploading Unit Test results to AppVeyor
task Upload_DscResourceTest_Results_To_AppVeyor -If { (property BuildSystem 'unknown') -eq 'AppVeyor' } {
# Get the vales for task variables, see https://github.com/gaelcolas/Sampler#task-variables.
. Set-SamplerTaskVariable

$DscTestOutputFolder = Get-SamplerAbsolutePath -Path $DscTestOutputFolder -RelativeTo $OutputDirectory

if (-not (Test-Path -Path $DscTestOutputFolder))
{
Write-Build -Color 'Yellow' -Text "Creating folder $DscTestOutputFolder"

$null = New-Item -Path $DscTestOutputFolder -ItemType Directory -Force -ErrorAction 'Stop'
}

$os = Get-OperatingSystemShortName

$builtDscResourcesFolder = Get-SamplerAbsolutePath -Path 'DSCResources' -RelativeTo $builtModuleBase

"`tBuilt DSC Resource Path = '$builtDscResourcesFolder'"

$psVersion = 'PSv.{0}' -f $PSVersionTable.PSVersion
$DscTestOutputFileFileName = "DscResource.Test_{0}_v{1}.{2}.{3}.xml" -f $ProjectName, $ModuleVersion, $os, $psVersion

$DscTestOutputFullPath = Join-Path -Path $DscTestOutputFolder -ChildPath "$($DscTestOutputFormat)_$DscTestOutputFileFileName"

$testResultFile = Get-Item -Path $DscTestOutputFullPath -ErrorAction 'Ignore'

if ($testResultFile)
{
Write-Build -Color 'Green' -Text " Uploading test results $testResultFile to Appveyor"

$testResultFile | Add-TestResultToAppveyor

Write-Build -Color 'Green' -Text " Upload Complete"
}
}

# Synopsis: Meta task that runs Quality Tests, and fails if they're not successful
task DscResource_Tests_Stop_On_Fail Invoke_DscResource_Tests, Upload_DscResourceTest_Results_To_AppVeyor, Fail_Build_If_DscResource_Tests_Failed
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- Deprecate the use of the task `DscResource_Tests_Stop_On_Fail` and warn the user to use `Invoke_HQRM_Tests_Stop_On_Fail` instead.
- Updated the choco and build templates to use `Invoke_HQRM_Tests_Stop_On_Fail` by default.

## [0.116.5] - 2023-04-19

- Fix Azure Pipeline bug to resolve errors and delays during the build process. Shallow fetch has been disabled to ensure complete repository cloning. Fixes [#424](https://github.com/gaelcolas/Sampler/issues/424)
Expand Down
4 changes: 3 additions & 1 deletion Sampler/Templates/Build/build.yaml.template
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ BuildWorkflow:
if($PLASTER_PARAM_ModuleType -in @('dsccommunity')) {
@"
hqrmtest:
- DscResource_Tests_Stop_On_Fail
- Invoke_HQRM_Tests_Stop_On_Fail
"@
}
%>
Expand Down Expand Up @@ -288,6 +288,8 @@ ModuleBuildTasks:
@"
Sampler.GitHubTasks:
- '*.ib.tasks'
DscResource.Test:
- 'Task.*'
"@
}
%>
Expand Down
4 changes: 3 additions & 1 deletion Sampler/Templates/ChocolateyPipeline/chocobuild.yml.template
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ BuildWorkflow:
- build

hqrmtest:
- DscResource_Tests_Stop_On_Fail
- Invoke_HQRM_Tests_Stop_On_Fail

# Defining test task to be run when invoking `./build.ps1 -Tasks test`
test:
Expand Down Expand Up @@ -102,6 +102,8 @@ ModuleBuildTasks:
- '*.build.Sampler.ib.tasks'
Sampler.GitHubTasks:
- '*.ib.tasks'
DscResource.Test:
- 'Task.*'

# Invoke-Build Header to be used to 'decorate' the terminal output of the tasks.
TaskHeader: |
Expand Down
1 change: 1 addition & 0 deletions build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ TaskHeader: |
GitHubConfig:
GitHubFilesToAdd:
- 'CHANGELOG.md'
ReleaseAssets:
GitHubConfigUserName: gaelcolas
GitHubConfigUserEmail: [email protected]
UpdateChangelogOnPrerelease: false
Expand Down