From 0d44b44cd8f74a0c13284ff0bf2931712b02680b Mon Sep 17 00:00:00 2001 From: ankit Date: Thu, 4 Feb 2010 17:29:57 +0000 Subject: [PATCH] In class/Microsoft.Build.Tasks: * 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. git-svn-id: svn+ssh://mono-cvs.ximian.com/source/trunk/mcs@150873 e3ebcda4-bce8-0310-ba0a-eca2169e7518 --- class/Microsoft.Build.Tasks/ChangeLog | 5 + .../Microsoft.Build.Tasks/ChangeLog | 5 + .../Microsoft.Build.Tasks/WriteLinesToFile.cs | 16 +- .../Microsoft.Build.Tasks_test.dll.sources | 1 + .../Test/Microsoft.Build.Tasks/ChangeLog | 4 + .../Microsoft.Build.Tasks/WriteLinesToFileTest.cs | 220 +++++++++++++++++++++ 6 files changed, 247 insertions(+), 4 deletions(-) create mode 100755 class/Microsoft.Build.Tasks/Test/Microsoft.Build.Tasks/WriteLinesToFileTest.cs diff --git a/class/Microsoft.Build.Tasks/ChangeLog b/class/Microsoft.Build.Tasks/ChangeLog index 595970cdb2..546db93b3b 100644 --- a/class/Microsoft.Build.Tasks/ChangeLog +++ b/class/Microsoft.Build.Tasks/ChangeLog @@ -1,3 +1,8 @@ +2010-02-04 Ankit Jain + + * Microsoft.Build.Tasks_test.dll.sources: Added + WriteLinesToFileTest.cs + 2009-12-22 Ankit Jain * Microsoft.Build.Tasks.dll.sources: Add DirectoryScanner.cs diff --git a/class/Microsoft.Build.Tasks/Microsoft.Build.Tasks/ChangeLog b/class/Microsoft.Build.Tasks/Microsoft.Build.Tasks/ChangeLog index cbd4e6f299..8461250815 100644 --- a/class/Microsoft.Build.Tasks/Microsoft.Build.Tasks/ChangeLog +++ b/class/Microsoft.Build.Tasks/Microsoft.Build.Tasks/ChangeLog @@ -1,3 +1,8 @@ +2010-02-04 Ankit Jain + + * WriteLinesToFile.cs: Delete the file if there is nothing to + be written and overwrite==true. + 2010-02-02 Rodrigo B. de Oliveira * AssemblyResolver.cs: diff --git a/class/Microsoft.Build.Tasks/Microsoft.Build.Tasks/WriteLinesToFile.cs b/class/Microsoft.Build.Tasks/Microsoft.Build.Tasks/WriteLinesToFile.cs index 84c140ae0f..80456985f5 100644 --- a/class/Microsoft.Build.Tasks/Microsoft.Build.Tasks/WriteLinesToFile.cs +++ b/class/Microsoft.Build.Tasks/Microsoft.Build.Tasks/WriteLinesToFile.cs @@ -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) { diff --git a/class/Microsoft.Build.Tasks/Microsoft.Build.Tasks_test.dll.sources b/class/Microsoft.Build.Tasks/Microsoft.Build.Tasks_test.dll.sources index ec52a76870..0b2d478491 100644 --- a/class/Microsoft.Build.Tasks/Microsoft.Build.Tasks_test.dll.sources +++ b/class/Microsoft.Build.Tasks/Microsoft.Build.Tasks_test.dll.sources @@ -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 diff --git a/class/Microsoft.Build.Tasks/Test/Microsoft.Build.Tasks/ChangeLog b/class/Microsoft.Build.Tasks/Test/Microsoft.Build.Tasks/ChangeLog index 887c0ef64c..a4b6caeb3a 100644 --- a/class/Microsoft.Build.Tasks/Test/Microsoft.Build.Tasks/ChangeLog +++ b/class/Microsoft.Build.Tasks/Test/Microsoft.Build.Tasks/ChangeLog @@ -1,3 +1,7 @@ +2010-02-04 Ankit Jain + + * WriteLinesToFileTest.cs: New. + 2009-12-22 Ankit Jain * CreateItemTest.cs (TestItemsWithWildcards): New. diff --git a/class/Microsoft.Build.Tasks/Test/Microsoft.Build.Tasks/WriteLinesToFileTest.cs b/class/Microsoft.Build.Tasks/Test/Microsoft.Build.Tasks/WriteLinesToFileTest.cs new file mode 100755 index 0000000000..62c03ac7e6 --- /dev/null +++ b/class/Microsoft.Build.Tasks/Test/Microsoft.Build.Tasks/WriteLinesToFileTest.cs @@ -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 (@" + +"); + + if (lines != null) + foreach (string line in lines) + sb.AppendFormat ("\t\t\n", line); + + sb.AppendFormat (@" + + + + "); + + 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 ()); + } + } +} -- 2.11.4.GIT