Skip to content

Commit cb7da7d

Browse files
author
LoneWandererProductions
committed
small touchups
1 parent 1233abc commit cb7da7d

3 files changed

Lines changed: 31 additions & 48 deletions

File tree

CommonLibraryTests/IoFileHandler.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ public async Task CutFilesValidSourceAndTargetCutsFilesAsync()
600600
File.WriteAllText(sourceFilePath, "Test Content");
601601

602602
// Act
603-
var result = FileHandleCut.CutFiles(TestSourceDir, TestTargetDir, true);
603+
var result = await FileHandleCut.CutFiles(TestSourceDir, TestTargetDir, true);
604604

605605
// Assert
606606
Assert.IsTrue(result);
@@ -615,9 +615,9 @@ public async Task CutFilesValidSourceAndTargetCutsFilesAsync()
615615
/// </summary>
616616
[TestMethod]
617617
[ExpectedException(typeof(FileHandlerException))]
618-
public void CutFilesSourceAndTargetEqualThrowsFileHandlerException()
618+
public async Task CutFilesSourceAndTargetEqualThrowsFileHandlerExceptionAsync()
619619
{
620-
_ = FileHandleCut.CutFiles(TestSourceDir, TestSourceDir, true);
620+
await FileHandleCut.CutFiles(TestSourceDir, TestSourceDir, true);
621621
}
622622

623623
/// <summary>
@@ -626,7 +626,7 @@ public void CutFilesSourceAndTargetEqualThrowsFileHandlerException()
626626
[TestMethod]
627627
public async Task CutFilesNonExistentSourceReturnsFalseAsync()
628628
{
629-
var result = FileHandleCut.CutFiles("NonExistentSource", TestTargetDir, true);
629+
var result = await FileHandleCut.CutFiles("NonExistentSource", TestTargetDir, true);
630630
Assert.IsFalse(result);
631631
}
632632

@@ -686,7 +686,7 @@ public async Task CutFilesWithOverwriteCutsFilesSuccessfullyAsync()
686686
File.WriteAllText(targetFilePath, "Old Content");
687687

688688
// Act
689-
var result = FileHandleCut.CutFiles(TestSourceDir, TestTargetDir, true);
689+
var result = await FileHandleCut.CutFiles(TestSourceDir, TestTargetDir, true);
690690

691691
// Assert
692692
Assert.IsTrue(result);
@@ -708,7 +708,7 @@ public async Task CutFilesWithoutOverwriteExistingFilesNotOverwrittenAsync()
708708
File.WriteAllText(targetFilePath, "Old Content");
709709

710710
// Act
711-
var result = FileHandleCut.CutFiles(TestSourceDir, TestTargetDir, false);
711+
var result = await FileHandleCut.CutFiles(TestSourceDir, TestTargetDir, false);
712712

713713
// Assert, show false, since we have not moved all files
714714
Assert.IsFalse(result);
@@ -730,7 +730,7 @@ public async Task CutFilesHandlesSubdirectoriesAsync()
730730
File.WriteAllText(sourceFilePath, "Test Content");
731731

732732
// Act
733-
var result = FileHandleCut.CutFiles(TestSourceDir, TestTargetDir, true);
733+
var result = await FileHandleCut.CutFiles(TestSourceDir, TestTargetDir, true);
734734

735735
// Assert
736736
Assert.IsTrue(result);

FileHandler/FileHandleCut.cs

Lines changed: 24 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using System.Diagnostics;
1515
using System.IO;
1616
using System.Linq;
17+
using System.Threading.Tasks;
1718

1819
namespace FileHandler;
1920

