Fix failures in MSBuild tests on Windows
[mono.git] / mcs / class / Microsoft.Build.Tasks / Test / Microsoft.Build.Tasks / WriteLinesToFileTest.cs
1 //
2 // WriteLinesToFileTest.cs
3 //
4 // Author:
5 //   Ankit Jain (jankit@novell.com)
6 //
7 // Copyright 2010 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
28 using System;
29 using System.Collections;
30 using System.IO;
31 using Microsoft.Build.BuildEngine;
32 using Microsoft.Build.Framework;
33 using Microsoft.Build.Tasks;
34 using Microsoft.Build.Utilities;
35 using NUnit.Framework;
36 using System.Text;
37
38 namespace MonoTests.Microsoft.Build.Tasks {
39
40         [TestFixture]
41         public class WriteLinesToFileTest
42         {
43                 string full_path, full_filepath;
44
45                 [SetUp]
46                 public void Setup ()
47                 {
48                         full_path = Path.GetFullPath (Path.Combine ("Test", "resources"));
49                         full_filepath = Path.Combine (full_path, "foo.txt");
50                         File.Delete (full_filepath);
51                 }
52
53                 [Test]
54                 public void TestDefault ()
55                 {
56                         CreateProjectAndCheck (full_filepath, null, true, false, delegate () {
57                                 CheckFileExists (full_filepath, true);
58                                 CheckLines (full_filepath, null);
59                         });
60                 }
61
62                 [Test]
63                 public void TestDefaultWithExistingFile ()
64                 {
65                         File.WriteAllText (full_filepath, "xyz");
66                         CreateProjectAndCheck (full_filepath, null, true, false, delegate () {
67                                 CheckFileExists (full_filepath, true);
68                                 CheckLines (full_filepath, new string [] {"xyz"});
69                         });
70                 }
71
72                 [Test]
73                 public void TestOverwriteFile ()
74                 {
75                         string[] lines = new string[] { "abc", "def" };
76                         CreateProjectAndCheck (full_filepath, lines, true, true, delegate () {
77                                 CheckFileExists (full_filepath, true);
78                                 CheckLines (full_filepath, lines);
79                         });
80                 }
81
82                 [Test]
83                 public void TestOverwriteFileWithExistingFile ()
84                 {
85                         File.WriteAllText (full_filepath, "xyz");
86                         string[] lines = new string[] { "abc", "def" };
87                         CreateProjectAndCheck (full_filepath, lines, true, true, delegate () {
88                                 CheckFileExists (full_filepath, true);
89                                 CheckLines (full_filepath, lines);
90                         });
91                 }
92
93                 [Test]
94                 [Category("NotWorking")] // this fails due to an xbuild bug, it works on MS.NET
95                 public void TestLineWithEscapedSemicolon ()
96                 {
97                         string[] lines = new string[] { "abc%3Btest%3B%3B", "%3Bdef" };
98                         CreateProjectAndCheck (full_filepath, lines, false, true, delegate () {
99                                 CheckFileExists (full_filepath, true);
100                                 CheckLines (full_filepath, new string [] {"abc;test;;", ";def"});
101                         });
102                 }
103
104                 [Test]
105                 [Category("NotWorking")] // this fails due to an xbuild bug, it works on MS.NET
106                 public void TestLineWithEscapedSpace ()
107                 {
108                         string[] lines = new string[] { "  %20%20abc%20test  ", "  def%20%20" };
109                         CreateProjectAndCheck (full_filepath, lines, false, true, delegate () {
110                                 CheckFileExists (full_filepath, true);
111                                 CheckLines (full_filepath, new string [] {"  abc test", "def  "});
112                         });
113                 }
114
115                 [Test]
116                 public void TestLineWithEscapedQuote ()
117                 {
118                         if (Environment.OSVersion.Platform != PlatformID.Unix) {
119                                 Assert.Ignore ("Throws \"Illegal characters in path\" on Windows since \" is not a legal Windows path character");
120                         }
121                         string[] lines = new string[] { "%22abc test%22 123 %22def%22" };
122                         CreateProjectAndCheck (full_filepath, lines, false, true, delegate () {
123                                 CheckFileExists (full_filepath, true);
124                                 CheckLines (full_filepath, new string [] {"\"abc test\" 123 \"def\""});
125                         });
126                 }
127
128                 [Test]
129                 public void TestNoOverwrite ()
130                 {
131                         string[] lines = new string[] { "abc", "def" };
132                         CreateProjectAndCheck (full_filepath, lines, false, true, delegate () {
133                                 CheckFileExists (full_filepath, true);
134                                 CheckLines (full_filepath, new string [] {"abc", "def"});
135                         });
136                 }
137
138                 [Test]
139                 // appends in this case
140                 public void TestNoOverwriteWithExistingFile ()
141                 {
142                         File.WriteAllText (full_filepath, "xyz");
143                         string[] lines = new string[] { "abc", "def" };
144                         CreateProjectAndCheck (full_filepath, lines, false, true, delegate () {
145                                 CheckFileExists (full_filepath, true);
146                                 CheckLines (full_filepath, new string [] {"xyzabc", "def"});
147                         });
148                 }
149
150                 [Test]
151                 public void TestEmptyLinesOverwrite ()
152                 {
153                         CreateProjectAndCheck (full_filepath, new string[0], true, true,
154                                 delegate () {
155                                         CheckFileExists (full_filepath, false);
156                                 });
157                 }
158
159                 [Test]
160                 public void TestEmptyLinesOverwriteWithExisting ()
161                 {
162                         File.WriteAllText (full_filepath, "xyz");
163                         CreateProjectAndCheck (full_filepath, new string[0], true, true,
164                                 delegate () {
165                                         CheckFileExists (full_filepath, false);
166                                 });
167                 }
168
169
170                 [Test]
171                 public void TestEmptyLinesNoOverwrite ()
172                 {
173                         CreateProjectAndCheck (full_filepath, new string[0], false, true,
174                                 delegate () {
175                                         CheckFileExists (full_filepath, true);
176                                         CheckLines (full_filepath, new string[0]);
177                                 });
178                 }
179
180                 [Test]
181                 public void TestEmptyLinesNoOverwriteWithExisting ()
182                 {
183                         File.WriteAllText (full_filepath, "xyz");
184                         CreateProjectAndCheck (full_filepath, new string[0], false, true,
185                                 delegate () {
186                                         CheckFileExists (full_filepath, true);
187                                         CheckLines (full_filepath, new string [] {"xyz"});
188                                 });
189                 }
190
191                 void CreateProjectAndCheck (string file, string[] lines, bool overwrite, bool use_overwrite, Action action)
192                 {
193                         Engine engine;
194                         Project project;
195
196                         StringBuilder sb = new StringBuilder ();
197                         sb.Append (@"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"" " + Consts.ToolsVersionString + @">
198         <ItemGroup>
199 ");
200
201                         if (lines != null)
202                                 foreach (string line in lines)
203                                         sb.AppendFormat ("\t\t<Lines Include='{0}'/>\n", line);
204
205                         sb.AppendFormat (@"</ItemGroup>
206                                         <Target Name='1'>
207                                                 <WriteLinesToFile File='{0}' Lines='@(Lines)'", file);
208
209                         if (use_overwrite)
210                                 sb.AppendFormat (" Overwrite='{0}' ", overwrite);
211                         sb.Append (@"/>
212                                         </Target>
213                                 </Project>");
214
215                         engine = new Engine (Consts.BinPath);
216                         project = engine.CreateNewProject ();
217
218                         TestMessageLogger logger = new TestMessageLogger ();
219                         engine.RegisterLogger (logger);
220
221                         project.LoadXml (sb.ToString ());
222
223                         try {
224                                 if (!project.Build ("1"))
225                                         Assert.Fail ("Build failed");
226
227                                 if (action != null)
228                                         action.Invoke ();
229                         } catch (AssertionException) {
230                                 logger.DumpMessages ();
231                                 Console.WriteLine (sb.ToString ());
232                                 throw;
233                         } finally {
234                                 File.Delete (file);
235                         }
236                 }
237
238                 static void CheckFileExists (string file, bool should_exist)
239                 {
240                         Assert.AreEqual (should_exist, File.Exists (file), "File existence");
241                 }
242
243                 static void CheckLines (string full_filepath, string[] expected)
244                 {
245                         string[] actual = File.ReadAllLines (full_filepath);
246                         Assert.AreEqual (expected != null ? expected.Length : 0, actual.Length, "Number of lines written don't match");
247
248                         if (expected == null)
249                                 return;
250                         int i = 0;
251                         foreach (string line in actual)
252                                 Assert.AreEqual (expected[i++], line, "Z#" + i.ToString ());
253                 }
254         }
255 }