Esempio n. 1
0
        public void ClientFilesTask_Safe_File_Move()
        {
            CleanClientFilesTask task            = new CleanClientFilesTask();
            MockBuildEngine      mockBuildEngine = new MockBuildEngine();

            task.BuildEngine = mockBuildEngine;

            string tempFolder = CodeGenHelper.GenerateTempFolder();

            try
            {
                // Do a simple move
                string file1 = Path.Combine(tempFolder, "File1.txt");
                string file2 = Path.Combine(tempFolder, "File2.txt");
                File.AppendAllText(file1, "stuff");

                bool success = task.SafeFileMove(file1, file2);
                Assert.IsTrue(success, "SafeFileMove reported failure");

                Assert.IsTrue(File.Exists(file2), "File2 did not get created");
                Assert.IsFalse(File.Exists(file1), "File1 still exists after move");

                string content = File.ReadAllText(file2);
                Assert.AreEqual("stuff", content, "File2 did not get right content");

                string errorMessage = String.Empty;

                // Finally, try a clearly illegal move and catch the error
                using (FileStream fs = new FileStream(file2, FileMode.Open, FileAccess.Read, FileShare.None))
                {
                    try
                    {
                        File.Move(file2, file1);
                    }
                    catch (IOException iox)
                    {
                        errorMessage = iox.Message;
                    }
                    success = task.SafeFileMove(file2, file1);
                }

                Assert.IsFalse(success, "Expected illegal move to report failure");

                string expectedWarning = string.Format(CultureInfo.CurrentCulture, Resource.Failed_To_Rename_File, file2, file1, errorMessage);
                TestHelper.AssertContainsWarnings(mockBuildEngine.ConsoleLogger, expectedWarning);
            }

            finally
            {
                CodeGenHelper.DeleteTempFolder(tempFolder);
            }
        }