Fixes process names trimmed to 15 chars.
[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                 public void TestLineWithEscapedSemicolon ()
95                 {
96                         string[] lines = new string[] { "abc%3Btest%3B%3B", "%3Bdef" };
97                         CreateProjectAndCheck (full_filepath, lines, false, true, delegate () {
98                                 CheckFileExists (full_filepath, true);
99                                 CheckLines (full_filepath, new string [] {"abc;test;;", ";def"});
100                         });
101                 }
102
103                 [Test]
104                 public void TestLineWithEscapedSpace ()
105                 {
106                         string[] lines = new string[] { "  %20%20abc%20test  ", "  def%20%20" };
107                         CreateProjectAndCheck (full_filepath, lines, false, true, delegate () {
108                                 CheckFileExists (full_filepath, true);
109                                 CheckLines (full_filepath, new string [] {"  abc test", "def  "});
110                         });
111                 }
112
113                 [Test]
114                 public void TestNoOverwrite ()
115                 {
116                         string[] lines = new string[] { "abc", "def" };
117                         CreateProjectAndCheck (full_filepath, lines, false, true, delegate () {
118                                 CheckFileExists (full_filepath, true);
119                                 CheckLines (full_filepath, new string [] {"abc", "def"});
120                         });
121                 }
122
123                 [Test]
124                 // appends in this case
125                 public void TestNoOverwriteWithExistingFile ()
126                 {
127                         File.WriteAllText (full_filepath, "xyz");
128                         string[] lines = new string[] { "abc", "def" };
129                         CreateProjectAndCheck (full_filepath, lines, false, true, delegate () {
130                                 CheckFileExists (full_filepath, true);
131                                 CheckLines (full_filepath, new string [] {"xyzabc", "def"});
132                         });
133                 }
134
135                 [Test]
136                 public void TestEmptyLinesOverwrite ()
137                 {
138                         CreateProjectAndCheck (full_filepath, new string[0], true, true,
139                                 delegate () {
140                                         CheckFileExists (full_filepath, false);
141                                 });
142                 }
143
144                 [Test]
145                 public void TestEmptyLinesOverwriteWithExisting ()
146                 {
147                         File.WriteAllText (full_filepath, "xyz");
148                         CreateProjectAndCheck (full_filepath, new string[0], true, true,
149                                 delegate () {
150                                         CheckFileExists (full_filepath, false);
151                                 });
152                 }
153
154
155                 [Test]
156                 public void TestEmptyLinesNoOverwrite ()
157                 {
158                         CreateProjectAndCheck (full_filepath, new string[0], false, true,
159                                 delegate () {
160                                         CheckFileExists (full_filepath, true);
161                                         CheckLines (full_filepath, new string[0]);
162                                 });
163                 }
164
165                 [Test]
166                 public void TestEmptyLinesNoOverwriteWithExisting ()
167                 {
168                         File.WriteAllText (full_filepath, "xyz");
169                         CreateProjectAndCheck (full_filepath, new string[0], false, true,
170                                 delegate () {
171                                         CheckFileExists (full_filepath, true);
172                                         CheckLines (full_filepath, new string [] {"xyz"});
173                                 });
174                 }
175
176                 void CreateProjectAndCheck (string file, string[] lines, bool overwrite, bool use_overwrite, Action action)
177                 {
178                         Engine engine;
179                         Project project;
180
181                         StringBuilder sb = new StringBuilder ();
182                         sb.Append (@"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"" " + Consts.ToolsVersionString + @">
183         <ItemGroup>
184 ");
185
186                         if (lines != null)
187                                 foreach (string line in lines)
188                                         sb.AppendFormat ("\t\t<Lines Include='{0}'/>\n", line);
189
190                         sb.AppendFormat (@"</ItemGroup>
191                                         <Target Name='1'>
192                                                 <WriteLinesToFile File='{0}' Lines='@(Lines)'", file);
193
194                         if (use_overwrite)
195                                 sb.AppendFormat (" Overwrite='{0}' ", overwrite);
196                         sb.Append (@"/>
197                                         </Target>
198                                 </Project>");
199
200                         engine = new Engine (Consts.BinPath);
201                         project = engine.CreateNewProject ();
202
203                         TestMessageLogger logger = new TestMessageLogger ();
204                         engine.RegisterLogger (logger);
205
206                         project.LoadXml (sb.ToString ());
207
208                         try {
209                                 if (!project.Build ("1"))
210                                         Assert.Fail ("Build failed");
211
212                                 if (action != null)
213                                         action.Invoke ();
214                         } catch (AssertionException) {
215                                 logger.DumpMessages ();
216                                 Console.WriteLine (sb.ToString ());
217                                 throw;
218                         } finally {
219                                 File.Delete (file);
220                         }
221                 }
222
223                 static void CheckFileExists (string file, bool should_exist)
224                 {
225                         Assert.AreEqual (should_exist, File.Exists (file), "File existence");
226                 }
227
228                 static void CheckLines (string full_filepath, string[] expected)
229                 {
230                         string[] actual = File.ReadAllLines (full_filepath);
231                         Assert.AreEqual (expected != null ? expected.Length : 0, actual.Length, "Number of lines written don't match");
232
233                         if (expected == null)
234                                 return;
235                         int i = 0;
236                         foreach (string line in actual)
237                                 Assert.AreEqual (expected[i++], line, "Z#" + i.ToString ());
238                 }
239         }
240 }