Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Enable NuGet Audit and Fix Errors #12120

Merged
merged 5 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
or $(MSBuildProjectName.EndsWith('.Analyzers.VisualBasic'))
or $(MSBuildProjectName.EndsWith('.Analyzers.CSharp')))"
>true</IsAnalyzerProject>

<!-- Only upgrade NuGetAudit warnings to errors for official builds. -->
<WarningsNotAsErrors Condition="'$(OfficialBuild)' != 'true'">$(WarningsNotAsErrors);NU1901;NU1902;NU1903;NU1904</WarningsNotAsErrors>
</PropertyGroup>

<!--
Expand Down
4 changes: 4 additions & 0 deletions NuGet.config
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
<!-- CsWin32 dailies -->
<add key="winsdk" value="https://pkgs.dev.azure.com/azure-public/winsdk/_packaging/CI/nuget/v3/index.json" />
</packageSources>
<auditSources>
<clear />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
</auditSources>
<disabledPackageSources>
<clear />
</disabledPackageSources>
Expand Down
6 changes: 4 additions & 2 deletions eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
<MicrosoftVisualStudioThreadingVersion>17.0.15-alpha</MicrosoftVisualStudioThreadingVersion>
<!-- This is needed for Verify.Xunit to pull correct version of System.Speech -->
<MicrosoftWindowsCompatibilityVersion>7.0.0</MicrosoftWindowsCompatibilityVersion>
<MoqPackageVersion>4.10.0</MoqPackageVersion>
<MoqPackageVersion>4.20.70</MoqPackageVersion>
<FluentAssertionsVersion>6.11.0</FluentAssertionsVersion>
<SystemComponentModelTypeConverterTestDataVersion>8.0.0-beta.23107.1</SystemComponentModelTypeConverterTestDataVersion>
<SystemDrawingCommonTestDataVersion>8.0.0-beta.23107.1</SystemDrawingCommonTestDataVersion>
Expand All @@ -103,13 +103,15 @@
<MicrosoftCodeAnalysisPublicApiAnalyzersVersion>$(MicrosoftCodeAnalysisAnalyzersVersion)</MicrosoftCodeAnalysisPublicApiAnalyzersVersion>
<MicrosoftCodeAnalysisNetAnalyzersVersion>8.0.0-preview.23327.3</MicrosoftCodeAnalysisNetAnalyzersVersion>
<StyleCopAnalyzersVersion>1.2.0-beta.556</StyleCopAnalyzersVersion>
<NugetPackagingVersion>6.3.4</NugetPackagingVersion>
<NugetPackagingVersion>6.11.0</NugetPackagingVersion>
</PropertyGroup>
<!-- Additional unchanging dependencies -->
<PropertyGroup>
<MicrosoftTargetingPackNETFrameworkv472PackageVersion>1.0.0</MicrosoftTargetingPackNETFrameworkv472PackageVersion>
<MicrosoftWindowsDesktopAppRefv30PackageVersion>3.0.0</MicrosoftWindowsDesktopAppRefv30PackageVersion>
<NewtonsoftJsonVersion>13.0.1</NewtonsoftJsonVersion>
<VsWherePackageVersion>2.6.7</VsWherePackageVersion>
<!-- Pin transitive dependency to avoid vulnerable 8.0.0 version. -->
<SystemFormatsAsn1PackageVersion>8.0.1</SystemFormatsAsn1PackageVersion>
Copy link
Member Author

Choose a reason for hiding this comment

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

Created issue to track unpinning this #12121

