public async Task CopyFileWithReplacement() { const string Src = "src"; const string Target = "target"; const string OriginalContents = "Your ad here."; const string NewContents = "This is your ad."; File.WriteAllText(GetFullPath(Src), OriginalContents); await FileUtilities.CopyFileAsync(GetFullPath(Src), GetFullPath(Target)); XAssert.AreEqual(OriginalContents, File.ReadAllText(GetFullPath(Target))); File.WriteAllText(GetFullPath(Src), NewContents); await FileUtilities.CopyFileAsync(GetFullPath(Src), GetFullPath(Target)); XAssert.AreEqual(NewContents, File.ReadAllText(GetFullPath(Target))); }
public async Task CopyPredicate() { const string Src = "src"; const string Target = "target"; File.WriteAllText(GetFullPath(Src), "Source"); XAssert.IsFalse(File.Exists(GetFullPath(Target))); bool copied = await FileUtilities.CopyFileAsync( GetFullPath(Src), GetFullPath(Target), (source, dest) => { XAssert.IsNull(dest, "Destination handle should be null if the file does not exist"); return(true); }); XAssert.IsTrue(File.Exists(GetFullPath(Target))); XAssert.IsTrue(copied); XAssert.AreEqual("Source", File.ReadAllText(GetFullPath(Target))); File.WriteAllText(GetFullPath(Target), "Leave this here"); copied = await FileUtilities.CopyFileAsync( GetFullPath(Src), GetFullPath(Target), (source, dest) => false); XAssert.IsTrue(File.Exists(GetFullPath(Target))); XAssert.IsFalse(copied); XAssert.AreEqual("Leave this here", File.ReadAllText(GetFullPath(Target))); copied = await FileUtilities.CopyFileAsync( GetFullPath(Src), GetFullPath(Target), (source, dest) => true); XAssert.IsTrue(File.Exists(GetFullPath(Target))); XAssert.IsTrue(copied); XAssert.AreEqual("Source", File.ReadAllText(GetFullPath(Target))); }
public async Task CopyCompletionCallback() { const string Src = "src"; const string Target = "target"; File.WriteAllText(GetFullPath(Src), "Source"); Usn closeUsn = default(Usn); var completionHandlerCalled = false; await FileUtilities.CopyFileAsync( GetFullPath(Src), GetFullPath(Target), onCompletion : (source, dest) => { completionHandlerCalled = true; if (!OperatingSystemHelper.IsUnixOS) { Usn?maybeCloseUsn = FileUtilities.TryWriteUsnCloseRecordByHandle(dest); XAssert.IsNotNull(maybeCloseUsn); closeUsn = maybeCloseUsn.Value; } }); XAssert.IsTrue(completionHandlerCalled); XAssert.IsTrue(File.Exists(GetFullPath(Target))); XAssert.AreEqual("Source", File.ReadAllText(GetFullPath(Target))); if (!OperatingSystemHelper.IsUnixOS) { XAssert.AreNotEqual(0, closeUsn, "Completion callback skipped"); using (FileStream dest = File.OpenRead(GetFullPath(Target))) { Usn usn = FileUtilities.ReadFileUsnByHandle(dest.SafeFileHandle).Value.Usn; XAssert.AreEqual(closeUsn, usn, "CLOSE usn should have been written during the callback."); } } }