Skip to content

Commit

Permalink
Merge pull request #8931 from JanKrivanek/bugfix-unicode-appconfig
Browse files Browse the repository at this point in the history
Support all codepages in path to app.config
  • Loading branch information
JanKrivanek committed Jun 22, 2023
2 parents 83ac8d9 + bdd0ad7 commit dfa7f02
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2911,7 +2911,7 @@ private static DateTime GetLastWriteTime(string path)
/// </summary>
/// <param name="appConfigFile"></param>
/// <param name="redirects"></param>
protected static string WriteAppConfig(string redirects)
protected static string WriteAppConfig(string redirects, string appConfigNameSuffix = null)
{
string appConfigContents =
"<configuration>\n" +
Expand All @@ -2920,7 +2920,7 @@ protected static string WriteAppConfig(string redirects)
" </runtime>\n" +
"</configuration>";

string appConfigFile = FileUtilities.GetTemporaryFileName();
string appConfigFile = FileUtilities.GetTemporaryFileName() + appConfigNameSuffix;
File.WriteAllText(appConfigFile, appConfigContents);
return appConfigFile;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,16 @@ internal new string[] DefaultPaths
/// - An app.config was passed in that promotes UnifyMe version from 1.0.0.0 to 2.0.0.0
/// - Version 1.0.0.0 of UnifyMe exists.
/// - Version 2.0.0.0 of UnifyMe exists.
/// - The case is attempted on special unicode characters in path as well.
/// Expected:
/// - The resulting UnifyMe returned should be 2.0.0.0.
/// Rationale:
/// Strongly named dependencies should unify according to the bindingRedirects in the app.config.
/// </summary>
[Fact]
public void Exists()
[Theory]
[InlineData(null)]
[InlineData("\uE025\uE026")]
public void Exists(string appConfigNameSuffix)
{
// Create the engine.
MockEngine engine = new MockEngine(_output);
Expand All @@ -59,7 +62,8 @@ public void Exists()
" <dependentAssembly>\n" +
" <assemblyIdentity name='UnifyMe' PublicKeyToken='b77a5c561934e089' culture='neutral' />\n" +
" <bindingRedirect oldVersion='1.0.0.0' newVersion='2.0.0.0' />\n" +
" </dependentAssembly>\n");
" </dependentAssembly>\n",
appConfigNameSuffix);

// Now, pass feed resolved primary references into ResolveAssemblyReference.
ResolveAssemblyReference t = new ResolveAssemblyReference();
Expand Down
8 changes: 6 additions & 2 deletions src/Tasks/AppConfig/AppConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.IO;
using System.Xml;

using Microsoft.Build.Shared;
Expand All @@ -24,13 +25,16 @@ internal void Load(string appConfigFile)
XmlReader reader = null;
try
{
var readerSettings = new XmlReaderSettings { DtdProcessing = DtdProcessing.Ignore };
var readerSettings = new XmlReaderSettings { DtdProcessing = DtdProcessing.Ignore, CloseInput = true};

// it's important to normalize the path as it may contain two slashes
// see https://github.com/dotnet/msbuild/issues/4335 for details.
appConfigFile = FileUtilities.NormalizePath(appConfigFile);

reader = XmlReader.Create(appConfigFile, readerSettings);
// Need a filestream as the XmlReader doesn't support nonstandard unicode characters in path.
// No need to dispose - as 'CloseInput' was passed to XmlReaderSettings
FileStream fs = File.OpenRead(appConfigFile);
reader = XmlReader.Create(fs, readerSettings);
Read(reader);
}
catch (XmlException e)
Expand Down

0 comments on commit dfa7f02

Please sign in to comment.