Skip to content

Commit

Permalink
Validate product resource strings against resx files (#4811)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesWTruher authored and TravisEz13 committed Sep 18, 2017
1 parent 0770746 commit 0229451
Show file tree
Hide file tree
Showing 11 changed files with 170 additions and 3 deletions.
Expand Up @@ -1055,7 +1055,7 @@ protected override void EndProcessing()
}
catch (Win32Exception exception)
{
WriteNonTerminatingError(process, exception, ProcessResources.Process_is_not_terminated, "ProcessNotTerminated", ErrorCategory.CloseError);
WriteNonTerminatingError(process, exception, ProcessResources.ProcessIsNotTerminated, "ProcessNotTerminated", ErrorCategory.CloseError);
}
}

Expand Down Expand Up @@ -1084,7 +1084,7 @@ protected override void EndProcessing()
}
catch (Win32Exception exception)
{
WriteNonTerminatingError(process, exception, ProcessResources.Process_is_not_terminated, "ProcessNotTerminated", ErrorCategory.CloseError);
WriteNonTerminatingError(process, exception, ProcessResources.ProcessIsNotTerminated, "ProcessNotTerminated", ErrorCategory.CloseError);
}
}
}
Expand Down
Expand Up @@ -150,7 +150,7 @@
<data name="UseShell" xml:space="preserve">
<value>The specified path is not a valid win32 application. Try again with the UseShellExecute.</value>
</data>
<data name="Process is not terminated" xml:space="preserve">
<data name="ProcessIsNotTerminated" xml:space="preserve">
<value>This command stopped operation of "{0} ({1})" because of the following error: {2}.</value>
</data>
<data name="RedirectionParams" xml:space="preserve">
Expand Down
@@ -0,0 +1,12 @@
. "$psscriptroot/TestRunner.ps1"

$assemblyName = "Microsoft.Management.Infrastructure.CimCmdlets"

# excluded resources, taken from the 'EmbeddedResource Remove'
# entries in the csproj for the assembly
$excludeList = @()
# load the module since it isn't there by default
import-module CimCmdlets

# run the tests
Test-ResourceStrings -AssemblyName $AssemblyName -ExcludeList $excludeList
@@ -0,0 +1,10 @@
. "$psscriptroot/TestRunner.ps1"

$AssemblyName = "Microsoft.PowerShell.ConsoleHost"

# excluded resources, taken from the 'EmbeddedResource Remove'
# entries in the csproj for the assembly
$excludeList = @("HostMshSnapinResources.resx")

# run the tests
Test-ResourceStrings -AssemblyName $AssemblyName -ExcludeList $excludeList
@@ -0,0 +1,11 @@
. "$psscriptroot/TestRunner.ps1"

$assemblyName = "Microsoft.PowerShell.CoreCLR.Eventing"

# excluded resources, taken from the 'EmbeddedResource Remove'
# entries in the csproj for the assembly
$excludeList = @()
# load the module since it isn't there by default

# run the tests
Test-ResourceStrings -AssemblyName $AssemblyName -ExcludeList $excludeList
@@ -0,0 +1,17 @@
. "$psscriptroot/TestRunner.ps1"
$AssemblyName = "Microsoft.PowerShell.Commands.Management"

# excluded resources, taken from the 'EmbeddedResource Remove'
# entries in the csproj for the assembly
$excludeList = "EventlogResources.resx",
"TransactionResources.resx",
"WebServiceResources.resx",
"HotFixResources.resx",
"ControlPanelResources.resx",
"WmiResources.resx",
"ManagementMshSnapInResources.resx",
"ClearRecycleBinResources.resx",
"ClipboardResources.resx"

# run the tests
Test-ResourceStrings -AssemblyName $AssemblyName -ExcludeList $excludeList
@@ -0,0 +1,9 @@
. "$psscriptroot/TestRunner.ps1"
$AssemblyName = "System.Management.Automation"

# excluded resources, taken from the 'EmbeddedResource Remove'
# entries in the csproj for the assembly
$excludeList = "CoreMshSnapinResources.resx",
"ErrorPackageRemoting.resx"
# run the tests
Test-ResourceStrings -AssemblyName $AssemblyName -ExcludeList $excludeList
@@ -0,0 +1,12 @@
. "$psscriptroot/TestRunner.ps1"

$assemblyName = "Microsoft.PowerShell.Security"

