Skip to content
Closed
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
171 changes: 171 additions & 0 deletions src/GitVersionTask.Tests/GitVersionTaskPropertiesTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
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
{


static string CreateProjectXml()
{
var currentDirectory = AppDomain.CurrentDomain.BaseDirectory;
return $@"
<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
<PropertyGroup>
<TargetFramework>net461</TargetFramework>
<Configuration>Debug</Configuration>
<GitVersionTaskRoot>{currentDirectory}\..\..\..\..\GitVersionTask\build\</GitVersionTaskRoot>
<GitVersionAssemblyFile>{currentDirectory}\..\..\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(CreateProjectXml()))
{
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();
}

if (!result)
{
var consoleOutput = builder.ToString();
TestContext.Error.Write(consoleOutput);
}

return (!result, project);
}
}

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

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

[Test]
[Category("NoMono")]
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]
[Category("NoMono")]
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]
[Category("NoMono")]
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]
[Category("NoMono")]
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]
[Category("NoMono")]
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]
[Category("NoMono")]
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);
}
}
}
8 changes: 8 additions & 0 deletions src/GitVersionTask/build/GitVersionTask.props
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,25 @@

<GitVersion_NoFetchEnabled Condition="$(GitVersion_NoFetchEnabled) == ''">false</GitVersion_NoFetchEnabled>

<DisableGitVersionTask Condition=" '$(DisableGitVersionTask)' == '' ">false</DisableGitVersionTask>

<!-- Property that enables WriteVersionInfoToBuildLog -->
<WriteVersionInfoToBuildLog Condition=" '$(DisableGitVersionTask)' == 'true' ">false</WriteVersionInfoToBuildLog>
<WriteVersionInfoToBuildLog Condition=" '$(WriteVersionInfoToBuildLog)' == '' ">true</WriteVersionInfoToBuildLog>

<!-- Property that enables UpdateAssemblyInfo. Default to off for SDK builds -->
<UpdateAssemblyInfo Condition=" '$(DisableGitVersionTask)' == 'true' ">false</UpdateAssemblyInfo>
<UpdateAssemblyInfo Condition=" '$(UpdateAssemblyInfo)' == '' ">true</UpdateAssemblyInfo>

<!-- Property that enables GenerateGitVersionInformation -->
<GenerateGitVersionInformation Condition=" '$(DisableGitVersionTask)' == 'true' ">false</GenerateGitVersionInformation>
<GenerateGitVersionInformation Condition=" '$(GenerateGitVersionInformation)' == '' ">true</GenerateGitVersionInformation>

<!-- Property that enables GetVersion -->
<GetVersion Condition=" '$(DisableGitVersionTask)' == 'true' ">false</GetVersion>
<GetVersion Condition=" '$(GetVersion)' == '' ">true</GetVersion>

<GenerateGitVersionWixDefines Condition=" '$(DisableGitVersionTask)' == 'true' ">false</GenerateGitVersionWixDefines>
<GenerateGitVersionWixDefines Condition=" '$(GenerateGitVersionWixDefines)' == '' ">true</GenerateGitVersionWixDefines>
<!--
Ensure GetVersion runs prior to XAML's Markup Compiler in order to have the assembly version available.
Expand All @@ -31,6 +38,7 @@
<GetPackageVersionDependsOn>$(GetPackageVersionDependsOn);GetVersion</GetPackageVersionDependsOn>

<!-- Property that enables setting of Version -->
<UpdateVersionProperties Condition=" '$(DisableGitVersionTask)' == 'true' ">false</UpdateVersionProperties>
<UpdateVersionProperties Condition=" '$(UpdateVersionProperties)' == '' ">true</UpdateVersionProperties>
<UseFullSemVerForNuGet Condition=" '$(UseFullSemVerForNuGet)' == '' ">false</UseFullSemVerForNuGet>
</PropertyGroup>
Expand Down