Skip to content

Commit 0d913bf

Browse files
authored
Fix unit test failed due to localized culture and line ending (#7779) (#8021)
* Fix unit test failed due to localized culture and line ending (#7779)
1 parent 7950ae6 commit 0d913bf

6 files changed

Lines changed: 65 additions & 25 deletions

File tree

tests/NuGetGallery.Core.Facts/NuGetGallery.Core.Facts.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@
115115
<Compile Include="Services\CloudBlobCoreFileStorageServiceIntegrationTests.cs" />
116116
<Compile Include="Services\CorePackageFileServiceFacts.cs" />
117117
<Compile Include="Services\CorePackageServiceFacts.cs" />
118+
<Compile Include="TestUtils\UseInvariantCultureAttribute.cs" />
118119
<Compile Include="TestUtils\CoreMockExtensions.cs" />
119120
<Compile Include="TestUtils\PackageServiceUtility.cs" />
120121
<Compile Include="TestUtils\TestPackage.cs" />

tests/NuGetGallery.Core.Facts/Packaging/PackageMetadataFacts.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Globalization;
67
using System.IO;
78
using System.IO.Compression;
89
using System.Linq;
10+
using System.Threading;
911
using NuGet.Packaging;
1012
using NuGet.Packaging.Core;
1113
using NuGet.Versioning;
14+
using NuGetGallery.TestUtils;
1215
using Xunit;
1316

1417
namespace NuGetGallery.Packaging
@@ -215,7 +218,8 @@ public void ThrowsWhenInvalidMinClientVersion(bool strict)
215218

216219
// Act & Assert
217220
var ex = Assert.Throws<ArgumentException>(() => PackageMetadata.FromNuspecReader(nuspec, strict));
218-
Assert.Equal("'bad' is not a valid version string.\r\nParameter name: value", ex.Message);
221+
Assert.StartsWith("'bad' is not a valid version string.", ex.Message);
222+
Assert.Equal("value", ex.ParamName);
219223
}
220224

221225
[Fact]
@@ -249,6 +253,7 @@ public void ThrowsPackagingExceptionWhenEmptyAndNonEmptyDuplicateMetadataElement
249253
}
250254

251255
[Fact]
256+
[UseInvariantCultureAttribute]
252257
public void ThrowsForEmptyAndNonEmptyDuplicatesWhenDuplicateMetadataElementsDetectedAndParsingIsNotStrict()
253258
{
254259
// Arrange
@@ -260,9 +265,10 @@ public void ThrowsForEmptyAndNonEmptyDuplicatesWhenDuplicateMetadataElementsDete
260265
var ex = Assert.Throws<ArgumentException>(() => PackageMetadata.FromNuspecReader(
261266
nuspec,
262267
strict: false));
268+
263269
Assert.Equal(
264-
"An item with the same key has already been added.",
265-
ex.Message);
270+
"An item with the same key has already been added.",
271+
ex.Message);
266272
}
267273

268274
[Fact]
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Globalization;
6+
using System.Reflection;
7+
using System.Threading;
8+
using Xunit.Sdk;
9+
10+
namespace NuGetGallery.TestUtils
11+
{
12+
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
13+
public class UseInvariantCultureAttribute : BeforeAfterTestAttribute
14+
{
15+
private CultureInfo originalUICulture;
16+
17+
public override void Before(MethodInfo methodUnderTest)
18+
{
19+
originalUICulture = Thread.CurrentThread.CurrentUICulture;
20+
Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;
21+
}
22+
23+
public override void After(MethodInfo methodUnderTest)
24+
{
25+
Thread.CurrentThread.CurrentUICulture = this.originalUICulture;
26+
}
27+
}
28+
}

tests/NuGetGallery.Facts/Authentication/AuthenticationServiceFacts.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -838,8 +838,8 @@ public async Task GivenPasswordCredential_ItThrowsArgumentException()
838838
var ex = await Assert.ThrowsAsync<ArgumentException>(async () => await _authenticationService.Authenticate(cred));
839839

