In class/Microsoft.Build.Tasks:
authorAnkit Jain <radical@corewars.org>
Thu, 4 Feb 2010 17:29:57 +0000 (17:29 -0000)
committerAnkit Jain <radical@corewars.org>
Thu, 4 Feb 2010 17:29:57 +0000 (17:29 -0000)
* Microsoft.Build.Tasks_test.dll.sources: Added
WriteLinesToFileTest.cs

In class/Microsoft.Build.Tasks/Microsoft.Build.Tasks:

* WriteLinesToFile.cs: Delete the file if there is nothing to
be written and overwrite==true.

In class/Microsoft.Build.Tasks/Test/Microsoft.Build.Tasks:

* WriteLinesToFileTest.cs: New.

svn path=/trunk/mcs/; revision=150873

mcs/class/Microsoft.Build.Tasks/ChangeLog
mcs/class/Microsoft.Build.Tasks/Microsoft.Build.Tasks/ChangeLog
mcs/class/Microsoft.Build.Tasks/Microsoft.Build.Tasks/WriteLinesToFile.cs
mcs/class/Microsoft.Build.Tasks/Microsoft.Build.Tasks_test.dll.sources
mcs/class/Microsoft.Build.Tasks/Test/Microsoft.Build.Tasks/ChangeLog
mcs/class/Microsoft.Build.Tasks/Test/Microsoft.Build.Tasks/WriteLinesToFileTest.cs [new file with mode: 0755]

index 595970cdb2691ad6569d82b1c77a183d2060b41d..546db93b3bbc53cf9145a6b140aaf798681b31b5 100644 (file)
@@ -1,3 +1,8 @@
+2010-02-04  Ankit Jain  <jankit@novell.com>
+
+       * Microsoft.Build.Tasks_test.dll.sources: Added
+       WriteLinesToFileTest.cs
+
 2009-12-22  Ankit Jain  <jankit@novell.com>
 
        * Microsoft.Build.Tasks.dll.sources: Add DirectoryScanner.cs
index cbd4e6f2991dc56ba427f64c3fa49d943392f2bd..8461250815908ce31f1ffcabce38786326b70a98 100644 (file)
@@ -1,3 +1,8 @@
+2010-02-04  Ankit Jain  <jankit@novell.com>
+
+       * WriteLinesToFile.cs: Delete the file if there is nothing to
+       be written and overwrite==true.
+
 2010-02-02     Rodrigo B. de Oliveira  <rodrigo@unity3d.com>
 
        * AssemblyResolver.cs:
