Files

65 lines
2.1 KiB
C#
Raw Permalink Normal View History

2026-10-22 21:19:02 +08:00
using System.IO;
using System.Linq;
using SongTidy.Services;
2026-10-22 21:24:13 +08:00
using SongTidy.Tests.TestUtilities;
2026-10-22 21:19:02 +08:00
2026-10-22 21:24:13 +08:00
namespace SongTidy.Tests.Services;
2026-10-22 21:19:02 +08:00
/// <summary>
/// Contains tests for <see cref="AudioFileScanner"/>.
/// </summary>
[TestClass]
public sealed class AudioFileScannerTests {
2026-10-22 21:24:13 +08:00
private readonly TempDirectory _tempDirectory = new();
2026-10-22 21:19:02 +08:00
/// <summary>
/// Removes the temporary directory created for the current test.
/// </summary>
[TestCleanup]
public void Cleanup() {
2026-10-22 21:24:13 +08:00
_tempDirectory.Dispose();
2026-10-22 21:19:02 +08:00
}
/// <summary>
/// Verifies that supported extensions are matched case-insensitively and others are ignored.
/// </summary>
[TestMethod]
public void ScansSupportedExtensionsCaseInsensitively() {
CreateFile("a.mp3");
CreateFile("b.FLAC");
CreateFile("c.txt");
2026-10-22 21:24:13 +08:00
IReadOnlyList<string> files = AudioFileScanner.Scan(_tempDirectory.FullPath);
2026-10-22 21:19:02 +08:00
Assert.HasCount(2, files);
Assert.IsTrue(files.Any(file => file.EndsWith("a.mp3", StringComparison.OrdinalIgnoreCase)));
Assert.IsTrue(files.Any(file => file.EndsWith("b.FLAC", StringComparison.OrdinalIgnoreCase)));
}
/// <summary>
/// Verifies that subdirectories are not scanned.
/// </summary>
[TestMethod]
public void DoesNotScanSubdirectories() {
CreateFile("root.mp3");
CreateFile(Path.Combine("sub", "nested.wav"));
2026-10-22 21:24:13 +08:00
IReadOnlyList<string> files = AudioFileScanner.Scan(_tempDirectory.FullPath);
2026-10-22 21:19:02 +08:00
Assert.HasCount(1, files);
Assert.IsTrue(files[0].EndsWith("root.mp3", StringComparison.OrdinalIgnoreCase));
}
/// <summary>
/// Creates an empty file inside the test directory.
/// </summary>
/// <param name="fileName">The relative file name, which may include a subdirectory.</param>
private void CreateFile(string fileName) {
2026-10-22 21:24:13 +08:00
string path = Path.Combine(_tempDirectory.FullPath, fileName);
string directory = Path.GetDirectoryName(path) ?? _tempDirectory.FullPath;
2026-10-22 21:19:02 +08:00
Directory.CreateDirectory(directory);
File.WriteAllText(path, string.Empty);
}
}