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.Equal(0, m.copyCount);
((MockEngine)t.BuildEngine).AssertLogDoesntContain("MSB3026"); // Didn't do retries
}
finally
{
File.Delete(file);
}
}
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");
}
public void CopyOverReadOnlyFileParameterIsSetWithDestinationFolder()
{
string source1 = FileUtilities.GetTemporaryFile();
string source2 = FileUtilities.GetTemporaryFile();
string destinationFolder = Path.Combine(Path.GetTempPath(), "2A333ED756AF4dc392E728D0F874A398");
string destination1 = Path.Combine(destinationFolder, Path.GetFileName(source1));
string destination2 = Path.Combine(destinationFolder, Path.GetFileName(source2));
try
{
Directory.CreateDirectory(destinationFolder);
using (StreamWriter sw = new StreamWriter(source1, true))
sw.Write("This is a source file1.");
using (StreamWriter sw = new StreamWriter(source2, true))
sw.Write("This is a source file2.");
using (StreamWriter sw = new StreamWriter(destination1, true))
sw.Write("This is a destination file1.");
using (StreamWriter sw = new StreamWriter(destination2, true))
sw.Write("This is a destination file2.");
// Set one destination readonly.
File.SetAttributes(destination1, FileAttributes.ReadOnly);
ITaskItem sourceItem1 = new TaskItem(source1);
ITaskItem sourceItem2 = new TaskItem(source2);
ITaskItem[] sourceFiles = new ITaskItem[] { sourceItem1, sourceItem2 };
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(destinationFolder);
t.OverwriteReadOnlyFiles = true;
// Should not fail although one target is readonly
Assert.IsTrue(t.Execute());
// Should have copied files anyway
Assert.AreEqual(2, t.CopiedFiles.Length);
string destinationContent1 = File.ReadAllText(destination1);
Assert.AreEqual("This is a source file1.", destinationContent1);
string destinationContent2 = File.ReadAllText(destination2);
Assert.AreEqual("This is a source file2.", destinationContent2);
Assert.IsTrue((File.GetAttributes(destination1) & FileAttributes.ReadOnly) != FileAttributes.ReadOnly);
Assert.IsTrue((File.GetAttributes(destination2) & FileAttributes.ReadOnly) != FileAttributes.ReadOnly);
((MockEngine)t.BuildEngine).AssertLogDoesntContain("MSB3026"); // Didn't do retries
}
finally
{
File.SetAttributes(destination1, FileAttributes.Normal); // just in case
File.SetAttributes(destination2, FileAttributes.Normal); // just in case
File.Delete(source1);
File.Delete(source2);
File.Delete(destination1);
File.Delete(destination2);
Directory.Delete(destinationFolder, true);
}
}
public void AlwaysRetryCopyEnvironmentOverride()
{
string source = FileUtilities.GetTemporaryFile();
string destination = FileUtilities.GetTemporaryFile();
string oldAlwaysOverwriteValue = Environment.GetEnvironmentVariable("MSBUILDALWAYSRETRY");
try
{
Environment.SetEnvironmentVariable("MSBUILDALWAYSRETRY", "1 ");
Copy.RefreshInternalEnvironmentValues();
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 = false;
t.Retries = 5;
// The file is read-only, so the retries will all fail.
Assert.IsFalse(t.Execute());
// 3 warnings per retry, except the last one which has only two.
((MockEngine)t.BuildEngine).AssertLogContains("MSB3026");
Assert.AreEqual(((t.Retries + 1) * 3) - 1, ((MockEngine)t.BuildEngine).Warnings);
// One error for "retrying failed", one error for "copy failed"
((MockEngine)t.BuildEngine).AssertLogContains("MSB3027");
((MockEngine)t.BuildEngine).AssertLogContains("MSB3021");
Assert.AreEqual(2, ((MockEngine)t.BuildEngine).Errors);
}
finally
{
Environment.SetEnvironmentVariable("MSBUILDALWAYSRETRY", oldAlwaysOverwriteValue);
Copy.RefreshInternalEnvironmentValues();
File.SetAttributes(destination, FileAttributes.Normal);
File.Delete(source);
File.Delete(destination);
}
}
// Ignore: Flaky test
public void CopyToDestinationFolderWithHardLinkFallbackNetwork()
{
string sourceFile = FileUtilities.GetTemporaryFile();
string temp = @"\\localhost\c$\temp";
string destFolder = Path.Combine(temp, "2A333ED756AF4dc392E728D0F864A398");
string destFile = Path.Combine(destFolder, Path.GetFileName(sourceFile));
try
{
if (!Directory.Exists(destFolder))
{
Directory.CreateDirectory(destFolder);
}
string nothingFile = Path.Combine(destFolder, "nothing.txt");
File.WriteAllText(nothingFile, "nothing");
File.Delete(nothingFile);
}
catch (Exception)
{
Console.WriteLine("CopyToDestinationFolderWithHardLinkFallbackNetwork test could not access the network.");
// Something caused us to not be able to access our "network" share, don't fail.
return;
}
try
{
using (StreamWriter sw = new StreamWriter(sourceFile, true)) // HIGHCHAR: Test writes in UTF8 without preamble.
sw.Write("This is a source temp file.");
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
// The system cannot move the file to a different disk drive. (Exception from HRESULT: 0x80070011)
// me.AssertLogContainsMessageFromResource(resourceDelegate, "Copy.RetryingAsFileCopy", sourceFile, destFile, String.Empty);
me.AssertLogContains("0x80070011");
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);
}
}
public void CopyWithDuplicatesUsingFiles()
{
string tempPath = Path.GetTempPath();
ITaskItem[] sourceFiles = new ITaskItem[]
{
new TaskItem(Path.Combine(tempPath, "a.cs")),
new TaskItem(Path.Combine(tempPath, "b.cs")),
new TaskItem(Path.Combine(tempPath, "a.cs")),
new TaskItem(Path.Combine(tempPath, "a.cs")),
new TaskItem(Path.Combine(tempPath, "a.cs")),
};
foreach (ITaskItem item in sourceFiles)
{
using (StreamWriter sw = new StreamWriter(item.ItemSpec)) // HIGHCHAR: Test writes in UTF8 without preamble.
{
sw.Write("This is a source temp file.");
}
}
ITaskItem[] destFiles = new ITaskItem[]
{
new TaskItem(Path.Combine(tempPath, @"xa.cs")), // a.cs -> xa.cs
new TaskItem(Path.Combine(tempPath, @"xa.cs")), // b.cs -> xa.cs should copy because it's a different source
new TaskItem(Path.Combine(tempPath, @"xb.cs")), // a.cs -> xb.cs should copy because it's a different destination
new TaskItem(Path.Combine(tempPath, @"xa.cs")), // a.cs -> xa.cs should copy because it's a different source
new TaskItem(Path.Combine(tempPath, @"xa.cs")), // a.cs -> xa.cs should not copy because it's the same source
};
var filesActuallyCopied = new List<KeyValuePair<FileState, FileState>>();
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 = destFiles;
bool success = t.Execute(delegate (FileState source, FileState dest)
{
filesActuallyCopied.Add(new KeyValuePair<FileState, FileState>(source, dest));
return true;
});
Assert.IsTrue(success);
Assert.AreEqual(4, filesActuallyCopied.Count);
Assert.AreEqual(5, t.CopiedFiles.Length);
Assert.AreEqual(Path.Combine(tempPath, "a.cs"), filesActuallyCopied[0].Key.Name);
Assert.AreEqual(Path.Combine(tempPath, "b.cs"), filesActuallyCopied[1].Key.Name);
Assert.AreEqual(Path.Combine(tempPath, "a.cs"), filesActuallyCopied[2].Key.Name);
Assert.AreEqual(Path.Combine(tempPath, "a.cs"), filesActuallyCopied[3].Key.Name);
Assert.AreEqual(Path.Combine(tempPath, "xa.cs"), filesActuallyCopied[0].Value.Name);
Assert.AreEqual(Path.Combine(tempPath, "xa.cs"), filesActuallyCopied[1].Value.Name);
Assert.AreEqual(Path.Combine(tempPath, "xb.cs"), filesActuallyCopied[2].Value.Name);
Assert.AreEqual(Path.Combine(tempPath, "xa.cs"), filesActuallyCopied[3].Value.Name);
((MockEngine)t.BuildEngine).AssertLogDoesntContain("MSB3026"); // Didn't do retries
}
public void TooFewRetriesThrows()
{
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;
CopyFunctor copyFunctor = new CopyFunctor(3, true /* throw */);
bool result = t.Execute(copyFunctor.Copy);
Assert.AreEqual(false, result);
engine.AssertLogContains("MSB3026");
engine.AssertLogContains("MSB3027");
}
public void Regress451057_ExitGracefullyIfPathNameIsTooLong()
{
string sourceFile = FileUtilities.GetTemporaryFile();
string destinationFile = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ";
try
{
using (StreamWriter sw = new StreamWriter(sourceFile, true)) // HIGHCHAR: Test writes in UTF8 without preamble.
sw.Write("This is a source temp file.");
ITaskItem[] sourceFiles = new ITaskItem[] { new TaskItem(sourceFile) };
ITaskItem[] destinationFiles = new ITaskItem[] { new TaskItem(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;
bool result = t.Execute();
// Expect for there to have been no copies.
Assert.AreEqual(false, result);
((MockEngine)t.BuildEngine).AssertLogDoesntContain("MSB3026"); // Didn't do retries
}
finally
{
File.Delete(sourceFile);
}
}
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);
}
}
请发表评论