Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
added unit test for the GitVersionTask property override
  • Loading branch information
DerAlbertCom committed Jun 28, 2019
commit f126e436de29916c1974dd51c078317c0619c389
154 changes: 154 additions & 0 deletions src/GitVersionTask.Tests/GitVersionTaskPropertiesTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
namespace GitVersionTask.Tests
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using Microsoft.Build.Evaluation;
using Microsoft.Build.Framework;
using Microsoft.Build.Logging;
using NUnit.Framework;
using Shouldly;

[TestFixture]
public class GitVersionTaskPropertiesTests
{
const string projectXml =
@"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
<PropertyGroup>
<TargetFramework>net461</TargetFramework>
<Configuration>Debug</Configuration>
<GitVersionTaskRoot>..\GitVersionTask\build\</GitVersionTaskRoot>
<GitVersionAssemblyFile>..\..\GitVersionTask.MsBuild\bin\$(Configuration)\$(TargetFramework)\GitVersionTask.MsBuild.dll</GitVersionAssemblyFile>
</PropertyGroup>
<Import Project='$(GitVersionTaskRoot)GitVersionTask.props'/>
<Import Project='$(GitVersionTaskRoot)GitVersionTask.targets'/>
</Project>";

(bool failed, Project project) CallMsBuild(IDictionary<string, string> globalProperties)
{
using (var stringReader = new StringReader(projectXml))
{
StringBuilder builder;
Project project;
bool result;
using (var collection = new ProjectCollection(globalProperties))
{
builder = new StringBuilder();
var writer = new StringWriter(builder);
WriteHandler handler = x => writer.WriteLine(x);
var logger = new ConsoleLogger(LoggerVerbosity.Quiet, handler, null, null);
collection.RegisterLogger(logger);
XmlReader reader = new XmlTextReader(stringReader);
project = collection.LoadProject(reader);
result = project.Build();
collection.UnregisterAllLoggers();
}

var consoleOutput = builder.ToString();
Console.WriteLine(consoleOutput);

return (!result, project);
}
}

[Test]
public void Zero_properties_should_fail_the_build_because()
{
var result = CallMsBuild(new Dictionary<string, string>());
result.failed.ShouldBeTrue();
}

[Test]
public void With_DisableGitVersionTask_the_build_should_work()
{
var globalProperties = new Dictionary<string, string>
{
{"DisableGitVersionTask", "true"}
};
var result = CallMsBuild(globalProperties);
result.failed.ShouldBeFalse();
}

[Test]
public void With_Enabled_GetVersionTask_the_Build_properties_should_be_initialized()
{
var result = CallMsBuild(new Dictionary<string, string>());
PropertyValueShouldBe(result, "DisableGitVersionTask", "false");
PropertyValueShouldBe(result, "WriteVersionInfoToBuildLog", "true");
PropertyValueShouldBe(result, "UpdateAssemblyInfo", "true");
PropertyValueShouldBe(result, "GenerateGitVersionInformation", "true");
PropertyValueShouldBe(result, "GetVersion", "true");
PropertyValueShouldBe(result, "UpdateVersionProperties", "true");
}



[Test]
public void With_DisabledGetVersionTask_the_WriteVersionInfoToBuildLog_should_be_false()
{
var globalProperties = new Dictionary<string, string>
{
{"DisableGitVersionTask", "true"}
};
var result = CallMsBuild(globalProperties);
PropertyValueShouldBe(result, "DisableGitVersionTask", "true");
PropertyValueShouldBe(result, "WriteVersionInfoToBuildLog", "false");
}

[Test]
public void With_DisabledGetVersionTask_the_UpdateAssemblyInfo_should_be_false()
{
var globalProperties = new Dictionary<string, string>
{
{"DisableGitVersionTask", "true"}
};
var result = CallMsBuild(globalProperties);
PropertyValueShouldBe(result, "UpdateAssemblyInfo", "false");
}

[Test]
public void With_DisabledGetVersionTask_the_GenerateGitVersionInformation_should_be_false()
{
var globalProperties = new Dictionary<string, string>
{
{"DisableGitVersionTask", "true"}
};
var result = CallMsBuild(globalProperties);
PropertyValueShouldBe(result, "GenerateGitVersionInformation", "false");
}

[Test]
public void With_DisabledGetVersionTask_the_GetVersion_should_be_false()
{
var globalProperties = new Dictionary<string, string>
{
{"DisableGitVersionTask", "true"}
};
var result = CallMsBuild(globalProperties);
PropertyValueShouldBe(result, "GetVersion", "false");
}

[Test]
public void With_DisabledGetVersionTask_the_UpdateVersionProperties_should_be_false()
{
var globalProperties = new Dictionary<string, string>
{
{"DisableGitVersionTask", "true"}
};
var result = CallMsBuild(globalProperties);

PropertyValueShouldBe(result, "UpdateVersionProperties", "false");
}


static void PropertyValueShouldBe((bool failed, Project project) result, string propertyName, string expectedValue)
{
var property = result.project.Properties.First(p => p.Name == propertyName);

property.EvaluatedValue.ShouldBe(expectedValue);
}
}
}
1 change: 0 additions & 1 deletion src/GitVersionTask/build/GitVersionTask.props
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
<!-- Property that enables setting of Version -->
<UpdateVersionProperties Condition=" '$(DisableGitVersionTask)' == 'true' ">false</UpdateVersionProperties>
<UpdateVersionProperties Condition=" '$(UpdateVersionProperties)' == '' ">true</UpdateVersionProperties>
<UseFullSemVerForNuGet Condition=" '$(DisableGitVersionTask)' == 'true' ">false</UseFullSemVerForNuGet>
<UseFullSemVerForNuGet Condition=" '$(UseFullSemVerForNuGet)' == '' ">false</UseFullSemVerForNuGet>
</PropertyGroup>
</Project>