# excluded resources, taken from the 'EmbeddedResource Remove'
# entries in the csproj for the assembly
$excludeList = @("SecurityMshSnapinResources.resx")
# load the module since it isn't there by default
import-module Microsoft.PowerShell.Security

# run the tests
Test-ResourceStrings -AssemblyName $AssemblyName -ExcludeList $excludeList
56 changes: 56 additions & 0 deletions test/powershell/engine/ResourceValidation/TestRunner.ps1
@@ -0,0 +1,56 @@
function Test-ResourceStrings
{
param ( $AssemblyName, $ExcludeList )

# determine the needed resource directory. If these tests are moved
# this logic will need to change
$repoBase = (Resolve-Path (Join-Path $psScriptRoot ../../../..)).Path
$asmBase = Join-Path $repoBase "src/$AssemblyName"
$resourceDir = Join-Path $asmBase resources
$resourceFiles = Get-ChildItem $resourceDir -Filter *.resx -ea stop |
Where-Object { $excludeList -notcontains $_.Name }

$bindingFlags = [reflection.bindingflags]"NonPublic,Static"

# the resource generation is based on the file name of the .resx file
# and is not in a namespace. We can find all of these classes in the
# ${AssemblyName} assembly
$ASSEMBLY = [appdomain]::CurrentDomain.GetAssemblies()|
Where-Object { $_.FullName -match "$AssemblyName" }

# Validate that the binary you're running has the proper message
# based on the resource files. It is possible that -ResGen could be
# skipped and the messages might become out of sync with what is in
# source
#
# you could argue that we should be generating conditions where the message is delivered
# and then test for that, but that is a check against an ErrorId while this is ensuring
# that the contents of the built binary matches the resx content
#
# Normally you don't want to use reflection to get at the implementation details
# but this tests generated code, so changes there will be consistent across
# all the resource classes we generate
#
# This is the reason why this is not a general module for use. There is
# no other way to run these tests
Describe "Resources strings in $AssemblyName (was -ResGen used with Start-PSBuild)" -tag Feature {
foreach ( $resourceFile in $resourceFiles )
{
# in the event that the id has a space in it, it is replaced with a '_'
$classname = $resourcefile.Name -replace ".resx"
It "'$classname' should be an available type and the strings should be correct" {
# get the type from the assembly
$resourceType = $ASSEMBLY.GetType($classname, $false, $true)
# the properties themselves are static internals, so we need
# to using the appropriate bindingflags
$resourceType | Should Not BeNullOrEmpty

# check all the resource strings
$xmlData = [xml](Get-Content $resourceFile.Fullname)
foreach ( $inResource in $xmlData.root.data ) {
$resourceType.GetProperty($inResource.name,$bindingFlags).GetValue(0) | should be $inresource.value
}
}
}
}
}
@@ -0,0 +1,18 @@
. "$psscriptroot/TestRunner.ps1"
$AssemblyName = "Microsoft.PowerShell.Commands.Utility"

# excluded resources, taken from the 'EmbeddedResource Remove'
# entries in the csproj for the assembly
$excludeList = "CoreMshSnapinResources.resx",
"ErrorPackageRemoting.resx",
"FormatAndOut_out_gridview.resx",
"UtilityMshSnapinResources.resx",
"OutPrinterDisplayStrings.resx",
"UpdateListStrings.resx",
"ConvertFromStringResources.resx",
"ConvertStringResources.resx",
"FlashExtractStrings.resx",
"ImmutableStrings.resx"
import-module Microsoft.Powershell.Utility
# run the tests
Test-ResourceStrings -AssemblyName $AssemblyName -ExcludeList $excludeList
22 changes: 22 additions & 0 deletions test/powershell/engine/ResourceValidation/WSManResources.Tests.ps1
@@ -0,0 +1,22 @@
. "$psscriptroot/TestRunner.ps1"

$assemblyName = "Microsoft.WSMan.Management"

# excluded resources, taken from the 'EmbeddedResource Remove'
# entries in the csproj for the assembly
$excludeList = @()
# load the module since it isn't there by default
import-module Microsoft.WSMan.Management


try {
if ( ! $IsWindows )
{
$PSDefaultParameterValues["it:skip"] = $true
}
# run the tests
Test-ResourceStrings -AssemblyName $AssemblyName -ExcludeList $excludeList
}
finally {
$PSDefaultParameterValues.Remove("it:skip")
}

0 comments on commit 0229451

Please sign in to comment.