</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing.XUnit" Version="$(MicrosoftCodeAnalysisPackagesVersion)" />
<PackageReference Include="Verify.Xunit" Version="$(VerifyXunitVersion)" />
<PackageReference Include="NuGet.Packaging" Version="$(NugetPackagingVersion)" />
<PackageReference Include="System.Formats.Asn1" Version="$(SystemFormatsAsn1PackageVersion)" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<PackageReference Include="Microsoft.CodeAnalysis.VisualBasic.SourceGenerators.Testing.XUnit" Version="$(MicrosoftCodeAnalysisPackagesVersion)" />
<PackageReference Include="Verify.Xunit" Version="$(VerifyXunitVersion)" />
<PackageReference Include="NuGet.Packaging" Version="$(NugetPackagingVersion)" />
<PackageReference Include="System.Formats.Asn1" Version="$(SystemFormatsAsn1PackageVersion)" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<PackageReference Include="Microsoft.CodeAnalysis.VisualBasic.CodeFix.Testing.XUnit" Version="$(MicrosoftCodeAnalysisPackagesVersion)" />
<PackageReference Include="Microsoft.CodeAnalysis.VisualBasic.CodeRefactoring.Testing.XUnit" Version="$(MicrosoftCodeAnalysisPackagesVersion)" />
<PackageReference Include="NuGet.Packaging" Version="$(NugetPackagingVersion)" />
<PackageReference Include="System.Formats.Asn1" Version="$(SystemFormatsAsn1PackageVersion)" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -602,9 +602,6 @@ public void CollectionEditor_EditValue_ValidProviderValidHostWithTransactionOK_C
.Returns(mockEditorService.Object);

Mock<DesignerTransaction> mockTransaction = new(MockBehavior.Strict);
mockTransaction
.Protected()
.Setup("Dispose", It.IsAny<bool>());
mockTransaction
.Protected()
.Setup("OnCommit")
Expand Down Expand Up @@ -652,9 +649,6 @@ public void CollectionEditor_EditValue_ValidProviderValidHostWithTransactionNotO
.Returns(mockEditorService.Object);

Mock<DesignerTransaction> mockTransaction = new(MockBehavior.Strict);
mockTransaction
.Protected()
.Setup("Dispose", It.IsAny<bool>());
mockTransaction
.Protected()
.Setup("OnCancel")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public void EditValue_ReturnsOriginalValue_WhenContextInstanceIsNull()
Mock<ITypeDescriptorContext> mockContext = new(MockBehavior.Strict);
mockContext
.Setup(c => c.Instance)
.Returns(null);
.Returns((object?)null);

Mock<IWindowsFormsEditorService> mockEditorService = new(MockBehavior.Strict);
mockEditorService
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,9 @@ public void DataGridViewCellAccessibleObject_Select_NoOwner_ThrowsInvalidOperati
public void DataGridViewCellAccessibleObject_Select_HasSelectionFlagsWithoutValidDataGridView_DoesNothing()
{
Mock<DataGridViewCell> mockCell = new(MockBehavior.Strict);
mockCell
.SetupSet(s => s.State = DataGridViewElementStates.None)
.Verifiable();
mockCell
.SetupSet(s => s.State = DataGridViewElementStates.Visible)
.Verifiable();
Expand Down Expand Up @@ -495,6 +498,9 @@ public void DataGridViewCellAccessibleObject_Select_TakeFocus()
Assert.True(dataGridView.IsHandleCreated);

Mock<DataGridViewCell> mockCell = new(MockBehavior.Strict);
mockCell
.SetupSet(s => s.State = DataGridViewElementStates.None)
.Verifiable();
mockCell
.SetupSet(s => s.State = DataGridViewElementStates.Visible)
.Verifiable();
Expand Down Expand Up @@ -525,6 +531,9 @@ public void DataGridViewCellAccessibleObject_Select_TakeSelection()
Assert.True(dataGridView.IsHandleCreated);

Mock<DataGridViewCell> mockCell = new(MockBehavior.Strict);
mockCell
.SetupSet(s => s.State = DataGridViewElementStates.None)
.Verifiable();
mockCell
.SetupSet(s => s.State = DataGridViewElementStates.Visible)
.Verifiable();
Expand Down Expand Up @@ -561,6 +570,9 @@ public void DataGridViewCellAccessibleObject_Select_AddSelection()
Assert.True(dataGridView.IsHandleCreated);

Mock<DataGridViewCell> mockCell = new(MockBehavior.Strict);
mockCell
.SetupSet(s => s.State = DataGridViewElementStates.None)
.Verifiable();
mockCell
.SetupSet(s => s.State = DataGridViewElementStates.Visible)
.Verifiable();
Expand Down Expand Up @@ -595,6 +607,9 @@ public void DataGridViewCellAccessibleObject_Select_RemoveSelection()
mockCell
.SetupSet(s => s.State = DataGridViewElementStates.Visible)
.Verifiable();
mockCell
.SetupSet(s => s.State = DataGridViewElementStates.None)
.Verifiable();
mockCell
.SetupSet(s => s.Selected = It.IsAny<bool>())
.Verifiable();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Moq;
using Moq.Protected;

namespace System.Windows.Forms.Tests;

public class ApplicationContextTests
Expand Down Expand Up @@ -210,20 +207,39 @@ public void Dispose_InvokeWithoutForm_Success()
Assert.Null(context.MainForm);
}