840840
// Assert
841-
Assert.Equal(
842-
Strings.PasswordCredentialsCannotBeUsedHere + Environment.NewLine + "Parameter name: credential",
841+
Assert.StartsWith(
842+
Strings.PasswordCredentialsCannotBeUsedHere,
843843
ex.Message);
844844
Assert.Equal("credential", ex.ParamName);
845845
}

tests/NuGetGallery.Facts/Controllers/PackagesControllerFacts.cs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System.Linq.Expressions;
1212
using System.Net;
1313
using System.Text;
14+
using System.Threading;
1415
using System.Threading.Tasks;
1516
using System.Web;
1617
using System.Web.Caching;
@@ -41,7 +42,9 @@
4142
using NuGetGallery.Security;
4243
using NuGetGallery.Services;
4344
using NuGetGallery.Services.Helpers;
45+
using NuGetGallery.TestUtils;
4446
using Xunit;
47+
using Xunit.Sdk;
4548

4649
namespace NuGetGallery
4750
{
@@ -216,7 +219,7 @@ private static PackagesController CreateController(
216219
featureFlagService = new Mock<IFeatureFlagService>();
217220
featureFlagService.SetReturnsDefault<bool>(true);
218221
}
219-
222+
220223
renameService = renameService ?? new Mock<IPackageRenameService>();
221224
if (deprecationService == null)
222225
{
@@ -826,8 +829,8 @@ public async Task GivenAnAbsoluteLatestVersionItReturnsTheFirstLatestSemVer2()
826829
var deprecationService = new Mock<IPackageDeprecationService>();
827830
var controller = CreateController(
828831
GetConfigurationService(),
829-
packageService: packageService,
830-
indexingService: indexingService,
832+
packageService: packageService,
833+
indexingService: indexingService,
831834
deprecationService: deprecationService);
832835
controller.SetCurrentUser(TestUtility.FakeUser);
833836

@@ -905,8 +908,8 @@ public async Task GivenAnAbsoluteLatestVersionAndNoLatestSemVer2ItFiltersTheList
905908
var deprecationService = new Mock<IPackageDeprecationService>();
906909
var controller = CreateController(
907910
GetConfigurationService(),
908-
packageService: packageService,
909-
indexingService: indexingService,
911+
packageService: packageService,
912+
indexingService: indexingService,
910913
deprecationService: deprecationService);
911914
controller.SetCurrentUser(TestUtility.FakeUser);
912915

@@ -1066,8 +1069,8 @@ private async Task<ActionResult> GetResultWithReadMe(string readMeHtml, bool has
10661069
var fileService = new Mock<IPackageFileService>();
10671070
var controller = CreateController(
10681071
GetConfigurationService(),
1069-
packageService: packageService,
1070-
indexingService: indexingService,
1072+
packageService: packageService,
1073+
indexingService: indexingService,
10711074
packageFileService: fileService,
10721075
deprecationService: deprecationService);
10731076
controller.SetCurrentUser(TestUtility.FakeUser);
@@ -1126,9 +1129,9 @@ public async Task GetsValidationIssues()
11261129

11271130
var controller = CreateController(
11281131
GetConfigurationService(),
1129-
packageService: packageService,
1130-
indexingService: indexingService,
1131-
packageFileService: fileService,
1132+
packageService: packageService,
1133+
indexingService: indexingService,
1134+
packageFileService: fileService,
11321135
validationService: validationService,
11331136
deprecationService: deprecationService);
11341137
controller.SetCurrentUser(TestUtility.FakeUser);
@@ -1803,7 +1806,7 @@ public async Task WhenCacheIsOccupiedGetProperPackageDependent()
18031806

18041807
var result = await controller.DisplayPackage(id, version: null);
18051808
var model = ResultAssert.IsView<DisplayPackageViewModel>(result);
1806-
1809+
18071810
Assert.Same(pd, model.PackageDependents);
18081811
packageService
18091812
.Verify(iup => iup.GetPackageDependents(It.IsAny<string>()), Times.Never());
@@ -1933,7 +1936,7 @@ protected override void Dispose(bool disposing)
19331936

19341937
base.Dispose(disposing);
19351938
}
1936-
1939+
19371940
private class TestIssue : ValidationIssue
19381941
{
19391942
private readonly string _message;
@@ -3809,7 +3812,7 @@ public void UsesProperIconUrl(User currentUser, User owner)
38093812
}
38103813

38113814

3812-
private ActionResult GetDeleteSymbolsResult(User currentUser, User owner, out PackagesController controller, Mock<IIconUrlProvider> iconUrlProvider = null)
3815+
private ActionResult GetDeleteSymbolsResult(User currentUser, User owner, out PackagesController controller, Mock<IIconUrlProvider> iconUrlProvider = null)
38133816
{
38143817
_packageRegistration.Owners.Add(owner);
38153818

@@ -4327,7 +4330,7 @@ public async Task WhenPackageRegistrationIsLockedReturns403()
43274330
.Returns(package);
43284331

43294332
var controller = CreateController(
4330-
GetConfigurationService(),
4333+
GetConfigurationService(),
43314334
packageService: packageService,
43324335
packageUpdateService: packageUpdateService);
43334336

@@ -6027,6 +6030,7 @@ public async Task WillShowViewWithErrorsIfEnsureValidThrowsExceptionMessage(Type
60276030
}
60286031

60296032
[Fact]
6033+
[UseInvariantCultureAttribute]
60306034
public async Task WillRejectBrokenZipFiles()
60316035
{
60326036
// Arrange
@@ -6041,7 +6045,7 @@ public async Task WillRejectBrokenZipFiles()
60416045
controller.SetCurrentUser(TestUtility.FakeUser);
60426046

60436047
var result = await controller.UploadPackage(fakeUploadedFile.Object) as JsonResult;
6044-
6048+
60456049
Assert.NotNull(result);
60466050
Assert.Equal("Central Directory corrupt.", (result.Data as JsonValidationMessage[])[0].PlainTextMessage);
60476051
}
@@ -6075,6 +6079,7 @@ public async Task WillShowViewWithErrorsIfPackageIdIsInvalid(string packageId)
60756079
[Theory]
60766080
[InlineData("Contains#Invalid$Characters!@#$%^&*")]
60776081
[InlineData("Contains#Invalid$Characters!@#$%^&*EndsOnValidCharacter")]
6082+
[UseInvariantCultureAttribute]
60786083
public async Task WillShowViewWithErrorsIfPackageIdIsBreaksParsing(string packageId)
60796084
{
60806085
// Arrange
@@ -9000,7 +9005,7 @@ public async Task ReturnsProperResponseModelWhenSucceeds()
90009005

90019006
readmeService
90029007
.Setup(rs => rs.GetReadMeHtmlAsync(request, It.IsAny<Encoding>()))
9003-
.ReturnsAsync(new RenderedReadMeResult { Content = html } );
9008+
.ReturnsAsync(new RenderedReadMeResult { Content = html });
90049009

90059010
var result = await controller.PreviewReadMe(request);
90069011

tests/NuGetGallery.Facts/TestUtils/ContractAssert.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,17 @@ public static void ThrowsArgException(Action act, string paramName, string messa
3838
{
3939
var argEx = Assert.Throws<ArgumentException>(() => act());
4040
Assert.Equal(paramName, argEx.ParamName);
41-
Assert.Equal(
42-
message + Environment.NewLine + $"Parameter name: {paramName}",
41+
Assert.StartsWith(
42+
message,
4343
argEx.Message);
4444
}
4545

4646
public static async void ThrowsArgExceptionAsync(Func<Task> act, string paramName, string message = "")
4747
{
4848
var argEx = await Assert.ThrowsAsync<ArgumentException>(async () => await act());
4949
Assert.Equal(paramName, argEx.ParamName);
50-
Assert.Equal(
51-
message + Environment.NewLine + $"Parameter name: {paramName}",
50+
Assert.StartsWith(
51+
message,
5252
argEx.Message);
5353
}
5454
}

0 commit comments

Comments
 (0)