public void CreateRiaClientFiles_Safe_Folder_Create()
        {
            CleanOpenRiaClientFilesTask task = new CleanOpenRiaClientFilesTask();
            MockBuildEngine mockBuildEngine = new MockBuildEngine();
            task.BuildEngine = mockBuildEngine;

            string fakeFolder = @"FAKE:\notAFolder";
            string realMessage = null;
            try
            {
                Directory.CreateDirectory(fakeFolder);
            }
            catch (Exception e)
            {
                realMessage = e.Message;
            }

            Assert.IsNotNull(realMessage, "Expected creation of fake folder " + fakeFolder + " to fail.");

            bool success = task.SafeFolderCreate(fakeFolder);
            Assert.IsFalse(success, "Expected SafeFolderCreate to report failure for " + fakeFolder);
            Assert.IsFalse(Directory.Exists(fakeFolder), "Did not expect SafeFolderCreate to really create " + fakeFolder);

            string expectedMessage = string.Format(CultureInfo.CurrentCulture, Resource.Failed_To_Create_Folder, fakeFolder, realMessage);
            TestHelper.AssertContainsErrors(mockBuildEngine.ConsoleLogger, expectedMessage);

            // Clear errors
            mockBuildEngine.ConsoleLogger.Reset();

            string realFolder = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
            Assert.IsFalse(Directory.Exists(realFolder), "Did not expect temp folder to exist.");

            try
            {
                success = task.SafeFolderCreate(realFolder);
                Assert.IsTrue(success, "Expected SafeFolderCreate to have reported success for " + realFolder);
                Assert.IsTrue(Directory.Exists(realFolder), "Expected SafeFolderCreate to have created " + realFolder);
            }
            finally
            {
                if (Directory.Exists(realFolder))
                {
                    Directory.Delete(realFolder);
                }
            }
        }
        public void RiaClientFilesTask_Safe_File_Copy()
        {
            CleanOpenRiaClientFilesTask task = new CleanOpenRiaClientFilesTask();
            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 CleanRiaClientFiles_Deletes_Generated_Files()
        {
            CreateOpenRiaClientFilesTask task = null;
            MockBuildEngine mockBuildEngine;
            try
            {
                // ====================================================
                // Test setup -- generate code by calling Create task
                // ====================================================
                task = CodeGenHelper.CreateOpenRiaClientFilesTaskInstance("CLRCF1", /*includeClientOutputAssembly*/ false);
                bool success = task.Execute();
                if (!success)
                {
                    mockBuildEngine = task.BuildEngine as MockBuildEngine;
                    Assert.Fail("CreateOpenRiaClientFilesTask failed:\r\n" + mockBuildEngine.ConsoleLogger.Errors);
                }

                string generatedCodeOutputFolder = task.GeneratedCodePath;
                Assert.IsTrue(Directory.Exists(generatedCodeOutputFolder), "Expected task to have created " + generatedCodeOutputFolder);

                string[] files = Directory.GetFiles(generatedCodeOutputFolder);
                Assert.AreEqual(3, files.Length, "Code gen should have generated 3 code files");

                string generatedFile = Path.Combine(generatedCodeOutputFolder, "ServerClassLib.g.cs");
                Assert.IsTrue(File.Exists(generatedFile), "Expected task to have generated " + generatedFile);

                string copiedFile = Path.Combine(generatedCodeOutputFolder, "TestEntity.shared.cs");
                Assert.IsTrue(File.Exists(copiedFile), "Expected task to have copied " + copiedFile);

                copiedFile = Path.Combine(generatedCodeOutputFolder, "TestComplexType.shared.cs");
                Assert.IsTrue(File.Exists(copiedFile), "Expected task to have copied " + copiedFile);

                string outputFolder = task.OutputPath;
                Assert.IsTrue(Directory.Exists(outputFolder), "Expected task to have created " + outputFolder);

                files = Directory.GetFiles(outputFolder);
                string generatedFiles = string.Empty;
                foreach (string file in files)
                    generatedFiles += (file + Environment.NewLine);

                Assert.AreEqual(5, files.Length, "Code gen should have generated this many ancillary files but instead saw:" + Environment.NewLine + generatedFiles);

                string fileList = Path.Combine(outputFolder, "ClientClassLib.OpenRiaFiles.txt");
                Assert.IsTrue(File.Exists(fileList), "Expected code gen to have created " + fileList + " but saw:" +
                    Environment.NewLine + generatedFiles);

                string refList = Path.Combine(outputFolder, "ClientClassLib.OpenRiaClientRefs.txt");
                Assert.IsTrue(File.Exists(refList), "Expected code gen to have created " + refList + " but saw:" +
                    Environment.NewLine + generatedFiles);

                refList = Path.Combine(outputFolder, "ClientClassLib.OpenRiaServerRefs.txt");
                Assert.IsTrue(File.Exists(refList), "Expected code gen to have created " + refList + " but saw:" +
                    Environment.NewLine + generatedFiles);

                string sourceFileList = Path.Combine(outputFolder, "ClientClassLib.OpenRiaSourceFiles.txt");
                Assert.IsTrue(File.Exists(sourceFileList), "Expected code gen to have created " + sourceFileList + " but saw:" +
                    Environment.NewLine + generatedFiles);

                string riaLinkList = Path.Combine(outputFolder, "ClientClassLib.OpenRiaLinks.txt");
                Assert.IsTrue(File.Exists(riaLinkList), "Expected code gen to have created " + riaLinkList + " but saw:" +
                    Environment.NewLine + generatedFiles);

                // ==========================================
                // Main body of test -- the Clean
                // ==========================================

                // Step 1: instantiate Clean task instance and execute it, giving it same info as the Create task
                CleanOpenRiaClientFilesTask cleanTask = new CleanOpenRiaClientFilesTask();
                mockBuildEngine = new MockBuildEngine();
                cleanTask.BuildEngine = mockBuildEngine;
                cleanTask.OutputPath = task.OutputPath;
                cleanTask.GeneratedCodePath = task.GeneratedCodePath;
                cleanTask.ClientProjectPath = task.ClientProjectPath;
                success = cleanTask.Execute();
                Assert.IsTrue(success, "Clean task returned false");

                // No errors or warnings allowed
                TestHelper.AssertNoErrorsOrWarnings(mockBuildEngine.ConsoleLogger);

                // Step 2: validate files created above are gone
                // TODO, 244509: we no longer remove empty folder
                // Assert.IsFalse(Directory.Exists(generatedCodeOutputFolder), "Expected clean to have deleted " + generatedCodeOutputFolder);
                Assert.IsFalse(File.Exists(fileList), "Expected clean to have deleted " + fileList);
                Assert.IsFalse(File.Exists(refList), "Expected clean to have deleted " + refList);
                Assert.IsFalse(File.Exists(sourceFileList), "Expected clean to have deleted " + sourceFileList);
                Assert.IsFalse(File.Exists(riaLinkList), "Expected clean to have deleted " + riaLinkList);

                // Step 3: verify redundant clean does no harm and succeeds
                success = cleanTask.Execute();
                Assert.IsTrue(success, "Clean task returned false");
            }
            finally
            {
                CodeGenHelper.DeleteTempFolder(task);
            }
        }
        public void RiaClientFilesTask_Safe_File_Move()
        {
            CleanOpenRiaClientFilesTask task = new CleanOpenRiaClientFilesTask();
            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);
            }
        }
        public void CleanRiaClientFiles_Safe_File_Delete()
        {
            CleanOpenRiaClientFilesTask task = new CleanOpenRiaClientFilesTask();
            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);
        }