Skip to content

Commit 253c8e2

Browse files
Pin the test run to the invariant culture
Seven tests asserted English text and a decimal point and failed on a French Windows ("1,5 KB" instead of "1.5 KB", "Langue" instead of "Language"). The app formats for the user's culture on purpose, so the tests are what needed fixing: SpaceSharp.Tests/TestCulture.cs sets the default thread culture to invariant when the test assembly loads, before xUnit starts its workers, so the suite runs the same on every machine. A new test switches to fr-FR and asserts the French output, so that behavior is expected rather than tolerated. Tests that need another culture keep setting it on their own thread and restoring it.
1 parent 78dfcfe commit 253c8e2

3 files changed

Lines changed: 43 additions & 0 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ What changed in each SpaceSharp release from 2.0 on, newest first; the 1.x relea
77
No visible change: a readability pass over the whole code base.
88

99
### Under the hood
10+
- The tests no longer depend on the developer's Windows language: the test assembly pins the invariant culture, so a French Windows no longer fails seven tests on `1,5 KB` and `Langue`. A new test asserts the French output on purpose. Reported by a SpaceMonger veteran trying SpaceSharp on a French PC.
1011
- Every C# file was reworked for readability without changing what it does: descriptive names in place of one-letter variables and lambda parameters, long methods split into focused ones (the MFT scanner, the Settings tabs, the Insights and Changes views, the scan card), nested blocks flattened with early returns, long expressions and argument lists broken up, and a comment on every intentionally ignored exception saying why it is safe to ignore. Public members, settings, file formats and localized strings are untouched.
1112
- The XML documentation now covers the whole solution: every documented method and constructor has a `<param>` for each parameter, in declaration order, with the unit, range, null meaning or callback contract rather than a restated name, plus `<returns>` and `<typeparam>` where they apply; public members and the non-trivial private ones that had no summary have one. The code itself is unchanged.
1213
- `CODING_STANDARDS.md` writes those rules down, with examples from the code of what to do and what not to do; CONTRIBUTING and DEVELOPING link to it.

‎SpaceSharp.Tests/LocalizationTests.cs‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Globalization;
22
using System.Text.RegularExpressions;
33
using System.Xml.Linq;
4+
using SpaceSharp.Services;
45
using SpaceSharp.Util;
56

67
namespace SpaceSharp.Tests;
@@ -143,4 +144,21 @@ public void FormatUsesTheCurrentCultureForNumbers()
143144
}
144145
finally { (CultureInfo.CurrentCulture, CultureInfo.CurrentUICulture) = before; }
145146
}
147+
148+
/// <summary>Sizes, percentages and resources follow the user's culture: a French Windows sees a decimal comma and French text.</summary>
149+
/// <remarks>The rest of the suite runs under the invariant culture (see <see cref="TestCulture"/>); this is the one test that asserts the other way.</remarks>
150+
[Fact]
151+
public void FrenchCultureFormatsWithADecimalComma()
152+
{
153+
(CultureInfo CurrentCulture, CultureInfo CurrentUICulture) before = (CultureInfo.CurrentCulture, CultureInfo.CurrentUICulture);
154+
try
155+
{
156+
CultureInfo.CurrentCulture = CultureInfo.CurrentUICulture = CultureInfo.GetCultureInfo("fr-FR");
157+
Assert.Equal("1,5 KB", SizeFormatter.Format(1536));
158+
Assert.Equal("1,07 KB", SizeFormatter.Compact(1100));
159+
Assert.Equal("8,9%", InspectSummary.Percent(0.089));
160+
Assert.Equal("Langue", Strings.Get("Settings_Language"));
161+
}
162+
finally { (CultureInfo.CurrentCulture, CultureInfo.CurrentUICulture) = before; }
163+
}
146164
}

‎SpaceSharp.Tests/TestCulture.cs‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System.Globalization;
2+
using System.Runtime.CompilerServices;
3+
4+
namespace SpaceSharp.Tests;
5+
6+
/// <summary>Pins the test run to the invariant culture so expected strings do not depend on the developer's Windows settings.</summary>
7+
/// <remarks>
8+
/// The app formats sizes, percentages and dates for the user's culture on purpose, and the resources fall back to English
9+
/// under the invariant culture. Without this, a French Windows turns "1.5 KB" into "1,5 KB" and "Language" into "Langue"
10+
/// and seven tests fail. A test that needs a specific culture sets it on its own thread and restores it in a finally block,
11+
/// as <see cref="LocalizationTests"/> does; the thread setting wins over the default set here.
12+
/// </remarks>
13+
public static class TestCulture
14+
{
15+
/// <summary>Runs once when the test assembly loads, before xUnit creates its worker threads.</summary>
16+
[ModuleInitializer]
17+
public static void Pin()
18+
{
19+
CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
20+
CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.InvariantCulture;
21+
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
22+
CultureInfo.CurrentUICulture = CultureInfo.InvariantCulture;
23+
}
24+
}

0 commit comments

Comments
 (0)