added some development tools
[windows-sources.git] / developer / VSSDK / VisualStudioIntegration / Common / Source / CSharp / UnitTest / FileGenerator.cs
blobce7d4c60816401bd0e7a46d604010c04f0d75522
1 /***************************************************************************
3 Copyright (c) Microsoft Corporation. All rights reserved.
4 This code is licensed under the Visual Studio SDK license terms.
5 THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
6 ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
7 IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
8 PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
10 ***************************************************************************/
12 using System;
13 using System.Xml;
14 using System.IO;
15 using System.Text;
16 using System.Reflection;
18 namespace Microsoft.VsSDK.UnitTestLibrary
20 public sealed class FileGenerator
22 private string path;
24 public FileGenerator(string relativePath)
26 // Generate temp directory name
27 path = Path.Combine(Path.GetTempPath(), relativePath);
29 // Delete it if it already exist to prevent being affected by previous runs
30 try
32 if (Directory.Exists(path))
33 Directory.Delete(path, true);
35 catch (IOException)
36 { }
38 // Create the directory
39 Directory.CreateDirectory(path);
42 /// <summary>
43 /// Create the specified path under a temp directory
44 /// The file will have some content
45 /// </summary>
46 /// <param name="fileName">FileName, can include relative path</param>
47 public string CreateFile(string fileName)
49 return CreateFileWithSpecificContent(fileName, fileName);
52 public string CreateFileFromEmbeddedContent(string fileName, string content)
54 return CreateFileWithSpecificContent(fileName, content);
57 public string CreateXmlFileFromEmbeddedContent(string fileName, string content)
59 // Create an XML document with the specific content
60 XmlDocument doc = new XmlDocument();
61 doc.LoadXml(content);
63 string outputPath = this.GetFullPath(fileName);
64 doc.Save(outputPath);
66 return outputPath;
69 /// <summary>
70 /// Create the specified path under a temp directory
71 /// Add the specified content to the file
72 /// </summary>
73 /// <param name="fileName">FileName, can include relative path</param>
74 /// <param name="content">Content to add to the file</param>
75 public string CreateFileWithSpecificContent(string fileName, string content)
77 string filePath = this.GetFullPath(fileName);
78 string directory = Path.GetDirectoryName(filePath);
79 if (!Directory.Exists(directory))
80 Directory.CreateDirectory(directory);
81 using (StreamWriter file = File.CreateText(filePath))
83 file.WriteLine(content);
86 return filePath;
89 /// <summary>
90 /// Verify that the files have the same content
91 /// </summary>
92 /// <param name="path1">Full path of one of the file</param>
93 /// <param name="path2">Full path of the other file</param>
94 /// <param name="comparaisonType">What kind of comparaison to use</param>
95 /// <returns></returns>
96 public static bool FilesContentIsSame(string path1, string path2, StringComparison comparisonType)
98 string content1;
99 string content2;
100 using (StreamReader contentReader = File.OpenText(path1))
102 content1 = contentReader.ReadToEnd();
104 using (StreamReader contentReader = File.OpenText(path2))
106 content2 = contentReader.ReadToEnd();
109 return String.Equals(content1, content2, comparisonType);
112 private string GetFullPath(string fileName)
114 string filePath = Path.Combine(path, fileName);
116 return filePath;