Removed nesting levels through block-scoped `using` statement (#10025)

Co-authored-by: John Doe <john@doe>
Co-authored-by: Lehonti Ramos <lehonti@ramos>
This commit is contained in:
Lehonti Ramos 2023-09-11 12:12:40 +02:00 committed by GitHub
parent c79c59a32b
commit bc959270b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 83 additions and 102 deletions

View File

@ -1,4 +1,4 @@
using System; using System;
using System.IO; using System.IO;
using System.Xml; using System.Xml;
using System.Xml.Serialization; using System.Xml.Serialization;
@ -59,22 +59,18 @@ public class MigrateMusicBrainzTimeout : IMigrationRoutine
private OldMusicBrainzConfiguration? ReadOld(string path) private OldMusicBrainzConfiguration? ReadOld(string path)
{ {
using (var xmlReader = XmlReader.Create(path)) using var xmlReader = XmlReader.Create(path);
{
var serverConfigSerializer = new XmlSerializer(typeof(OldMusicBrainzConfiguration), new XmlRootAttribute("PluginConfiguration")); var serverConfigSerializer = new XmlSerializer(typeof(OldMusicBrainzConfiguration), new XmlRootAttribute("PluginConfiguration"));
return serverConfigSerializer.Deserialize(xmlReader) as OldMusicBrainzConfiguration; return serverConfigSerializer.Deserialize(xmlReader) as OldMusicBrainzConfiguration;
} }
}
private void WriteNew(string path, PluginConfiguration newPluginConfiguration) private void WriteNew(string path, PluginConfiguration newPluginConfiguration)
{ {
var pluginConfigurationSerializer = new XmlSerializer(typeof(PluginConfiguration), new XmlRootAttribute("PluginConfiguration")); var pluginConfigurationSerializer = new XmlSerializer(typeof(PluginConfiguration), new XmlRootAttribute("PluginConfiguration"));
var xmlWriterSettings = new XmlWriterSettings { Indent = true }; var xmlWriterSettings = new XmlWriterSettings { Indent = true };
using (var xmlWriter = XmlWriter.Create(path, xmlWriterSettings)) using var xmlWriter = XmlWriter.Create(path, xmlWriterSettings);
{
pluginConfigurationSerializer.Serialize(xmlWriter, newPluginConfiguration); pluginConfigurationSerializer.Serialize(xmlWriter, newPluginConfiguration);
} }
}
#pragma warning disable #pragma warning disable
public sealed class OldMusicBrainzConfiguration public sealed class OldMusicBrainzConfiguration

View File

@ -43,11 +43,9 @@ public class MigrateNetworkConfiguration : IMigrationRoutine
try try
{ {
using (var xmlReader = XmlReader.Create(path)) using var xmlReader = XmlReader.Create(path);
{
oldNetworkConfiguration = (OldNetworkConfiguration?)oldNetworkConfigSerializer.Deserialize(xmlReader); oldNetworkConfiguration = (OldNetworkConfiguration?)oldNetworkConfigSerializer.Deserialize(xmlReader);
} }
}
catch (InvalidOperationException ex) catch (InvalidOperationException ex)
{ {
_logger.LogError(ex, "Migrate NetworkConfiguration deserialize Invalid Operation error"); _logger.LogError(ex, "Migrate NetworkConfiguration deserialize Invalid Operation error");
@ -97,12 +95,10 @@ public class MigrateNetworkConfiguration : IMigrationRoutine
var networkConfigSerializer = new XmlSerializer(typeof(NetworkConfiguration)); var networkConfigSerializer = new XmlSerializer(typeof(NetworkConfiguration));
var xmlWriterSettings = new XmlWriterSettings { Indent = true }; var xmlWriterSettings = new XmlWriterSettings { Indent = true };
using (var xmlWriter = XmlWriter.Create(path, xmlWriterSettings)) using var xmlWriter = XmlWriter.Create(path, xmlWriterSettings);
{
networkConfigSerializer.Serialize(xmlWriter, networkConfiguration); networkConfigSerializer.Serialize(xmlWriter, networkConfiguration);
} }
} }
}
#pragma warning disable #pragma warning disable
public sealed class OldNetworkConfiguration public sealed class OldNetworkConfiguration

View File

