1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
// Util.cs
//
// Author:
// Lluis Sanchez Gual <lluis@novell.com>
//
// Copyright (c) 2008 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
//
using System.IO;
using System.Xml;
using System.Collections;
using System.Text;
using MonoDevelop.Core;
using MonoDevelop.Core.ProgressMonitoring;
namespace UnitTests
{
public static class Util
{
static string rootDir;
static int projectId = 1;
public static string TestsRootDir {
get {
if (rootDir == null) {
rootDir = Path.GetDirectoryName (typeof(Util).Assembly.Location);
// If the test suite is running outside the source directory,
// the test-projects folder should be a direct subdirectory
if (!Directory.Exists (Path.Combine (rootDir, "test-projects"))) {
rootDir = Path.Combine (Path.Combine (rootDir, ".."), "..");
rootDir = Path.GetFullPath (Path.Combine (rootDir, "tests"));
}
}
return rootDir;
}
}
public static string TmpDir {
get { return Path.Combine (TestsRootDir, "tmp"); }
}
public static ProgressMonitor GetMonitor ()
{
return GetMonitor (true);
}
public static ProgressMonitor GetMonitor (bool ignoreLogMessages)
{
ConsoleProgressMonitor m = new ConsoleProgressMonitor ();
m.IgnoreLogMessages = ignoreLogMessages;
return m;
}
public static string GetSampleProject (params string[] projectName)
{
string srcDir = Path.Combine (Path.Combine (TestsRootDir, "test-projects"), Combine (projectName));
string projDir = srcDir;
srcDir = Path.GetDirectoryName (srcDir);
string tmpDir = CreateTmpDir (Path.GetFileName (projDir));
CopyDir (srcDir, tmpDir);
return Path.Combine (tmpDir, Path.GetFileName (projDir));
}
public static string GetSampleProjectPath (params string[] projectName)
{
return Path.Combine (Path.Combine (TestsRootDir, "test-projects"), Combine (projectName));
}
public static string CreateTmpDir (string hint)
{
string tmpDir = Path.Combine (TmpDir, hint + "-" + projectId.ToString ());
projectId++;
if (!Directory.Exists (tmpDir))
Directory.CreateDirectory (tmpDir);
return tmpDir;
}
public static void ClearTmpDir ()
{
if (Directory.Exists (TmpDir))
Directory.Delete (TmpDir, true);
projectId = 1;
}
public static string GetXmlFileInfoset (params string[] path)
{
string file = Combine (path);
XmlDocument doc = new XmlDocument ();
doc.Load (file);
return Infoset (doc);
}
public static string ToWindowsEndings (string s)
{
return s.Replace ("\r\n", "\n").Replace ("\n", "\r\n");
}
public static string ToSystemEndings (string s)
{
if (!Platform.IsWindows)
return s.Replace ("\r\n", "\n");
else
return s;
}
public static string ReadAllWithWindowsEndings (string fileName)
{
return File.ReadAllText (fileName).Replace ("\r\n", "\n").Replace ("\n", "\r\n");
}
static void CopyDir (string src, string dst)
{
if (Path.GetFileName (src) == ".svn")
return;
if (!Directory.Exists (dst))
Directory.CreateDirectory (dst);
foreach (string file in Directory.GetFiles (src))
File.Copy (file, Path.Combine (dst, Path.GetFileName (file)), overwrite: true);
foreach (string dir in Directory.GetDirectories (src))
CopyDir (dir, Path.Combine (dst, Path.GetFileName (dir)));
}
public static string Infoset (XmlNode nod)
{
StringBuilder sb = new StringBuilder ();
GetInfoset (nod, sb);
return sb.ToString ();
}
static void GetInfoset (XmlNode nod, StringBuilder sb)
{
switch (nod.NodeType) {
case XmlNodeType.Document:
GetInfoset (((XmlDocument)nod).DocumentElement, sb);
break;
case XmlNodeType.Attribute:
if (nod.LocalName == "xmlns" && nod.NamespaceURI == "http://www.w3.org/2000/xmlns/") return;
sb.Append (" " + nod.NamespaceURI + ":" + nod.LocalName + "='" + nod.Value + "'");
break;
case XmlNodeType.Element:
XmlElement elem = (XmlElement) nod;
sb.Append ("<" + elem.NamespaceURI + ":" + elem.LocalName);
ArrayList ats = new ArrayList ();
foreach (XmlAttribute at in elem.Attributes)
ats.Add (at.LocalName + " " + at.NamespaceURI);
ats.Sort ();
foreach (string name in ats) {
string[] nn = name.Split (' ');
GetInfoset (elem.Attributes[nn[0], nn[1]], sb);
}
sb.Append (">");
foreach (XmlNode cn in elem.ChildNodes)
GetInfoset (cn, sb);
sb.Append ("</>");
break;
default:
sb.Append (nod.OuterXml);
break;
}
}
public static string Combine (params string[] paths)
{
string p = paths [0];
for (int n=1; n<paths.Length; n++)
p = Path.Combine (p, paths [n]);
return p;
}
}
}
|