diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d40af3d8..a32e8560a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - AzureDevOpsDsc - Updated pipeline files to support change of default branch to main. - - Added GitHub issue templates and pull request template ([issue #1](https://github.com/dsccommunity/AzureDevOpsDsc/issues/1)) + - Added GitHub issue templates and pull request template + ([issue #1](https://github.com/dsccommunity/AzureDevOpsDsc/issues/1)) - Added the `AzDevOpsProject`, DSC Resource + - Fixed non-terminating, integration tests ([issue #18](https://github.com/dsccommunity/AzureDevOpsDsc/issues/18)) - AzureDevOpsDsc.Common - Added 'wrapper' functionality around the [Azure DevOps REST API](https://docs.microsoft.com/en-us/rest/api/azure/devops/) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index c4e371f08..cafa70440 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -84,13 +84,15 @@ development), the following actions need to be performed: initiated. * You need to set the following environment variables so the build will determine - it needs to execute the Integration tests, and it has the Personal Access Token - (PAT) and API URI + it needs to execute the Integration tests, and it can use the [Personal Access Token + (PAT)](https://docs.microsoft.com/en-us/azure/devops/organizations/accounts/use-personal-access-tokens-to-authenticate?view=azure-devops&tabs=preview-page) + and API URI ```Powershell $env:CI = $true - $env:AZUREDEVOPSINTEGRATIONAPIURI = 'YourApiUriHere' - $env:AZUREDEVOPSINTEGRATIONPAT = 'YourPatHere' + $env:CONFIGURATION = 'Integration' + $env:AZUREDEVOPSINTEGRATIONAPIURI = 'YourApiUriHere' # IMPORTANT: Ensure this is a destructable organization/collection + $env:AZUREDEVOPSINTEGRATIONPAT = 'YourPatHere' # This PAT must have access to update resources ``` >**Important**: It is not recommended to store any Personal Access Token (PAT) diff --git a/source/Classes/001.DscResourceBase.ps1 b/source/Classes/001.DscResourceBase.ps1 index d0a0c029e..4dd52ddf4 100644 --- a/source/Classes/001.DscResourceBase.ps1 +++ b/source/Classes/001.DscResourceBase.ps1 @@ -6,7 +6,8 @@ class DscResourceBase if ([String]::IsNullOrWhiteSpace($dscResourceKeyPropertyName)) { - throw "Cannot obtain a 'DscResourceKey' value for the '$($this.GetType().Name)' instance." + $errorMessage = "Cannot obtain a 'DscResourceKey' value for the '$($this.GetType().Name)' instance." + New-InvalidOperationException -Message $errorMessage } return $this."$dscResourceKeyPropertyName" @@ -37,12 +38,14 @@ class DscResourceBase if ($null -eq $dscResourceKeyPropertyNames -or $dscResourceKeyPropertyNames.Count -eq 0) { - throw "Could not obtain a 'DscResourceDscKey' property for type '$($this.GetType().Name)'." + $errorMessage = "Could not obtain a 'DscResourceDscKey' property for type '$($this.GetType().Name)'." + New-InvalidOperationException -Message $errorMessage } elseif ($dscResourceKeyPropertyNames.Count -gt 1) { - throw "Obtained more than 1 property for type '$($this.GetType().Name)' that was marked as a 'Key'. There must only be 1 property on the class set as the 'Key' for DSC." + $errorMessage = "Obtained more than 1 property for type '$($this.GetType().Name)' that was marked as a 'Key'. There must only be 1 property on the class set as the 'Key' for DSC." + New-InvalidOperationException -Message $errorMessage } return $dscResourceKeyPropertyNames[0] diff --git a/source/Classes/003.AzDevOpsDscResourceBase.ps1 b/source/Classes/003.AzDevOpsDscResourceBase.ps1 index fa1505986..aa7bf4593 100644 --- a/source/Classes/003.AzDevOpsDscResourceBase.ps1 +++ b/source/Classes/003.AzDevOpsDscResourceBase.ps1 @@ -79,7 +79,8 @@ class AzDevOpsDscResourceBase : AzDevOpsApiDscResourceBase $thisType = $this.GetType() if ($thisType -eq [AzDevOpsDscResourceBase]) { - throw "Method 'GetCurrentState()' in '$($thisType.Name)' must be overidden and called by an inheriting class." + $errorMessage = "Method 'GetCurrentState()' in '$($thisType.Name)' must be overidden and called by an inheriting class." + New-InvalidOperationException -Message $errorMessage } return $null } @@ -143,8 +144,8 @@ class AzDevOpsDscResourceBase : AzDevOpsApiDscResourceBase if ($($currentProperties[$_].ToString()) -ne $($desiredProperties[$_].ToString())) { - throw "The '$($this.GetType().Name)', DSC Resource does not support changes for/to the '$_' property." - break + $errorMessage = "The '$($this.GetType().Name)', DSC Resource does not support changes for/to the '$_' property." + New-InvalidOperationException -Message $errorMessage } } } @@ -188,8 +189,8 @@ class AzDevOpsDscResourceBase : AzDevOpsApiDscResourceBase break } default { - throw "Could not obtain a valid 'Ensure' value within 'AzDevOpsProject' Test() function. Value was '$($desiredProperties.Ensure)'." - return [RequiredAction]::Error + $errorMessage = "Could not obtain a valid 'Ensure' value within '$($this.GetResourceName())' Test() function. Value was '$($desiredProperties.Ensure)'." + New-InvalidOperationException -Message $errorMessage } } @@ -258,7 +259,8 @@ class AzDevOpsDscResourceBase : AzDevOpsApiDscResourceBase } else { - throw "A required action of '$RequiredAction' has not been catered for in GetDesiredStateParameters() method." + $errorMessage = "A required action of '$RequiredAction' has not been catered for in GetDesiredStateParameters() method." + New-InvalidOperationException -Message $errorMessage } @@ -273,7 +275,15 @@ class AzDevOpsDscResourceBase : AzDevOpsApiDscResourceBase [System.Boolean] Test() { - return $this.TestDesiredState() + # TestDesiredState() will throw an exception in certain expected circumstances. Return $false if this occurs. + try + { + return $this.TestDesiredState() + } + catch + { + return $false + } } diff --git a/tests/Integration/DSCClassResources/AzDevOpsProject.Integration.Tests.ps1 b/tests/Integration/DSCClassResources/AzDevOpsProject.Integration.Tests.ps1 index 230f5cd95..1bb93179d 100644 --- a/tests/Integration/DSCClassResources/AzDevOpsProject.Integration.Tests.ps1 +++ b/tests/Integration/DSCClassResources/AzDevOpsProject.Integration.Tests.ps1 @@ -96,7 +96,7 @@ try It 'Should return $true when Test-DscConfiguration is run' { - Test-DscConfiguration -Verbose | Should -Be 'True' + Test-DscConfiguration -Verbose -ErrorAction Stop | Should -Be 'True' } } @@ -156,7 +156,7 @@ try It 'Should return $true when Test-DscConfiguration is run' { - Test-DscConfiguration -Verbose | Should -Be 'True' + Test-DscConfiguration -Verbose -ErrorAction Stop | Should -Be 'True' } } @@ -201,9 +201,9 @@ try } | Should -Not -Throw } - It 'Should return $false or $null when Test-DscConfiguration is run' { - Test-DscConfiguration -Verbose | Should -BeIn @('False',$null) - } + It 'Should return $false or $null when Test-DscConfiguration is run' { + Test-DscConfiguration -Verbose -ErrorAction Stop | Should -Be 'False' + } } @@ -258,7 +258,7 @@ try It 'Should return $true when Test-DscConfiguration is run' { - Test-DscConfiguration -Verbose | Should -Be 'True' + Test-DscConfiguration -Verbose -ErrorAction Stop | Should -Be 'True' } } @@ -315,7 +315,7 @@ try It 'Should return $true when Test-DscConfiguration is run' { - Test-DscConfiguration -Verbose | Should -Be 'True' + Test-DscConfiguration -Verbose -ErrorAction Stop | Should -Be 'True' } } @@ -375,7 +375,7 @@ try It 'Should return $true when Test-DscConfiguration is run' { - Test-DscConfiguration -Verbose | Should -Be 'True' + Test-DscConfiguration -Verbose -ErrorAction Stop | Should -Be 'True' } } @@ -421,7 +421,7 @@ try } It 'Should return $false or $null when Test-DscConfiguration is run' { - Test-DscConfiguration -Verbose | Should -BeIn @('False',$null) + Test-DscConfiguration -Verbose -ErrorAction Stop | Should -Be 'False' } } @@ -477,7 +477,7 @@ try It 'Should return $true when Test-DscConfiguration is run' { - Test-DscConfiguration -Verbose | Should -Be 'True' + Test-DscConfiguration -Verbose -ErrorAction Stop | Should -Be 'True' } } @@ -538,7 +538,7 @@ try It 'Should return $true when Test-DscConfiguration is run' { - Test-DscConfiguration -Verbose | Should -Be 'True' + Test-DscConfiguration -Verbose -ErrorAction Stop | Should -Be 'True' } } @@ -599,7 +599,7 @@ try It 'Should return $true when Test-DscConfiguration is run' { - Test-DscConfiguration -Verbose | Should -Be 'True' + Test-DscConfiguration -Verbose -ErrorAction Stop | Should -Be 'True' } } @@ -659,7 +659,7 @@ try It 'Should return $true when Test-DscConfiguration is run' { - Test-DscConfiguration -Verbose | Should -Be 'True' + Test-DscConfiguration -Verbose -ErrorAction Stop | Should -Be 'True' } } @@ -716,7 +716,7 @@ try It 'Should return $true when Test-DscConfiguration is run' { - Test-DscConfiguration -Verbose | Should -Be 'True' + Test-DscConfiguration -Verbose -ErrorAction Stop | Should -Be 'True' } }