// 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); } }
// 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 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); } }
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); } }
public static void ClassSetup(TestContext context) { s_fakeSDKStructureRoot = CreateFakeSDKReferenceAssemblyDirectory1(out s_sdkDirectory); s_fakeSDKStructureRoot2 = CreateFakeSDKReferenceAssemblyDirectory2(out s_sdkDirectory2); s_resourceDelegate = new Microsoft.Build.UnitTests.MockEngine.GetStringDelegate(AssemblyResources.GetString); }