상속: Microsoft.Build.Tasks.TaskExtension
예제 #1
0
		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
0
        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);
            }
        }
예제 #3
0
        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");
        }
예제 #4
0
        public void OutputsOnlyIncludeSuccessfulCopies()
        {
            string temp = Path.GetTempPath();
            string inFile1 = Path.Combine(temp, "2A333ED756AF4dc392E728D0F864A392");
            string inFile2 = Path.Combine(temp, "2A333ED756AF4dc392E728D0F864A393");
            string invalidFile = "!@#$%^&*()|";
            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);
            }
        }
예제 #5
0
        public void DoNotRetryWhenDestinationLockedDueToAcl()
        {
            string tempDirectory = Path.Combine(Path.GetTempPath(), "DoNotRetryWhenDestinationLockedDueToAcl");
            string destinationFile = Path.Combine(tempDirectory, "DestinationFile.txt");
            string sourceFile = Path.Combine(tempDirectory, "SourceFile.txt");

            if (Directory.Exists(tempDirectory))
            {
                FileUtilities.DeleteDirectoryNoThrow(tempDirectory, true);
            }

            Directory.CreateDirectory(tempDirectory);

            File.WriteAllText(destinationFile, "Destination");
            File.WriteAllText(sourceFile, "SourceFile");

            string userAccount = string.Format(@"{0}\{1}", System.Environment.UserDomainName, System.Environment.UserName);

            FileSystemAccessRule denyFile = new FileSystemAccessRule(userAccount, FileSystemRights.Write | FileSystemRights.Delete | FileSystemRights.DeleteSubdirectoriesAndFiles | FileSystemRights.WriteData, AccessControlType.Deny);
            FileSystemAccessRule denyDirectory = new FileSystemAccessRule(userAccount, FileSystemRights.DeleteSubdirectoriesAndFiles, AccessControlType.Deny);

            FileSecurity fSecurity = File.GetAccessControl(destinationFile);
            DirectorySecurity dSecurity = Directory.GetAccessControl(tempDirectory);

            try
            {
                fSecurity.AddAccessRule(denyFile);
                File.SetAccessControl(destinationFile, fSecurity);

                dSecurity.AddAccessRule(denyDirectory);
                Directory.SetAccessControl(tempDirectory, dSecurity);

                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 = new TaskItem[] { new TaskItem(sourceFile) };
                t.DestinationFiles = new TaskItem[] { new TaskItem(destinationFile) };

                bool result = t.Execute();
                Assert.IsFalse(result);

                engine.AssertLogContains("MSB3021"); // copy failed
                engine.AssertLogDoesntContain("MSB3026"); // Didn't retry

                Assert.IsTrue(engine.Errors == 1);
                Assert.IsTrue(engine.Warnings == 0);
            }
            finally
            {
                fSecurity.RemoveAccessRule(denyFile);
                File.SetAccessControl(destinationFile, fSecurity);

                dSecurity.RemoveAccessRule(denyDirectory);
                Directory.SetAccessControl(tempDirectory, dSecurity);

                if (Directory.Exists(tempDirectory))
                {
                    FileUtilities.DeleteDirectoryNoThrow(tempDirectory, true);
                }
            }
        }
예제 #6
0
        public void DoNotRetryCopyNotSupportedException()
        {
            string sourceFile = FileUtilities.GetTemporaryFile();
            string destinationFile = "foo:bar";

            try
            {
                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;
                }
                MockEngine engine = new MockEngine();
                t.BuildEngine = engine;
                t.SourceFiles = sourceFiles;
                t.DestinationFiles = destinationFiles;
                t.SkipUnchangedFiles = true;

                bool result = t.Execute();
                Assert.IsFalse(result);
                Assert.IsTrue(engine.Errors == 1);
                Assert.IsTrue(engine.Warnings == 0);
                engine.AssertLogContains("MSB3021");
                ((MockEngine)t.BuildEngine).AssertLogDoesntContain("MSB3026"); // Didn't do retries
            }
            finally
            {
                File.Delete(sourceFile);
            }
        }
예제 #7
0
        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);
            }
        }
예제 #8
0
        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);
            }
        }
예제 #9
0
        // 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);
            }
        }
예제 #10
0
        public void DestinationFilesLengthNotEqualSourceFilesLength()
        {
            string temp = Path.GetTempPath();
            string inFile1 = Path.Combine(temp, "2A333ED756AF4dc392E728D0F864A398");
            string inFile2 = Path.Combine(temp, "2A333ED756AF4dc392E728D0F864A399");
            string outFile1 = Path.Combine(temp, "2A333ED756AF4dc392E728D0F864A400");

            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;

                t.SourceFiles = new ITaskItem[] { new TaskItem(inFile1), new TaskItem(inFile2) };
                t.DestinationFiles = new ITaskItem[] { new TaskItem(outFile1) };

                bool success = t.Execute();

                Assert.IsTrue(!success);
                Assert.AreEqual(1, t.DestinationFiles.Length);
                Assert.IsNull(t.CopiedFiles);
                Assert.IsTrue(!File.Exists(outFile1));

                ((MockEngine)t.BuildEngine).AssertLogDoesntContain("MSB3026"); // Didn't do retries
            }
            finally
            {
                File.Delete(inFile1);
                File.Delete(inFile2);
                File.Delete(outFile1);
            }
        }