@@ -31,75 +32,58 @@ public static class FileHandleCut
3132
/// <param name="overwrite">Is overwrite allowed</param>
3233
/// <returns>Status if we encountered any problems</returns>
3334
/// <exception cref="FileHandlerException">No Correct Path was provided</exception>
34-
public static bool CutFiles(string source, string target, bool overwrite)
35+
public static async Task<bool> CutFiles(string source, string target, bool overwrite)
3536
{
3637
FileHandlerProcessing.ValidatePaths(source, target);
3738

38-
//if nothing exists we can return anyways
3939
if (!Directory.Exists(source))
40-
{
4140
return false;
42-
}
4341

4442
var check = true;
4543
var dir = new DirectoryInfo(source);
4644
var dirs = dir.GetDirectories();
4745
var files = dir.GetFiles();
4846

49-
//Give the User Optional Infos about the Amount we Copy
50-
var lstFiles = (from file in files
51-
select file.Name).ToList();
47+
// Inform user
48+
var lstFiles = files.Select(f => f.Name).ToList();
49+
FileHandlerRegister.SendOverview?.Invoke(nameof(CutFiles),
50+
new FileItems { Elements = new List<string>(lstFiles), Message = FileHandlerResources.InformationFileDeletion });
5251

53-
var itm = new FileItems
52+
// Move files
53+
foreach (var file in files)
5454
{
55-
Elements = new List<string>(lstFiles), Message = FileHandlerResources.InformationFileDeletion
56-
};
57-
58-
FileHandlerRegister.SendOverview?.Invoke(nameof(CutFiles), itm);
59-
60-
//do the actual work
61-
if (files.Length > 0)
62-
{
63-
if (!Directory.Exists(target))
55+
var tempPath = Path.Combine(target, file.Name);
56+
try
6457
{
65-
_ = Directory.CreateDirectory(target);
66-
}
58+
if (!Directory.Exists(target))
59+
_ = Directory.CreateDirectory(target);
6760

68-
foreach (var file in files)
61+
await Task.Run(() => file.MoveTo(tempPath, overwrite)).ConfigureAwait(false);
62+
FileHandlerRegister.SendStatus?.Invoke(nameof(CutFiles), file.Name);
63+
}
64+
catch (Exception ex) when (ex is UnauthorizedAccessException or ArgumentException or IOException
65+
or NotSupportedException)
6966
{
70-
var tempPath = Path.Combine(target, file.Name);
71-
72-
try
73-
{
74-
file.MoveTo(tempPath, overwrite);
75-
76-
FileHandlerRegister.SendStatus?.Invoke(nameof(CutFiles), file.Name);
77-
}
78-
catch (Exception ex) when (ex is UnauthorizedAccessException or ArgumentException or IOException
79-
or NotSupportedException)
80-
{
81-
FileHandlerRegister.AddError(nameof(CutFiles), file.Name, ex);
82-
Trace.WriteLine(ex);
83-
check = false;
84-
}
67+
FileHandlerRegister.AddError(nameof(CutFiles), file.Name, ex);
68+
Trace.WriteLine(ex);
69+
check = false;
8570
}
8671
}
8772

73+
// Recurse subdirectories
8874
foreach (var subDir in dirs)
8975
{
9076
var tempPath = Path.Combine(target, subDir.Name);
9177

9278
if (!Directory.Exists(target))
93-
{
9479
_ = Directory.CreateDirectory(target);
95-
}
9680

9781
if (Directory.Exists(tempPath))
98-
{
9982
continue;
100-
}
10183

102-
_ = CutFiles(subDir.FullName, tempPath, overwrite);
84+
var subCheck = await CutFiles(subDir.FullName, tempPath, overwrite).ConfigureAwait(false);
85+
if (!subCheck)
86+
check = false;
10387
}
10488

10589
return check;

FileHandler/FileHandlerRessources.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ internal static class FileHandlerResources
8888
/// </summary>
8989
internal const string ErrorEqualPath = "Paths are equal Input.";
9090

91-
9291
/// <summary>
9392
/// The error file already exists (const). Value: "Error File already exists:".
9493
/// </summary>

0 commit comments

Comments
 (0)