private class TestApplicationContext : ApplicationContext
{
public TestApplicationContext() : base()
{
}

public int DisposeCallCount { get; private set; }

public int ExitThreadCoreCount { get; private set; }

protected override void Dispose(bool disposing)
{
DisposeCallCount++;
base.Dispose(disposing);
}

protected override void ExitThreadCore()
{
ExitThreadCoreCount++;
base.ExitThreadCore();
}
}

[WinFormsFact]
public void Dispose_Invoke_CallsDisposeDisposing()
{
Mock<ApplicationContext> mockContext = new(MockBehavior.Strict);
mockContext
Copy link
Member Author

@lonitra lonitra Sep 11, 2024

Choose a reason for hiding this comment

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

With the upgrade to latest moq version, I get errors with moq complaining that there is no protected Dispose(bool) method even though there is. I have opted to avoiding mocking a test object and creating a real test object instead or just removing the Dispose setup if it doesn't look to be necessary.

.Protected()
.Setup("Dispose", true)
.Verifiable();
mockContext.Object.Dispose();
mockContext.Protected().Verify("Dispose", Times.Once(), true);
TestApplicationContext context = new();
context.Dispose();
context.DisposeCallCount.Should().Be(1);

// Call again.
mockContext.Object.Dispose();
mockContext.Protected().Verify("Dispose", Times.Exactly(2), true);
context.Dispose();
context.DisposeCallCount.Should().Be(2);
}

[WinFormsFact]
Expand Down Expand Up @@ -296,20 +312,13 @@ public void ExitThread_InvokeWithThreadExit_CallsHandler()
[WinFormsFact]
public void ExitThread_Invoke_CallsExitThreadCore()
{
Mock<ApplicationContext> mockContext = new(MockBehavior.Strict);
mockContext
.Protected()
.Setup("ExitThreadCore")
.Verifiable();
mockContext
.Protected()
.Setup("Dispose", false);
mockContext.Object.ExitThread();
mockContext.Protected().Verify("ExitThreadCore", Times.Once());
TestApplicationContext context = new();
context.ExitThread();
context.ExitThreadCoreCount.Should().Be(1);

// Call again.
mockContext.Object.ExitThread();
mockContext.Protected().Verify("ExitThreadCore", Times.Exactly(2));
context.ExitThread();
context.ExitThreadCoreCount.Should().Be(2);
}

[WinFormsFact]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.ComponentModel;
using System.Reflection;
using Castle.Core.Internal;

namespace System.Windows.Forms.Tests;

Expand Down Expand Up @@ -38,7 +37,7 @@ public void AxSystemMonitor_WhenInitialized_ExpectsProperties()
foreach(PropertyDescriptor prop in properties)
{
string assemblyFromTestingControl = prop.ComponentType.Assembly.GetName().Name;
if (!assemblyFromTestingControl.IsNullOrEmpty()
if (!string.IsNullOrEmpty(assemblyFromTestingControl)
&& assemblyFromTestingControl == assemblyNameFromType)
{
testingControlProps.Add(prop.Name);
Expand All @@ -49,7 +48,7 @@ public void AxSystemMonitor_WhenInitialized_ExpectsProperties()
foreach(EventDescriptor singleEvent in events)
{
string assemblyFromTestingControl = singleEvent.ComponentType.Assembly.GetName().Name;
if (!assemblyFromTestingControl.IsNullOrEmpty()
if (!string.IsNullOrEmpty(assemblyFromTestingControl)
&& assemblyFromTestingControl == assemblyNameFromType)
{
testingControlEvents.Add(singleEvent.Name);
Expand Down