@ -489,11 +489,9 @@ public class SkiaEncoder : IImageEncoder
Directory.CreateDirectory(directory); Directory.CreateDirectory(directory);
using (var outputStream = new SKFileWStream(outputPath)) using (var outputStream = new SKFileWStream(outputPath))
{ {
using (var pixmap = new SKPixmap(new SKImageInfo(width, height), saveBitmap.GetPixels())) using var pixmap = new SKPixmap(new SKImageInfo(width, height), saveBitmap.GetPixels());
{
pixmap.Encode(outputStream, skiaOutputFormat, quality); pixmap.Encode(outputStream, skiaOutputFormat, quality);
} }
}
return outputPath; return outputPath;
} }

View File

@ -111,11 +111,9 @@ public sealed class ImageProcessor : IImageProcessor, IDisposable
public async Task ProcessImage(ImageProcessingOptions options, Stream toStream) public async Task ProcessImage(ImageProcessingOptions options, Stream toStream)
{ {
var file = await ProcessImage(options).ConfigureAwait(false); var file = await ProcessImage(options).ConfigureAwait(false);
using (var fileStream = AsyncFile.OpenRead(file.Path)) using var fileStream = AsyncFile.OpenRead(file.Path);
{
await fileStream.CopyToAsync(toStream).ConfigureAwait(false); await fileStream.CopyToAsync(toStream).ConfigureAwait(false);
} }
}
/// <inheritdoc /> /// <inheritdoc />
public IReadOnlyCollection<ImageFormat> GetSupportedImageOutputFormats() public IReadOnlyCollection<ImageFormat> GetSupportedImageOutputFormats()

View File

@ -26,11 +26,9 @@ namespace Jellyfin.Extensions
/// <returns>All lines in the stream.</returns> /// <returns>All lines in the stream.</returns>
public static string[] ReadAllLines(this Stream stream, Encoding encoding) public static string[] ReadAllLines(this Stream stream, Encoding encoding)
{ {
using (StreamReader reader = new StreamReader(stream, encoding)) using StreamReader reader = new StreamReader(stream, encoding);
{
return ReadAllLines(reader).ToArray(); return ReadAllLines(reader).ToArray();
} }
}
/// <summary> /// <summary>
/// Reads all lines in the <see cref="TextReader" />. /// Reads all lines in the <see cref="TextReader" />.

View File

@ -12,8 +12,8 @@ namespace Jellyfin.MediaEncoding.Subtitles.Tests
[Fact] [Fact]
public void Parse_Valid_Success() public void Parse_Valid_Success()
{ {
using (var stream = File.OpenRead("Test Data/example.ass")) using var stream = File.OpenRead("Test Data/example.ass");
{
var parsed = new SubtitleEditParser(new NullLogger<SubtitleEditParser>()).Parse(stream, "ass"); var parsed = new SubtitleEditParser(new NullLogger<SubtitleEditParser>()).Parse(stream, "ass");
Assert.Single(parsed.TrackEvents); Assert.Single(parsed.TrackEvents);
var trackEvent = parsed.TrackEvents[0]; var trackEvent = parsed.TrackEvents[0];
@ -25,4 +25,3 @@ namespace Jellyfin.MediaEncoding.Subtitles.Tests
} }
} }
} }
}

View File

@ -12,8 +12,8 @@ namespace Jellyfin.MediaEncoding.Subtitles.Tests
[Fact] [Fact]
public void Parse_Valid_Success() public void Parse_Valid_Success()
{ {
using (var stream = File.OpenRead("Test Data/example.srt")) using var stream = File.OpenRead("Test Data/example.srt");
{
var parsed = new SubtitleEditParser(new NullLogger<SubtitleEditParser>()).Parse(stream, "srt"); var parsed = new SubtitleEditParser(new NullLogger<SubtitleEditParser>()).Parse(stream, "srt");
Assert.Equal(2, parsed.TrackEvents.Count); Assert.Equal(2, parsed.TrackEvents.Count);
@ -29,13 +29,12 @@ namespace Jellyfin.MediaEncoding.Subtitles.Tests
Assert.Equal(TimeSpan.Parse("00:02:22.501", CultureInfo.InvariantCulture).Ticks, trackEvent2.EndPositionTicks); Assert.Equal(TimeSpan.Parse("00:02:22.501", CultureInfo.InvariantCulture).Ticks, trackEvent2.EndPositionTicks);
Assert.Equal("Very good, Lieutenant.", trackEvent2.Text); Assert.Equal("Very good, Lieutenant.", trackEvent2.Text);
} }
}
[Fact] [Fact]
public void Parse_EmptyNewlineBetweenText_Success() public void Parse_EmptyNewlineBetweenText_Success()
{ {
using (var stream = File.OpenRead("Test Data/example2.srt")) using var stream = File.OpenRead("Test Data/example2.srt");
{
var parsed = new SubtitleEditParser(new NullLogger<SubtitleEditParser>()).Parse(stream, "srt"); var parsed = new SubtitleEditParser(new NullLogger<SubtitleEditParser>()).Parse(stream, "srt");
Assert.Equal(2, parsed.TrackEvents.Count); Assert.Equal(2, parsed.TrackEvents.Count);
@ -53,4 +52,3 @@ namespace Jellyfin.MediaEncoding.Subtitles.Tests
} }
} }
} }
}

