MegaBites-AI/Windows-powershell
0372
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3function Test-ResourceStrings4{5 param ( $AssemblyName, $ExcludeList )6 7 # determine the needed resource directory. If these tests are moved8 # this logic will need to change9 $repoBase = (Resolve-Path (Join-Path $PSScriptRoot ../../../..)).Path10 $asmBase = Join-Path $repoBase "src/$AssemblyName"11 $resourceDir = Join-Path $asmBase resources12 $resourceFiles = Get-ChildItem $resourceDir -Filter *.resx -ErrorAction stop |13 Where-Object { $excludeList -notcontains $_.Name }14 15 $bindingFlags = [reflection.bindingflags]"NonPublic,Static"16 17 # the resource generation is based on the file name of the .resx file18 # and is not in a namespace. We can find all of these classes in the19 # ${AssemblyName} assembly20 $ASSEMBLY = [appdomain]::CurrentDomain.GetAssemblies()|21 Where-Object { $_.FullName -match "$AssemblyName" }22 23 # Validate that the binary you're running has the proper message24 # based on the resource files. It is possible that -ResGen could be25 # skipped and the messages might become out of sync with what is in26 # source27 #28 # you could argue that we should be generating conditions where the message is delivered29 # and then test for that, but that is a check against an ErrorId while this is ensuring30 # that the contents of the built binary matches the resx content31 #32 # Normally you don't want to use reflection to get at the implementation details33 # but this tests generated code, so changes there will be consistent across34 # all the resource classes we generate35 #36 # This is the reason why this is not a general module for use. There is37 # no other way to run these tests38 Describe "Resources strings in $AssemblyName (was -ResGen used with Start-PSBuild)" -Tag Feature {39 40 function NormalizeLineEnd41 {42 param (43 [string] $string44 )45 46 $string -replace "`r`n", "`n"47 }48 49 foreach ( $resourceFile in $resourceFiles )50 {51 # in the event that the id has a space in it, it is replaced with a '_'52 $classname = $resourcefile.Name -replace ".resx"53 It "'$classname' should be an available type and the strings should be correct" -Skip:(!$IsWindows) {54 # get the type from the assembly55 $resourceType = $ASSEMBLY.GetType($classname, $false, $true)56 # the properties themselves are static internals, so we need57 # to using the appropriate bindingflags58 $resourceType | Should -Not -BeNullOrEmpty59 60 # check all the resource strings61 $xmlData = [xml](Get-Content $resourceFile.Fullname)62 foreach ( $inResource in $xmlData.root.data ) {63 $resourceStringToCheck = $resourceType.GetProperty($inResource.name,$bindingFlags).GetValue(0)64 NormalizeLineEnd($resourceStringToCheck) | Should -Be (NormalizeLineEnd($inresource.value))65 }66 }67 }68 }69}70 