//
// TargetTest.cs
//
// Authors:
// Marek Sieradzki (marek.sieradzki@gmail.com)
// Andres G. Aragoneses (knocte@gmail.com)
//
// (C) 2006 Marek Sieradzki
// (C) 2012 Andres G. Aragoneses
//
// 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 Microsoft.Build.BuildEngine;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using MonoTests.Microsoft.Build.Tasks;
using NUnit.Framework;
using System.IO;
using System.Xml;
namespace MonoTests.Microsoft.Build.BuildEngine {
[TestFixture]
public class TargetTest {
Engine engine;
Project project;
[Test]
public void TestFromXml1 ()
{
string documentString = @"
";
engine = new Engine (Consts.BinPath);
project = engine.CreateNewProject ();
project.LoadXml (documentString);
Target[] t = new Target [1];
project.Targets.CopyTo (t, 0);
Assert.AreEqual (String.Empty, t [0].Condition, "A1");
Assert.AreEqual (String.Empty, t [0].DependsOnTargets, "A2");
Assert.IsFalse (t [0].IsImported, "A3");
Assert.AreEqual ("Target", t [0].Name, "A4");
}
[Test]
public void TestFromXml2 ()
{
string documentString = @"
";
engine = new Engine (Consts.BinPath);
project = engine.CreateNewProject ();
project.LoadXml (documentString);
Target[] t = new Target [1];
project.Targets.CopyTo (t, 0);
Assert.AreEqual ("false", t [0].Condition, "A1");
Assert.AreEqual ("X", t [0].DependsOnTargets, "A2");
t [0].Condition = "true";
t [0].DependsOnTargets = "A;B";
Assert.AreEqual ("true", t [0].Condition, "A3");
Assert.AreEqual ("A;B", t [0].DependsOnTargets, "A4");
}
[Test]
public void TestAddNewTask1 ()
{
string documentString = @"
";
engine = new Engine (Consts.BinPath);
project = engine.CreateNewProject ();
project.LoadXml (documentString);
Target[] t = new Target [1];
project.Targets.CopyTo (t, 0);
BuildTask bt = t [0].AddNewTask ("Message");
Assert.AreEqual ("Message", bt.Name, "A1");
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void TestAddNewTask2 ()
{
string documentString = @"
";
engine = new Engine (Consts.BinPath);
project = engine.CreateNewProject ();
project.LoadXml (documentString);
Target[] t = new Target [1];
project.Targets.CopyTo (t, 0);
t [0].AddNewTask (null);
}
[Test]
public void TestGetEnumerator ()
{
string documentString = @"
";
engine = new Engine (Consts.BinPath);
project = engine.CreateNewProject ();
project.LoadXml (documentString);
Target[] t = new Target [1];
project.Targets.CopyTo (t, 0);
IEnumerator e = t [0].GetEnumerator ();
e.MoveNext ();
Assert.AreEqual ("Message", ((BuildTask) e.Current).Name, "A1");
e.MoveNext ();
Assert.AreEqual ("Warning", ((BuildTask) e.Current).Name, "A2");
Assert.IsFalse (e.MoveNext (), "A3");
}
[Test]
public void TestOutOfRangeElementsOfTheEnumerator()
{
string documentString =
@"
";
engine = new Engine (Consts.BinPath);
project = engine.CreateNewProject ();
project.LoadXml (documentString);
Assert.IsFalse (project.Targets == null, "A1");
Assert.AreEqual (1, project.Targets.Count, "A2");
Target target = project.Targets ["A"];
Assert.IsFalse (target == null, "A3");
IEnumerator e = target.GetEnumerator ();
bool thrown = false;
try {
var name = ((BuildTask)e.Current).Name;
} catch (InvalidOperationException) { // "Enumeration has not started. Call MoveNext"
thrown = true;
}
if (!thrown)
Assert.Fail ("A4: Should have thrown IOE");
Assert.AreEqual (true, e.MoveNext (), "A5");
Assert.AreEqual ("Message", ((BuildTask)e.Current).Name, "A6");
Assert.AreEqual (false, e.MoveNext (), "A7");
try {
var name = ((BuildTask) e.Current).Name;
} catch (InvalidOperationException) { //"Enumeration already finished."
return;
}
Assert.Fail ("A8: Should have thrown IOE, because there's only one buidTask");
}
[Test]
[ExpectedException (typeof (InvalidProjectFileException))]
public void TestOnError1 ()
{
Engine engine;
Project project;
string documentString = @"
";
engine = new Engine (Consts.BinPath);
project = engine.CreateNewProject ();
project.LoadXml (documentString);
}
[Test]
public void TestRemoveTask1 ()
{
string documentString = @"
";
engine = new Engine (Consts.BinPath);
project = engine.CreateNewProject ();
project.LoadXml (documentString);
Target[] t = new Target [1];
project.Targets.CopyTo (t, 0);
IEnumerator e = t [0].GetEnumerator ();
e.MoveNext ();
t [0].RemoveTask ((BuildTask) e.Current);
e = t [0].GetEnumerator ();
e.MoveNext ();
Assert.AreEqual ("Warning", ((BuildTask) e.Current).Name, "A1");
Assert.IsFalse (e.MoveNext (), "A2");
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void TestRemoveTask2 ()
{
string documentString = @"
";
engine = new Engine (Consts.BinPath);
project = engine.CreateNewProject ();
project.LoadXml (documentString);
Target[] t = new Target [1];
project.Targets.CopyTo (t, 0);
t [0].RemoveTask (null);
}
[Test]
public void TestTargetOutputs1 ()
{
Engine engine;
Project project;
string documentString = @"
";
engine = new Engine (Consts.BinPath);
project = engine.CreateNewProject ();
project.LoadXml (documentString);
MonoTests.Microsoft.Build.Tasks.TestMessageLogger logger =
new MonoTests.Microsoft.Build.Tasks.TestMessageLogger ();
engine.RegisterLogger (logger);
bool result = project.Build ("Main");
if (!result) {
logger.DumpMessages ();
Assert.Fail ("Build failed");
}
try {
Assert.AreEqual (3, logger.NormalMessageCount, "Expected number of messages");
logger.CheckLoggedMessageHead ("foo called", "A1");
logger.CheckLoggedMessageHead ("FooItem: apple;rhubarb;apricot", "A2");
logger.CheckLoggedMessageHead ("AllOut: apple;rhubarb;apricot;apple;rhubarb;apricot", "A3");
Assert.AreEqual (0, logger.NormalMessageCount, "Extra messages found");
Assert.AreEqual (2, logger.TargetStarted, "TargetStarted count");
Assert.AreEqual (2, logger.TargetFinished, "TargetFinished count");
Assert.AreEqual (8, logger.TaskStarted, "TaskStarted count");
Assert.AreEqual (8, logger.TaskFinished, "TaskFinished count");
} catch (AssertionException) {
logger.DumpMessages ();
throw;
}
}
#if NET_3_5
bool Build (string projectXml, ILogger logger)
{
if (Environment.OSVersion.Platform == PlatformID.Win32NT) {
var reader = new StringReader (projectXml);
var xml = XmlReader.Create (reader);
return BuildOnWindows (xml, logger);
} else {
return BuildOnLinux (projectXml, logger);
}
}
bool BuildOnWindows (XmlReader reader, ILogger logger)
{
var type = Type.GetType ("Microsoft.Build.Evaluation.ProjectCollection, Microsoft.Build, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
var prop = type.GetProperty ("GlobalProjectCollection");
var coll = prop.GetValue (null, null);
var loadProject = coll.GetType ().GetMethod (
"LoadProject", new Type[] { typeof (XmlReader), typeof (string) });
var proj = loadProject.Invoke (coll, new object[] { reader, "4.0" });
var build = proj.GetType ().GetMethod ("Build", new Type[] { typeof (string), typeof (ILogger[]) });
var ret = (bool)build.Invoke (proj, new object[] { "Main", new ILogger[] { logger }});
return ret;
}
bool BuildOnLinux (string projectXml, ILogger logger)
{
var engine = new Engine (Consts.BinPath);
var project = engine.CreateNewProject ();
project.LoadXml (projectXml);
engine.RegisterLogger (logger);
return project.Build ("Main");
}
TestMessageLogger CreateLogger (string projectXml)
{
var logger = new TestMessageLogger ();
var result = Build (projectXml, logger);
if (!result) {
logger.DumpMessages ();
Assert.Fail ("Build failed");
}
return logger;
}
void ItemGroupInsideTarget (string xml, params string[] messages)
{
ItemGroupInsideTarget (xml, 1, messages);
}
void ItemGroupInsideTarget (string xml, int expectedTargetCount, params string[] messages)
{
var logger = CreateLogger (xml);
try
{
Assert.AreEqual(messages.Length, logger.NormalMessageCount, "Expected number of messages");
for (int i = 0; i < messages.Length; i++)
logger.CheckLoggedMessageHead (messages [i], i.ToString ());
Assert.AreEqual(0, logger.NormalMessageCount, "Extra messages found");
Assert.AreEqual(0, logger.WarningMessageCount, "Extra warningmessages found");
Assert.AreEqual(expectedTargetCount, logger.TargetStarted, "TargetStarted count");
Assert.AreEqual(expectedTargetCount, logger.TargetFinished, "TargetFinished count");
Assert.AreEqual(messages.Length, logger.TaskStarted, "TaskStarted count");
Assert.AreEqual(messages.Length, logger.TaskFinished, "TaskFinished count");
}
catch (AssertionException)
{
logger.DumpMessages();
throw;
}
}
[Test]
public void BuildProjectWithItemGroupInsideTarget ()
{
ItemGroupInsideTarget (
@"
", "apple", "apricot", "raspberry");
}
[Test]
public void BuildProjectWithItemGroupInsideTarget2 ()
{
ItemGroupInsideTarget (
@"
Foo
Bar
$(Foo);Hello
", "1;2", "Foo", "Bar;Hello");
}
[Test]
public void BuildProjectWithPropertyGroupInsideTarget ()
{
ItemGroupInsideTarget (
@"
Foo
Bar
$(B)
", "Foo", "Bar");
}
[Test]
public void BuildProjectWithPropertyGroupInsideTarget2 ()
{
ItemGroupInsideTarget (
@"
Foo
Bar
False
$(B)
Equal
", "Foo", "Bar", "Bar", "Equal");
}
[Test]
public void ItemGroupInsideTarget_ModifyMetadata ()
{
ItemGroupInsideTarget (
@"
Mono
Mono
Root
Monkey
", "Server1 : Monkey", "Server2 : Monkey", "Server3 : Root");
}
[Test]
public void ItemGroupInsideTarget_RemoveItem ()
{
ItemGroupInsideTarget (
@"
", "A;C;D");
}
[Test]
public void ItemGroupInsideTarget_DontKeepDuplicates ()
{
ItemGroupInsideTarget (
@"
World
Boston
Boston
", "A;B;C;D;B;C");
}
[Test]
public void ItemGroupInsideTarget_RemoveMetadata ()
{
ItemGroupInsideTarget (
@"
World
Boston
Monkey
Hello
Monkey
", "E");
}
[Test]
public void ItemGroupInsideTarget_RemoveMetadata2 ()
{
ItemGroupInsideTarget (
@"
World
Boston
Monkey
Hello
Monkey
", "E");
}
[Test]
public void ItemGroupInsideTarget_KeepMetadata ()
{
ItemGroupInsideTarget (
@"
World
Boston
Monkey
Monkey
", "D");
}
[Test]
public void ItemGroupInsideTarget_Batching ()
{
ItemGroupInsideTarget (
@"
", "A", "B");
}
[Test]
public void ItemGroupInsideTarget_Condition ()
{
ItemGroupInsideTarget (
@"
true
", "Sun", "Rain");
}
[Test]
public void PropertyGroupInsideTarget_Condition ()
{
ItemGroupInsideTarget (
@"
true
", "Rain");
}
[Test]
// Bug #14661
public void ItemGroupInsideTarget_Expression_in_Metadata ()
{
ItemGroupInsideTarget (
@"
input1a;input1b
input2a;input2b
<_Foo Include='%(Foo.Inputs)'>
%(Foo.Identity)
",
3, "COMPILE: input1a;input1b - output1", "COMPILE: input2a;input2b - output2");
}
#endif
[Test]
public void TestTargetOutputsIncludingMetadata ()
{
Engine engine;
Project project;
string documentString = @"
a
b
c
";
engine = new Engine (Consts.BinPath);
project = engine.CreateNewProject ();
project.LoadXml (documentString);
MonoTests.Microsoft.Build.Tasks.TestMessageLogger logger =
new MonoTests.Microsoft.Build.Tasks.TestMessageLogger ();
engine.RegisterLogger (logger);
bool result = project.Build ("Main");
if (!result) {
logger.DumpMessages ();
Assert.Fail ("Build failed");
}
try {
logger.CheckLoggedMessageHead ("foo called", "A1");
logger.CheckLoggedMessageHead ("FooItem: apple metadata: a", "A2");
logger.CheckLoggedMessageHead ("FooItem: rhubarb metadata: b", "A3");
logger.CheckLoggedMessageHead ("FooItem: apricot metadata: c", "A4");
logger.CheckLoggedMessageHead ("AllOut: apple;apple metadata: a", "A5");
logger.CheckLoggedMessageHead ("AllOut: rhubarb;rhubarb metadata: b", "A6");
logger.CheckLoggedMessageHead ("AllOut: apricot;apricot metadata: c", "A7");
Assert.AreEqual (0, logger.NormalMessageCount, "Extra messages found");
Assert.AreEqual (2, logger.TargetStarted, "TargetStarted count");
Assert.AreEqual (2, logger.TargetFinished, "TargetFinished count");
Assert.AreEqual (10, logger.TaskStarted, "TaskStarted count");
Assert.AreEqual (10, logger.TaskFinished, "TaskFinished count");
} catch (AssertionException) {
logger.DumpMessages ();
throw;
}
}
[Test]
public void TestOverridingTargets ()
{
Engine engine;
Project project;
string second = @"
";
using (StreamWriter sw = new StreamWriter (Path.Combine ("Test", Path.Combine ("resources", "second.proj")))) {
sw.Write (second);
}
string documentString = @"
";
engine = new Engine (Consts.BinPath);
project = engine.CreateNewProject ();
project.LoadXml (documentString);
MonoTests.Microsoft.Build.Tasks.TestMessageLogger logger =
new MonoTests.Microsoft.Build.Tasks.TestMessageLogger ();
engine.RegisterLogger (logger);
bool result = project.Build ("Build");
if (!result) {
logger.DumpMessages ();
Assert.Fail ("Build failed");
}
logger.CheckLoggedMessageHead ("Overriding BeforeBuild", "A1");
logger.CheckLoggedMessageHead ("Build executing", "A1");
Assert.AreEqual (0, logger.NormalMessageCount, "Unexpected extra messages found");
}
#if NET_4_0
[Test]
[Category ("NotDotNet")]
public void TestBeforeAndAfterTargets ()
{
Engine engine;
Project project;
string projectString = @"
";
engine = new Engine ();
project = engine.CreateNewProject ();
project.LoadXml (projectString);
MonoTests.Microsoft.Build.Tasks.TestMessageLogger logger =
new MonoTests.Microsoft.Build.Tasks.TestMessageLogger ();
engine.RegisterLogger (logger);
if (!project.Build ("Default")) {
logger.DumpMessages ();
Assert.Fail ("Build failed");
}
logger.CheckLoggedMessageHead ("Hello from DefaultDependsOn", "A1");
logger.CheckLoggedMessageHead ("Hello from DefaultBeforeTarget1", "A1");
logger.CheckLoggedMessageHead ("Hello from DefaultBeforeTarget2", "A1");
logger.CheckLoggedMessageHead ("Hello from Default", "A1");
logger.CheckLoggedMessageHead ("Hello from DefaultAfterTarget", "A1");
Assert.AreEqual (0, logger.NormalMessageCount, "Unexpected messages found");
//warnings for referencing unknown targets: NonExistant and Foo
Assert.AreEqual (2, logger.WarningsCount, "Expected warnings not raised");
}
#endif
[Test]
public void TestTargetReturns ()
{
engine = new Engine (Consts.BinPath);
project = engine.CreateNewProject ();
project.Load (Path.Combine ("Test", Path.Combine ("resources", "TestReturns.csproj")));
var logger = new TestMessageLogger ();
engine.RegisterLogger (logger);
bool result = project.Build ("Main");
if (!result) {
logger.DumpMessages ();
Assert.Fail ("Build failed");
}
logger.CheckLoggedMessageHead ("Result: Bar", "A1");
Assert.AreEqual (0, logger.NormalMessageCount, "Unexpected extra messages found");
}
}
}