Skip to content

Commit

Permalink
Add tests for teamcity properties files loading
Browse files Browse the repository at this point in the history
Required adding IFileSystem and then updates to other bits to pull the IFileSystem through
  • Loading branch information
BlythMeister committed Feb 11, 2021
1 parent 5181897 commit edac95f
Show file tree
Hide file tree
Showing 11 changed files with 167 additions and 34 deletions.
6 changes: 4 additions & 2 deletions src/Cake.Common.Tests/Fixtures/Build/TeamCityFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,23 @@
using Cake.Common.Build.TeamCity;
using Cake.Common.Tests.Fakes;
using Cake.Core;
using Cake.Testing;
using Cake.Core.IO;
using NSubstitute;

namespace Cake.Common.Tests.Fixtures.Build
{
internal sealed class TeamCityFixture
{
public ICakeEnvironment Environment { get; set; }
public IFileSystem FileSystem { get; set; }
public FakeBuildSystemServiceMessageWriter Writer { get; set; }

public TeamCityFixture()
{
Environment = Substitute.For<ICakeEnvironment>();
Environment.WorkingDirectory.Returns("C:\\build\\CAKE-CAKE-JOB1");
Environment.GetEnvironmentVariable("TEAMCITY_VERSION").Returns((string)null);
FileSystem = Substitute.For<IFileSystem>();
Writer = new FakeBuildSystemServiceMessageWriter();
}

Expand All @@ -30,7 +32,7 @@ public void IsRunningOnTeamCity()

public TeamCityProvider CreateTeamCityService()
{
return new TeamCityProvider(Environment, Writer);
return new TeamCityProvider(Environment, FileSystem, Writer);
}
}
}
34 changes: 25 additions & 9 deletions src/Cake.Common.Tests/Fixtures/Build/TeamCityInfoFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,42 @@

using Cake.Common.Build.TeamCity.Data;
using Cake.Core;
using NSubstitute;
using Cake.Core.IO;
using Cake.Testing;

