Skip to content

Commit

Permalink
UWP unit tests framework has been changed from NUnit to MSTest. Becau…
Browse files Browse the repository at this point in the history
…se for UWP only MSTests work well. Fixing issue PCLExt#17
  • Loading branch information
ArtjomP committed Feb 7, 2018
1 parent 8adde1c commit 59328c8
Show file tree
Hide file tree
Showing 8 changed files with 655 additions and 100 deletions.
4 changes: 2 additions & 2 deletions src/PCLExt.FileStorage.UWP/Extensions/StorageExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static Windows.Storage.NameCollisionOption ConvertToNameCollision(
return Windows.Storage.NameCollisionOption.FailIfExists;

if (collisionOption == NameCollisionOption.GenerateUniqueName)
return Windows.Storage.NameCollisionOption.GenerateUniqueName;
return Windows.Storage.NameCollisionOption.FailIfExists;

if (collisionOption == NameCollisionOption.ReplaceExisting)
return Windows.Storage.NameCollisionOption.ReplaceExisting;
Expand All @@ -29,7 +29,7 @@ public static CreationCollisionOption ConvertToCreationCollision(
if (collisionOption == NameCollisionOption.FailIfExists)
return CreationCollisionOption.FailIfExists;
if (collisionOption == NameCollisionOption.GenerateUniqueName)
return CreationCollisionOption.GenerateUniqueName;
return CreationCollisionOption.FailIfExists;
if (collisionOption == NameCollisionOption.ReplaceExisting)
return CreationCollisionOption.ReplaceExisting;

Expand Down
79 changes: 52 additions & 27 deletions src/PCLExt.FileStorage.UWP/StorageFileImplementation.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using PCLExt.FileStorage.Extensions;
using PCLExt.FileStorage.UWP.Extensions;
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -7,8 +9,6 @@
using Windows.Storage.FileProperties;

using PCLExt.FileStorage.Exceptions;
using PCLExt.FileStorage.Extensions;
using PCLExt.FileStorage.UWP.Extensions;

namespace PCLExt.FileStorage.UWP
{
Expand Down Expand Up @@ -36,7 +36,7 @@ public bool Exists
}
}

public long Size => (long) GetStorageFileProperties().Size;
public long Size => (long)GetStorageFileProperties().Size;

public DateTime CreationTime =>
_storageFile.DateCreated.ToLocalTime().DateTime;
Expand Down Expand Up @@ -121,10 +121,32 @@ await _storageFile.CopyAsync(newFolder, newFile.Name).

var newFolder = await StorageFolder.GetFolderFromPathAsync(
System.IO.Path.GetDirectoryName(newPath));
var newName = System.IO.Path.GetFileName(newPath);
var initialNewName = System.IO.Path.GetFileName(newPath);
var newName = initialNewName;
var windowsNameCollision = StorageExtensions.ConvertToNameCollision(collisionOption);
var newFile = await _storageFile.CopyAsync(
newFolder, newName, windowsNameCollision);
var nameCollisionCounter = 2;
StorageFile newFile = null;
while (true)
{
try
{
newFile = await _storageFile.CopyAsync(
newFolder, newName, windowsNameCollision);
break;
}
catch (Exception ex)
{
if (collisionOption != NameCollisionOption.GenerateUniqueName)
{
if (ex.Message.Contains("HRESULT: 0x800700B7"))
throw new Exceptions.FileExistException(newPath, ex);
else
throw;
}
newName = $"{initialNewName} ({nameCollisionCounter++})";
}

}
return new StorageFileImplementation(newFile.Path);
}

Expand All @@ -135,7 +157,14 @@ public void Delete()

public async Task DeleteAsync(CancellationToken cancellationToken = default(CancellationToken))
{
await _storageFile.DeleteAsync().AsTask(cancellationToken);
try
{
await _storageFile.DeleteAsync().AsTask(cancellationToken);
}
catch (System.IO.FileNotFoundException ex)
{
throw new Exceptions.FileNotFoundException(_storageFile.Path, ex);
}
}

