From 6778a6ed99985ba3f1432ebaa28e06257ae09807 Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Sun, 20 Nov 2011 16:35:29 -0800 Subject: [PATCH] Moved duplicated NormalizePath method to PathExtentions --- NzbDrone.Common.Test/PathExtentionFixture.cs | 31 +++++++++++++++++-- NzbDrone.Common/PathExtentions.cs | 19 +++++++++++- NzbDrone.Core.Test/ParserTest.cs | 24 +------------- NzbDrone.Core/Parser.cs | 15 --------- NzbDrone.Core/Providers/DiskScanProvider.cs | 2 +- NzbDrone.Core/Providers/MediaFileProvider.cs | 3 +- .../Providers/PostDownloadProvider.cs | 2 +- NzbDrone.Core/Providers/RootDirProvider.cs | 2 +- NzbDrone/Providers/IISProvider.cs | 16 +--------- 9 files changed, 54 insertions(+), 60 deletions(-) diff --git a/NzbDrone.Common.Test/PathExtentionFixture.cs b/NzbDrone.Common.Test/PathExtentionFixture.cs index 7924da49a..d90c776e6 100644 --- a/NzbDrone.Common.Test/PathExtentionFixture.cs +++ b/NzbDrone.Common.Test/PathExtentionFixture.cs @@ -1,4 +1,5 @@ -using System.Linq; +using System; +using System.Linq; using FluentAssertions; using Moq; using NUnit.Framework; @@ -13,7 +14,7 @@ namespace NzbDrone.Common.Test private EnviromentProvider GetEnviromentProvider() { var envMoq = new Mock(); - + envMoq.SetupGet(c => c.ApplicationPath).Returns(@"C:\NzbDrone\"); envMoq.SetupGet(c => c.SystemTemp).Returns(@"C:\Temp\"); @@ -21,6 +22,32 @@ namespace NzbDrone.Common.Test 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] public void AppDataDirectory_path_test() diff --git a/NzbDrone.Common/PathExtentions.cs b/NzbDrone.Common/PathExtentions.cs index 49f2d46a6..6caf93ecb 100644 --- a/NzbDrone.Common/PathExtentions.cs +++ b/NzbDrone.Common/PathExtentions.cs @@ -1,4 +1,5 @@ -using System.IO; +using System; +using System.IO; namespace NzbDrone.Common { @@ -22,6 +23,22 @@ namespace NzbDrone.Common private const string UPDATE_CLIENT_EXE = "nzbdrone.update.exe"; 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) { return Path.Combine(enviromentProvider.ApplicationPath, IIS_FOLDER); diff --git a/NzbDrone.Core.Test/ParserTest.cs b/NzbDrone.Core.Test/ParserTest.cs index 034885c5c..3704a7263 100644 --- a/NzbDrone.Core.Test/ParserTest.cs +++ b/NzbDrone.Core.Test/ParserTest.cs @@ -199,16 +199,7 @@ namespace NzbDrone.Core.Test 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("peri.od", "period")] @@ -316,19 +307,6 @@ namespace NzbDrone.Core.Test var result = Parser.ParseLanguage(postTitle); 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("30 Rock S03 WS PDTV XviD FUtV", "30 Rock", 3)] diff --git a/NzbDrone.Core/Parser.cs b/NzbDrone.Core/Parser.cs index a59e231fe..126784e38 100644 --- a/NzbDrone.Core/Parser.cs +++ b/NzbDrone.Core/Parser.cs @@ -379,21 +379,6 @@ namespace NzbDrone.Core 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) { var match = ReportSizeRegex.Matches(sizeString); diff --git a/NzbDrone.Core/Providers/DiskScanProvider.cs b/NzbDrone.Core/Providers/DiskScanProvider.cs index b9691a7e9..edefc3f69 100644 --- a/NzbDrone.Core/Providers/DiskScanProvider.cs +++ b/NzbDrone.Core/Providers/DiskScanProvider.cs @@ -145,7 +145,7 @@ namespace NzbDrone.Core.Providers var episodeFile = new EpisodeFile(); episodeFile.DateAdded = DateTime.Now; episodeFile.SeriesId = series.SeriesId; - episodeFile.Path = Parser.NormalizePath(filePath); + episodeFile.Path = filePath.NormalizePath(); episodeFile.Size = size; episodeFile.Quality = parseResult.Quality.QualityType; episodeFile.Proper = parseResult.Quality.Proper; diff --git a/NzbDrone.Core/Providers/MediaFileProvider.cs b/NzbDrone.Core/Providers/MediaFileProvider.cs index 2df284100..5f50a978a 100644 --- a/NzbDrone.Core/Providers/MediaFileProvider.cs +++ b/NzbDrone.Core/Providers/MediaFileProvider.cs @@ -9,6 +9,7 @@ using NzbDrone.Core.Providers.Core; using NzbDrone.Core.Repository; using NzbDrone.Core.Repository.Quality; using PetaPoco; +using NzbDrone.Common; namespace NzbDrone.Core.Providers { @@ -48,7 +49,7 @@ namespace NzbDrone.Core.Providers public virtual bool Exists(string path) { - return _database.Exists("WHERE Path =@0", Parser.NormalizePath(path)); + return _database.Exists("WHERE Path =@0", path.NormalizePath()); } public virtual EpisodeFile GetEpisodeFile(int episodeFileId) diff --git a/NzbDrone.Core/Providers/PostDownloadProvider.cs b/NzbDrone.Core/Providers/PostDownloadProvider.cs index 90330875c..50c1523d6 100644 --- a/NzbDrone.Core/Providers/PostDownloadProvider.cs +++ b/NzbDrone.Core/Providers/PostDownloadProvider.cs @@ -94,7 +94,7 @@ namespace NzbDrone.Core.Providers { 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); } diff --git a/NzbDrone.Core/Providers/RootDirProvider.cs b/NzbDrone.Core/Providers/RootDirProvider.cs index 98574b5c2..8d1a16517 100644 --- a/NzbDrone.Core/Providers/RootDirProvider.cs +++ b/NzbDrone.Core/Providers/RootDirProvider.cs @@ -72,7 +72,7 @@ namespace NzbDrone.Core.Providers 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)) { diff --git a/NzbDrone/Providers/IISProvider.cs b/NzbDrone/Providers/IISProvider.cs index 4b5ad3ef7..42e188ef4 100644 --- a/NzbDrone/Providers/IISProvider.cs +++ b/NzbDrone/Providers/IISProvider.cs @@ -94,7 +94,7 @@ namespace NzbDrone.Providers foreach (var process in _processProvider.GetProcessByName("IISExpress")) { 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); _processProvider.Kill(process.Id); @@ -129,19 +129,5 @@ namespace NzbDrone.Providers 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(); - } } } \ No newline at end of file