Skip to content

Commit fd1f2fa

Browse files
Check type before add-type and add trace log
1 parent 45cf1b5 commit fd1f2fa

2 files changed

Lines changed: 10 additions & 13 deletions

File tree

src/dsc/psresourceget.ps1

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -228,13 +228,17 @@ function SatisfiesVersion {
228228
[string]$versionRange
229229
)
230230

231-
try {
232-
Add-Type -Path "$PSScriptRoot/dependencies/NuGet.Versioning.dll" -ErrorAction Stop | Out-Null
231+
$typeName = 'NuGet.Versioning.VersionRange'
232+
233+
if ($typeName -as [type]) {
234+
Write-Trace -message "NuGet.Versioning assembly is already loaded. Using existing assembly." -level trace
233235
}
234-
catch [System.IO.FileLoadException] {
235-
Write-Trace -message "NuGet.Versioning assembly is already loaded. Continuing with existing assembly." -level trace
236+
else {
237+
Write-Trace -message "Loading NuGet.Versioning assembly from $PSScriptRoot/dependencies/NuGet.Versioning.dll" -level trace
238+
Add-Type -Path "$PSScriptRoot/dependencies/NuGet.Versioning.dll" -ErrorAction Stop | Out-Null
236239
}
237240

241+
238242
try {
239243
$versionRangeObj = [NuGet.Versioning.VersionRange]::Parse($versionRange)
240244
$resourceVersion = [NuGet.Versioning.NuGetVersion]::Parse($version)
@@ -269,14 +273,7 @@ function ConvertInputToPSResource(
269273

270274
# catch any un-caught exception and write it to the error stream
271275
trap {
272-
273-
if ($_.Exception.GetType().FullName -eq 'System.IO.FileLoadException') {
274-
Write-Trace -message "NuGet.Versioning assembly is already loaded. Continuing with existing assembly, as all the versions have the functionality available." -level trace
275-
}
276-
else {
277-
Write-Trace -Level Error -message $_.Exception.Message
278-
}
279-
276+
Write-Trace -Level Error -message $_.Exception.Message
280277
exit 1
281278
}
282279

test/DscResource/PSResourceGetDSCResource.Tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ Describe 'Repository Resource Tests' -Tags 'CI' {
103103

104104
$resourceInput = $repoParams | ConvertTo-Json -Depth 5
105105

106-
$getResult = & $script:dscExe resource get --resource Microsoft.PowerShell.PSResourceGet/Repository --input $resourceInput -o json | ConvertFrom-Json
106+
$getResult = & $script:dscExe -l trace resource get --resource Microsoft.PowerShell.PSResourceGet/Repository --input $resourceInput -o json | ConvertFrom-Json
107107

108108
$getResult.actualState.name | Should -BeExactly 'TestRepo'
109109
$getResult.actualState.uri | Should -BeExactly 'https://www.doesnotexist.com/'

0 commit comments

Comments
 (0)