/// <summary> /// Creates a new CreateClientFilesTask instance to use to generate code /// </summary> /// <param name="relativeTestDir"></param> /// <param name="includeClientOutputAssembly">if <c>true</c> include clients own output assembly in analysis</param> /// <returns>A new task instance that can be invoked to do code gen</returns> public static CreateClientFilesTask CreateClientFilesTaskInstance_CopyClientProjectToOutput(string serverProjectPath, string clientProjectPath, bool includeClientOutputAssembly) { CreateClientFilesTask task = new CreateClientFilesTask(); MockBuildEngine mockBuildEngine = new MockBuildEngine(); task.BuildEngine = mockBuildEngine; task.Language = "C#"; task.ServerProjectPath = serverProjectPath; task.ServerAssemblies = new TaskItem[] { new TaskItem(CodeGenHelper.ServerClassLibOutputAssembly(task.ServerProjectPath)) }; task.ServerReferenceAssemblies = MsBuildHelper.AsTaskItems(CodeGenHelper.ServerClassLibReferences(task.ServerProjectPath)).ToArray(); task.ClientFrameworkPath = CodeGenHelper.GetRuntimeDirectory(); // Generate the code to our deployment directory string tempFolder = CodeGenHelper.GenerateTempFolder(); task.OutputPath = Path.Combine(tempFolder, "FileWrites"); task.GeneratedCodePath = Path.Combine(tempFolder, "Generated_Code"); string clientProjectFileName = Path.GetFileName(clientProjectPath); string clientProjectDestPath = Path.Combine(tempFolder, clientProjectFileName); File.Copy(clientProjectPath, clientProjectDestPath); task.ClientProjectPath = clientProjectDestPath; task.ClientReferenceAssemblies = MsBuildHelper.AsTaskItems(CodeGenHelper.ClientClassLibReferences(clientProjectPath, includeClientOutputAssembly)).ToArray(); task.ClientSourceFiles = MsBuildHelper.AsTaskItems(CodeGenHelper.ClientClassLibSourceFiles(clientProjectPath)).ToArray(); return(task); }
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); } }
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); } }