index 84c140ae0f05edc0ac7270202a9f168e1a7c3a35..80456985f5445ad449eae6acc462a2b211256e70 100644 (file)
@@ -49,10 +49,18 @@ namespace Microsoft.Build.Tasks {
                public override bool Execute ()
                {
                        try {
-                               streamWriter = new StreamWriter (file.GetMetadata ("FullPath"), !overwrite);
-                               if (lines != null)
-                                       foreach (ITaskItem line in lines)
-                                               streamWriter.WriteLine (line);
+                               string fullpath = file.GetMetadata ("FullPath");
+                               if (lines == null && overwrite) {
+                                       System.IO.File.Delete (fullpath);
+                                       return true;
+                               }
+
+                               using (streamWriter = new StreamWriter (fullpath, !overwrite)) {
+                                       if (lines != null)
+                                               foreach (ITaskItem line in lines)
+                                                       streamWriter.WriteLine (line);
+                               }
+
                                return true;
                        }
                        catch (Exception ex) {
index ec52a76870dc8567919f491a15a5bdbb0edb0cd8..0b2d478491bfce2df0c2bf5644edcf7498cdae27 100644 (file)
@@ -25,3 +25,4 @@ Microsoft.Build.Tasks/TestEngine.cs
 Microsoft.Build.Tasks/TestMessageLogger.cs
 Microsoft.Build.Tasks/TaskBatchingTest.cs
 Microsoft.Build.Tasks/WarningTest.cs
+Microsoft.Build.Tasks/WriteLinesToFileTest.cs
index 887c0ef64ced8ba6ee2039bb03097129f38b648f..a4b6caeb3afe8988941ed11c459855a5036be336 100644 (file)
@@ -1,3 +1,7 @@
+2010-02-04  Ankit Jain  <jankit@novell.com>
+
+       * WriteLinesToFileTest.cs: New.
+
 2009-12-22  Ankit Jain  <jankit@novell.com>
 
        * CreateItemTest.cs (TestItemsWithWildcards): New.
diff --git a/mcs/class/Microsoft.Build.Tasks/Test/Microsoft.Build.Tasks/WriteLinesToFileTest.cs b/mcs/class/Microsoft.Build.Tasks/Test/Microsoft.Build.Tasks/WriteLinesToFileTest.cs
new file mode 100755 (executable)
index 0000000..62c03ac
--- /dev/null
@@ -0,0 +1,220 @@
+//
+// WriteLinesToFileTest.cs
+//
+// Author:
+//   Ankit Jain (jankit@novell.com)
+//
+// Copyright 2010 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;
+using System.Collections;
+using System.IO;
+using Microsoft.Build.BuildEngine;
+using Microsoft.Build.Framework;
+using Microsoft.Build.Tasks;
+using Microsoft.Build.Utilities;
+using NUnit.Framework;
+using System.Text;
+
+namespace MonoTests.Microsoft.Build.Tasks {
+
+       [TestFixture]
+       public class WriteLinesToFileTest
+       {
+               string full_path, full_filepath;
+
+               [SetUp]
+               public void Setup ()
+               {
+                       full_path = Path.GetFullPath (Path.Combine ("Test", "resources"));
+                       full_filepath = Path.Combine (full_path, "foo.txt");
+                       File.Delete (full_filepath);
+               }
+
+               [Test]
+               public void TestDefault ()
+               {
+                       CreateProjectAndCheck (full_filepath, null, true, false, delegate () {
+                               CheckFileExists (full_filepath, true);
+                               CheckLines (full_filepath, null);
+                       });
+               }
+
+               [Test]
+               public void TestDefaultWithExistingFile ()
+               {
+                       File.WriteAllText (full_filepath, "xyz");
+                       CreateProjectAndCheck (full_filepath, null, true, false, delegate () {
+                               CheckFileExists (full_filepath, true);
+                               CheckLines (full_filepath, new string [] {"xyz"});
+                       });
+               }
+
+               [Test]
+               public void TestOverwriteFile ()
+               {
+                       string[] lines = new string[] { "abc", "def" };
+                       CreateProjectAndCheck (full_filepath, lines, true, true, delegate () {
+                               CheckFileExists (full_filepath, true);
+                               CheckLines (full_filepath, lines);
+                       });
+               }
+
+               [Test]
+               public void TestOverwriteFileWithExistingFile ()
+               {
+                       File.WriteAllText (full_filepath, "xyz");
+                       string[] lines = new string[] { "abc", "def" };
+                       CreateProjectAndCheck (full_filepath, lines, true, true, delegate () {
+                               CheckFileExists (full_filepath, true);
+                               CheckLines (full_filepath, lines);
+                       });
+               }
+
+               [Test]
+               public void TestNoOverwrite ()
+               {
+                       string[] lines = new string[] { "abc", "def" };
+                       CreateProjectAndCheck (full_filepath, lines, false, true, delegate () {
+                               CheckFileExists (full_filepath, true);
+                               CheckLines (full_filepath, new string [] {"abc", "def"});
+                       });
+               }
+
+               [Test]
+               // appends in this case
+               public void TestNoOverwriteWithExistingFile ()
+               {
+                       File.WriteAllText (full_filepath, "xyz");
+                       string[] lines = new string[] { "abc", "def" };
+                       CreateProjectAndCheck (full_filepath, lines, false, true, delegate () {
+                               CheckFileExists (full_filepath, true);
+                               CheckLines (full_filepath, new string [] {"xyzabc", "def"});
+                       });
+               }
+
+               [Test]
+               public void TestEmptyLinesOverwrite ()
+               {
+                       CreateProjectAndCheck (full_filepath, new string[0], true, true,
+                               delegate () {
+                                       CheckFileExists (full_filepath, false);
+                               });
+               }
+
+               [Test]
+               public void TestEmptyLinesOverwriteWithExisting ()
+               {
+                       File.WriteAllText (full_filepath, "xyz");
+                       CreateProjectAndCheck (full_filepath, new string[0], true, true,
+                               delegate () {
+                                       CheckFileExists (full_filepath, false);
+                               });
+               }
+
+
+               [Test]
+               public void TestEmptyLinesNoOverwrite ()
+               {
+                       CreateProjectAndCheck (full_filepath, new string[0], false, true,
+                               delegate () {
+                                       CheckFileExists (full_filepath, true);
+                                       CheckLines (full_filepath, new string[0]);
+                               });
+               }
+
+               [Test]
+               public void TestEmptyLinesNoOverwriteWithExisting ()
+               {
+                       File.WriteAllText (full_filepath, "xyz");
+                       CreateProjectAndCheck (full_filepath, new string[0], false, true,
+                               delegate () {
+                                       CheckFileExists (full_filepath, true);
+                                       CheckLines (full_filepath, new string [] {"xyz"});
+                               });
+               }
+
+               void CreateProjectAndCheck (string file, string[] lines, bool overwrite, bool use_overwrite, Action action)
+               {
+                       Engine engine;
+                       Project project;
+
+                       StringBuilder sb = new StringBuilder ();
+                       sb.Append (@"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"" ToolsVersion='3.5'>
+       <ItemGroup>
+");
+
+                       if (lines != null)
+                               foreach (string line in lines)
+                                       sb.AppendFormat ("\t\t<Lines Include='{0}'/>\n", line);
+
+                       sb.AppendFormat (@"</ItemGroup>
+                                       <Target Name='1'>
+                                               <WriteLinesToFile File='{0}' Lines='@(Lines)'", file);
+
+                       if (use_overwrite)
+                               sb.AppendFormat (" Overwrite='{0}' ", overwrite);
+                       sb.Append (@"/>
+                                       </Target>
+                               </Project>");
+
+                       engine = new Engine (Consts.BinPath);
+                       project = engine.CreateNewProject ();
+
+                       TestMessageLogger logger = new TestMessageLogger ();
+                       engine.RegisterLogger (logger);
+
+                       project.LoadXml (sb.ToString ());
+
+                       try {
+                               if (!project.Build ("1"))
+                                       Assert.Fail ("Build failed");
+
+                               if (action != null)
+                                       action.Invoke ();
+                       } catch (AssertionException) {
+                               logger.DumpMessages ();
+                               Console.WriteLine (sb.ToString ());
+                               throw;
+                       } finally {
+                               File.Delete (file);
+                       }
+               }
+
+               static void CheckFileExists (string file, bool should_exist)
+               {
+                       Assert.AreEqual (should_exist, File.Exists (file), "File existence");
+               }
+
+               static void CheckLines (string full_filepath, string[] expected)
+               {
+                       string[] actual = File.ReadAllLines (full_filepath);
+                       Assert.AreEqual (expected != null ? expected.Length : 0, actual.Length, "Number of lines written don't match");
+
+                       if (expected != null)
+                               return;
+                       int i = 0;
+                       foreach (string line in actual)
+                               Assert.AreEqual (expected[i++], line, "Z#" + i.ToString ());
+               }
+       }
+}