예제 #11
0
        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
        }
예제 #12
0
        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);
            }
        }
예제 #13
0
        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);
            }
        }
예제 #14
0
        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);
            }
        }
예제 #15
0
        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");
        }
예제 #16
0
        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);
            }
        }
예제 #17
0
        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);
            }
        }
예제 #18
0
        public void Regress451057_ExitGracefullyIfPathNameIsTooLong2()
        {
            string sourceFile = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ";
            string destinationFile = FileUtilities.GetTemporaryFile();
            File.Delete(destinationFile);

            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);
            Assert.IsTrue(!File.Exists(destinationFile));

            ((MockEngine)t.BuildEngine).AssertLogDoesntContain("MSB3026"); // Didn't do retries
        }
예제 #19
0
        // 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);
            }
        }
예제 #20
0
        public void ExitGracefullyOnInvalidPathCharacters()
        {
            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 = new ITaskItem[] { new TaskItem("foo | bar") }; ;
            t.DestinationFolder = new TaskItem("dest");

            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
        }
예제 #21
0
        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);
            }
        }
예제 #22
0
        public void DefaultRetriesIs10()
        {
            Copy t = new Copy();
            t.RetryDelayMilliseconds = 1; // speed up tests!

            Assert.AreEqual(10, t.Retries);
        }
예제 #23
0
        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);
            }
        }
예제 #24
0
        public void DefaultRetryDelayIs1000()
        {
            Copy t = new Copy();

            Assert.AreEqual(1000, t.RetryDelayMilliseconds);
        }
예제 #25
0
        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);
            }
        }
예제 #26
0
        public void DefaultNoHardlink()
        {
            Copy t = new Copy();
            t.RetryDelayMilliseconds = 1; // speed up tests!

            Assert.AreEqual(false, t.UseHardlinksIfPossible);
        }
예제 #27
0
        public void DoNotRetryCopyWhenDestinationFileIsFolder()
        {
            string destinationFile = Path.GetTempPath();
            string sourceFile = FileUtilities.GetTemporaryFile();

            try
            {
                using (StreamWriter sw = new StreamWriter(sourceFile, 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) };

                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.IsFalse(result);
                Assert.IsTrue(engine.Errors == 1);
                Assert.IsTrue(engine.Warnings == 0);
                engine.AssertLogContains("MSB3024");
                engine.AssertLogDoesntContain("MSB3026");
            }
            finally
            {
                File.Delete(sourceFile);
            }
        }
예제 #28
0
        public void SuccessAfterOneRetryContinueToNextFile()
        {
            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"), new TaskItem("c:\\source2") };
            t.DestinationFiles = new ITaskItem[] { new TaskItem("c:\\destination"), new TaskItem("c:\\destination2") };
            t.Retries = 1;
            t.RetryDelayMilliseconds = 1; // Can't really test the delay, but at least try passing in a value

            CopyFunctor copyFunctor = new CopyFunctor(2, false /* do not throw on failure */);
            bool result = t.Execute(copyFunctor.Copy);

            Assert.AreEqual(true, result);
            engine.AssertLogContains("MSB3026");
            engine.AssertLogDoesntContain("MSB3027");
            Assert.AreEqual(copyFunctor.FilesCopiedSuccessfully[0].Name, "c:\\source");
            Assert.AreEqual(copyFunctor.FilesCopiedSuccessfully[1].Name, "c:\\source2");
        }
예제 #29
0
        public void CopyFileOnItself()
        {
            string temp = Path.GetTempPath();
            string file = Path.Combine(temp, "2A333ED756AF4dc392E728D0F864A395");

            try
            {
                FileStream fs = null;

                try
                {
                    fs = File.Create(file);
                }
                finally
                {
                    fs.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;
                t.SourceFiles = new ITaskItem[] { new TaskItem(file) };
                t.DestinationFiles = new ITaskItem[] { new TaskItem(file) };
                t.SkipUnchangedFiles = true;
                bool success = t.Execute();

                Assert.IsTrue(success);
                Assert.AreEqual(1, t.DestinationFiles.Length);
                Assert.AreEqual(file, t.DestinationFiles[0].ItemSpec);

                ((MockEngine)t.BuildEngine).AssertLogDoesntContain("MSB3026"); // Didn't do retries, nothing to do

                t = new Copy();
                // Allow the task's default (false) to have a chance
                if (useHardLinks)
                {
                    t.UseHardlinksIfPossible = useHardLinks;
                }
                engine = new MockEngine();
                t.BuildEngine = engine;
                t.SourceFiles = new ITaskItem[] { new TaskItem(file) };
                t.DestinationFiles = new ITaskItem[] { new TaskItem(file) };
                t.SkipUnchangedFiles = false;

                success = t.Execute();

                Assert.IsTrue(success);
                Assert.AreEqual(1, t.DestinationFiles.Length);
                Assert.AreEqual(file, t.DestinationFiles[0].ItemSpec);
                Assert.AreEqual(1, t.CopiedFiles.Length);

                ((MockEngine)t.BuildEngine).AssertLogDoesntContain("MSB3026"); // Didn't do retries, nothing to do
            }
            finally
            {
                File.Delete(file);
            }
        }
예제 #30
0
        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);
            }
        }