namespace Cake.Common.Tests.Fixtures.Build
{
internal sealed class TeamCityInfoFixture
{
public IFileSystem FileSystem { get; set; }
public ICakeEnvironment Environment { get; set; }

public TeamCityInfoFixture()
{
Environment = Substitute.For<ICakeEnvironment>();
Environment = FakeEnvironment.CreateUnixEnvironment();
FileSystem = new FakeFileSystem(Environment);
((FakeFileSystem)FileSystem).CreateDirectory("/Working");

Environment.GetEnvironmentVariable("TEAMCITY_BUILDCONF_NAME").Returns(@"Cake Build");
Environment.GetEnvironmentVariable("BUILD_NUMBER").Returns("10-Foo");
((FakeEnvironment)Environment).SetEnvironmentVariable("TEAMCITY_BUILDCONF_NAME", @"Cake Build");
((FakeEnvironment)Environment).SetEnvironmentVariable("BUILD_NUMBER", "10-Foo");
((FakeEnvironment)Environment).SetEnvironmentVariable("TEAMCITY_PROJECT_NAME", "Cake");
((FakeEnvironment)Environment).SetEnvironmentVariable("TEAMCITY_BUILD_PROPERTIES_FILE", "/Working/file.properties");
((FakeEnvironment)Environment).SetEnvironmentVariable("Git_Branch", "refs/pull-requests/7/from");
}

Environment.GetEnvironmentVariable("TEAMCITY_PROJECT_NAME").Returns("Cake");
public void SetPropertiesFileContent(string xml)
{
((FakeFileSystem)FileSystem).GetFile("/Working/file.properties.xml").SetContent(xml);
}

Environment.GetEnvironmentVariable("TEAMCITY_BUILD_PROPERTIES_FILE").Returns("path/to/file");
public void SetBuildConfigurationFileContent(string xml)
{
((FakeFileSystem)FileSystem).GetFile("/Working/file.build.configuration.xml").SetContent(xml);
}

Environment.GetEnvironmentVariable("Git_Branch").Returns("refs/pull-requests/7/from");
public void SetGitBranch(string branch)
{
((FakeEnvironment)Environment).SetEnvironmentVariable("Git_Branch", branch);
}

public TeamCityPullRequestInfo CreatePullRequestInfo()
Expand All @@ -33,12 +49,12 @@ public TeamCityPullRequestInfo CreatePullRequestInfo()

public TeamCityEnvironmentInfo CreateEnvironmentInfo()
{
return new TeamCityEnvironmentInfo(Environment);
return new TeamCityEnvironmentInfo(Environment, FileSystem);
}

public TeamCityBuildInfo CreateBuildInfo()
{
return new TeamCityBuildInfo(Environment);
return new TeamCityBuildInfo(Environment, FileSystem);
}

public TeamCityProjectInfo CreateProjectInfo()
Expand Down
31 changes: 31 additions & 0 deletions src/Cake.Common.Tests/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions src/Cake.Common.Tests/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1328,4 +1328,23 @@ Line #3]]&gt;&lt;/releaseNotes&gt;
&lt;/files&gt;
&lt;/package&gt;</value>
</data>
<data name="TeamCity_Build_Configuration_Xml" xml:space="preserve">
<value>&lt;?xml version="1.0" encoding="utf-8" standalone="no"?&gt;
&lt;!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"&gt;
&lt;properties&gt;
&lt;comment&gt;TeamCity configuration parameters for build with id 812869&lt;/comment&gt;
&lt;entry key="build.counter"&gt;414&lt;/entry&gt;
&lt;entry key="build.number"&gt;3246&lt;/entry&gt;
&lt;entry key="teamcity.build.branch"&gt;branchName&lt;/entry&gt;
&lt;entry key="teamcity.build.branch.is_default"&gt;true&lt;/entry&gt;
&lt;/properties&gt;</value>
</data>
<data name="TeamCity_Properties_Xml" xml:space="preserve">
<value>&lt;?xml version="1.0" encoding="utf-8" standalone="no"?&gt;
&lt;!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"&gt;
&lt;properties&gt;
&lt;comment&gt;TeamCity build properties without 'system.' prefix&lt;/comment&gt;
&lt;entry key="teamcity.configuration.properties.file"&gt;/Working/file.build.configuration&lt;/entry&gt;
&lt;/properties&gt;</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void Should_Return_Correct_Value()
public sealed class TheBranchProperty
{
[Fact]
public void Should_Return_Correct_Value()
public void Should_Return_Empty_When_No_Properties()
{
// Given
var info = new TeamCityInfoFixture().CreateBuildInfo();
Expand All @@ -55,22 +55,70 @@ public void Should_Return_Correct_Value()
// Then
Assert.Equal(string.Empty, result);
}

[Fact]
public void Should_Return_Value_From_Properties()
{
// Given
var fixture = new TeamCityInfoFixture();
fixture.SetPropertiesFileContent(Properties.Resources.TeamCity_Properties_Xml);
fixture.SetBuildConfigurationFileContent(Properties.Resources.TeamCity_Build_Configuration_Xml);
var info = fixture.CreateBuildInfo();

// When
var result = info.BranchName;

// Then
Assert.Equal("branchName", result);
}
}

public sealed class ThePropertiesProperty
{
[Fact]
public void Should_Return_Empty_When_Path_Unknown()
public void Should_Return_Empty_When_File_Not_Created()
{
// Given
var info = new TeamCityInfoFixture().CreateBuildInfo();
var fixture = new TeamCityInfoFixture();
var info = fixture.CreateBuildInfo();

// When
var result = info.Properties;

// Then
Assert.Empty(result);
}

[Fact]
public void Should_Return_Empty_When_Build_Properties_File_Not_Created()
{
// Given
var fixture = new TeamCityInfoFixture();
fixture.SetPropertiesFileContent(Properties.Resources.TeamCity_Properties_Xml);
var info = fixture.CreateBuildInfo();

// When
var result = info.Properties;

// Then
Assert.Empty(result);
}

[Fact]
public void Should_Return_Values_When_Files_Exist()
{
// Given
var fixture = new TeamCityInfoFixture();
fixture.SetPropertiesFileContent(Properties.Resources.TeamCity_Properties_Xml);
fixture.SetBuildConfigurationFileContent(Properties.Resources.TeamCity_Build_Configuration_Xml);
var info = fixture.CreateBuildInfo();

// When
var result = info.Properties;

// Then
Assert.NotEmpty(result);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// See the LICENSE file in the project root for more information.

using Cake.Common.Tests.Fixtures.Build;
using NSubstitute;
using Xunit;

namespace Cake.Common.Tests.Unit.Build.TeamCity.Data
Expand All @@ -23,7 +22,7 @@ public void Should_Return_Correct_Value(string value, bool expected)
{
// Given
var fixture = new TeamCityInfoFixture();
fixture.Environment.GetEnvironmentVariable("Git_Branch").Returns(value);
fixture.SetGitBranch(value);
var info = fixture.CreatePullRequestInfo();

// When
Expand All @@ -46,7 +45,7 @@ public void Should_Return_Correct_Value(string value, int? expected)
{
// Given
var fixture = new TeamCityInfoFixture();
fixture.Environment.GetEnvironmentVariable("Git_Branch").Returns(value);
fixture.SetGitBranch(value);
var info = fixture.CreatePullRequestInfo();

// When
Expand Down
18 changes: 15 additions & 3 deletions src/Cake.Common.Tests/Unit/Build/TeamCity/TeamCityProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Cake.Common.Build.TeamCity;
using Cake.Common.Tests.Fixtures.Build;
using Cake.Core.IO;
using Cake.Testing.Extensions;
using Xunit;

namespace Cake.Common.Tests.Unit.Build.TeamCity
Expand All @@ -19,20 +18,33 @@ public sealed class TheConstructor
public void Should_Throw_If_Environment_Is_Null()
{
// Given, When
var result = Record.Exception(() => new TeamCityProvider(null, null));
var result = Record.Exception(() => new TeamCityProvider(null, null, null));

// Then
AssertEx.IsArgumentNullException(result, "environment");
}

[Fact]
public void Should_Throw_If_FileSystem_Is_Null()
{
// Given
var fixture = new TeamCityFixture();

// When
var result = Record.Exception(() => new TeamCityProvider(fixture.Environment, null, null));

// Then
AssertEx.IsArgumentNullException(result, "fileSystem");
}

[Fact]
public void Should_Throw_If_Writer_Is_Null()
{
// Given
var fixture = new TeamCityFixture();

// When
var result = Record.Exception(() => new TeamCityProvider(fixture.Environment, null));
var result = Record.Exception(() => new TeamCityProvider(fixture.Environment, fixture.FileSystem, null));

// Then
AssertEx.IsArgumentNullException(result, "writer");
Expand Down
2 changes: 1 addition & 1 deletion src/Cake.Common/Build/BuildSystemAliases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static BuildSystem BuildSystem(this ICakeContext context)
}

var appVeyorProvider = new AppVeyorProvider(context.Environment, context.ProcessRunner, context.Log);
var teamCityProvider = new TeamCityProvider(context.Environment, new BuildSystemServiceMessageWriter());
var teamCityProvider = new TeamCityProvider(context.Environment, context.FileSystem, new BuildSystemServiceMessageWriter());
var myGetProvider = new MyGetProvider(context.Environment, new BuildSystemServiceMessageWriter());
var bambooProvider = new BambooProvider(context.Environment);
var continuaCIProvider = new ContinuaCIProvider(context.Environment, new BuildSystemServiceMessageWriter());
Expand Down
19 changes: 10 additions & 9 deletions src/Cake.Common/Build/TeamCity/Data/TeamCityBuildInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using System.Xml.XPath;
using Cake.Core;
using Cake.Core.IO;

namespace Cake.Common.Build.TeamCity.Data
{
Expand Down Expand Up @@ -55,33 +55,34 @@ public class TeamCityBuildInfo : TeamCityInfo
/// Initializes a new instance of the <see cref="TeamCityBuildInfo"/> class.
/// </summary>
/// <param name="environment">The environment.</param>
public TeamCityBuildInfo(ICakeEnvironment environment)
/// <param name="fileSystem">The file system.</param>
public TeamCityBuildInfo(ICakeEnvironment environment, IFileSystem fileSystem)
: base(environment)
{
_properties = new Lazy<Dictionary<string, string>>(() =>
{
var buildPropertiesFile = GetEnvironmentString("TEAMCITY_BUILD_PROPERTIES_FILE");
var buildPropertiesXmlFile = $"{buildPropertiesFile}.xml";
if (!File.Exists(buildPropertiesXmlFile))
var buildPropertiesXmlFile = fileSystem.GetFile($"{buildPropertiesFile}.xml");
if (!buildPropertiesXmlFile.Exists)
{
return new Dictionary<string, string>();
}

var buildPropertiesXml = XDocument.Load(buildPropertiesXmlFile);
var buildPropertiesXml = XDocument.Load(buildPropertiesXmlFile.OpenRead());

var configurationPropertiesFile = buildPropertiesXml.XPathSelectElement("//entry[key='teamcity.configuration.properties.file']")?.Value;
var configurationPropertiesFile = buildPropertiesXml.XPathSelectElement("//entry[@key='teamcity.configuration.properties.file']")?.Value;
if (configurationPropertiesFile == null)
{
return new Dictionary<string, string>();
}

var configurationPropertiesXmlFile = $"{configurationPropertiesFile}.xml";
if (!File.Exists(configurationPropertiesXmlFile))
var configurationPropertiesXmlFile = fileSystem.GetFile($"{configurationPropertiesFile}.xml");
if (!configurationPropertiesXmlFile.Exists)
{
return new Dictionary<string, string>();
}

var configurationPropertiesXml = XDocument.Load(configurationPropertiesXmlFile);
var configurationPropertiesXml = XDocument.Load(configurationPropertiesXmlFile.OpenRead());

return configurationPropertiesXml.XPathSelectElements("//entry")
.Where(entry => entry.Attribute("key") != null)
Expand Down
Loading

0 comments on commit edac95f

Please sign in to comment.