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

Fix tests failing when run on systems with non-English language/locale settings #5442

Conversation

dotnokato
Copy link
Contributor

Bug

Fixes: NuGet/Home#12820

Regression? Last working version:

Description

Fixed tests that were failing when run on systems with non-English language/locale settings

  • tests that relied on English language
    • updated asserts to use product resource string instead of hardcoded en-US strings where viable
    • for tests asserting exception message text, set the Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;
  • tests failing due to expected values being English-localized (e.g. decimal separator = .)
    • converted the expected values from string to decimal and back to (localized) string
    • set Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; where the above was not viable
  • updated test PerformUpToDateCheck_WhenNonBuildIntegratedProjectIsAParentOfADirtySpec_ReturnsAListWithoutNonBuildIntegratedProjects to use TestDirectory instead of creating test files in C:\ drive
  • renamed test ToReadableTimeFormat_Localized_AssumesEnglishLocale_ContainsTimeNumber_Succeeds to ToReadableTimeFormat_Localized_AssumesCurrentCulture_ContainsTimeNumber_Succeeds so that the name corresponds to the behaviour of method under test (after verifying the way it is being used)

PR Checklist

  • PR has a meaningful title

  • PR has a linked issue.

  • Described changes

  • Tests

    • Automated tests added
    • OR
    • Test exception
    • OR
    • N/A
  • Documentation

    • Documentation PR or issue filled
    • OR
    • N/A

@dotnokato dotnokato requested a review from a team as a code owner October 1, 2023 19:42
@ghost ghost added the Community PRs created by someone not in the NuGet team label Oct 1, 2023
@@ -2290,6 +2291,9 @@ public async Task TestPackageManager_DowngradePackageFor_BottomtProject_Success(
[Fact]
public async Task TestPackageManager_CancellationTokenPassed()
{
// Get exception messages in English
Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to set this globally for all of our tests so that in the future we don't cause this to happen again?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would. The question is how to do it assembly/codebase wise?
Or do we want at least to set this on a test class level instead of individual tests level?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest, making the threads run in invariant culture doesn't make me proud of our code quality.

Something I particularly don't like is that there's no cleanup afterwards to set the culture back to the original value. If there are other tests that explicitly want to test some other culture (I think Fernando added one or two within the last 2 years, but before that I'd say NuGet has none), then this could introduce flakiness depending on whether or not the other test re-uses the same thread. We also run tests in parllel, and this method is async, which increases risk that another test could re-use the same thread.

My memory is that xunit has its own set of threads to run tests on, which means that anywhere NuGet does .ConfigureAwait(false) (which I think we only have two or three examples of, in NuGet.Protocol, but we might end up doing more in the future), or anywhere Task.Run() is used, code might end up running on the thread pool, which is still using the default UI culture.

There's always a case of "don't let perfect be the enemy of good", so we don't have to change the test code to work correctly when using the default system culture. Maybe this "set current thread UI culture to invariant" is good enough. But how difficult is it to "properly fix" tests instead? I've been meaning to try myself, but I don't want to change my system locale on a machine I really use, and creating a new VM, installing all the dev tools, and cloning the repo has been too much effort for me so far.

@jeffkl jeffkl self-assigned this Oct 3, 2023
@ghost ghost added the Status:No recent activity PRs that have not had any recent activity and will be closed if the label is not removed label Oct 10, 2023
@ghost
Copy link

ghost commented Oct 10, 2023

This PR has been automatically marked as stale because it has no activity for 7 days. It will be closed if no further activity occurs within another 90 days of this comment. If it is closed, you may reopen it anytime when you're ready again, as long as you don't delete the branch.

@ghost ghost removed the Status:No recent activity PRs that have not had any recent activity and will be closed if the label is not removed label Oct 10, 2023
…\ drive

Test name: `PerformUpToDateCheck_WhenNonBuildIntegratedProjectIsAParentOfADirtySpec_ReturnsAListWithoutNonBuildIntegratedProjects`
…th non-English locales

renamed test to align with the tested behaviour
@dotnokato dotnokato force-pushed the dev-dotnokato-fix-tests-failing-with-non-english-locales branch from a1f55b7 to c621d6a Compare October 14, 2023 16:50
@dotnokato
Copy link
Contributor Author

I've pushed updated version that uses UseCulture attribute similar to https://github.com/xunit/samples.xunit/blob/main/UseCulture/UseCultureAttribute.cs. This way, the thread culture is only changed for the time of the test. Then it is being reset to the original value.

@ghost ghost added the Status:No recent activity PRs that have not had any recent activity and will be closed if the label is not removed label Oct 24, 2023
@ghost
Copy link

ghost commented Oct 24, 2023

This PR has been automatically marked as stale because it has no activity for 7 days. It will be closed if no further activity occurs within another 90 days of this comment. If it is closed, you may reopen it anytime when you're ready again, as long as you don't delete the branch.

@jeffkl jeffkl assigned martinrrm and unassigned jeffkl Nov 1, 2023
@jeffkl jeffkl removed the Status:No recent activity PRs that have not had any recent activity and will be closed if the label is not removed label Nov 1, 2023
@martinrrm martinrrm merged commit 9364a64 into NuGet:dev Nov 1, 2023
@dotnokato dotnokato deleted the dev-dotnokato-fix-tests-failing-with-non-english-locales branch November 2, 2023 00:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Community PRs created by someone not in the NuGet team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Test: use product resource string instead of hard-coded en-US string
4 participants