Ejemplo n.º 1
0
        public void ClientFilesTask_Safe_File_Copy()
        {
            CleanClientFilesTask task            = new CleanClientFilesTask();
            MockBuildEngine      mockBuildEngine = new MockBuildEngine();

            task.BuildEngine = mockBuildEngine;

            string tempFolder = CodeGenHelper.GenerateTempFolder();

            try
            {
                // Do a simple copy with no special handling for attributes
                string file1 = Path.Combine(tempFolder, "File1.txt");
                string file2 = Path.Combine(tempFolder, "File2.txt");
                File.AppendAllText(file1, "stuff");

                bool success = task.SafeFileCopy(file1, file2, /*isProjectFile*/ false);
                Assert.IsTrue(success, "SafeFileCopy reported failure");

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

                FileAttributes fa = File.GetAttributes(file2);
                Assert.AreEqual(0, (int)(fa & FileAttributes.ReadOnly), "Expected RO bit not to be set");

                Assert.IsFalse(task.FilesWereWritten, "Should not have marked files as written");
                File.Delete(file2);

                // Repeat, but ask for it to be treated as a project file
                success = task.SafeFileCopy(file1, file2, /*isProjectFile*/ true);
                Assert.IsTrue(success, "SafeFileCopy reported failure");

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

                fa = File.GetAttributes(file2);
                Assert.AreEqual((int)FileAttributes.ReadOnly, (int)(fa & FileAttributes.ReadOnly), "Expected RO bit to be set");

                Assert.IsTrue(task.FilesWereWritten, "Should have marked files as written");
                task.SafeFileDelete(file2);

                string errorMessage = String.Empty;

                // Finally, try a clearly illegal copy and catch the error
                using (FileStream fs = new FileStream(file1, FileMode.Open, FileAccess.Read, FileShare.None))
                {
                    try
                    {
                        File.Copy(file1, file2, true);
                    }
                    catch (IOException iox)
                    {
                        errorMessage = iox.Message;
                    }
                    success = task.SafeFileCopy(file1, file2, /*isProjectFile*/ false);
                }

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


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

            finally
            {
                CodeGenHelper.DeleteTempFolder(tempFolder);
            }
        }
        public void CleanClientFiles_Safe_File_Delete()
        {
            CleanClientFilesTask task            = new CleanClientFilesTask();
            MockBuildEngine      mockBuildEngine = new MockBuildEngine();

            task.BuildEngine = mockBuildEngine;

            // Test 1 -- null and empty deletes do nothing
            task.SafeFileDelete(null);
            TestHelper.AssertNoErrorsOrWarnings(mockBuildEngine.ConsoleLogger);

            task.SafeFileDelete(string.Empty);
            TestHelper.AssertNoErrorsOrWarnings(mockBuildEngine.ConsoleLogger);

            // Test 2 -- nonexistant file does nothing
            string fileName = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());

            Assert.IsFalse(File.Exists(fileName));
            task.SafeFileDelete(fileName);
            TestHelper.AssertNoErrorsOrWarnings(mockBuildEngine.ConsoleLogger);

            // Test 3 -- verify delete on actual file succeeds without error
            File.WriteAllText(fileName, "stuff");
            Assert.IsTrue(File.Exists(fileName));
            task.SafeFileDelete(fileName);
            Assert.IsFalse(File.Exists(fileName));
            TestHelper.AssertNoErrorsOrWarnings(mockBuildEngine.ConsoleLogger);

            // Test 4 -- verify delete on actual file with READONLY attribute set succeeds without error
            File.WriteAllText(fileName, "stuff");
            File.SetAttributes(fileName, FileAttributes.ReadOnly);
            Assert.IsTrue(File.Exists(fileName));
            task.SafeFileDelete(fileName);
            Assert.IsFalse(File.Exists(fileName));
            TestHelper.AssertNoErrorsOrWarnings(mockBuildEngine.ConsoleLogger);

            // Test 5 -- attempt to delete while file is open.
            // Verify we log a warning containing the exception's message
            File.WriteAllText(fileName, "stuff");
            Assert.IsTrue(File.Exists(fileName));
            string errorMessage = null;

            using (StreamReader t1 = new StreamReader(fileName))
            {
                // We do a delete here to capture the exception we expect the SafeFileDelete to encounter
                try
                {
                    File.Delete(fileName);
                }
                catch (IOException ioe)
                {
                    errorMessage = ioe.Message;
                }
                Assert.IsNotNull(errorMessage, "Expected File.Delete to throw IOException");
                task.SafeFileDelete(fileName);
            }
            Assert.IsTrue(File.Exists(fileName));
            File.Delete(fileName);
            string expectedWarning = string.Format(CultureInfo.CurrentCulture, Resource.Failed_To_Delete_File_Error, fileName, errorMessage);

            TestHelper.AssertContainsWarnings(mockBuildEngine.ConsoleLogger, expectedWarning);
        }