Skip to content

Commit

Permalink
Fix failing test.
Browse files Browse the repository at this point in the history
  • Loading branch information
Corniel committed Jul 31, 2024
1 parent 9e37b30 commit 457aa99
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
40 changes: 30 additions & 10 deletions src/CodeAnalysis.TestTools/References/NuGetLatestVersions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace CodeAnalysis.TestTools.References;

/// <summary>Contains NuGet packages and their latest versions.</summary>
[Serializable]
public sealed class NuGetLatestVersions : Dictionary<string, NuGetLatestVersionCheck>
{
/// <summary>Initializes a new instance of the <see cref="NuGetLatestVersions"/> class.</summary>
Expand All @@ -12,8 +11,8 @@ public NuGetLatestVersions() { }
/// <summary>Saves the latests versions to a file.</summary>
public Task SaveAsync(FileInfo file)
{
using var stream = new FileStream(file.FullName, FileMode.Create, FileAccess.Write);
return JsonSerializer.SerializeAsync(stream, this, new JsonSerializerOptions { WriteIndented = true });
using var stream = new FileStream(file.FullName, SaveOptions);
return JsonSerializer.SerializeAsync(stream, this, JsonOptions);
}

/// <summary>Saves the latests versions to a stream.</summary>
Expand All @@ -27,14 +26,35 @@ public static async Task<NuGetLatestVersions> LoadAsync(FileInfo file)
Guard.NotNull(file);
if (file.Exists)
{
using var stream = new FileStream(file.FullName, FileMode.Open, FileAccess.Read);
return await LoadAsync(stream);
using var stream = new FileStream(file.FullName, LoadOptions);
try
{
return (await JsonSerializer.DeserializeAsync<NuGetLatestVersions>(stream)) ?? [];
}
catch (JsonException)
{
return [];
}
}
else return new();
else return [];
}

/// <summary>Loads the latests versions from a stream.</summary>
[Pure]
public static Task<NuGetLatestVersions> LoadAsync(Stream stream)
=> JsonSerializer.DeserializeAsync<NuGetLatestVersions>(stream).AsTask()!;
private static readonly FileStreamOptions LoadOptions = new()
{
Access = FileAccess.Read,
Mode = FileMode.Open,
Options = FileOptions.Asynchronous,
};

private static readonly FileStreamOptions SaveOptions = new()
{
Access = FileAccess.Write,
Mode = FileMode.Create,
Options = FileOptions.Asynchronous,
};

private static readonly JsonSerializerOptions JsonOptions = new()
{
WriteIndented = true,
};
}
1 change: 0 additions & 1 deletion src/CodeAnalysis.TestTools/References/NuGetRepository.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using NuGet.Common;
using NuGet.Configuration;
using NuGet.Packaging;
using NuGet.Protocol;
using NuGet.Protocol.Core.Types;
Expand Down

0 comments on commit 457aa99

Please sign in to comment.