Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update RID parsing's LastIndexOf call to use invariant culture #35637

Merged
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
4 changes: 2 additions & 2 deletions src/Cli/dotnet/CommonOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,9 @@ public static string GetCurrentRuntimeId()
return currentRuntimeIdentifiers[0]; // First rid is the most specific (ex win-x64)
}

private static string GetOsFromRid(string rid) => rid.Substring(0, rid.LastIndexOf("-"));
private static string GetOsFromRid(string rid) => rid.Substring(0, rid.LastIndexOf("-", StringComparison.InvariantCulture));

private static string GetArchFromRid(string rid) => rid.Substring(rid.LastIndexOf("-") + 1, rid.Length - rid.LastIndexOf("-") - 1);
private static string GetArchFromRid(string rid) => rid.Substring(rid.LastIndexOf("-", StringComparison.InvariantCulture) + 1, rid.Length - rid.LastIndexOf("-", StringComparison.InvariantCulture) - 1);

private static IEnumerable<string> ForwardSelfContainedOptions(bool isSelfContained, ParseResult parseResult)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ public static void AddImplicitRestoreOptions(CliCommand command, bool showHelp =
command.Options.Add(option);
}
}
private static string GetOsFromRid(string rid) => rid.Substring(0, rid.LastIndexOf("-"));
private static string GetArchFromRid(string rid) => rid.Substring(rid.LastIndexOf("-") + 1, rid.Length - rid.LastIndexOf("-") - 1);
public static string RestoreRuntimeArgFunc(IEnumerable<string> rids)
private static string GetOsFromRid(string rid) => rid.Substring(0, rid.LastIndexOf("-", StringComparison.InvariantCulture));
private static string GetArchFromRid(string rid) => rid.Substring(rid.LastIndexOf("-", StringComparison.InvariantCulture) + 1, rid.Length - rid.LastIndexOf("-", StringComparison.InvariantCulture) - 1);
public static string RestoreRuntimeArgFunc(IEnumerable<string> rids)
{
List<string> convertedRids = new();
foreach (string rid in rids)
Expand Down
49 changes: 49 additions & 0 deletions src/Tests/dotnet.Tests/dotnet-msbuild/GivenDotnetOsArchOptions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Globalization;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Tools;
using BuildCommand = Microsoft.DotNet.Tools.Build.BuildCommand;
Expand Down Expand Up @@ -148,5 +149,53 @@ public void ArchOptionsAMD64toX64()
.StartWith($"{ExpectedPrefix} -restore -consoleloggerparameters:Summary -property:RuntimeIdentifier=os-x64 -property:SelfContained=false");
});
}

[Fact]
public void ArchOptionIsResolvedFromRidUnderDifferentCulture()
{
CultureInfo currentCultureBefore = CultureInfo.CurrentCulture;
try
{
CultureInfo.CurrentCulture = new CultureInfo("th");
CommandDirectoryContext.PerformActionWithBasePath(WorkingDirectory, () =>
{
var msbuildPath = "<msbuildpath>";
var command = BuildCommand.FromArgs(new string[] { "--os", "os" }, msbuildPath);
var expectedArch = RuntimeInformation.ProcessArchitecture.Equals(Architecture.Arm64) ? "arm64" : Environment.Is64BitOperatingSystem ? "x64" : "x86";
command.GetArgumentsToMSBuild()
.Should()
.StartWith($"{ExpectedPrefix} -restore -consoleloggerparameters:Summary -property:RuntimeIdentifier=os-{expectedArch}");
});
}
finally { CultureInfo.CurrentCulture = currentCultureBefore; }
}

[Fact]
public void OsOptionIsResolvedFromRidUnderDifferentCulture()
{
CultureInfo currentCultureBefore = CultureInfo.CurrentCulture;
try
{
CultureInfo.CurrentCulture = new CultureInfo("th");
CommandDirectoryContext.PerformActionWithBasePath(WorkingDirectory, () =>
{
var msbuildPath = "<msbuildpath>";
var command = BuildCommand.FromArgs(new string[] { "--arch", "arch" }, msbuildPath);
var expectedOs = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "win" :
RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "linux" :
RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? "osx" :
null;
if (expectedOs == null)
{
// Not a supported OS for running test
return;
}
command.GetArgumentsToMSBuild()
.Should()
.StartWith($"{ExpectedPrefix} -restore -consoleloggerparameters:Summary -property:RuntimeIdentifier={expectedOs}-arch");
});
}
finally { CultureInfo.CurrentCulture = currentCultureBefore;}
}
}
}
Loading