2 // WriteLinesToFileTest.cs
5 // Ankit Jain (jankit@novell.com)
7 // Copyright 2010 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using System.Collections;
31 using Microsoft.Build.BuildEngine;
32 using Microsoft.Build.Framework;
33 using Microsoft.Build.Tasks;
34 using Microsoft.Build.Utilities;
35 using NUnit.Framework;
38 namespace MonoTests.Microsoft.Build.Tasks {
41 public class WriteLinesToFileTest
43 string full_path, full_filepath;
48 full_path = Path.GetFullPath (Path.Combine ("Test", "resources"));
49 full_filepath = Path.Combine (full_path, "foo.txt");
50 File.Delete (full_filepath);
54 public void TestDefault ()
56 CreateProjectAndCheck (full_filepath, null, true, false, delegate () {
57 CheckFileExists (full_filepath, true);
58 CheckLines (full_filepath, null);
63 public void TestDefaultWithExistingFile ()
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"});
73 public void TestOverwriteFile ()
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);
83 public void TestOverwriteFileWithExistingFile ()
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);
94 [Category("NotWorking")] // this fails due to an xbuild bug, it works on MS.NET
95 public void TestLineWithEscapedSemicolon ()
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"});
105 [Category("NotWorking")] // this fails due to an xbuild bug, it works on MS.NET
106 public void TestLineWithEscapedSpace ()
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 "});
116 public void TestLineWithEscapedQuote ()
118 string[] lines = new string[] { "%22abc test%22 123 %22def%22" };
119 CreateProjectAndCheck (full_filepath, lines, false, true, delegate () {
120 CheckFileExists (full_filepath, true);
121 CheckLines (full_filepath, new string [] {"\"abc test\" 123 \"def\""});
126 public void TestNoOverwrite ()
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 [] {"abc", "def"});
136 // appends in this case
137 public void TestNoOverwriteWithExistingFile ()
139 File.WriteAllText (full_filepath, "xyz");
140 string[] lines = new string[] { "abc", "def" };
141 CreateProjectAndCheck (full_filepath, lines, false, true, delegate () {
142 CheckFileExists (full_filepath, true);
143 CheckLines (full_filepath, new string [] {"xyzabc", "def"});
148 public void TestEmptyLinesOverwrite ()
150 CreateProjectAndCheck (full_filepath, new string[0], true, true,
152 CheckFileExists (full_filepath, false);
157 public void TestEmptyLinesOverwriteWithExisting ()
159 File.WriteAllText (full_filepath, "xyz");
160 CreateProjectAndCheck (full_filepath, new string[0], true, true,
162 CheckFileExists (full_filepath, false);
168 public void TestEmptyLinesNoOverwrite ()
170 CreateProjectAndCheck (full_filepath, new string[0], false, true,
172 CheckFileExists (full_filepath, true);
173 CheckLines (full_filepath, new string[0]);
178 public void TestEmptyLinesNoOverwriteWithExisting ()
180 File.WriteAllText (full_filepath, "xyz");
181 CreateProjectAndCheck (full_filepath, new string[0], false, true,
183 CheckFileExists (full_filepath, true);
184 CheckLines (full_filepath, new string [] {"xyz"});
188 void CreateProjectAndCheck (string file, string[] lines, bool overwrite, bool use_overwrite, Action action)
193 StringBuilder sb = new StringBuilder ();
194 sb.Append (@"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"" " + Consts.ToolsVersionString + @">
199 foreach (string line in lines)
200 sb.AppendFormat ("\t\t<Lines Include='{0}'/>\n", line);
202 sb.AppendFormat (@"</ItemGroup>
204 <WriteLinesToFile File='{0}' Lines='@(Lines)'", file);
207 sb.AppendFormat (" Overwrite='{0}' ", overwrite);
212 engine = new Engine (Consts.BinPath);
213 project = engine.CreateNewProject ();
215 TestMessageLogger logger = new TestMessageLogger ();
216 engine.RegisterLogger (logger);
218 project.LoadXml (sb.ToString ());
221 if (!project.Build ("1"))
222 Assert.Fail ("Build failed");
226 } catch (AssertionException) {
227 logger.DumpMessages ();
228 Console.WriteLine (sb.ToString ());
235 static void CheckFileExists (string file, bool should_exist)
237 Assert.AreEqual (should_exist, File.Exists (file), "File existence");
240 static void CheckLines (string full_filepath, string[] expected)
242 string[] actual = File.ReadAllLines (full_filepath);
243 Assert.AreEqual (expected != null ? expected.Length : 0, actual.Length, "Number of lines written don't match");
245 if (expected == null)
248 foreach (string line in actual)
249 Assert.AreEqual (expected[i++], line, "Z#" + i.ToString ());