本文整理汇总了C#中Microsoft.Build.Tasks.Copy.Execute方法的典型用法代码示例。如果您正苦于以下问题:C# Copy.Execute方法的具体用法?C# Copy.Execute怎么用?C# Copy.Execute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.Build.Tasks.Copy
的用法示例。
在下文中一共展示了Copy.Execute方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestCopy_MissingSourceFile
public void TestCopy_MissingSourceFile ()
{
Copy copy = new Copy ();
copy.BuildEngine = new TestEngine ();
copy.SourceFiles = new ITaskItem [1];
copy.SourceFiles [0] = new TaskItem ("SourceDoesNotExist");
copy.DestinationFiles = new ITaskItem [1];
copy.DestinationFiles [0] = new TaskItem ("DestDoesNotExist");
Assert.IsFalse (copy.Execute ());
}
示例2: CopyWithHardAndSymbolicLinks
public void CopyWithHardAndSymbolicLinks()
{
string sourceFile = FileUtilities.GetTemporaryFile();
string temp = Path.GetTempPath();
string destFolder = Path.Combine(temp, "2A333ED756AF4dc392E728D0F864A398");
string destFile = Path.Combine(destFolder, Path.GetFileName(sourceFile));
try
{
ITaskItem[] sourceFiles = { new TaskItem(sourceFile) };
MockEngine me = new MockEngine(true);
Copy t = new Copy
{
RetryDelayMilliseconds = 1, // speed up tests!
UseHardlinksIfPossible = true,
UseSymboliclinksIfPossible = true,
BuildEngine = me,
SourceFiles = sourceFiles,
DestinationFolder = new TaskItem(destFolder),
SkipUnchangedFiles = true
};
bool success = t.Execute();
Assert.False(success);
MockEngine.GetStringDelegate resourceDelegate = AssemblyResources.GetString;
me.AssertLogContainsMessageFromResource(resourceDelegate, "Copy.ExactlyOneTypeOfLink", "UseHardlinksIfPossible", "UseSymboliclinksIfPossible");
}
finally
{
Helpers.DeleteFiles(sourceFile, destFile);
}
}
示例3: OutputsOnlyIncludeSuccessfulCopies
public void OutputsOnlyIncludeSuccessfulCopies()
{
string temp = Path.GetTempPath();
string inFile1 = Path.Combine(temp, "2A333ED756AF4dc392E728D0F864A392");
string inFile2 = Path.Combine(temp, "2A333ED756AF4dc392E728D0F864A393");
string invalidFile = "[email protected]#$%^&*()|";
string validOutFile = Path.Combine(temp, "2A333ED756AF4dc392E728D0F864A394");
try
{
FileStream fs = null;
FileStream fs2 = null;
try
{
fs = File.Create(inFile1);
fs2 = File.Create(inFile2);
}
finally
{
fs.Close();
fs2.Close();
}
Copy t = new Copy();
t.RetryDelayMilliseconds = 1; // speed up tests!
// Allow the task's default (false) to have a chance
if (useHardLinks)
{
t.UseHardlinksIfPossible = useHardLinks;
}
MockEngine engine = new MockEngine();
t.BuildEngine = engine;
ITaskItem i1 = new TaskItem(inFile1);
i1.SetMetadata("Locale", "en-GB");
i1.SetMetadata("Color", "taupe");
t.SourceFiles = new ITaskItem[] { new TaskItem(inFile2), i1 };
ITaskItem o1 = new TaskItem(validOutFile);
o1.SetMetadata("Locale", "fr");
o1.SetMetadata("Flavor", "Pumpkin");
t.DestinationFiles = new ITaskItem[] { new TaskItem(invalidFile), o1 };
bool success = t.Execute();
Assert.IsTrue(!success);
Assert.AreEqual(1, t.CopiedFiles.Length);
Assert.AreEqual(validOutFile, t.CopiedFiles[0].ItemSpec);
Assert.AreEqual(2, t.DestinationFiles.Length);
Assert.AreEqual("fr", t.DestinationFiles[1].GetMetadata("Locale"));
// Output ItemSpec should not be overwritten.
Assert.AreEqual(invalidFile, t.DestinationFiles[0].ItemSpec);
Assert.AreEqual(validOutFile, t.DestinationFiles[1].ItemSpec);
Assert.AreEqual(validOutFile, t.CopiedFiles[0].ItemSpec);
// Sources attributes should be left untouched.
Assert.AreEqual("en-GB", t.SourceFiles[1].GetMetadata("Locale"));
Assert.AreEqual("taupe", t.SourceFiles[1].GetMetadata("Color"));
// Attributes not on Sources should be left untouched.
Assert.AreEqual("Pumpkin", t.DestinationFiles[1].GetMetadata("Flavor"));
Assert.AreEqual("Pumpkin", t.CopiedFiles[0].GetMetadata("Flavor"));
// Attribute should have been forwarded
Assert.AreEqual("taupe", t.DestinationFiles[1].GetMetadata("Color"));
Assert.AreEqual("taupe", t.CopiedFiles[0].GetMetadata("Color"));
// Attribute should not have been updated if it already existed on destination
Assert.AreEqual("fr", t.DestinationFiles[1].GetMetadata("Locale"));
Assert.AreEqual("fr", t.CopiedFiles[0].GetMetadata("Locale"));
((MockEngine)t.BuildEngine).AssertLogDoesntContain("MSB3026"); // Didn't do retries
}
finally
{
File.Delete(inFile1);
File.Delete(inFile2);
File.Delete(validOutFile);
}
}
示例4: DontCopyOverSameFile
public void DontCopyOverSameFile()
{
string file = FileUtilities.GetTemporaryFile();
try
{
using (StreamWriter sw = new StreamWriter(file, true)) // HIGHCHAR: Test writes in UTF8 without preamble.
sw.Write("This is a temp file.");
ITaskItem f = new TaskItem(file);
ITaskItem[] sourceFiles = new ITaskItem[] { f };
ITaskItem[] destinationFiles = new ITaskItem[] { f };
CopyMonitor m = new CopyMonitor();
Copy t = new Copy();
t.RetryDelayMilliseconds = 1; // speed up tests!
// Allow the task's default (false) to have a chance
if (useHardLinks)
{
t.UseHardlinksIfPossible = useHardLinks;
}
t.BuildEngine = new MockEngine();
t.SourceFiles = sourceFiles;
t.DestinationFiles = destinationFiles;
t.SkipUnchangedFiles = true;
t.Execute
(
new Microsoft.Build.Tasks.CopyFileWithState(m.CopyFile)
);
// Expect for there to have been no copies.
Assert.AreEqual(0, m.copyCount);
((MockEngine)t.BuildEngine).AssertLogDoesntContain("MSB3026"); // Didn't do retries
}
finally
{
File.Delete(file);
}
}
示例5: DoRetryWhenDestinationLocked
public void DoRetryWhenDestinationLocked()
{
string destinationFile = Path.GetTempFileName();
string sourceFile = Path.GetTempFileName();
try
{
using (StreamWriter sw = new StreamWriter(destinationFile, true)) // Keep it locked
{
ITaskItem[] sourceFiles = new ITaskItem[] { new TaskItem(sourceFile) };
Copy t = new Copy();
t.RetryDelayMilliseconds = 1; // speed up tests!
// Allow the task's default (false) to have a chance
if (useHardLinks)
{
t.UseHardlinksIfPossible = useHardLinks;
}
MockEngine engine = new MockEngine();
t.BuildEngine = engine;
t.SourceFiles = sourceFiles;
t.DestinationFiles = new TaskItem[] { new TaskItem(destinationFile) };
bool result = t.Execute();
Assert.IsFalse(result);
engine.AssertLogContains("MSB3021"); // copy failed
engine.AssertLogContains("MSB3026"); // DID retry
Assert.IsTrue(engine.Errors == 2); // retries failed, and actual failure
Assert.IsTrue(engine.Warnings == 10);
}
}
finally
{
File.Delete(sourceFile);
File.Delete(destinationFile);
}
}
示例6: DoCopyOverNonExistentFile
public void DoCopyOverNonExistentFile()
{
string sourceFile = FileUtilities.GetTemporaryFile();
string destinationFile = FileUtilities.GetTemporaryFile();
try
{
using (StreamWriter sw = new StreamWriter(sourceFile, true)) // HIGHCHAR: Test writes in UTF8 without preamble.
sw.Write("This is a source temp file.");
using (StreamWriter sw = new StreamWriter(destinationFile, true)) // HIGHCHAR: Test writes in UTF8 without preamble.
sw.Write("This is a destination temp file.");
ITaskItem[] sourceFiles = new ITaskItem[] { new TaskItem(sourceFile) };
ITaskItem[] destinationFiles = new ITaskItem[] { new TaskItem(destinationFile) };
File.Delete(destinationFile);
Copy t = new Copy();
t.RetryDelayMilliseconds = 1; // speed up tests!
// Allow the task's default (false) to have a chance
if (useHardLinks)
{
t.UseHardlinksIfPossible = useHardLinks;
}
t.BuildEngine = new MockEngine();
t.SourceFiles = sourceFiles;
t.DestinationFiles = destinationFiles;
t.SkipUnchangedFiles = true;
t.Execute();
Assert.IsTrue(File.Exists(destinationFile), "Expected the destination file to exist.");
((MockEngine)t.BuildEngine).AssertLogDoesntContain("MSB3026"); // Didn't do retries
}
finally
{
File.Delete(sourceFile);
File.Delete(destinationFile);
}
}
示例7: CopyOverReadOnlyFileParameterIsSet
public void CopyOverReadOnlyFileParameterIsSet()
{
string source = FileUtilities.GetTemporaryFile();
string destination = FileUtilities.GetTemporaryFile();
try
{
using (StreamWriter sw = new StreamWriter(source, true))
sw.Write("This is a source file.");
using (StreamWriter sw = new StreamWriter(destination, true))
sw.Write("This is a destination file.");
File.SetAttributes(destination, FileAttributes.ReadOnly);
ITaskItem sourceItem = new TaskItem(source);
ITaskItem destinationItem = new TaskItem(destination);
ITaskItem[] sourceFiles = new ITaskItem[] { sourceItem };
ITaskItem[] destinationFiles = new ITaskItem[] { destinationItem };
Copy t = new Copy();
t.RetryDelayMilliseconds = 1; // speed up tests!
// Allow the task's default (false) to have a chance
if (useHardLinks)
{
t.UseHardlinksIfPossible = useHardLinks;
}
t.BuildEngine = new MockEngine();
t.SourceFiles = sourceFiles;
t.DestinationFiles = destinationFiles;
t.SkipUnchangedFiles = true;
t.OverwriteReadOnlyFiles = true;
// Should not fail although target is readonly
Assert.IsTrue(t.Execute());
// Should have copied file anyway
Assert.AreEqual(1, t.CopiedFiles.Length);
string destinationContent = File.ReadAllText(destination);
Assert.AreEqual("This is a source file.", destinationContent);
((MockEngine)t.BuildEngine).AssertLogDoesntContain("MSB3026"); // Didn't do retries
}
finally
{
File.Delete(source);
File.Delete(destination);
}
}
示例8: CopyToDestinationFolderWithHardLinkFallbackTooManyLinks
// Ignore: Flaky test
public void CopyToDestinationFolderWithHardLinkFallbackTooManyLinks()
{
string sourceFile = FileUtilities.GetTemporaryFile();
string temp = Path.GetTempPath();
string destFolder = Path.Combine(temp, "2A333ED756AF4dc392E728D0F864A398");
string destFile = Path.Combine(destFolder, Path.GetFileName(sourceFile));
try
{
using (StreamWriter sw = new StreamWriter(sourceFile, true)) // HIGHCHAR: Test writes in UTF8 without preamble.
sw.Write("This is a source temp file.");
if (!Directory.Exists(destFolder))
{
Directory.CreateDirectory(destFolder);
}
// Exhaust the number (1024) of directory entries that can be created for a file
// This is 1 + (1 x hard links)
// We need to test the fallback code path when we're out of directory entries for a file..
for (int n = 0; n < 1025 /* make sure */; n++)
{
string destLink = Path.Combine(destFolder, Path.GetFileNameWithoutExtension(sourceFile) + "." + n.ToString());
NativeMethods.CreateHardLink(destLink, sourceFile, IntPtr.Zero);
}
ITaskItem[] sourceFiles = new ITaskItem[] { new TaskItem(sourceFile) };
Copy t = new Copy();
t.RetryDelayMilliseconds = 1; // speed up tests!
t.UseHardlinksIfPossible = true;
MockEngine me = new MockEngine(true);
t.BuildEngine = me;
t.SourceFiles = sourceFiles;
t.DestinationFolder = new TaskItem(destFolder);
t.SkipUnchangedFiles = true;
bool success = t.Execute();
Assert.IsTrue(success, "success");
Assert.IsTrue(File.Exists(destFile), "destination exists");
Microsoft.Build.UnitTests.MockEngine.GetStringDelegate resourceDelegate = new Microsoft.Build.UnitTests.MockEngine.GetStringDelegate(AssemblyResources.GetString);
me.AssertLogContainsMessageFromResource(resourceDelegate, "Copy.HardLinkComment", sourceFile, destFile);
// Can't do this below, because the real message doesn't end with String.Empty, it ends with a CLR exception string, and so matching breaks in PLOC.
// Instead look for the HRESULT that CLR unfortunately puts inside its exception string. Something like this
// Tried to create more than a few links to a file that is supported by the file system. (! yhMcE! Exception from HRESULT: Table c?! 0x80070476)
// me.AssertLogContainsMessageFromResource(resourceDelegate, "Copy.RetryingAsFileCopy", sourceFile, destFile, String.Empty);
me.AssertLogContains("0x80070476");
string destinationFileContents;
using (StreamReader sr = new StreamReader(destFile))
destinationFileContents = sr.ReadToEnd();
Assert.IsTrue
(
destinationFileContents == "This is a source temp file.",
"Expected the destination file to contain the contents of source file."
);
Assert.AreEqual(1, t.DestinationFiles.Length);
Assert.AreEqual(1, t.CopiedFiles.Length);
Assert.AreEqual(destFile, t.DestinationFiles[0].ItemSpec);
Assert.AreEqual(destFile, t.CopiedFiles[0].ItemSpec);
// Now we will write new content to the source file
// we'll then check that the destination file automatically
// has the same content (i.e. it's been hard linked)
using (StreamWriter sw = new StreamWriter(sourceFile, false)) // HIGHCHAR: Test writes in UTF8 without preamble.
sw.Write("This is another source temp file.");
// Read the destination file (it should have the same modified content as the source)
using (StreamReader sr = new StreamReader(destFile))
destinationFileContents = sr.ReadToEnd();
Assert.IsTrue
(
destinationFileContents == "This is a source temp file.",
"Expected the destination copied file to contain the contents of original source file only."
);
((MockEngine)t.BuildEngine).AssertLogDoesntContain("MSB3026"); // Didn't do retries
}
finally
{
File.Delete(sourceFile);
File.Delete(destFile);
Directory.Delete(destFolder, true);
}
}
示例9: CopyDoubleEscapableFileToDestinationFolder
public void CopyDoubleEscapableFileToDestinationFolder()
{
string sourceFileEscaped = Path.GetTempPath() + "a%253A_" + Guid.NewGuid().ToString("N") + ".txt";
string sourceFile = EscapingUtilities.UnescapeAll(sourceFileEscaped);
string temp = Path.GetTempPath();
string destFolder = Path.Combine(temp, "2A333ED756AF4dc392E728D0F864A398");
string destFile = Path.Combine(destFolder, Path.GetFileName(sourceFile));
try
{
using (StreamWriter sw = new StreamWriter(sourceFile, true)) // HIGHCHAR: Test writes in UTF8 without preamble.
{
sw.Write("This is a source temp file.");
}
// Don't create the dest folder, let task do that
ITaskItem[] sourceFiles = new ITaskItem[] { new TaskItem(sourceFileEscaped) };
Copy t = new Copy();
t.RetryDelayMilliseconds = 1; // speed up tests!
// Allow the task's default (false) to have a chance
if (useHardLinks)
{
t.UseHardlinksIfPossible = useHardLinks;
}
t.BuildEngine = new MockEngine();
t.SourceFiles = sourceFiles;
t.DestinationFolder = new TaskItem(destFolder);
t.SkipUnchangedFiles = true;
bool success = t.Execute();
Assert.IsTrue(success, "success");
Assert.IsTrue(File.Exists(destFile), "destination exists");
string destinationFileContents;
using (StreamReader sr = new StreamReader(destFile))
{
destinationFileContents = sr.ReadToEnd();
}
Assert.IsTrue
(
destinationFileContents == "This is a source temp file.",
"Expected the destination file to contain the contents of source file."
);
Assert.AreEqual(1, t.DestinationFiles.Length);
Assert.AreEqual(1, t.CopiedFiles.Length);
Assert.AreEqual(destFile, t.DestinationFiles[0].ItemSpec);
Assert.AreEqual(destFile, t.CopiedFiles[0].ItemSpec);
((MockEngine)t.BuildEngine).AssertLogDoesntContain("MSB3026"); // Didn't do retries
}
finally
{
Helpers.DeleteFiles(sourceFile, destFile);
}
}
示例10: DoNotRetryCopyNonExistentSourceFile
public void DoNotRetryCopyNonExistentSourceFile()
{
string sourceFile = "Nannanacat";
string destinationFile = FileUtilities.GetTemporaryFile();
try
{
using (StreamWriter sw = new StreamWriter(destinationFile, true)) // HIGHCHAR: Test writes in UTF8 without preamble.
sw.Write("This is a destination temp file.");
ITaskItem[] sourceFiles = new ITaskItem[] { new TaskItem(sourceFile) };
ITaskItem[] destinationFiles = new ITaskItem[] { new TaskItem(destinationFile) };
File.Delete(destinationFile);
Copy t = new Copy();
t.RetryDelayMilliseconds = 1; // speed up tests!
// Allow the task's default (false) to have a chance
if (useHardLinks)
{
t.UseHardlinksIfPossible = useHardLinks;
}
MockEngine engine = new MockEngine();
t.BuildEngine = engine;
t.SourceFiles = sourceFiles;
t.DestinationFiles = destinationFiles;
t.SkipUnchangedFiles = true;
bool result = t.Execute();
Assert.False(result);
Assert.Equal(1, engine.Errors);
Assert.Equal(0, engine.Warnings);
engine.AssertLogContains("MSB3030");
((MockEngine)t.BuildEngine).AssertLogDoesntContain("MSB3026"); // Didn't do retries
}
finally
{
File.Delete(destinationFile);
}
}
示例11: DoNotRetryCopyWhenDestinationFolderIsFile
public void DoNotRetryCopyWhenDestinationFolderIsFile()
{
string destinationFile = FileUtilities.GetTemporaryFile();
string sourceFile = FileUtilities.GetTemporaryFile();
try
{
using (StreamWriter sw = new StreamWriter(sourceFile, true))
sw.Write("This is a destination temp file.");
ITaskItem[] sourceFiles = new ITaskItem[] { new TaskItem(sourceFile) };
Copy t = new Copy();
t.RetryDelayMilliseconds = 1; // speed up tests!
// Allow the task's default (false) to have a chance
if (useHardLinks)
{
t.UseHardlinksIfPossible = useHardLinks;
}
MockEngine engine = new MockEngine();
t.BuildEngine = engine;
t.SourceFiles = sourceFiles;
t.DestinationFolder = new TaskItem(destinationFile);
t.SkipUnchangedFiles = true;
bool result = t.Execute();
Assert.False(result);
engine.AssertLogContains("MSB3021"); // copy failed
engine.AssertLogDoesntContain("MSB3026"); // Didn't retry
Assert.Equal(1, engine.Errors);
Assert.Equal(0, engine.Warnings);
}
finally
{
File.Delete(sourceFile);
}
}
示例12: InvalidRetryDelayCount
public void InvalidRetryDelayCount()
{
Copy t = new Copy();
t.RetryDelayMilliseconds = 1; // speed up tests!
// Allow the task's default (false) to have a chance
if (useHardLinks)
{
t.UseHardlinksIfPossible = useHardLinks;
}
MockEngine engine = new MockEngine(true /* log to console */);
t.BuildEngine = engine;
t.SourceFiles = new ITaskItem[] { new TaskItem("c:\\source") };
t.DestinationFiles = new ITaskItem[] { new TaskItem("c:\\destination") };
t.Retries = 1;
t.RetryDelayMilliseconds = -1;
bool result = t.Execute();
Assert.Equal(false, result);
engine.AssertLogContains("MSB3029");
}
示例13: DoNotNormallyCopyOverReadOnlyFile
public void DoNotNormallyCopyOverReadOnlyFile()
{
string source = FileUtilities.GetTemporaryFile();
string destination = FileUtilities.GetTemporaryFile();
try
{
using (StreamWriter sw = new StreamWriter(source, true))
sw.Write("This is a source file.");
using (StreamWriter sw = new StreamWriter(destination, true))
sw.Write("This is a destination file.");
File.SetAttributes(destination, FileAttributes.ReadOnly);
ITaskItem sourceItem = new TaskItem(source);
ITaskItem destinationItem = new TaskItem(destination);
ITaskItem[] sourceFiles = new ITaskItem[] { sourceItem };
ITaskItem[] destinationFiles = new ITaskItem[] { destinationItem };
Copy t = new Copy();
t.RetryDelayMilliseconds = 1; // speed up tests!
// Allow the task's default (false) to have a chance
if (useHardLinks)
{
t.UseHardlinksIfPossible = useHardLinks;
}
t.BuildEngine = new MockEngine();
t.SourceFiles = sourceFiles;
t.DestinationFiles = destinationFiles;
t.SkipUnchangedFiles = true;
// OverwriteReadOnlyFiles defaults to false
// Should fail: target is readonly
Assert.False(t.Execute());
// Expect for there to have been no copies.
Assert.Equal(0, t.CopiedFiles.Length);
string destinationContent = File.ReadAllText(destination);
Assert.Equal("This is a destination file.", destinationContent);
((MockEngine)t.BuildEngine).AssertLogDoesntContain("MSB3026"); // did not do retries as it was r/o
}
finally
{
File.SetAttributes(source, FileAttributes.Normal);
File.SetAttributes(destination, FileAttributes.Normal);
File.Delete(source);
File.Delete(destination);
}
}
示例14: CopyToDestinationFolder
public void CopyToDestinationFolder()
{
string sourceFile = FileUtilities.GetTemporaryFile();
string temp = Path.GetTempPath();
string destFolder = Path.Combine(temp, "2A333ED756AF4dc392E728D0F864A398");
string destFile = Path.Combine(destFolder, Path.GetFileName(sourceFile));
try
{
using (StreamWriter sw = new StreamWriter(sourceFile, true)) // HIGHCHAR: Test writes in UTF8 without preamble.
sw.Write("This is a source temp file.");
// Don't create the dest folder, let task do that
ITaskItem[] sourceFiles = new ITaskItem[] { new TaskItem(sourceFile) };
Copy t = new Copy();
t.RetryDelayMilliseconds = 1; // speed up tests!
// Allow the task's default (false) to have a chance
if (useHardLinks)
{
t.UseHardlinksIfPossible = useHardLinks;
}
MockEngine me = new MockEngine();
t.BuildEngine = me;
t.SourceFiles = sourceFiles;
t.DestinationFolder = new TaskItem(destFolder);
t.SkipUnchangedFiles = true;
bool success = t.Execute();
Assert.True(success); // "success"
Assert.True(File.Exists(destFile)); // "destination exists"
string destinationFileContents;
using (StreamReader sr = new StreamReader(destFile))
destinationFileContents = sr.ReadToEnd();
if (!useHardLinks)
{
Microsoft.Build.UnitTests.MockEngine.GetStringDelegate resourceDelegate = new Microsoft.Build.UnitTests.MockEngine.GetStringDelegate(AssemblyResources.GetString);
me.AssertLogDoesntContainMessageFromResource(resourceDelegate, "Copy.HardLinkComment", sourceFile, destFile);
}
else
{
Microsoft.Build.UnitTests.MockEngine.GetStringDelegate resourceDelegate = new Microsoft.Build.UnitTests.MockEngine.GetStringDelegate(AssemblyResources.GetString);
me.AssertLogContainsMessageFromResource(resourceDelegate, "Copy.HardLinkComment", sourceFile, destFile);
}
Assert.Equal(destinationFileContents, "This is a source temp file."); // "Expected the destination file to contain the contents of source file."
Assert.Equal(1, t.DestinationFiles.Length);
Assert.Equal(1, t.CopiedFiles.Length);
Assert.Equal(destFile, t.DestinationFiles[0].ItemSpec);
Assert.Equal(destFile, t.CopiedFiles[0].ItemSpec);
((MockEngine)t.BuildEngine).AssertLogDoesntContain("MSB3026"); // Didn't do retries
}
finally
{
Helpers.DeleteFiles(sourceFile, destFile);
}
}
示例15: CopyToDestinationFolderWithHardLinkCheck
public void CopyToDestinationFolderWithHardLinkCheck()
{
string sourceFile = FileUtilities.GetTemporaryFile();
string temp = Path.GetTempPath();
string destFolder = Path.Combine(temp, "2A333ED756AF4dc392E728D0F864A398");
string destFile = Path.Combine(destFolder, Path.GetFileName(sourceFile));
try
{
using (StreamWriter sw = new StreamWriter(sourceFile, true)) // HIGHCHAR: Test writes in UTF8 without preamble.
sw.Write("This is a source temp file.");
// Don't create the dest folder, let task do that
ITaskItem[] sourceFiles = new ITaskItem[] { new TaskItem(sourceFile) };
Copy t = new Copy();
t.RetryDelayMilliseconds = 1; // speed up tests!
// Allow the task's default (false) to have a chance
t.UseHardlinksIfPossible = true;
MockEngine me = new MockEngine(true);
t.BuildEngine = me;
t.SourceFiles = sourceFiles;
t.DestinationFolder = new TaskItem(destFolder);
t.SkipUnchangedFiles = true;
bool success = t.Execute();
Assert.IsTrue(success, "success");
Assert.IsTrue(File.Exists(destFile), "destination exists");
Microsoft.Build.UnitTests.MockEngine.GetStringDelegate resourceDelegate = new Microsoft.Build.UnitTests.MockEngine.GetStringDelegate(AssemblyResources.GetString);
me.AssertLogContainsMessageFromResource(resourceDelegate, "Copy.HardLinkComment", sourceFile, destFile);
string destinationFileContents;
using (StreamReader sr = new StreamReader(destFile))
destinationFileContents = sr.ReadToEnd();
Assert.IsTrue
(
destinationFileContents == "This is a source temp file.",
"Expected the destination hard linked file to contain the contents of source file."
);
Assert.AreEqual(1, t.DestinationFiles.Length);
Assert.AreEqual(1, t.CopiedFiles.Length);
Assert.AreEqual(destFile, t.DestinationFiles[0].ItemSpec);
Assert.AreEqual(destFile, t.CopiedFiles[0].ItemSpec);
// Now we will write new content to the source file
// we'll then check that the destination file automatically
// has the same content (i.e. it's been hard linked)
using (StreamWriter sw = new StreamWriter(sourceFile, false)) // HIGHCHAR: Test writes in UTF8 without preamble.
sw.Write("This is another source temp file.");
// Read the destination file (it should have the same modified content as the source)
using (StreamReader sr = new StreamReader(destFile))
destinationFileContents = sr.ReadToEnd();
Assert.IsTrue
(
destinationFileContents == "This is another source temp file.",
"Expected the destination hard linked file to contain the contents of source file. Even after modification of the source"
);
((MockEngine)t.BuildEngine).AssertLogDoesntContain("MSB3026"); // Didn't do retries
}
finally
{
Helpers.DeleteFiles(sourceFile, destFile);
}
}