Skip to content

Commit 54bbdb7

Browse files
authored
Update to latest Xunit and Moq, fix test bugs, stop suppressing analyzers (#10077)
* Fix first wave of warnings (not suppressed by .editorconfig) * Move to latest test dependencies (xunit and Moq) * Fix Moq build errors * Only build MVC view on Release * Fix warnings in NuGetGallery.Core * Fix non-Xunit warnings (except App Insights connection string) * Fix tests that weren't running * Move to matching NetAnalyzers * Delete .editorconfig suppression now that we're clean * Move to latest working Moq, 4.11.0 breaks controller tests so fixes were needed
1 parent 63b2ed2 commit 54bbdb7

122 files changed

Lines changed: 706 additions & 530 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.editorconfig

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +0,0 @@
1-
[*.cs]
2-
3-
# Suppress performance, usage, naming, maintainability, style diagnostics.
4-
dotnet_analyzer_diagnostic.category-design.severity = none
5-
dotnet_analyzer_diagnostic.category-globalization.severity = none
6-
dotnet_analyzer_diagnostic.category-naming.severity = none
7-
dotnet_analyzer_diagnostic.category-performance.severity = none
8-
dotnet_analyzer_diagnostic.category-reliability.severity = none
9-
dotnet_analyzer_diagnostic.category-security.severity = warning
10-
dotnet_analyzer_diagnostic.category-usage.severity = none
11-
dotnet_analyzer_diagnostic.category-Maintainability.severity = none
12-
dotnet_analyzer_diagnostic.category-Style.severity = none
13-
14-
# Addtional suppress to be compatible with different version among VS2019
15-
16-
# CA1001: Types that own disposable fields should be disposable
17-
dotnet_diagnostic.CA1001.severity = none
18-
19-
# CA1063: Implement IDisposable Correctly
20-
dotnet_diagnostic.CA1063.severity = none
21-
22-
# CA1033: Interface methods should be callable by child types
23-
dotnet_diagnostic.CA1033.severity = none
24-
25-
# CA2229: Implement serialization constructors
26-
dotnet_diagnostic.CA2229.severity = none
27-
28-
# CA2200: Rethrow to preserve stack details.
29-
dotnet_diagnostic.CA2200.severity = none
30-
31-
# CA1065: Do not raise exceptions in unexpected locations
32-
dotnet_diagnostic.CA1065.severity = none
33-
34-
# CA2214: Do not call overridable methods in constructors
35-
dotnet_diagnostic.CA2214.severity = none
36-
37-
# Suppressions we want to reset
38-
dotnet_diagnostic.CA2017.severity = warning
39-
dotnet_diagnostic.IDE0005.severity = default # unnecessary usings

.nuget/packages.config

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@
33
<package id="MicroBuild.Core" version="0.3.0" />
44
<package id="Microsoft.CodeAnalysis.BinSkim" version="1.3.6" />
55
<package id="xunit.runner.console" version="2.1.0" />
6-
<package id="xunit.runner.visualstudio" version="2.1.0" />
76
</packages>

Directory.Build.props

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
<Project>
22
<PropertyGroup>
3+
<LangVersion>9.0</LangVersion>
4+
35
<NuGetClientPackageVersion>6.9.1</NuGetClientPackageVersion>
46
<ServerCommonPackageVersion>2.120.0</ServerCommonPackageVersion>
57
<NuGetJobsPackageVersion>4.3.0-agr-gal-stsdk-9768098</NuGetJobsPackageVersion>
68
</PropertyGroup>
79
<ItemGroup>
810
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers">
9-
<Version>7.0.3</Version>
11+
<Version>8.0.0</Version>
1012
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
1113
<PrivateAssets>all</PrivateAssets>
1214
</PackageReference>

build.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ Invoke-BuildStep 'Removing .editorconfig file in NuGetGallery' { Remove-Editorco
101101

102102
Invoke-BuildStep 'Building solution' {
103103
$SolutionPath = Join-Path $PSScriptRoot "NuGetGallery.sln"
104-
Build-Solution -Configuration $Configuration -BuildNumber $BuildNumber -SolutionPath $SolutionPath -SkipRestore:$SkipRestore -MSBuildProperties "/p:MvcBuildViews=true" `
104+
$MvcBuildViews = $Configuration -eq "release"
105+
Build-Solution -Configuration $Configuration -BuildNumber $BuildNumber -SolutionPath $SolutionPath -SkipRestore:$SkipRestore -MSBuildProperties "/p:MvcBuildViews=$MvcBuildViews" `
105106
} `
106107
-ev +BuildErrors
107108

src/GitHubVulnerabilities2Db/Gallery/GalleryDbVulnerabilityWriter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ public async Task WriteVulnerabilityAsync(PackageVulnerability vulnerability, bo
5656
}
5757
catch (Exception ex)
5858
{
59-
_logger.LogError("Failed to write vulnerability {GitHubAdvisoryUrl}", vulnerability.AdvisoryUrl);
60-
throw ex;
59+
_logger.LogError(ex, "Failed to write vulnerability {GitHubAdvisoryUrl}", vulnerability.AdvisoryUrl);
60+
throw;
6161
}
6262
}
6363
}

src/GitHubVulnerabilities2v3/Extensions/BlobStorageVulnerabilityWriter.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ public BlobStorageVulnerabilityWriter(
5353
_telemetryService = telemetryService ?? throw new ArgumentNullException(nameof(telemetryService));
5454

5555
_storage = _storageFactory.Create();
56-
((AzureStorage)_storage).CompressContent = _configuration.GzipFileContent;
56+
if (_storage is AzureStorage azureStorage)
57+
{
58+
azureStorage.CompressContent = _configuration.GzipFileContent;
59+
}
5760

5861
_firstVulnWrittenTimestamp = DateTimeOffset.MinValue;
5962
_vulnerabilityDict = new Dictionary<string, List<Advisory>>(StringComparer.OrdinalIgnoreCase);

src/GitHubVulnerabilities2v3/GitHubVulnerabilities2V3.csproj

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,6 @@
9393
<Name>NuGetGallery.Services</Name>
9494
</ProjectReference>
9595
</ItemGroup>
96-
<ItemGroup>
97-
<Folder Include="Properties\" />
98-
</ItemGroup>
9996
<PropertyGroup>
10097
<SignPath>..\..\build</SignPath>
10198
<SignPath Condition="'$(BUILD_SOURCESDIRECTORY)' != ''">$(BUILD_SOURCESDIRECTORY)\build</SignPath>

src/NuGetGallery.Core/Entities/EntitiesContext.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Data.Entity.Infrastructure;
99
using System.Data.Entity.Infrastructure.Annotations;
1010
using System.Linq;
11+
using System.Threading;
1112
using System.Threading.Tasks;
1213
using NuGet.Services.Entities;
1314

@@ -97,14 +98,30 @@ DbSet<T> IReadOnlyEntitiesContext.Set<T>()
9798
return base.Set<T>();
9899
}
99100

101+
public override int SaveChanges()
102+
{
103+
ThrowIfReadOnly();
104+
return base.SaveChanges();
105+
}
106+
107+
public override Task<int> SaveChangesAsync(CancellationToken cancellationToken)
108+
{
109+
ThrowIfReadOnly();
110+
return base.SaveChangesAsync(cancellationToken);
111+
}
112+
100113
public override async Task<int> SaveChangesAsync()
114+
{
115+
ThrowIfReadOnly();
116+
return await base.SaveChangesAsync();
117+
}
118+
119+
private void ThrowIfReadOnly()
101120
{
102121
if (ReadOnly)
103122
{
104123
throw new ReadOnlyModeException("Save changes unavailable: the gallery is currently in read only mode, with limited service. Please try again later.");
105124
}
106-
107-
return await base.SaveChangesAsync();
108125
}
109126

110127
public void DeleteOnCommit<T>(T entity) where T : class

src/NuGetGallery/Controllers/JsonApiController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public async Task<JsonResult> AddPackageOwner(AddPackageOwnerViewModel addOwnerD
123123
string username = addOwnerData.Username;
124124
string message = addOwnerData.Message;
125125

126-
if (Regex.IsMatch(username, GalleryConstants.EmailValidationRegex, RegexOptions.None, GalleryConstants.EmailValidationRegexTimeout))
126+
if (username is not null && Regex.IsMatch(username, GalleryConstants.EmailValidationRegex, RegexOptions.None, GalleryConstants.EmailValidationRegexTimeout))
127127
{
128128
return Json(new { success = false, message = Strings.AddOwner_NameIsEmail }, JsonRequestBehavior.AllowGet);
129129
}

test.ps1

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
[CmdletBinding(DefaultParameterSetName='RegularBuild')]
1+
[CmdletBinding(DefaultParameterSetName = 'RegularBuild')]
22
param (
33
[ValidateSet("debug", "release")]
44
[string]$Configuration = 'debug',
55
[int]$BuildNumber
66
)
77

8-
# For TeamCity - If any issue occurs, this script fail the build. - By default, TeamCity returns an exit code of 0 for all powershell scripts, even if they fail
98
trap {
109
Write-Host "BUILD FAILED: $_" -ForegroundColor Red
1110
Write-Host "ERROR DETAILS:" -ForegroundColor Red
@@ -16,28 +15,33 @@ trap {
1615

1716
. "$PSScriptRoot\build\common.ps1"
1817

19-
Function Run-Tests {
18+
Function Invoke-Tests {
2019
[CmdletBinding()]
2120
param()
2221

2322
Trace-Log 'Running tests'
2423

2524
$xUnitExe = (Join-Path $PSScriptRoot "packages\xunit.runner.console\tools\xunit.console.exe")
2625

27-
$TestAssemblies = `
28-
"tests\AccountDeleter.Facts\bin\$Configuration\AccountDeleter.Facts.dll", `
29-
"tests\GitHubVulnerabilities2Db.Facts\bin\$Configuration\GitHubVulnerabilities2Db.Facts.dll", `
26+
$GalleryTestAssemblies = `
27+
"tests\AccountDeleter.Facts\bin\$Configuration\net472\AccountDeleter.Facts.dll", `
28+
"tests\GitHubVulnerabilities2Db.Facts\bin\$Configuration\net472\GitHubVulnerabilities2Db.Facts.dll", `
3029
"tests\GitHubVulnerabilities2v3.Facts\bin\$Configuration\GitHubVulnerabilities2v3.Facts.dll", `
31-
"tests\NuGet.Services.DatabaseMigration.Facts\bin\$Configuration\NuGet.Services.DatabaseMigration.Facts.dll", `
32-
"tests\NuGet.Services.Entities.Tests\bin\$Configuration\NuGet.Services.Entities.Tests.dll", `
30+
"tests\NuGet.Services.DatabaseMigration.Facts\bin\$Configuration\net472\NuGet.Services.DatabaseMigration.Facts.dll", `
31+
"tests\NuGet.Services.Entities.Tests\bin\$Configuration\net472\NuGet.Services.Entities.Tests.dll", `
3332
"tests\NuGetGallery.Core.Facts\bin\$Configuration\NuGetGallery.Core.Facts.dll", `
3433
"tests\NuGetGallery.Facts\bin\$Configuration\NuGetGallery.Facts.dll", `
3534
"tests\VerifyMicrosoftPackage.Facts\bin\$Configuration\NuGet.VerifyMicrosoftPackage.Facts.dll"
3635

3736
$TestCount = 0
38-
39-
foreach ($Test in $TestAssemblies) {
40-
& $xUnitExe (Join-Path $PSScriptRoot $Test) -xml "Results.$TestCount.xml"
37+
38+
$GalleryTestAssemblies | ForEach-Object {
39+
$TestResultFile = Join-Path $PSScriptRoot "Results.$TestCount.xml"
40+
& $xUnitExe (Join-Path $PSScriptRoot $_) -xml $TestResultFile
41+
if (-not (Test-Path $TestResultFile)) {
42+
Write-Error "The test run failed to produce a result file";
43+
exit 1;
44+
}
4145
$TestCount++
4246
}
4347

@@ -56,7 +60,7 @@ Trace-Log "Build #$BuildNumber started at $startTime"
5660

5761
$BuildErrors = @()
5862

59-
Invoke-BuildStep 'Running tests' { Run-Tests } `
63+
Invoke-BuildStep 'Running tests' { Invoke-Tests } `
6064
-ev +BuildErrors
6165

6266
Trace-Log ('-' * 60)
@@ -69,7 +73,7 @@ Trace-Log "Time elapsed $(Format-ElapsedTime ($endTime - $startTime))"
6973
Trace-Log ('=' * 60)
7074

7175
if ($BuildErrors) {
72-
$ErrorLines = $BuildErrors | %{ ">>> $($_.Exception.Message)" }
76+
$ErrorLines = $BuildErrors | ForEach-Object { ">>> $($_.Exception.Message)" }
7377
Error-Log "Tests completed with $($BuildErrors.Count) error(s):`r`n$($ErrorLines -join "`r`n")" -Fatal
7478
}
7579

0 commit comments

Comments
 (0)