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 ***************************************************************************/
16 using System
.Reflection
;
18 namespace Microsoft
.VsSDK
.UnitTestLibrary
20 public sealed class FileGenerator
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
32 if (Directory
.Exists(path
))
33 Directory
.Delete(path
, true);
38 // Create the directory
39 Directory
.CreateDirectory(path
);
43 /// Create the specified path under a temp directory
44 /// The file will have some content
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();
63 string outputPath
= this.GetFullPath(fileName
);
70 /// Create the specified path under a temp directory
71 /// Add the specified content to the file
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
);
90 /// Verify that the files have the same content
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
)
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
);