Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/MSBuildProjectCreator.UnitTests/BuildTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) Jeff Kluge. All rights reserved.
//
// Licensed under the MIT license.

using Microsoft.Build.Execution;
using Shouldly;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Xunit;

namespace Microsoft.Build.Utilities.ProjectCreation.UnitTests
{
public class BuildTests : TestBase
{
[Fact]
public void BuildTargetOutputsTest()
{
ProjectCreator
.Create(Path.Combine(TestRootPath, "project1.proj"))
.Target("Build", returns: "@(MyItems)")
.TargetItemInclude("MyItems", "E32099C7AF4E481885B624E5600C718A")
.TargetItemInclude("MyItems", "7F38E64414104C6182F492B535926187")
.Save()
.TryBuild("Build", out bool result, out BuildOutput _, out IDictionary<string, TargetResult> targetOutputs);

result.ShouldBeTrue();

KeyValuePair<string, TargetResult> item = targetOutputs.ShouldHaveSingleItem();

item.Key.ShouldBe("Build");

item.Value.Items.Select(i => i.ItemSpec).ShouldBe(new[] { "E32099C7AF4E481885B624E5600C718A", "7F38E64414104C6182F492B535926187" });
}
}
}
47 changes: 47 additions & 0 deletions src/MSBuildProjectCreator.UnitTests/TestBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) Jeff Kluge. All rights reserved.
//
// Licensed under the MIT license.

using System;
using System.IO;

namespace Microsoft.Build.Utilities.ProjectCreation.UnitTests
{
public abstract class TestBase : MSBuildTestBase
{
private readonly string _testRootPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());

public string TestRootPath
{
get
{
Directory.CreateDirectory(_testRootPath);
return _testRootPath;
}
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (Directory.Exists(TestRootPath))
{
Directory.Delete(TestRootPath, recursive: true);
}
}
}

protected string GetTempFileName(string extension = null)
{
Directory.CreateDirectory(TestRootPath);

return Path.Combine(TestRootPath, $"{Path.GetRandomFileName()}{extension ?? String.Empty}");
}
}
}
50 changes: 50 additions & 0 deletions src/MSBuildProjectCreator/ProjectCreator.Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// Licensed under the MIT license.

using Microsoft.Build.Execution;
using System.Collections.Generic;
using System.Linq;

namespace Microsoft.Build.Utilities.ProjectCreation
{
Expand Down Expand Up @@ -75,5 +77,53 @@ public ProjectCreator TryBuild(out bool result, out BuildOutput buildOutput)

return this;
}

/// <summary>
/// Attempts to build the current project.
/// </summary>
/// <param name="target">The name of the target to build.</param>
/// <param name="result">A value indicating the result of the build.</param>
/// <param name="buildOutput">A <see cref="BuildOutput"/> object that captured the logging from the build.</param>
/// <param name="targetOutputs">A <see cref="IDictionary{String,TargetResult}" /> containing the target outputs.</param>
/// <returns>The current <see cref="ProjectCreator"/>.</returns>
public ProjectCreator TryBuild(string target, out bool result, out BuildOutput buildOutput, out IDictionary<string, TargetResult> targetOutputs)
{
return TryBuild(new[] { target }, out result, out buildOutput, out targetOutputs);
}

/// <summary>
/// Attempts to build the current project.
/// </summary>
/// <param name="targets">The names of the targets to build.</param>
/// <param name="result">A value indicating the result of the build.</param>
/// <param name="buildOutput">A <see cref="BuildOutput"/> object that captured the logging from the build.</param>
/// <param name="targetOutputs">A <see cref="IDictionary{String,TargetResult}" /> containing the target outputs.</param>
/// <returns>The current <see cref="ProjectCreator"/>.</returns>
public ProjectCreator TryBuild(string[] targets, out bool result, out BuildOutput buildOutput, out IDictionary<string, TargetResult> targetOutputs)
{
buildOutput = BuildOutput.Create();

lock (BuildManager.DefaultBuildManager)
{
ProjectInstance projectInstance = Project.CreateProjectInstance();

result = projectInstance.Build(targets, buildOutput.AsEnumerable(), out targetOutputs);
}

return this;
}

/// <summary>
/// Attempts to build the current project.
/// </summary>
/// <param name="targets">The names of the targets to build.</param>
/// <param name="result">A value indicating the result of the build.</param>
/// <param name="buildOutput">A <see cref="BuildOutput"/> object that captured the logging from the build.</param>
/// <param name="targetOutputs">A <see cref="IDictionary{String,TargetResult}" /> containing the target outputs.</param>
/// <returns>The current <see cref="ProjectCreator"/>.</returns>
public ProjectCreator TryBuild(IEnumerable<string> targets, out bool result, out BuildOutput buildOutput, out IDictionary<string, TargetResult> targetOutputs)
{
return TryBuild(targets.ToArray(), out result, out buildOutput, out targetOutputs);
}
}
}