74 lines
2.3 KiB
C#
74 lines
2.3 KiB
C#
using System.IO;
|
|
using System.Linq;
|
|
|
|
using SongTidy.Services;
|
|
|
|
namespace SongTidy.Tests;
|
|
|
|
/// <summary>
|
|
/// Contains tests for <see cref="AudioFileScanner"/>.
|
|
/// </summary>
|
|
[TestClass]
|
|
public sealed class AudioFileScannerTests {
|
|
private string _tempDirectory = string.Empty;
|
|
|
|
/// <summary>
|
|
/// Creates a unique temporary directory for the current test.
|
|
/// </summary>
|
|
[TestInitialize]
|
|
public void Initialize() {
|
|
_tempDirectory = Path.Combine(Path.GetTempPath(), $"SongTidy Scanner {Guid.NewGuid():N}");
|
|
Directory.CreateDirectory(_tempDirectory);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Removes the temporary directory created for the current test.
|
|
/// </summary>
|
|
[TestCleanup]
|
|
public void Cleanup() {
|
|
Directory.Delete(_tempDirectory, recursive: true);
|
|
}
|
|
|
|
/// <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");
|
|
|
|
IReadOnlyList<string> files = AudioFileScanner.Scan(_tempDirectory);
|
|
|
|
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"));
|
|
|
|
IReadOnlyList<string> files = AudioFileScanner.Scan(_tempDirectory);
|
|
|
|
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) {
|
|
string path = Path.Combine(_tempDirectory, fileName);
|
|
string directory = Path.GetDirectoryName(path) ?? _tempDirectory;
|
|
|
|
Directory.CreateDirectory(directory);
|
|
File.WriteAllText(path, string.Empty);
|
|
}
|
|
}
|