-
Notifications
You must be signed in to change notification settings - Fork 11
Skip re-downloads for existing converted files in UniversalDownloaderPlatform #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ProtagNeptune
wants to merge
1
commit into
AlexCSDev:master
Choose a base branch
from
ProtagNeptune:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
217 changes: 217 additions & 0 deletions
217
UniversalDownloaderPlatform.Common.Tests/FileExistsActionHelperTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,217 @@ | ||
| using System; | ||
| using System.IO; | ||
| using UniversalDownloaderPlatform.Common.Enums; | ||
| using UniversalDownloaderPlatform.Common.Helpers; | ||
| using Xunit; | ||
|
|
||
| namespace UniversalDownloaderPlatform.Common.Tests | ||
| { | ||
| public class FileExistsActionHelperTests : IDisposable | ||
| { | ||
| private readonly string _temporaryDirectoryPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N")); | ||
|
|
||
| public FileExistsActionHelperTests() | ||
| { | ||
| Directory.CreateDirectory(_temporaryDirectoryPath); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ResolveExistingFilePath_ReturnsRequestedPath_WhenExactFileExists() | ||
| { | ||
| string requestedPath = Path.Combine(_temporaryDirectoryPath, "media_123.png"); | ||
| File.WriteAllText(requestedPath, "test"); | ||
|
|
||
| string existingPath = FileExistsActionHelper.ResolveExistingFilePath(requestedPath); | ||
|
|
||
| Assert.Equal(requestedPath, existingPath); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ResolveExistingFilePath_ReturnsSameBasenameFile_WhenDifferentExtensionExists() | ||
| { | ||
| string requestedPath = Path.Combine(_temporaryDirectoryPath, "media_123.png"); | ||
| string existingPath = Path.Combine(_temporaryDirectoryPath, "media_123.mp3"); | ||
| File.WriteAllText(existingPath, "test"); | ||
|
|
||
| string resolvedPath = FileExistsActionHelper.ResolveExistingFilePath(requestedPath); | ||
|
|
||
| Assert.Equal(existingPath, resolvedPath); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ResolveExistingFilePath_ReturnsNull_WhenOnlyPartialNameMatches() | ||
| { | ||
| string requestedPath = Path.Combine(_temporaryDirectoryPath, "media_123.png"); | ||
| string partialMatchPath = Path.Combine(_temporaryDirectoryPath, "media_123_extra.mp3"); | ||
| File.WriteAllText(partialMatchPath, "test"); | ||
|
|
||
| string existingPath = FileExistsActionHelper.ResolveExistingFilePath(requestedPath); | ||
|
|
||
| Assert.Null(existingPath); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ResolveExistingFilePath_ReturnsNull_WhenDirectoryDoesNotExist() | ||
| { | ||
| string requestedPath = Path.Combine(_temporaryDirectoryPath, "missing", "media_123.png"); | ||
|
|
||
| string existingPath = FileExistsActionHelper.ResolveExistingFilePath(requestedPath); | ||
|
|
||
| Assert.Null(existingPath); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(FileExistsAction.BackupIfDifferent)] | ||
| [InlineData(FileExistsAction.ReplaceIfDifferent)] | ||
| [InlineData(FileExistsAction.KeepExisting)] | ||
| public void DoFileExistsActionBeforeDownload_SkipsConvertedEquivalent_WhenNotAlwaysReplace(FileExistsAction fileExistsAction) | ||
| { | ||
| string requestedPath = Path.Combine(_temporaryDirectoryPath, "media_123.png"); | ||
| string existingPath = Path.Combine(_temporaryDirectoryPath, "media_123.mp3"); | ||
| File.WriteAllText(existingPath, "converted-content"); | ||
|
|
||
| bool shouldContinue = FileExistsActionHelper.DoFileExistsActionBeforeDownload( | ||
| existingPath, | ||
| requestedPath, | ||
| remoteFileSize: 9999, | ||
| isCheckRemoteFileSize: true, | ||
| fileExistsAction, | ||
| (_, _, _) => { }); | ||
|
|
||
| Assert.False(shouldContinue); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void DoFileExistsActionBeforeDownload_ContinuesConvertedEquivalent_WhenAlwaysReplace() | ||
| { | ||
| string requestedPath = Path.Combine(_temporaryDirectoryPath, "media_123.png"); | ||
| string existingPath = Path.Combine(_temporaryDirectoryPath, "media_123.mp3"); | ||
| File.WriteAllText(existingPath, "converted-content"); | ||
|
|
||
| bool shouldContinue = FileExistsActionHelper.DoFileExistsActionBeforeDownload( | ||
| existingPath, | ||
| requestedPath, | ||
| remoteFileSize: 9999, | ||
| isCheckRemoteFileSize: true, | ||
| FileExistsAction.AlwaysReplace, | ||
| (_, _, _) => { }); | ||
|
|
||
| Assert.True(shouldContinue); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void DoFileExistsActionBeforeDownload_UsesSizeCheck_ForExactPathMatch() | ||
| { | ||
| string requestedPath = Path.Combine(_temporaryDirectoryPath, "media_123.png"); | ||
| File.WriteAllText(requestedPath, "12345"); | ||
|
|
||
| bool shouldContinueWhenDifferent = FileExistsActionHelper.DoFileExistsActionBeforeDownload( | ||
| requestedPath, | ||
| requestedPath, | ||
| remoteFileSize: 9999, | ||
| isCheckRemoteFileSize: true, | ||
| FileExistsAction.BackupIfDifferent, | ||
| (_, _, _) => { }); | ||
|
|
||
| bool shouldSkipWhenIdentical = FileExistsActionHelper.DoFileExistsActionBeforeDownload( | ||
| requestedPath, | ||
| requestedPath, | ||
| remoteFileSize: new FileInfo(requestedPath).Length, | ||
| isCheckRemoteFileSize: true, | ||
| FileExistsAction.BackupIfDifferent, | ||
| (_, _, _) => { }); | ||
|
|
||
| Assert.True(shouldContinueWhenDifferent); | ||
| Assert.False(shouldSkipWhenIdentical); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(FileExistsAction.BackupIfDifferent)] | ||
| [InlineData(FileExistsAction.ReplaceIfDifferent)] | ||
| public void DoFileExistsActionBeforeDownload_Continues_WhenRemoteSizeUnknown(FileExistsAction fileExistsAction) | ||
| { | ||
| string requestedPath = Path.Combine(_temporaryDirectoryPath, "media_123.png"); | ||
| File.WriteAllText(requestedPath, "12345"); | ||
|
|
||
| bool shouldContinue = FileExistsActionHelper.DoFileExistsActionBeforeDownload( | ||
| requestedPath, | ||
| requestedPath, | ||
| remoteFileSize: -1, | ||
| isCheckRemoteFileSize: true, | ||
| fileExistsAction, | ||
| (_, _, _) => { }); | ||
|
|
||
| Assert.True(shouldContinue); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void DoFileExistsActionBeforeDownload_Skips_WhenRemoteSizeUnknownAndKeepExisting() | ||
| { | ||
| string requestedPath = Path.Combine(_temporaryDirectoryPath, "media_123.png"); | ||
| File.WriteAllText(requestedPath, "12345"); | ||
|
|
||
| bool shouldContinue = FileExistsActionHelper.DoFileExistsActionBeforeDownload( | ||
| requestedPath, | ||
| requestedPath, | ||
| remoteFileSize: 0, | ||
| isCheckRemoteFileSize: true, | ||
| FileExistsAction.KeepExisting, | ||
| (_, _, _) => { }); | ||
|
|
||
| Assert.False(shouldContinue); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ResolveExistingFilePath_MatchesBasename_IgnoringCase_WhenFilesystemSurfacesCandidate() | ||
| { | ||
| // Create both casings when the filesystem allows it. On case-insensitive systems | ||
| // only one file entry exists and Directory.EnumerateFiles returns it for either casing. | ||
| string lowerExistingPath = Path.Combine(_temporaryDirectoryPath, "media_123.mp3"); | ||
| string mixedRequestedPath = Path.Combine(_temporaryDirectoryPath, "Media_123.png"); | ||
| File.WriteAllText(lowerExistingPath, "test"); | ||
|
|
||
| string resolvedPath = FileExistsActionHelper.ResolveExistingFilePath(mixedRequestedPath); | ||
|
|
||
| // On case-insensitive filesystems the candidate is found and basename compare must ignore case. | ||
| // On case-sensitive filesystems only an exact casing match is expected from the filesystem listing. | ||
| if (resolvedPath != null) | ||
| { | ||
| Assert.Equal(Path.GetFileNameWithoutExtension(lowerExistingPath), Path.GetFileNameWithoutExtension(resolvedPath), ignoreCase: true); | ||
| Assert.Equal(Path.GetExtension(lowerExistingPath), Path.GetExtension(resolvedPath), ignoreCase: true); | ||
| Assert.False(string.Equals(Path.GetExtension(mixedRequestedPath), Path.GetExtension(resolvedPath), StringComparison.OrdinalIgnoreCase)); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ResolveExistingFilePath_DoesNotTreatWildcardCharactersInBasenameAsGlobs() | ||
| { | ||
| string requestedPath = Path.Combine(_temporaryDirectoryPath, "media*123.png"); | ||
| string unrelatedMatchPath = Path.Combine(_temporaryDirectoryPath, "mediaX123.mp3"); | ||
| string exactConvertedPath = Path.Combine(_temporaryDirectoryPath, "media*123.mp3"); | ||
| File.WriteAllText(unrelatedMatchPath, "unrelated"); | ||
| File.WriteAllText(exactConvertedPath, "converted"); | ||
|
|
||
| string resolvedPath = FileExistsActionHelper.ResolveExistingFilePath(requestedPath); | ||
|
|
||
| Assert.Equal(exactConvertedPath, resolvedPath); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ResolveExistingFilePath_ReturnsNull_WhenWildcardBasenameHasOnlyUnrelatedMatches() | ||
| { | ||
| string requestedPath = Path.Combine(_temporaryDirectoryPath, "media?123.png"); | ||
| string unrelatedMatchPath = Path.Combine(_temporaryDirectoryPath, "mediaA123.mp3"); | ||
| File.WriteAllText(unrelatedMatchPath, "unrelated"); | ||
|
|
||
| string resolvedPath = FileExistsActionHelper.ResolveExistingFilePath(requestedPath); | ||
|
|
||
| Assert.Null(resolvedPath); | ||
| } | ||
|
|
||
| public void Dispose() | ||
| { | ||
| if (Directory.Exists(_temporaryDirectoryPath)) | ||
| Directory.Delete(_temporaryDirectoryPath, true); | ||
| } | ||
| } | ||
| } |
18 changes: 18 additions & 0 deletions
18
UniversalDownloaderPlatform.Common.Tests/UniversalDownloaderPlatform.Common.Tests.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net9.0</TargetFramework> | ||
| <IsPackable>false</IsPackable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" /> | ||
| <PackageReference Include="xunit" Version="2.9.2" /> | ||
| <PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\UniversalDownloaderPlatform.Common\UniversalDownloaderPlatform.Common.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.