From 649044926f8cb7cb344cf2f70f5916567ab6e4f4 Mon Sep 17 00:00:00 2001 From: ankit Date: Wed, 24 Sep 2008 12:15:53 +0000 Subject: [PATCH] In class/Microsoft.Build.Engine/Microsoft.Build.BuildEngine: 2008-09-24 Ankit Jain * Engine.cs: Add argument checks. * Project.cs: Likewise. In class/Microsoft.Build.Engine/Test/Microsoft.Build.BuildEngine: 2008-09-24 Ankit Jain * EngineTest.cs: * ProjectTest.cs: Add tests for Build* git-svn-id: svn+ssh://mono-cvs.ximian.com/source/trunk/mcs@113946 e3ebcda4-bce8-0310-ba0a-eca2169e7518 --- .../Microsoft.Build.BuildEngine/ChangeLog | 5 + .../Microsoft.Build.BuildEngine/Engine.cs | 11 ++ .../Microsoft.Build.BuildEngine/Project.cs | 8 + .../Test/Microsoft.Build.BuildEngine/ChangeLog | 5 + .../Test/Microsoft.Build.BuildEngine/EngineTest.cs | 61 ++++++++ .../Microsoft.Build.BuildEngine/ProjectTest.cs | 168 +++++++++++++++++++++ 6 files changed, 258 insertions(+) diff --git a/class/Microsoft.Build.Engine/Microsoft.Build.BuildEngine/ChangeLog b/class/Microsoft.Build.Engine/Microsoft.Build.BuildEngine/ChangeLog index 648ecfdc2e..90e4aff258 100644 --- a/class/Microsoft.Build.Engine/Microsoft.Build.BuildEngine/ChangeLog +++ b/class/Microsoft.Build.Engine/Microsoft.Build.BuildEngine/ChangeLog @@ -1,3 +1,8 @@ +2008-09-24 Ankit Jain + + * Engine.cs: Add argument checks. + * Project.cs: Likewise. + 2007-03-06 Marek Sieradzki * Project.cs (Build): Add outputs to targetOutputs. diff --git a/class/Microsoft.Build.Engine/Microsoft.Build.BuildEngine/Engine.cs b/class/Microsoft.Build.Engine/Microsoft.Build.BuildEngine/Engine.cs index 9f6befa5da..a884324201 100644 --- a/class/Microsoft.Build.Engine/Microsoft.Build.BuildEngine/Engine.cs +++ b/class/Microsoft.Build.Engine/Microsoft.Build.BuildEngine/Engine.cs @@ -81,12 +81,19 @@ namespace Microsoft.Build.BuildEngine { [MonoTODO] public bool BuildProject (Project project) { + if (project == null) + throw new ArgumentException ("project"); return project.Build (); } [MonoTODO] public bool BuildProject (Project project, string targetName) { + if (project == null) + throw new ArgumentException ("project"); + if (targetName == null) + return false; + return BuildProject (project, new string[] { targetName}, null, BuildSettings.None); } @@ -111,6 +118,10 @@ namespace Microsoft.Build.BuildEngine { BuildSettings buildFlags) { bool result; + if (project == null) + throw new ArgumentException ("project"); + if (targetNames == null) + return false; StartBuild (); diff --git a/class/Microsoft.Build.Engine/Microsoft.Build.BuildEngine/Project.cs b/class/Microsoft.Build.Engine/Microsoft.Build.BuildEngine/Project.cs index 0e31bd01a8..7377e7cb7b 100644 --- a/class/Microsoft.Build.Engine/Microsoft.Build.BuildEngine/Project.cs +++ b/class/Microsoft.Build.Engine/Microsoft.Build.BuildEngine/Project.cs @@ -226,6 +226,8 @@ namespace Microsoft.Build.BuildEngine { [MonoTODO ("Not tested")] public bool Build (string targetName) { + if (targetName == null) + return false; return Build (new string [1] { targetName }); } @@ -248,6 +250,9 @@ namespace Microsoft.Build.BuildEngine { BuildSettings buildFlags) { + if (targetNames == null) + return false; + CheckUnloaded (); ParentEngine.StartBuild (); NeedToReevaluate (); @@ -262,6 +267,9 @@ namespace Microsoft.Build.BuildEngine { } foreach (string target in targetNames) { + if (target == null) + throw new ArgumentException ("targetNames cannot contain null strings"); + if (!targets.Exists (target)) // FIXME: test if it's logged return false; diff --git a/class/Microsoft.Build.Engine/Test/Microsoft.Build.BuildEngine/ChangeLog b/class/Microsoft.Build.Engine/Test/Microsoft.Build.BuildEngine/ChangeLog index 0618895e66..4e7436e86c 100644 --- a/class/Microsoft.Build.Engine/Test/Microsoft.Build.BuildEngine/ChangeLog +++ b/class/Microsoft.Build.Engine/Test/Microsoft.Build.BuildEngine/ChangeLog @@ -1,3 +1,8 @@ +2008-09-24 Ankit Jain + + * EngineTest.cs: + * ProjectTest.cs: Add tests for Build* + 2007-03-17 Marek Sieradzki * Consts.cs: Check for runtime not platform. diff --git a/class/Microsoft.Build.Engine/Test/Microsoft.Build.BuildEngine/EngineTest.cs b/class/Microsoft.Build.Engine/Test/Microsoft.Build.BuildEngine/EngineTest.cs index 082abfd263..73e2cd54ae 100644 --- a/class/Microsoft.Build.Engine/Test/Microsoft.Build.BuildEngine/EngineTest.cs +++ b/class/Microsoft.Build.Engine/Test/Microsoft.Build.BuildEngine/EngineTest.cs @@ -217,5 +217,66 @@ namespace MonoTests.Microsoft.Build.BuildEngine { Assert.IsFalse (cul.Anything, "A1"); } + + [Test] + public void TestBuildError1 () + { + engine = new Engine (Consts.BinPath); + Project project = engine.CreateNewProject (); + + Assert.IsFalse (project.Build ()); + Assert.IsFalse (project.Build ((string)null)); + Assert.IsFalse (project.Build ((string [])null)); + Assert.IsFalse (project.Build (null, null)); + Assert.IsFalse (project.Build (null, null, BuildSettings.None)); + //FIXME: Add test for Build (null, non-null-target) + } + + [Test] + [ExpectedException (typeof (ArgumentException))] + public void TestBuildProject1 () + { + engine = new Engine (Consts.BinPath); + engine.BuildProject (null); + } + + [Test] + public void TestBuildProject2 () + { + engine = new Engine (Consts.BinPath); + Project project = engine.CreateNewProject (); + + Assert.IsFalse (engine.BuildProject (project, (string)null), "#A1"); + Assert.IsFalse (engine.BuildProject (project, (string [])null), "#A2"); + Assert.IsFalse (engine.BuildProject (project, (string [])null, null), "#A3"); + Assert.IsFalse (engine.BuildProject (project, (string [])null, null, BuildSettings.None), "#A4"); + + bool caught_exception = false; + try { + //null string in targetNames [] param + engine.BuildProject (project, new string [] {null}, null); + } catch { + caught_exception = true; + } + if (!caught_exception) + Assert.Fail ("Expected exception for Engine.BuildProject"); + } + + [Test] + [ExpectedException (typeof (ArgumentException))] + public void TestBuildProjectNull1 () + { + engine = new Engine (Consts.BinPath); + engine.BuildProject (null, "foo"); + } + + [Test] + [ExpectedException (typeof (ArgumentException))] + public void TestBuildProjectNull2 () + { + engine = new Engine (Consts.BinPath); + engine.BuildProject (null, (string)null); + } + } } diff --git a/class/Microsoft.Build.Engine/Test/Microsoft.Build.BuildEngine/ProjectTest.cs b/class/Microsoft.Build.Engine/Test/Microsoft.Build.BuildEngine/ProjectTest.cs index 413965a0e1..e4228f54d0 100644 --- a/class/Microsoft.Build.Engine/Test/Microsoft.Build.BuildEngine/ProjectTest.cs +++ b/class/Microsoft.Build.Engine/Test/Microsoft.Build.BuildEngine/ProjectTest.cs @@ -33,6 +33,7 @@ using Microsoft.Build.BuildEngine; using Microsoft.Build.Framework; using Microsoft.Build.Utilities; using NUnit.Framework; +using System.Text; namespace MonoTests.Microsoft.Build.BuildEngine { @@ -1143,5 +1144,172 @@ namespace MonoTests.Microsoft.Build.BuildEngine { Assert.AreEqual ("1", project.GetProjectExtensions ("name"), "A1"); Assert.IsTrue (project.IsDirty, "A2"); } + + [Test] + public void TestBuildProjectError1 () + { + Engine engine = new Engine (Consts.BinPath); + Project project = engine.CreateNewProject (); + + Assert.IsFalse (project.Build ((string) null), "A1"); + Assert.IsFalse (project.Build ((string[]) null), "A2"); + Assert.IsFalse (project.Build ((string []) null, null), "A3"); + Assert.IsFalse (project.Build ((string []) null, null, BuildSettings.None), "A4"); + } + + [Test] + public void TestBuildProjectError2 () + { + Engine engine = new Engine (Consts.BinPath); + Project project = engine.CreateNewProject (); + + try { + project.Build (new string [] { null }); + } catch { + return; + } + Assert.Fail ("Expected exception for project.Build, null string in targetNames []"); + } + + [Test] + [Category ("NotWorking")] + public void TestBuildProjectFile1 () + { + Engine engine = new Engine (Consts.BinPath); + Project project = engine.CreateNewProject (); + + string projectText = CreateProjectString (false, new string [] { "1", "2" }, new bool [] { true, true }); + project.LoadXml (projectText); + CheckProjectBuild (engine, project, new string [] { "main" }, true, new string [] { "main" }); + } + + [Test] + public void TestBuildProjectFile2 () + { + Engine engine = new Engine (Consts.BinPath); + Project project = engine.CreateNewProject (); + + string projectText = CreateProjectString (false, new string [] { "1", "2", "3" }, new bool [] { true, false, true }); + project.LoadXml (projectText); + CheckProjectBuild (engine, project, new string [] { "main" }, false, new string [0]); + } + + [Test] + public void TestBuildProjectFile3 () + { + Engine engine = new Engine (Consts.BinPath); + Project project = engine.CreateNewProject (); + + string projectText = CreateProjectString (false, new string [] { "1", "2", "3" }, new bool [] { true, true, true }); + project.LoadXml (projectText); + CheckProjectBuild (engine, project, new string [] { "1", "2" }, true, new string [] { "1", "2" }); + } + + [Test] + public void TestBuildProjectFile4 () + { + Engine engine = new Engine (Consts.BinPath); + Project project = engine.CreateNewProject (); + + string projectText = CreateProjectString (false, new string [] { "1", "2", "3" }, new bool [] { true, false, true }); + project.LoadXml (projectText); + CheckProjectBuild (engine, project, new string [] { "main" }, false, new string [0]); + } + + //Run separate tests + + //Run single target + [Test] + public void TestBuildProjectFile5 () + { + Engine engine = new Engine (Consts.BinPath); + Project project = engine.CreateNewProject (); + + string projectText = CreateProjectString (true, new string [] { "1", "2", "3" }, new bool [] { true, false, true }); + project.LoadXml (projectText); + CheckProjectBuild (engine, project, new string [] { "main" }, false, new string [0]); + } + + [Test] + [Category ("NotWorking")] + public void TestBuildProjectFile6 () + { + Engine engine = new Engine (Consts.BinPath); + Project project = engine.CreateNewProject (); + + string projectText = CreateProjectString (true, new string [] { "1", "2", "3" }, new bool [] { true, true, true }); + project.LoadXml (projectText); + CheckProjectBuild (engine, project, new string [] { "main" }, true, new string [] { "main" }); + } + + // run multiple targets + [Test] + public void TestBuildProjectFile7 () + { + Engine engine = new Engine (Consts.BinPath); + Project project = engine.CreateNewProject (); + + string projectText = CreateProjectString (true, new string [] { "1", "2", "3" }, new bool [] { true, true, true }); + project.LoadXml (projectText); + + CheckProjectBuild (engine, project, new string [] { "1", "2", "3" }, true, new string [] { "1", "2", "3" }); + } + + [Test] + public void TestBuildProjectFile8 () + { + Engine engine = new Engine (Consts.BinPath); + Project project = engine.CreateNewProject (); + + string projectText = CreateProjectString (true, new string [] { "1", "2", "3" }, new bool [] { true, true, false }); + project.LoadXml (projectText); + + CheckProjectBuild (engine, project, new string [] { "1", "2", "3" }, false, new string [] { "1", "2"}); + } + + void CheckProjectBuild (Engine engine, Project project, string [] targetNames, bool result, string [] outputNames) + { + IDictionary targetOutputs = new Hashtable (); + + Assert.AreEqual (result, project.Build (targetNames, targetOutputs), "A1"); + Assert.AreEqual (outputNames.Length, targetOutputs.Keys.Count, "A2"); + + foreach (string outputName in outputNames) { + Assert.IsTrue (targetOutputs.Contains (outputName), "A3: target " + outputName); + + object o = targetOutputs [outputName]; + Assert.IsTrue (typeof (ITaskItem []).IsAssignableFrom (o.GetType ()), "A4: target " + outputName); + + ITaskItem [] items = (ITaskItem [])o; + Assert.AreEqual (0, items.Length, "A5: target " + outputName); + } + } + + string CreateProjectString (bool run_separate, string [] targets, bool [] results) + { + StringBuilder sb = new StringBuilder (); + sb.Append (@""); + sb.AppendFormat ("", "main"); + + sb.AppendFormat ("\n"); + + for (int i = 0; i < targets.Length; i++) { + sb.AppendFormat ("", targets [i]); + if (!results [i]) + sb.AppendFormat ("", targets [i]); + sb.Append ("\n"); + } + + sb.Append (""); + + return sb.ToString (); + } } } -- 2.11.4.GIT