View File

@ -18,8 +18,8 @@ namespace Jellyfin.MediaEncoding.Subtitles.Tests
[MemberData(nameof(Parse_MultipleDialogues_TestData))] [MemberData(nameof(Parse_MultipleDialogues_TestData))]
public void Parse_MultipleDialogues_Success(string ssa, IReadOnlyList<SubtitleTrackEvent> expectedSubtitleTrackEvents) public void Parse_MultipleDialogues_Success(string ssa, IReadOnlyList<SubtitleTrackEvent> expectedSubtitleTrackEvents)
{ {
using (Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(ssa))) using Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(ssa));
{
SubtitleTrackInfo subtitleTrackInfo = _parser.Parse(stream, "ssa"); SubtitleTrackInfo subtitleTrackInfo = _parser.Parse(stream, "ssa");
Assert.Equal(expectedSubtitleTrackEvents.Count, subtitleTrackInfo.TrackEvents.Count); Assert.Equal(expectedSubtitleTrackEvents.Count, subtitleTrackInfo.TrackEvents.Count);
@ -35,7 +35,6 @@ namespace Jellyfin.MediaEncoding.Subtitles.Tests
Assert.Equal(expected.EndPositionTicks, actual.EndPositionTicks); Assert.Equal(expected.EndPositionTicks, actual.EndPositionTicks);
} }
} }
}
public static TheoryData<string, IReadOnlyList<SubtitleTrackEvent>> Parse_MultipleDialogues_TestData() public static TheoryData<string, IReadOnlyList<SubtitleTrackEvent>> Parse_MultipleDialogues_TestData()
{ {
@ -73,8 +72,8 @@ namespace Jellyfin.MediaEncoding.Subtitles.Tests
[Fact] [Fact]
public void Parse_Valid_Success() public void Parse_Valid_Success()
{ {
using (var stream = File.OpenRead("Test Data/example.ssa")) using var stream = File.OpenRead("Test Data/example.ssa");
{
var parsed = _parser.Parse(stream, "ssa"); var parsed = _parser.Parse(stream, "ssa");
Assert.Single(parsed.TrackEvents); Assert.Single(parsed.TrackEvents);
var trackEvent = parsed.TrackEvents[0]; var trackEvent = parsed.TrackEvents[0];
@ -86,4 +85,3 @@ namespace Jellyfin.MediaEncoding.Subtitles.Tests
} }
} }
} }
}

View File

@ -488,8 +488,9 @@ namespace Jellyfin.Model.Tests
private static async ValueTask<T> TestData<T>(string name) private static async ValueTask<T> TestData<T>(string name)
{ {
var path = Path.Join("Test Data", typeof(T).Name + "-" + name + ".json"); var path = Path.Join("Test Data", typeof(T).Name + "-" + name + ".json");
using (var stream = File.OpenRead(path))
{ using var stream = File.OpenRead(path);
var value = await JsonSerializer.DeserializeAsync<T>(stream, JsonDefaults.Options); var value = await JsonSerializer.DeserializeAsync<T>(stream, JsonDefaults.Options);
if (value is not null) if (value is not null)
{ {
@ -498,7 +499,6 @@ namespace Jellyfin.Model.Tests
throw new SerializationException("Invalid test data: " + name); throw new SerializationException("Invalid test data: " + name);
} }
}
private StreamBuilder GetStreamBuilder() private StreamBuilder GetStreamBuilder()
{ {