public void Move(IFile newFile)
Expand Down Expand Up @@ -167,30 +196,26 @@ await MoveAsync(
var newFolder = await StorageFolder.GetFolderFromPathAsync(
System.IO.Path.GetDirectoryName(newPath));

string newName;
if (collisionOption == NameCollisionOption.GenerateUniqueName)
{
newName = $"{Guid.NewGuid()}";
var extension = PortablePath.GetExtension(newPath);
if (!string.IsNullOrEmpty(extension))
newName = newName + "." + extension;
}
else
{
newName = System.IO.Path.GetFileName(newPath);
}

var windowsCollisionOption = StorageExtensions.
ConvertToNameCollision(collisionOption);

var oldFile = await StorageFile.GetFileFromPathAsync(_storageFile.Path);
try
var initialName = System.IO.Path.GetFileName(newPath);
var newName = initialName;
var nameCollisionCounter = 2;
while (true)
{
await _storageFile.MoveAsync(newFolder, newName, windowsCollisionOption);
}
catch (Exception ex)
{
throw new FileExistException(newPath, ex);
try
{
await _storageFile.MoveAsync(newFolder, newName, windowsCollisionOption);
break;
}
catch (Exception ex)
{
if (collisionOption != NameCollisionOption.GenerateUniqueName)
throw new FileExistException(newPath, ex);
newName = $"{initialName} ({nameCollisionCounter++})";
}
}
var result = new StorageFileImplementation(_storageFile);
_storageFile = oldFile;
Expand Down
56 changes: 46 additions & 10 deletions src/PCLExt.FileStorage.UWP/StorageFolderImplementation.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -90,7 +91,7 @@ public async Task<ExistenceCheckResult> CheckExistsAsync(
return ExistenceCheckResult.NotFound;
}

if (finded.Attributes == FileAttributes.Directory)
if (finded.Attributes == Windows.Storage.FileAttributes.Directory)
{
return ExistenceCheckResult.FolderExists;
}
Expand Down Expand Up @@ -124,7 +125,6 @@ private async Task CopyAsync(
NameCollisionOption option = NameCollisionOption.ReplaceExisting,
CancellationToken cancellationToken = default(CancellationToken))
{
var creationCollisionOption = StorageExtensions.ConvertToCreationCollision(option);

// Get all files (shallow) from source
var queryOptions = new QueryOptions
Expand All @@ -151,9 +151,26 @@ private async Task CopyAsync(
findedFile.Path == storageFile.Path)
continue;
}
var copiedFile = await storageFile.CopyAsync(targetStorageFolder,
storageFile.Name,
windowsOption);

var nameCollisionCounter = 2;
var filename = storageFile.Name;
while (true)
{
try
{
var copiedFile = await storageFile.CopyAsync(
targetStorageFolder,
filename,
windowsOption);
break;
}
catch (Exception)
{
if(option != NameCollisionOption.GenerateUniqueName)
throw;
filename = $"{filename} ({nameCollisionCounter++})";
}
}
}
catch (Exception ex)
{
Expand All @@ -165,11 +182,30 @@ private async Task CopyAsync(
var queryFolders = source.CreateFolderQueryWithOptions(queryOptions);
var folders = await queryFolders.GetFoldersAsync();

var creationCollisionOption = StorageExtensions.ConvertToCreationCollision(option);

// For each folder call CopyAsync with new destination as destination
foreach (var storageFolder in folders)
{
var targetFolder = await folder.CreateFolderAsync(storageFolder.Name,
creationCollisionOption, cancellationToken) as StorageFolderImplementation;
var nameCollisionCounter = 2;
var subfolderName = storageFolder.Name;
StorageFolderImplementation targetFolder = null;
while (true)
{
try
{

targetFolder = await folder.CreateFolderAsync(subfolderName,
creationCollisionOption, cancellationToken) as StorageFolderImplementation;
break;
}
catch (FolderExistException)
{
if (option != NameCollisionOption.GenerateUniqueName)
throw;
subfolderName = $"{storageFolder.Name} ({nameCollisionCounter++})";
}
}

if (targetFolder == null)
throw new Exception("Can't create StorageFolder");
Expand Down Expand Up @@ -240,7 +276,7 @@ public void Delete()
public async Task DeleteAsync(
CancellationToken cancellationToken = default(CancellationToken))
{
if(Windows.ApplicationModel.Package.Current.InstalledLocation.Path == _storageFolder.Path)
if (Windows.ApplicationModel.Package.Current.InstalledLocation.Path == _storageFolder.Path)
{
throw new RootFolderDeletionException("Cannot delete root storage folder.");
}
Expand All @@ -249,7 +285,7 @@ public async Task DeleteAsync(
{
await _storageFolder.DeleteAsync().AsTask(cancellationToken);
}
catch(Exception ex)
catch (Exception ex)
{
throw new FolderNotFoundException(_storageFolder.Path, ex);
}
Expand All @@ -272,7 +308,7 @@ public async Task<IFile> GetFileAsync(
}
catch (Exception ex)
{
throw new FileNotFoundException(name, ex);
throw new Exceptions.FileNotFoundException(name, ex);
}

}
Expand Down
Loading

0 comments on commit 59328c8

Please sign in to comment.