Skip to content
This repository has been archived by the owner on Dec 6, 2024. It is now read-only.

Commit

Permalink
Update Directory CopyTimestamps unit test.
Browse files Browse the repository at this point in the history
  • Loading branch information
Yomodo committed Jul 9, 2018
1 parent f01a84a commit 51c6c50
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
*/

using System;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AlphaFS.UnitTest
Expand All @@ -37,37 +36,39 @@ public void AlphaFS_Directory_CopyTimestamps_LocalAndNetwork_Success()
AlphaFS_Directory_CopyTimestamps(true);
}


private void AlphaFS_Directory_CopyTimestamps(bool isNetwork)
{
using (var tempRoot = new TemporaryDirectory(isNetwork))
{
var folder1 = tempRoot.CreateDirectoryRandomizedAttributes();
var folderSrc = tempRoot.CreateRecursiveRandomizedDatesAndAttributesTree(5);
var folderDst = tempRoot.CreateDirectory();

Console.WriteLine("Src Directory Path: [{0}]", folderSrc.FullName);
Console.WriteLine("Dst Directory Path: [{0}]", folderDst);


Thread.Sleep(1500);
var creationTime = tempRoot.GetRandomFileDate();
var lastAccessTime = tempRoot.GetRandomFileDate();
var lastWriteTime = tempRoot.GetRandomFileDate();

var folder2 = tempRoot.CreateDirectoryRandomizedAttributes();


Console.WriteLine("Input Directory1 Path: [{0}]", folder1.FullName);
Console.WriteLine("Input Directory2 Path: [{0}]", folder2.FullName);
Alphaleonis.Win32.Filesystem.Directory.SetTimestamps(folderSrc.FullName, creationTime, lastAccessTime, lastWriteTime);

Assert.AreEqual(System.IO.Directory.GetCreationTimeUtc(folderSrc.FullName), folderSrc.CreationTimeUtc);
Assert.AreEqual(System.IO.Directory.GetLastAccessTimeUtc(folderSrc.FullName), folderSrc.LastAccessTimeUtc);
Assert.AreEqual(System.IO.Directory.GetLastWriteTimeUtc(folderSrc.FullName), folderSrc.LastWriteTimeUtc);

Assert.AreNotEqual(System.IO.Directory.GetCreationTime(folder1.FullName), System.IO.Directory.GetCreationTime(folder2.FullName));
Assert.AreNotEqual(System.IO.Directory.GetLastAccessTime(folder1.FullName), System.IO.Directory.GetLastAccessTime(folder2.FullName));
Assert.AreNotEqual(System.IO.Directory.GetLastWriteTime(folder1.FullName), System.IO.Directory.GetLastWriteTime(folder2.FullName));


Alphaleonis.Win32.Filesystem.Directory.CopyTimestamps(folder1.FullName, folder2.FullName);
Alphaleonis.Win32.Filesystem.Directory.CopyTimestamps(folderSrc.FullName, folderDst.FullName);


UnitTestConstants.Dump(folder1);
UnitTestConstants.Dump(folder2);
UnitTestConstants.Dump(folderDst);


Assert.AreEqual(System.IO.Directory.GetCreationTime(folder1.FullName), System.IO.Directory.GetCreationTime(folder2.FullName));
Assert.AreEqual(System.IO.Directory.GetLastAccessTime(folder1.FullName), System.IO.Directory.GetLastAccessTime(folder2.FullName));
Assert.AreEqual(System.IO.Directory.GetLastWriteTime(folder1.FullName), System.IO.Directory.GetLastWriteTime(folder2.FullName));
Assert.AreEqual(System.IO.Directory.GetCreationTimeUtc(folderSrc.FullName), folderDst.CreationTimeUtc);
Assert.AreEqual(System.IO.Directory.GetLastAccessTimeUtc(folderSrc.FullName), folderDst.LastAccessTimeUtc);
Assert.AreEqual(System.IO.Directory.GetLastWriteTimeUtc(folderSrc.FullName), folderDst.LastWriteTimeUtc);
}

Console.WriteLine();
Expand Down
158 changes: 91 additions & 67 deletions AlphaFS.UnitTest/UnitTest Utility/TemporaryDirectory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ internal sealed class TemporaryDirectory : IDisposable
#region Constructors

public TemporaryDirectory() : this(false) { }


public TemporaryDirectory(bool isNetwork, string folderPrefix = null, string root = null)
{
Expand Down Expand Up @@ -99,8 +99,8 @@ public string RandomFileNoExtensionFullPath
{
get { return System.IO.Path.Combine(Directory.FullName, System.IO.Path.GetFileNameWithoutExtension(RandomTxtFileName)); }
}


/// <summary>Returns a random string of 8 characters in length, possibly with diacritic characters.</summary>
public string RandomString
{
Expand Down Expand Up @@ -147,106 +147,89 @@ public System.IO.DirectoryInfo CreateDirectory(string directoryNameSuffix)
/// <summary>Returns a <see cref="System.IO.DirectoryInfo"/> instance to an existing directory, possibly with read-only and/or hidden attributes set.</summary>
public System.IO.DirectoryInfo CreateDirectoryRandomizedAttributes()
{
return CreateDirectoryCore(null, true, true);
return CreateDirectoryCore(null, false, true, true);
}


/// <summary>Returns a <see cref="System.IO.DirectoryInfo"/> instance to an existing directory, possibly with read-only and/or hidden attributes set.</summary>
public System.IO.DirectoryInfo CreateDirectoryRandomizedAttributes(string directoryNameSuffix)
{
return CreateDirectoryCore(Directory.FullName + directoryNameSuffix, true, true);
return CreateDirectoryCore(Directory.FullName + directoryNameSuffix, false, true, true);
}




/// <summary>Returns a <see cref="System.IO.FileInfo"/> instance to an existing file.</summary>
public System.IO.FileInfo CreateFile()
{
return CreateFileCore(null);
}


/// <summary>Returns a <see cref="System.IO.DirectoryInfo"/> instance to an existing file, possibly with read-only and/or hidden attributes set.</summary>
/// <summary>Returns a <see cref="System.IO.FileInfo"/> instance to an existing file, possibly with read-only and/or hidden attributes set.</summary>
public System.IO.FileInfo CreateFileRandomizedAttributes()
{
return CreateFileCore(null, true, true);
return CreateFileCore(null, false, true, true);
}




/// <summary>Creates a directory structure of <param name="level"/> levels deep, populated with subdirectories and files with of random size.</summary>
/// <summary>Creates a directory structure populated with subdirectories and files of random size.</summary>
public System.IO.DirectoryInfo CreateTree(int level = 1)
{
return CreateTreeCore(null, level);
}


/// <summary>Creates a recursive directory structure of <param name="level"/> levels deep, populated with subdirectories and files with of random size.</summary>
/// <summary>Creates a recursive directory structure populated with subdirectories and files of random size.</summary>
public System.IO.DirectoryInfo CreateRecursiveTree(int level = 1)
{
return CreateTreeCore(null, level, true);
}


/// <summary>Creates a recursive directory structure of <param name="level"/> levels deep, populated with subdirectories and files with of random size.</summary>
/// <summary>Creates a recursive directory structure populated with subdirectories and files of random size.</summary>
public System.IO.DirectoryInfo CreateRecursiveTree(int level, string rootFullPath)
{
return CreateTreeCore(rootFullPath, level, true);
}


/// <summary>Creates a directory structure of <param name="level"/> levels deep, populated with subdirectories and files with of random size.</summary>
/// <summary>Creates a directory structure populated with subdirectories and files of random size and possibly with read-only and/or hidden attributes set.</summary>
public System.IO.DirectoryInfo CreateRandomizedAttributesTree(int level = 1)
{
return CreateTreeCore(null, level, false, true, true);
return CreateTreeCore(null, level, false, false, true, true);
}


/// <summary>Creates a recursive directory structure of <param name="level"/> levels deep, populated with subdirectories and files with of random size.</summary>
/// <summary>Creates a recursive directory structure populated with subdirectories and files of random size and possibly with read-only and/or hidden attributes set.</summary>
public System.IO.DirectoryInfo CreateRecursiveRandomizedAttributesTree(int level = 1)
{
return CreateTreeCore(null, level, true, true, true);
return CreateTreeCore(null, level, true, false, true, true);
}


/// <summary>Creates an, optional recursive, directory structure of <param name="level"/> levels deep, populated with subdirectories and files with of random size and possibly with read-only and/or hidden attributes set.</summary>
private System.IO.DirectoryInfo CreateTreeCore(string rootFullPath, int level = 1, bool recurse = false, bool readOnly = false, bool hidden = false)
{
var dirInfo = CreateDirectoryCore(rootFullPath, readOnly, hidden);

var folderCount = 0;


for (var fsoCount = 0; fsoCount < level; fsoCount++)
{
folderCount++;

var fsoName = RandomString + "-" + fsoCount;

// Always create folder.
var di = CreateDirectoryCore(System.IO.Path.Combine(dirInfo.FullName, string.Format(CultureInfo.InvariantCulture, "Directory_{0}_directory", fsoName)), readOnly, hidden);

// Create file, every other iteration.
CreateFileCore(System.IO.Path.Combine(fsoCount % 2 == 0 ? di.FullName : dirInfo.FullName, string.Format(CultureInfo.InvariantCulture, "File_{0}_file.txt", fsoName)), readOnly, hidden);
}


if (recurse)
{
foreach (var folder in System.IO.Directory.EnumerateDirectories(dirInfo.FullName))
CreateTreeCore(folder, level, false, readOnly, hidden);
}
/// <summary>Creates a recursive directory structure populated with subdirectories and files, possibly with randomized CreationTime, LastAccessTime and/or LastWriteTime.</summary>
public System.IO.DirectoryInfo CreateRecursiveRandomizedDatesTree(int level = 1)
{
return CreateTreeCore(null, level, true, true);
}

/// <summary>Creates a recursive directory structure populated with subdirectories and files, possibly with randomized CreationTime, LastAccessTime and/or LastWriteTime.
/// The file size, read-only and/or hidden attributes might also be randomized.
/// </summary>
public System.IO.DirectoryInfo CreateRecursiveRandomizedDatesAndAttributesTree(int level = 1)
{
return CreateTreeCore(null, level, true, true, true, true);
}

Assert.AreEqual(level, folderCount, "The number of folders does not equal the level folder-level, but is expected to.");

return dirInfo;
public DateTime GetRandomFileDate()
{
var rnd = new Random(DateTime.Now.Millisecond);
return new DateTime(rnd.Next(1971, DateTime.Now.Year), rnd.Next(1, 12), rnd.Next(1, 28), rnd.Next(0, 23), rnd.Next(0, 59), rnd.Next(0, 59));
}


/// <summary> Enables or disables deny access for the current User.</summary>
/// <summary>Enables or disables deny access for the current User.</summary>
public void SetDirectoryDenyPermission(bool enable, string folderFullPath)
{
// ╔═════════════╦═════════════╦═══════════════════════════════╦════════════════════════╦══════════════════╦═══════════════════════╦═════════════╦═════════════╗
Expand Down Expand Up @@ -298,32 +281,81 @@ public override string ToString()


/// <summary>Returns a <see cref="System.IO.DirectoryInfo"/> instance to an existing directory, possibly with read-only and/or hidden attributes set.</summary>
private System.IO.DirectoryInfo CreateDirectoryCore(string folderFullPath, bool readOnly = false, bool hidden = false)
private System.IO.DirectoryInfo CreateDirectoryCore(string folderFullPath, bool randomizedDates = false, bool readOnly = false, bool hidden = false)
{
var dirInfo = System.IO.Directory.CreateDirectory(!Alphaleonis.Utils.IsNullOrWhiteSpace(folderFullPath) ? folderFullPath : RandomDirectoryFullPath);

SetAttributes(dirInfo, readOnly, hidden);
SetRandomizedDates(dirInfo, randomizedDates);

SetReadOnlyAndOrHiddenAttributes(dirInfo, readOnly, hidden);

return dirInfo;
}


/// <summary>Returns a <see cref="System.IO.FileInfo"/> instance to an existing file, possibly with read-only and/or hidden attributes set.</summary>
private System.IO.FileInfo CreateFileCore(string fileFullPath, bool readOnly = false, bool hidden = false)
private System.IO.FileInfo CreateFileCore(string fileFullPath, bool randomizedDates = false, bool readOnly = false, bool hidden = false)
{
var fileInfo = new System.IO.FileInfo(!Alphaleonis.Utils.IsNullOrWhiteSpace(fileFullPath) ? fileFullPath : RandomTxtFileFullPath);

// File size is min 0 bytes, level 1 MB.
using (var fs = fileInfo.Create())
fs.SetLength(new Random(DateTime.UtcNow.Millisecond).Next(0, 1048576));
fs.SetLength(new Random(DateTime.UtcNow.Millisecond).Next(0, UnitTestConstants.OneMebibyte));

SetAttributes(fileInfo, readOnly, hidden);
SetRandomizedDates(fileInfo, randomizedDates);

SetReadOnlyAndOrHiddenAttributes(fileInfo, readOnly, hidden);

return fileInfo;
}


private static void SetAttributes(System.IO.FileSystemInfo fsi, bool readOnly = false, bool hidden = false)
/// <summary>Creates an, optional recursive, directory structure of <param name="level"/> levels deep, populated with subdirectories and files of random size and possibly with read-only and/or hidden attributes set.</summary>
private System.IO.DirectoryInfo CreateTreeCore(string rootFullPath, int level = 1, bool recurse = false, bool randomizedDates = false, bool readOnly = false, bool hidden = false)
{
var dirInfo = CreateDirectoryCore(rootFullPath, randomizedDates, readOnly, hidden);

var folderCount = 0;


for (var fsoCount = 0; fsoCount < level; fsoCount++)
{
folderCount++;

var fsoName = RandomString + "-" + fsoCount;

// Always create folder.
var di = CreateDirectoryCore(System.IO.Path.Combine(dirInfo.FullName, string.Format(CultureInfo.InvariantCulture, "Directory_{0}_directory", fsoName)), randomizedDates, readOnly, hidden);

// Create file, every other iteration.
CreateFileCore(System.IO.Path.Combine(fsoCount % 2 == 0 ? di.FullName : dirInfo.FullName, string.Format(CultureInfo.InvariantCulture, "File_{0}_file.txt", fsoName)), randomizedDates, readOnly, hidden);
}


if (recurse)
{
foreach (var folder in System.IO.Directory.EnumerateDirectories(dirInfo.FullName))
CreateTreeCore(folder, level, false, randomizedDates, readOnly, hidden);
}


Assert.AreEqual(level, folderCount, "The number of folders does not equal level argument, but is expected to.");

return dirInfo;
}


private void SetRandomizedDates(System.IO.FileSystemInfo fsi, bool randomizedDates = false)
{
if (randomizedDates && new Random(DateTime.UtcNow.Millisecond).Next(0, 1000) % 2 == 0)
{
fsi.CreationTime = GetRandomFileDate();
fsi.LastAccessTime = GetRandomFileDate();
fsi.LastWriteTime = GetRandomFileDate();
}
}


private static void SetReadOnlyAndOrHiddenAttributes(System.IO.FileSystemInfo fsi, bool readOnly = false, bool hidden = false)
{
if (readOnly && new Random(DateTime.UtcNow.Millisecond).Next(0, 1000) % 2 == 0)
fsi.Attributes |= System.IO.FileAttributes.ReadOnly;
Expand Down Expand Up @@ -357,26 +389,18 @@ private void Dispose(bool isDisposing)
if (isDisposing)
System.IO.Directory.Delete(Directory.FullName, true);
}
catch (Exception ex)
catch
{
Console.WriteLine("\n\nDelete TemporaryDirectory: [{0}]. Error: [{1}]", Directory.FullName, ex.Message.Replace(Environment.NewLine, string.Empty));
Console.Write("Retry using AlphaFS... ");

try
{
var dirInfo = new Alphaleonis.Win32.Filesystem.DirectoryInfo(Directory.FullName, Alphaleonis.Win32.Filesystem.PathFormat.FullPath);

if (dirInfo.Exists)
{
dirInfo.Delete(true, true);
Console.WriteLine("Success.");
}

else
Console.WriteLine("TemporaryDirectory was already removed.");
}
catch (Exception ex2)
catch (Exception ex)
{
Console.WriteLine("Delete failure TemporaryDirectory. Error: {0}", ex2.Message.Replace(Environment.NewLine, string.Empty));
Console.WriteLine("TemporaryDirectory delete failure. Error: {0}", ex.Message.Replace(Environment.NewLine, string.Empty));
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions AlphaFS.UnitTest/UnitTest Utility/UnitTestConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public static partial class UnitTestConstants
public static readonly string SysDrive = Environment.GetEnvironmentVariable("SystemDrive");


public const int OneMebibyte = 1048576;


public static readonly string[] StreamArrayContent =
{
"(1) Nikolai Tesla: \"Today's scientists have substituted mathematics for experiments, and they wander off through equation after equation, and eventually build a structure which has no relation to reality.\"",
Expand Down

0 comments on commit 51c6c50

Please sign in to comment.