Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,7 @@ internal sealed class StringResourceReference
{
internal DatabaseLoadingInfo loadingInfo = null;
internal string assemblyName = null;
internal string assemblyLocation = null;
internal string baseName = null;
internal string resourceId = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ private string GetStringHelper(StringResourceReference resourceReference, out Lo
result = LoadingResult.AssemblyNotFound;
return null;
}

else
{
resourceReference.assemblyLocation = loadResult.a.Location;
};

// load now the resource from the resource manager cache
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1832,8 +1832,7 @@ private void ReportStringResourceFailure(StringResourceReference resource,
{
case DisplayResourceManagerCache.AssemblyBindingStatus.FoundInPath:
{
assemblyDisplayName =
System.IO.Path.Combine(resource.loadingInfo.fileDirectory, resource.assemblyName);
assemblyDisplayName = resource.assemblyLocation;
}
break;
case DisplayResourceManagerCache.AssemblyBindingStatus.FoundInGac:
Expand Down
34 changes: 32 additions & 2 deletions src/System.Management.Automation/utils/ResourceManagerCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,38 @@ internal static string GetResourceString(
throw PSTraceSource.NewArgumentException("resourceId");
}

ResourceManager resourceManager = GetResourceManager(assembly, baseName);
string text = resourceManager.GetString(resourceId);
ResourceManager resourceManager = null;
string text = string.Empty;

// For a non-existing resource defined by {assembly,baseName,resourceId}
// MissingManifestResourceException is thrown only at the time when resource retrieval method
// such as ResourceManager.GetString or ResourceManager.GetObject is called,
// not when you instantiate a ResourceManager object.
try
{
// try with original baseName first
// if it fails then try with alternative resource path format
resourceManager = GetResourceManager(assembly, baseName);
text = resourceManager.GetString(resourceId);
}
catch (MissingManifestResourceException)
{
const string resourcesSubstring = ".resources.";
int resourcesSubstringIndex = baseName.IndexOf(resourcesSubstring);
string newBaseName = string.Empty;
if (resourcesSubstringIndex != -1)
{
newBaseName = baseName.Substring(resourcesSubstringIndex + resourcesSubstring.Length); // e.g. "FileSystemProviderStrings"
}
else
{
newBaseName = string.Concat(assembly.GetName().Name, resourcesSubstring, baseName); // e.g. "System.Management.Automation.resources.FileSystemProviderStrings"
}

resourceManager = GetResourceManager(assembly, newBaseName);
text = resourceManager.GetString(resourceId);
}

if (String.IsNullOrEmpty(text) && s_DFT_monitorFailingResourceLookup)
{
Diagnostics.Assert(false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ Describe "Update-FormatData" -Tags "CI" {
}
Context "Validate Update-FormatData update correctly" {

It "Should not throw upon reloading previous formatting file" {
{ Update-FormatData } | Should Not throw
}
It "Should not throw upon reloading previous formatting file" {
{ Update-FormatData } | Should Not throw
}

It "Should validly load formatting data" {
Get-FormatData -typename System.Diagnostics.Process | Export-FormatData -Path $path
It "Should validly load formatting data" {
Get-FormatData -typename System.Diagnostics.Process | Export-FormatData -Path $path
$null = $ps.AddScript("Update-FormatData -prependPath $path")
$ps.Invoke()
$ps.HadErrors | Should be $false
}
}
}
}

Expand All @@ -32,7 +32,7 @@ Describe "Update-FormatData basic functionality" -Tags "CI" {
$testfilename = "testfile.ps1xml"
$testfile = Join-Path -Path $TestDrive -ChildPath $testfilename

$xmlContent=@"
$xmlContent=@"
<Types>
<Type>
<Name>AnyName</Name>
Expand All @@ -48,12 +48,60 @@ Describe "Update-FormatData basic functionality" -Tags "CI" {
</Type>
</Types>
"@
$xmlContent > $testfile
$xmlContent > $testfile
}

It "Update-FormatData with WhatIf should work"{
It "Update-FormatData with WhatIf should work"{

{ Update-FormatData -Append $testfile -WhatIf } | Should Not Throw
{ Update-FormatData -Prepend $testfile -WhatIf } | Should Not Throw
}
}
}


Describe "Update-FormatData with resources in CustomControls" -Tags "CI" {

BeforeAll {
$templatePath = Join-Path $PSScriptRoot (Join-Path 'assets' 'UpdateFormatDataTests.format.ps1xml')
$formatFilePath = Join-Path $TestDrive 'UpdateFormatDataTests.format.ps1xml'
$ps = [powershell]::Create()
$iss = [system.management.automation.runspaces.initialsessionstate]::CreateDefault2()
$rs = [system.management.automation.runspaces.runspacefactory]::CreateRunspace($iss)
$rs.Open()
$ps.Runspace = $rs
}
AfterAll {
$rs.Close()
$ps.Dispose()
}
Context "Validate Update-FormatData" {
It "Resources in WindowsPS syntax should be loaded successfully" {
$format = Get-Content -Path $templatePath -Raw
$format.Replace("%BaseName%","FileSystemProviderStrings") | Set-Content -Path $formatFilePath -Force
$null = $ps.AddScript("Update-FormatData -PrependPath $formatFilePath")
$ps.Streams.Error.Clear()
$ps.Invoke()
$ps.Streams.Error | Should BeNullOrEmpty

}
It "Resources in CorePS syntax should be loaded successfully" {
$format = Get-Content -Path $templatePath -Raw
$format.Replace("%BaseName%","System.Management.Automation.resources.FileSystemProviderStrings") | Set-Content -Path $formatFilePath -Force
$null = $ps.AddScript("Update-FormatData -PrependPath $formatFilePath")
$ps.Streams.Error.Clear()
$ps.Invoke()
$ps.Streams.Error | Should BeNullOrEmpty
}
It "Verify assembly path in error message when resource is Not found" {
$format = Get-Content -Path $templatePath -Raw
$format.Replace("%BaseName%","NonExistingResource") | Set-Content -Path $formatFilePath -Force
$null = $ps.AddScript("Update-FormatData -PrependPath $formatFilePath")
$ps.Streams.Error.Clear()
$ps.Invoke()
$sma = [appdomain]::CurrentDomain.GetAssemblies() | ? { if ($_.Location) {$_.Location.EndsWith("System.Management.Automation.dll")}}
$smaLocation = $sma.Location
$ps.Streams.Error | %{ $_.Exception.Message.Contains($smaLocation) | Should be $true }
$ps.Streams.Error | %{ $_.FullyQualifiedErrorId | Should Match 'FormatXmlUpdateException' }
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<Configuration>
<SelectionSets>
<SelectionSet>
<Name>TestTypes</Name>
<Types>
<TypeName>NonExistingTestType</TypeName>
</Types>
</SelectionSet>
</SelectionSets>

<Controls>
<Control>
<Name>TestTypes-GroupingFormat</Name>
<CustomControl>
<CustomEntries>
<CustomEntry>
<CustomItem>
<Frame>
<LeftIndent>0</LeftIndent>
<CustomItem>
<Text AssemblyName="System.Management.Automation" BaseName="%BaseName%" ResourceId="DirectoryDisplayGrouping"/>
<NewLine/>
</CustomItem>
</Frame>
</CustomItem>
</CustomEntry>
</CustomEntries>
</CustomControl>
</Control>
</Controls>
</Configuration>