From 0734af6eb4fb3103ef08502423c6659ecfedf9db Mon Sep 17 00:00:00 2001 From: Tomas Matousek Date: Sun, 18 May 2025 08:05:58 -0700 Subject: [PATCH] Fix CA2025: Unawaited tasks that use 'IDisposable' instances ... --- .../Commands/NuGetExeRestoreCommand.cs | 31 +++++++++---------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/test/Microsoft.NET.TestFramework/Commands/NuGetExeRestoreCommand.cs b/test/Microsoft.NET.TestFramework/Commands/NuGetExeRestoreCommand.cs index a31545b142ab..165953dedc4f 100644 --- a/test/Microsoft.NET.TestFramework/Commands/NuGetExeRestoreCommand.cs +++ b/test/Microsoft.NET.TestFramework/Commands/NuGetExeRestoreCommand.cs @@ -24,17 +24,6 @@ public NuGetExeRestoreCommand(ITestOutputHelper log, string projectRootPath, str protected override SdkCommandSpec CreateCommand(IEnumerable args) { - var newArgs = new List(); - - newArgs.Add("restore"); - - newArgs.Add(FullPathProjectFile); - - newArgs.Add("-PackagesDirectory"); - newArgs.Add(PackagesDirectory ?? TestContext.Current.NuGetCachePath ?? string.Empty); - - newArgs.AddRange(args); - if (string.IsNullOrEmpty(TestContext.Current.NuGetExePath)) { throw new InvalidOperationException("Path to nuget.exe not set"); @@ -58,18 +47,28 @@ protected override SdkCommandSpec CreateCommand(IEnumerable args) "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe" : $"https://dist.nuget.org/win-x86-commandline/v{NuGetExeVersion}/nuget.exe"; - using (var client = new System.Net.Http.HttpClient()) - using (var response = client.GetAsync(url).ConfigureAwait(false).GetAwaiter().GetResult()) - using (var fs = new FileStream(nugetExePath, FileMode.CreateNew)) + DownloadNuGetAsync().ConfigureAwait(false).GetAwaiter().GetResult(); + + async Task DownloadNuGetAsync() { - response.Content.CopyToAsync(fs).ConfigureAwait(false).GetAwaiter().GetResult(); + using var client = new System.Net.Http.HttpClient(); + using var response = await client.GetAsync(url).ConfigureAwait(false); + using var fs = new FileStream(nugetExePath, FileMode.CreateNew); + await response.Content.CopyToAsync(fs).ConfigureAwait(false); } } var ret = new SdkCommandSpec() { FileName = nugetExePath, - Arguments = newArgs + Arguments = + [ + "restore", + FullPathProjectFile, + "-PackagesDirectory", + PackagesDirectory ?? TestContext.Current.NuGetCachePath ?? string.Empty, + .. args + ] }; TestContext.Current.AddTestEnvironmentVariables(ret.Environment);