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
27 changes: 27 additions & 0 deletions src/BuildPrediction/Predictors/FakesOutputPathPredictor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Microsoft.Build.Execution;

namespace Microsoft.Build.Prediction.Predictors
{
/// <summary>
/// Predicts the output directory for Microsoft Fakes assemblies based on the FakesOutputPath property.
/// </summary>
public sealed class FakesOutputPathPredictor : IProjectPredictor
{
internal const string FakesOutputPathPropertyName = "FakesOutputPath";

/// <inheritdoc/>
public void PredictInputsAndOutputs(
ProjectInstance projectInstance,
ProjectPredictionReporter predictionReporter)
{
string fakesOutputPath = projectInstance.GetPropertyValue(FakesOutputPathPropertyName);
if (!string.IsNullOrWhiteSpace(fakesOutputPath))
{
predictionReporter.ReportOutputDirectory(fakesOutputPath);
}
}
}
}
2 changes: 2 additions & 0 deletions src/BuildPrediction/ProjectPredictors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public static class ProjectPredictors
/// <item><see cref="GenerateBuildDependencyFilePredictor"/></item>
/// <item><see cref="GeneratePublishDependencyFilePredictor"/></item>
/// <item><see cref="GenerateRuntimeConfigurationFilesPredictor"/></item>
/// <item><see cref="FakesOutputPathPredictor"/></item>
/// </list>
/// </remarks>
/// <returns>A collection of <see cref="IProjectPredictor"/>.</returns>
Expand Down Expand Up @@ -114,6 +115,7 @@ public static class ProjectPredictors
new GenerateBuildDependencyFilePredictor(),
new GeneratePublishDependencyFilePredictor(),
new GenerateRuntimeConfigurationFilesPredictor(),
new FakesOutputPathPredictor(),
//// NOTE! When adding a new predictor here, be sure to update the doc comment above.
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Microsoft.Build.Construction;
using Microsoft.Build.Execution;
using Microsoft.Build.Prediction.Predictors;
using Xunit;

namespace Microsoft.Build.Prediction.Tests.Predictors
{
public class FakesOutputPathPredictorTests
{
[Fact]
public void FakesOutputPathFoundAsOutputDir()
{
const string FakesOutputPath = @"C:\repo\FakesAssemblies";
ProjectInstance projectInstance = CreateTestProjectInstance(FakesOutputPath);
new FakesOutputPathPredictor()
.GetProjectPredictions(projectInstance)
.AssertPredictions(
projectInstance,
null,
null,
null,
new[] { new PredictedItem(FakesOutputPath, nameof(FakesOutputPathPredictor)) });
}

[Fact]
public void RelativeFakesOutputPathFoundAsOutputDir()
{
const string FakesOutputPath = @"bin\FakesAssemblies";
ProjectInstance projectInstance = CreateTestProjectInstance(FakesOutputPath);
new FakesOutputPathPredictor()
.GetProjectPredictions(projectInstance)
.AssertPredictions(
projectInstance,
null,
null,
null,
new[] { new PredictedItem(FakesOutputPath, nameof(FakesOutputPathPredictor)) });
}

[Fact]
public void DefaultFakesAssembliesDirectoryFoundAsOutputDir()
{
const string FakesOutputPath = @"FakesAssemblies";
ProjectInstance projectInstance = CreateTestProjectInstance(FakesOutputPath);
new FakesOutputPathPredictor()
.GetProjectPredictions(projectInstance)
.AssertPredictions(
projectInstance,
null,
null,
null,
new[] { new PredictedItem(FakesOutputPath, nameof(FakesOutputPathPredictor)) });
}

[Fact]
public void NoOutputsReportedIfNoFakesOutputPath()
{
ProjectInstance projectInstance = CreateTestProjectInstance(null);
new FakesOutputPathPredictor()
.GetProjectPredictions(projectInstance)
.AssertNoPredictions();
}

[Fact]
public void NoOutputsReportedIfEmptyFakesOutputPath()
{
ProjectInstance projectInstance = CreateTestProjectInstance(string.Empty);
new FakesOutputPathPredictor()
.GetProjectPredictions(projectInstance)
.AssertNoPredictions();
}

[Fact]
public void NoOutputsReportedIfWhitespaceFakesOutputPath()
{
ProjectInstance projectInstance = CreateTestProjectInstance(" ");
new FakesOutputPathPredictor()
.GetProjectPredictions(projectInstance)
.AssertNoPredictions();
}

private static ProjectInstance CreateTestProjectInstance(string fakesOutputPath)
{
ProjectRootElement projectRootElement = ProjectRootElement.Create();
if (fakesOutputPath != null)
{
projectRootElement.AddProperty(FakesOutputPathPredictor.FakesOutputPathPropertyName, fakesOutputPath);
}

return TestHelpers.CreateProjectInstanceFromRootElement(projectRootElement);
}
}
}
Loading