Do not rename episodeFiles if the source and destination are the same (file is already named correctly).

This commit is contained in:
Mark McDowall 2011-12-13 22:42:24 -08:00
parent 2856c3dca3
commit 1c99541731
2 changed files with 58 additions and 2 deletions

View File

@ -1,13 +1,16 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using FizzWare.NBuilder;
using FluentAssertions;
using Moq;
using NUnit.Framework;
using NzbDrone.Common;
using NzbDrone.Core.Providers;
using NzbDrone.Core.Providers.Core;
using NzbDrone.Core.Repository;
using NzbDrone.Core.Repository.Quality;
using NzbDrone.Core.Test.Framework;
using NzbDrone.Test.Common;
using NzbDrone.Test.Common.AutoMoq;
@ -163,5 +166,51 @@ namespace NzbDrone.Core.Test.ProviderTests
mocker.VerifyAllMocks();
ExceptionVerification.ExcpectedWarns(1);
}
[Test]
public void move_should_not_move_file_if_source_and_destination_are_the_same_path()
{
var fakeSeries = Builder<Series>.CreateNew()
.With(s => s.SeriesId = 5)
.With(s => s.Title = "30 Rock")
.Build();
var fakeEpisode = Builder<Episode>.CreateListOfSize(1)
.All()
.With(e => e.SeriesId = fakeSeries.SeriesId)
.With(e => e.SeasonNumber = 1)
.With(e => e.EpisodeNumber = 1)
.Build();
const string filename = @"30 Rock - S01E01 - TBD";
var fi = new FileInfo(Path.Combine(@"C:\Test\TV\30 Rock\Season 01\", filename + ".avi"));
var file = Builder<EpisodeFile>.CreateNew()
.With(f => f.SeriesId = fakeSeries.SeriesId)
.With(f => f.Path = fi.FullName)
.Build();
Mocker.GetMock<SeriesProvider>()
.Setup(e => e.GetSeries(fakeSeries.SeriesId))
.Returns(fakeSeries);
Mocker.GetMock<EpisodeProvider>()
.Setup(e => e.GetEpisodesByFileId(file.EpisodeFileId))
.Returns(fakeEpisode);
Mocker.GetMock<MediaFileProvider>()
.Setup(e => e.GetNewFilename(fakeEpisode, fakeSeries.Title, It.IsAny<QualityTypes>()))
.Returns(filename);
Mocker.GetMock<MediaFileProvider>()
.Setup(e => e.CalculateFilePath(It.IsAny<Series>(), fakeEpisode.First().SeasonNumber, filename, ".avi"))
.Returns(fi);
//Act
var result = Mocker.Resolve<DiskScanProvider>().MoveEpisodeFile(file, false);
//Assert
result.Should().BeFalse();
}
}
}

View File

@ -175,10 +175,17 @@ namespace NzbDrone.Core.Providers
string newFileName = _mediaFileProvider.GetNewFilename(episodes, series.Title, episodeFile.Quality);
var newFile = _mediaFileProvider.CalculateFilePath(series, episodes.First().SeasonNumber, newFileName, Path.GetExtension(episodeFile.Path));
//Only rename if existing and new filenames don't match
if (episodeFile.Path == newFile.FullName)
{
Logger.Debug("Skipping file rename, source and destination are the same: {0}", episodeFile.Path);
return false;
}
//Ensure the folder Exists before trying to move it (No error is thrown if the folder already exists)
_diskProvider.CreateDirectory(newFile.DirectoryName);
//Do the rename
//Rename the file
Logger.Debug("Moving [{0}] > [{1}]", episodeFile.Path, newFile.FullName);
_diskProvider.MoveFile(episodeFile.Path, newFile.FullName);