Skip to content

Commit

Permalink
Add globalization scenario test (#6012)
Browse files Browse the repository at this point in the history
  • Loading branch information
lbussell committed Nov 7, 2024
1 parent 2676db4 commit 8b917f2
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 2 deletions.
4 changes: 2 additions & 2 deletions tests/Microsoft.DotNet.Docker.Tests/FileHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static string DiffFiles(string file1Path, string file2Path, ITestOutputHe
}
}

public class TempFolderContext : IDisposable
public sealed class TempFolderContext : IDisposable
{
public TempFolderContext()
{
Expand All @@ -81,7 +81,7 @@ public void Dispose()
}
}

public class TempFileContext : IDisposable
public sealed class TempFileContext : IDisposable
{
public TempFileContext()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
</ItemGroup>

<ItemGroup>
<Compile Remove="TestAppArtifacts\GlobalizationTest.cs" />
<Compile Remove="TestAppArtifacts\UnitTests.cs" />
</ItemGroup>

Expand Down
1 change: 1 addition & 0 deletions tests/Microsoft.DotNet.Docker.Tests/RuntimeImageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.DotNet.Docker.Tests.TestScenarios;
using Xunit;
using Xunit.Abstractions;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System.Globalization;
using static System.Console;

const string envVarName = "DOTNET_SYSTEM_GLOBALIZATION_INVARIANT";
string envVarValue = Environment.GetEnvironmentVariable(envVarName) ?? "null";
bool invariantModeEnvVarSetting = envVarValue == "true" || envVarValue == "1";
bool invariantModeEnabled = IsInvariantModeEnabled();

string isEnabledString = $"Globalization invariant mode is {(invariantModeEnabled ? "enabled" : "disabled")}";
WriteLine(isEnabledString);

if (invariantModeEnabled != invariantModeEnvVarSetting)
{
throw new Exception("Environment variable mis-match: " + isEnabledString
+ $", but {envVarName} is set to `{envVarValue}`, which evaluates to {invariantModeEnvVarSetting}.");
}

try
{
WriteLine($"The following should {(invariantModeEnabled ? "" : "not ")}produce an exception:");
TestGlobalizationFunctionality();
TestTimeZoneFunctionality();
if (invariantModeEnabled)
{
throw new Exception("Expected an exception when testing globalization functionality but one did not occur.");
}
}
catch (CultureNotFoundException)
{
if (invariantModeEnabled)
{
WriteLine("Successfully caught a CultureNotFoundException, invariant mode is working as expected.");
}
else
{
throw;
}
}

WriteLine("Globalization test succeeded");

void TestGlobalizationFunctionality()
{
const int Value = 1337;
WriteLine($"Value: {Value}");
WriteLine($" en-US: {Value.ToString("c", new CultureInfo("en-US"))}");
WriteLine($" jp-JP: {Value.ToString("c", new CultureInfo("jp-JP"))}");
}

void TestTimeZoneFunctionality()
{
DateTime localTime = DateTime.Now;
TimeZoneInfo pacificZone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
DateTime pacificTime = TimeZoneInfo.ConvertTime(localTime, pacificZone);
WriteLine("Local Time: " + localTime);
WriteLine("Pacific Time: " + pacificTime);
}

// https://stackoverflow.com/a/75299176
bool IsInvariantModeEnabled()
{
try
{
return CultureInfo.GetCultureInfo("en-US").NumberFormat.CurrencySymbol == "¤";
}
catch (CultureNotFoundException)
{
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.IO;
using System.Threading.Tasks;
using FluentAssertions;

namespace Microsoft.DotNet.Docker.Tests.TestScenarios;

#nullable enable
public sealed class GlobalizationScenario : ITestScenario, IDisposable
{
private const string Dockerfile = """
ARG sdk_image
ARG runtime_image

FROM ${sdk_image} AS sdk
RUN dotnet new console -n App -o /src --no-restore
WORKDIR /src
COPY Program.cs /src/Program.cs
RUN dotnet restore
RUN dotnet publish --no-restore -o /app

FROM ${runtime_image} AS runtime
COPY --from=sdk /app /app/
ENTRYPOINT ["/app/App"]
""";

private readonly TempFolderContext _tempFolderContext = FileHelper.UseTempFolder();
private readonly ProductImageData _imageData;
private readonly DotNetImageRepo _repo;
private readonly DockerHelper _dockerHelper;

public GlobalizationScenario(
ProductImageData imageData,
DotNetImageRepo repo,
DockerHelper dockerHelper)
{
_imageData = imageData;
_repo = repo;
_dockerHelper = dockerHelper;
}

public async Task ExecuteAsync()
{
// Setup project in temp dir
string dockerfilePath = Path.Combine(_tempFolderContext.Path, "Dockerfile");
await File.WriteAllTextAsync(path: dockerfilePath, contents: Dockerfile);

File.Copy(
sourceFileName: Path.Combine(DockerHelper.TestArtifactsDir, "GlobalizationTest.cs"),
destFileName: Path.Combine(_tempFolderContext.Path, "Program.cs"));

string tag = nameof(GlobalizationScenario).ToLowerInvariant();
_dockerHelper.Build(
tag: tag,
dockerfile: dockerfilePath,
contextDir: _tempFolderContext.Path,
pull: Config.PullImages,
buildArgs:
[
$"sdk_image={_imageData.GetImage(DotNetImageRepo.SDK, _dockerHelper)}",
$"runtime_image={_imageData.GetImage(_repo, _dockerHelper)}",
]);

string containerName = ImageData.GenerateContainerName(nameof(GlobalizationScenario));
string output = _dockerHelper.Run(tag, containerName);

output.Should().NotBeNullOrWhiteSpace();
output.Should().ContainEquivalentOf("Globalization test succeeded", Exactly.Once());
}

public void Dispose() => _tempFolderContext.Dispose();
}

0 comments on commit 8b917f2

Please sign in to comment.