Moved duplicated NormalizePath method to PathExtentions

pull/3113/head
kay.one 13 years ago
parent 95d1832379
commit 6778a6ed99

@ -1,4 +1,5 @@
using System.Linq; using System;
using System.Linq;
using FluentAssertions; using FluentAssertions;
using Moq; using Moq;
using NUnit.Framework; using NUnit.Framework;
@ -13,7 +14,7 @@ namespace NzbDrone.Common.Test
private EnviromentProvider GetEnviromentProvider() private EnviromentProvider GetEnviromentProvider()
{ {
var envMoq = new Mock<EnviromentProvider>(); var envMoq = new Mock<EnviromentProvider>();
envMoq.SetupGet(c => c.ApplicationPath).Returns(@"C:\NzbDrone\"); envMoq.SetupGet(c => c.ApplicationPath).Returns(@"C:\NzbDrone\");
envMoq.SetupGet(c => c.SystemTemp).Returns(@"C:\Temp\"); envMoq.SetupGet(c => c.SystemTemp).Returns(@"C:\Temp\");
@ -21,6 +22,32 @@ namespace NzbDrone.Common.Test
return envMoq.Object; return envMoq.Object;
} }
[TestCase(@"c:\test\", @"c:\test")]
[TestCase(@"c:\\test\\", @"c:\test")]
[TestCase(@"C:\\Test\\", @"C:\Test")]
[TestCase(@"C:\\Test\\Test\", @"C:\Test\Test")]
[TestCase(@"\\Testserver\Test\", @"\\Testserver\Test")]
public void Normalize_Path(string dirty, string clean)
{
var result = dirty.NormalizePath();
result.Should().Be(clean);
}
[Test]
[ExpectedException(typeof(ArgumentException), ExpectedMessage = "Path can not be null or empty")]
public void normalize_path_exception_empty()
{
"".NormalizePath();
}
[Test]
[ExpectedException(typeof(ArgumentException), ExpectedMessage = "Path can not be null or empty")]
public void normalize_path_exception_null()
{
string nullPath = null;
nullPath.NormalizePath();
}
[Test] [Test]
public void AppDataDirectory_path_test() public void AppDataDirectory_path_test()

@ -1,4 +1,5 @@
using System.IO; using System;
using System.IO;
namespace NzbDrone.Common namespace NzbDrone.Common
{ {
@ -22,6 +23,22 @@ namespace NzbDrone.Common
private const string UPDATE_CLIENT_EXE = "nzbdrone.update.exe"; private const string UPDATE_CLIENT_EXE = "nzbdrone.update.exe";
private const string UPDATE_CLIENT_FOLDER_NAME = "NzbDrone.Update\\"; private const string UPDATE_CLIENT_FOLDER_NAME = "NzbDrone.Update\\";
public static string NormalizePath(this string path)
{
if (String.IsNullOrWhiteSpace(path))
throw new ArgumentException("Path can not be null or empty");
var info = new FileInfo(path);
if (info.FullName.StartsWith(@"\\")) //UNC
{
return info.FullName.TrimEnd('/', '\\', ' ');
}
return info.FullName.Trim('/', '\\', ' ');
}
public static string GetIISFolder(this EnviromentProvider enviromentProvider) public static string GetIISFolder(this EnviromentProvider enviromentProvider)
{ {
return Path.Combine(enviromentProvider.ApplicationPath, IIS_FOLDER); return Path.Combine(enviromentProvider.ApplicationPath, IIS_FOLDER);

@ -199,16 +199,7 @@ namespace NzbDrone.Core.Test
result.Should().Be(seriesName); result.Should().Be(seriesName);
} }
[TestCase(@"c:\test\", @"c:\test")]
[TestCase(@"c:\\test\\", @"c:\test")]
[TestCase(@"C:\\Test\\", @"C:\Test")]
[TestCase(@"C:\\Test\\Test\", @"C:\Test\Test")]
[TestCase(@"\\Testserver\Test\", @"\\Testserver\Test")]
public void Normalize_Path(string dirty, string clean)
{
var result = Parser.NormalizePath(dirty);
result.Should().Be(clean);
}
[TestCase("CaPitAl", "capital")] [TestCase("CaPitAl", "capital")]
[TestCase("peri.od", "period")] [TestCase("peri.od", "period")]
@ -316,19 +307,6 @@ namespace NzbDrone.Core.Test
var result = Parser.ParseLanguage(postTitle); var result = Parser.ParseLanguage(postTitle);
result.Should().Be(language); result.Should().Be(language);
} }
[Test]
[ExpectedException(typeof(ArgumentException), ExpectedMessage = "Path can not be null or empty")]
public void normalize_path_exception_empty()
{
Parser.NormalizePath("");
}
[Test]
[ExpectedException(typeof(ArgumentException), ExpectedMessage = "Path can not be null or empty")]
public void normalize_path_exception_null()
{
Parser.NormalizePath(null);
}
[TestCase("Hawaii Five 0 S01 720p WEB DL DD5 1 H 264 NT", "Hawaii Five", 1)] [TestCase("Hawaii Five 0 S01 720p WEB DL DD5 1 H 264 NT", "Hawaii Five", 1)]
[TestCase("30 Rock S03 WS PDTV XviD FUtV", "30 Rock", 3)] [TestCase("30 Rock S03 WS PDTV XviD FUtV", "30 Rock", 3)]

@ -379,21 +379,6 @@ namespace NzbDrone.Core
return NormalizeRegex.Replace(title, String.Empty).ToLower(); return NormalizeRegex.Replace(title, String.Empty).ToLower();
} }
public static string NormalizePath(string path)
{
if (String.IsNullOrWhiteSpace(path))
throw new ArgumentException("Path can not be null or empty");
var info = new FileInfo(path);
if (info.FullName.StartsWith(@"\\")) //UNC
{
return info.FullName.TrimEnd('/', '\\', ' ');
}
return info.FullName.Trim('/', '\\', ' ');
}
public static long GetReportSize(string sizeString) public static long GetReportSize(string sizeString)
{ {
var match = ReportSizeRegex.Matches(sizeString); var match = ReportSizeRegex.Matches(sizeString);

@ -145,7 +145,7 @@ namespace NzbDrone.Core.Providers
var episodeFile = new EpisodeFile(); var episodeFile = new EpisodeFile();
episodeFile.DateAdded = DateTime.Now; episodeFile.DateAdded = DateTime.Now;
episodeFile.SeriesId = series.SeriesId; episodeFile.SeriesId = series.SeriesId;
episodeFile.Path = Parser.NormalizePath(filePath); episodeFile.Path = filePath.NormalizePath();
episodeFile.Size = size; episodeFile.Size = size;
episodeFile.Quality = parseResult.Quality.QualityType; episodeFile.Quality = parseResult.Quality.QualityType;
episodeFile.Proper = parseResult.Quality.Proper; episodeFile.Proper = parseResult.Quality.Proper;

@ -9,6 +9,7 @@ using NzbDrone.Core.Providers.Core;
using NzbDrone.Core.Repository; using NzbDrone.Core.Repository;
using NzbDrone.Core.Repository.Quality; using NzbDrone.Core.Repository.Quality;
using PetaPoco; using PetaPoco;
using NzbDrone.Common;
namespace NzbDrone.Core.Providers namespace NzbDrone.Core.Providers
{ {
@ -48,7 +49,7 @@ namespace NzbDrone.Core.Providers
public virtual bool Exists(string path) public virtual bool Exists(string path)
{ {
return _database.Exists<EpisodeFile>("WHERE Path =@0", Parser.NormalizePath(path)); return _database.Exists<EpisodeFile>("WHERE Path =@0", path.NormalizePath());
} }
public virtual EpisodeFile GetEpisodeFile(int episodeFileId) public virtual EpisodeFile GetEpisodeFile(int episodeFileId)

@ -94,7 +94,7 @@ namespace NzbDrone.Core.Providers
{ {
var target = GetTaggedFolderName(directory, status); var target = GetTaggedFolderName(directory, status);
if (!String.Equals(Parser.NormalizePath(target), Parser.NormalizePath(directory.FullName), StringComparison.InvariantCultureIgnoreCase)) if (!String.Equals(target.NormalizePath(), directory.FullName.NormalizePath(), StringComparison.InvariantCultureIgnoreCase))
{ {
_diskProvider.MoveDirectory(directory.FullName, target); _diskProvider.MoveDirectory(directory.FullName, target);
} }

@ -72,7 +72,7 @@ namespace NzbDrone.Core.Providers
foreach (string seriesFolder in _diskProvider.GetDirectories(path)) foreach (string seriesFolder in _diskProvider.GetDirectories(path))
{ {
var cleanPath = Parser.NormalizePath(new DirectoryInfo(seriesFolder).FullName); var cleanPath = new DirectoryInfo(seriesFolder).FullName.NormalizePath();
if (!_seriesProvider.SeriesPathExists(cleanPath)) if (!_seriesProvider.SeriesPathExists(cleanPath))
{ {

@ -94,7 +94,7 @@ namespace NzbDrone.Providers
foreach (var process in _processProvider.GetProcessByName("IISExpress")) foreach (var process in _processProvider.GetProcessByName("IISExpress"))
{ {
Logger.Info("[{0}]IIS Process found. Path:{1}", process.Id, process.StartPath); Logger.Info("[{0}]IIS Process found. Path:{1}", process.Id, process.StartPath);
if (NormalizePath(process.StartPath) == NormalizePath(_enviromentProvider.GetIISExe())) if (process.StartPath.NormalizePath() == _enviromentProvider.GetIISExe().NormalizePath())
{ {
Logger.Info("[{0}]Process is considered orphaned.", process.Id); Logger.Info("[{0}]Process is considered orphaned.", process.Id);
_processProvider.Kill(process.Id); _processProvider.Kill(process.Id);
@ -129,19 +129,5 @@ namespace NzbDrone.Providers
IISLogger.Trace(e.Data); IISLogger.Trace(e.Data);
} }
private string NormalizePath(string path)
{
if (String.IsNullOrWhiteSpace(path))
throw new ArgumentException("Path can not be null or empty");
var info = new FileInfo(path);
if (info.FullName.StartsWith(@"\\")) //UNC
{
return info.FullName.TrimEnd('/', '\\', ' ');
}
return info.FullName.Trim('/', '\\', ' ').ToLower();
}
} }
} }
Loading…
Cancel
Save