2020-04-20 10:16:22 +02:00
|
|
|
using System;
|
2023-03-30 16:59:21 +02:00
|
|
|
using System.IO;
|
2020-04-19 15:18:28 +02:00
|
|
|
using Emby.Server.Implementations.Library;
|
|
|
|
using Xunit;
|
|
|
|
|
|
|
|
namespace Jellyfin.Server.Implementations.Tests.Library
|
|
|
|
{
|
|
|
|
public class PathExtensionsTests
|
|
|
|
{
|
|
|
|
[Theory]
|
|
|
|
[InlineData("Superman: Red Son [imdbid=tt10985510]", "imdbid", "tt10985510")]
|
2021-12-31 16:51:50 +01:00
|
|
|
[InlineData("Superman: Red Son [imdbid-tt10985510]", "imdbid", "tt10985510")]
|
2020-04-19 15:18:28 +02:00
|
|
|
[InlineData("Superman: Red Son - tt10985510", "imdbid", "tt10985510")]
|
|
|
|
[InlineData("Superman: Red Son", "imdbid", null)]
|
2020-09-20 14:02:41 +02:00
|
|
|
[InlineData("Superman: Red Son", "something", null)]
|
2021-11-28 00:25:21 +01:00
|
|
|
[InlineData("Superman: Red Son [imdbid1=tt11111111][imdbid=tt10985510]", "imdbid", "tt10985510")]
|
2021-12-31 16:51:50 +01:00
|
|
|
[InlineData("Superman: Red Son [imdbid1-tt11111111][imdbid=tt10985510]", "imdbid", "tt10985510")]
|
2021-11-28 00:25:21 +01:00
|
|
|
[InlineData("Superman: Red Son [tmdbid=618355][imdbid=tt10985510]", "imdbid", "tt10985510")]
|
2021-12-31 16:51:50 +01:00
|
|
|
[InlineData("Superman: Red Son [tmdbid-618355][imdbid-tt10985510]", "imdbid", "tt10985510")]
|
|
|
|
[InlineData("Superman: Red Son [tmdbid-618355][imdbid-tt10985510]", "tmdbid", "618355")]
|
2021-12-09 16:06:06 +01:00
|
|
|
[InlineData("[tmdbid=618355]", "tmdbid", "618355")]
|
2021-12-31 16:51:50 +01:00
|
|
|
[InlineData("[tmdbid-618355]", "tmdbid", "618355")]
|
2021-12-09 20:31:32 +01:00
|
|
|
[InlineData("tmdbid=111111][tmdbid=618355]", "tmdbid", "618355")]
|
|
|
|
[InlineData("[tmdbid=618355]tmdbid=111111]", "tmdbid", "618355")]
|
2021-12-09 16:06:06 +01:00
|
|
|
[InlineData("tmdbid=618355]", "tmdbid", null)]
|
|
|
|
[InlineData("[tmdbid=618355", "tmdbid", null)]
|
|
|
|
[InlineData("tmdbid=618355", "tmdbid", null)]
|
|
|
|
[InlineData("tmdbid=", "tmdbid", null)]
|
|
|
|
[InlineData("tmdbid", "tmdbid", null)]
|
2021-12-12 03:43:01 +01:00
|
|
|
[InlineData("[tmdbid=][imdbid=tt10985510]", "tmdbid", null)]
|
2021-12-31 16:51:50 +01:00
|
|
|
[InlineData("[tmdbid-][imdbid-tt10985510]", "tmdbid", null)]
|
|
|
|
[InlineData("Superman: Red Son [tmdbid-618355][tmdbid=1234567]", "tmdbid", "618355")]
|
2020-04-21 10:18:26 +02:00
|
|
|
public void GetAttributeValue_ValidArgs_Correct(string input, string attribute, string? expectedResult)
|
2020-04-19 15:18:28 +02:00
|
|
|
{
|
2020-04-21 10:18:26 +02:00
|
|
|
Assert.Equal(expectedResult, PathExtensions.GetAttributeValue(input, attribute));
|
2020-04-19 15:18:28 +02:00
|
|
|
}
|
2020-04-20 10:16:22 +02:00
|
|
|
|
|
|
|
[Theory]
|
|
|
|
[InlineData("", "")]
|
|
|
|
[InlineData("Superman: Red Son [imdbid=tt10985510]", "")]
|
|
|
|
[InlineData("", "imdbid")]
|
|
|
|
public void GetAttributeValue_EmptyString_ThrowsArgumentException(string input, string attribute)
|
|
|
|
{
|
|
|
|
Assert.Throws<ArgumentException>(() => PathExtensions.GetAttributeValue(input, attribute));
|
|
|
|
}
|
2021-03-05 14:09:23 +01:00
|
|
|
|
|
|
|
[Theory]
|
2021-03-06 14:01:37 +01:00
|
|
|
[InlineData("C:/Users/jeff/myfile.mkv", "C:/Users/jeff", "/home/jeff", "/home/jeff/myfile.mkv")]
|
|
|
|
[InlineData("C:/Users/jeff/myfile.mkv", "C:/Users/jeff/", "/home/jeff", "/home/jeff/myfile.mkv")]
|
|
|
|
[InlineData("/home/jeff/music/jeff's band/consistently inconsistent.mp3", "/home/jeff/music/jeff's band", "/home/not jeff", "/home/not jeff/consistently inconsistent.mp3")]
|
2023-10-08 00:26:12 +02:00
|
|
|
[InlineData(@"C:\Users\jeff\myfile.mkv", "C:\\Users/jeff", "/home/jeff", "/home/jeff/myfile.mkv")]
|
|
|
|
[InlineData(@"C:\Users\jeff\myfile.mkv", "C:\\Users/jeff", "/home/jeff/", "/home/jeff/myfile.mkv")]
|
|
|
|
[InlineData(@"C:\Users\jeff\myfile.mkv", "C:\\Users/jeff/", "/home/jeff/", "/home/jeff/myfile.mkv")]
|
|
|
|
[InlineData(@"C:\Users\jeff\myfile.mkv", "C:\\Users/jeff/", "/", "/myfile.mkv")]
|
2021-05-05 16:43:20 +02:00
|
|
|
[InlineData("/o", "/o", "/s", "/s")] // regression test for #5977
|
2021-03-06 14:01:37 +01:00
|
|
|
public void TryReplaceSubPath_ValidArgs_Correct(string path, string subPath, string newSubPath, string? expectedResult)
|
2021-03-05 14:09:23 +01:00
|
|
|
{
|
2021-03-06 14:01:37 +01:00
|
|
|
Assert.True(PathExtensions.TryReplaceSubPath(path, subPath, newSubPath, out var result));
|
2021-03-05 14:09:23 +01:00
|
|
|
Assert.Equal(expectedResult, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Theory]
|
2021-03-09 01:28:21 +01:00
|
|
|
[InlineData(null, null, null)]
|
|
|
|
[InlineData(null, "/my/path", "/another/path")]
|
|
|
|
[InlineData("/my/path", null, "/another/path")]
|
|
|
|
[InlineData("/my/path", "/another/path", null)]
|
2021-03-05 14:09:23 +01:00
|
|
|
[InlineData("", "", "")]
|
|
|
|
[InlineData("/my/path", "", "")]
|
|
|
|
[InlineData("", "/another/path", "")]
|
|
|
|
[InlineData("", "", "/new/subpath")]
|
2021-03-06 14:01:37 +01:00
|
|
|
[InlineData("/home/jeff/music/jeff's band/consistently inconsistent.mp3", "/home/jeff/music/not jeff's band", "/home/not jeff")]
|
2021-03-09 01:28:21 +01:00
|
|
|
public void TryReplaceSubPath_InvalidInput_ReturnsFalseAndNull(string? path, string? subPath, string? newSubPath)
|
2021-03-05 14:09:23 +01:00
|
|
|
{
|
2021-03-06 14:01:37 +01:00
|
|
|
Assert.False(PathExtensions.TryReplaceSubPath(path, subPath, newSubPath, out var result));
|
|
|
|
Assert.Null(result);
|
2021-03-05 14:09:23 +01:00
|
|
|
}
|
2023-03-30 16:59:21 +02:00
|
|
|
|
|
|
|
[Theory]
|
|
|
|
[InlineData(null, '/', null)]
|
|
|
|
[InlineData(null, '\\', null)]
|
2023-10-08 00:26:12 +02:00
|
|
|
[InlineData("/home/jeff/myfile.mkv", '\\', @"\home\jeff\myfile.mkv")]
|
|
|
|
[InlineData(@"C:\Users\Jeff\myfile.mkv", '/', "C:/Users/Jeff/myfile.mkv")]
|
|
|
|
[InlineData(@"\home/jeff\myfile.mkv", '\\', @"\home\jeff\myfile.mkv")]
|
|
|
|
[InlineData(@"\home/jeff\myfile.mkv", '/', "/home/jeff/myfile.mkv")]
|
2023-03-30 16:59:21 +02:00
|
|
|
[InlineData("", '/', "")]
|
|
|
|
public void NormalizePath_SpecifyingSeparator_Normalizes(string path, char separator, string expectedPath)
|
|
|
|
{
|
|
|
|
Assert.Equal(expectedPath, path.NormalizePath(separator));
|
|
|
|
}
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
[InlineData("/home/jeff/myfile.mkv")]
|
2023-10-08 00:26:12 +02:00
|
|
|
[InlineData(@"C:\Users\Jeff\myfile.mkv")]
|
|
|
|
[InlineData(@"\home/jeff\myfile.mkv")]
|
2023-03-30 16:59:21 +02:00
|
|
|
public void NormalizePath_NoArgs_UsesDirectorySeparatorChar(string path)
|
|
|
|
{
|
|
|
|
var separator = Path.DirectorySeparatorChar;
|
|
|
|
|
|
|
|
Assert.Equal(path.Replace('\\', separator).Replace('/', separator), path.NormalizePath());
|
|
|
|
}
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
[InlineData("/home/jeff/myfile.mkv", '/')]
|
2023-10-08 00:26:12 +02:00
|
|
|
[InlineData(@"C:\Users\Jeff\myfile.mkv", '\\')]
|
|
|
|
[InlineData(@"\home/jeff\myfile.mkv", '/')]
|
2023-03-30 16:59:21 +02:00
|
|
|
public void NormalizePath_OutVar_Correct(string path, char expectedSeparator)
|
|
|
|
{
|
|
|
|
var result = path.NormalizePath(out var separator);
|
|
|
|
|
|
|
|
Assert.Equal(expectedSeparator, separator);
|
|
|
|
Assert.Equal(path.Replace('\\', separator).Replace('/', separator), result);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void NormalizePath_SpecifyInvalidSeparator_ThrowsException()
|
|
|
|
{
|
|
|
|
Assert.Throws<ArgumentException>(() => string.Empty.NormalizePath('a'));
|
|
|
|
}
|
2020-04-19 15:18:28 +02:00
|
|
|
}
|
|
|
|
}
|