Skip to content

Commit b1ee343

Browse files
committed
Write out gitversion.properties, for #313
1 parent 3cc9f0b commit b1ee343

File tree

4 files changed

+101
-11
lines changed

4 files changed

+101
-11
lines changed

src/GitVersionCore.Tests/BuildServers/EnvironmentVariableJenkinsTests.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,19 @@ private void ClearEnvironmentVariableForDetection()
1717
Environment.SetEnvironmentVariable(key, null, EnvironmentVariableTarget.Process);
1818
}
1919

20-
[Test, Explicit]
20+
[Test]
2121
public void CanApplyCurrentContextWhenEnvironmentVariableIsSet()
2222
{
2323
SetEnvironmentVariableForDetection();
2424
var j = new Jenkins();
2525
Assert.True(j.CanApplyToCurrentContext());
2626
}
2727

28-
[Test, Explicit]
28+
[Test]
2929
public void CanNotApplyCurrentContextWhenEnvironmentVariableIsNotSet()
3030
{
3131
ClearEnvironmentVariableForDetection();
3232
var j = new Jenkins();
3333
Assert.False(j.CanApplyToCurrentContext());
3434
}
35-
3635
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using GitVersion;
5+
using NUnit.Framework;
6+
using Shouldly;
7+
8+
[TestFixture]
9+
public class JenkinsMessageGenerationTests
10+
{
11+
[Test]
12+
public void GenerateSetVersionMessageReturnsVersionAsIs_AlthoughThisIsNotUsedByJenkins()
13+
{
14+
var j = new Jenkins();
15+
var v = j.GenerateSetVersionMessage("0.0.0-Beta4.7");
16+
Assert.AreEqual ("0.0.0-Beta4.7", v);
17+
}
18+
19+
[Test]
20+
public void GenerateMessageTest()
21+
{
22+
var j = new Jenkins();
23+
var actual = j.GenerateSetParameterMessage("name", "value");
24+
Assert.AreEqual(1, actual.Length);
25+
Assert.AreEqual("GitVersion_name=value", actual[0]);
26+
}
27+
28+
[Test, Explicit]
29+
public void WriteAllVariablesToTheTextWriter()
30+
{
31+
// this test method writes to disc, hence marked explicit
32+
var f = "this_file_should_be_deleted.properties";
33+
34+
try
35+
{
36+
AssertVariablesAreWrittenToFile(f);
37+
}
38+
finally
39+
{
40+
File.Delete(f);
41+
}
42+
}
43+
44+
static void AssertVariablesAreWrittenToFile(string f)
45+
{
46+
var writes = new List<string>();
47+
var semanticVersion = new SemanticVersion
48+
{
49+
Major = 1,
50+
Minor = 2,
51+
Patch = 3,
52+
PreReleaseTag = "beta1",
53+
BuildMetaData = "5"
54+
};
55+
56+
semanticVersion.BuildMetaData.CommitDate = DateTimeOffset.Parse("2014-03-06 23:59:59Z");
57+
semanticVersion.BuildMetaData.Sha = "commitSha";
58+
var variables = VariableProvider.GetVariablesFor(semanticVersion, AssemblyVersioningScheme.MajorMinorPatch, VersioningMode.ContinuousDelivery, "ci", false);
59+
60+
var j = new Jenkins(f);
61+
62+
j.WriteIntegration(writes.Add, variables);
63+
64+
writes[1].ShouldBe("1.2.3-beta.1+5");
65+
66+
Assert.True(File.Exists(f));
67+
68+
var props = File.ReadAllText(f);
69+
70+
StringAssert.Contains("GitVersion_Major=1", props);
71+
StringAssert.Contains("GitVersion_Minor=2", props);
72+
}
73+
}

src/GitVersionCore.Tests/GitVersionCore.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
<Compile Include="BuildServers\BuildServerBaseTests.cs" />
9999
<Compile Include="BuildServers\ContinuaCiTests.cs" />
100100
<Compile Include="BuildServers\EnvironmentVariableJenkinsTests.cs" />
101+
<Compile Include="BuildServers\JenkinsMessageGenerationTests.cs" />
101102
<Compile Include="BuildServers\MyGetTests.cs" />
102103
<Compile Include="BuildServers\TeamCityTests.cs" />
103104
<Compile Include="Fixtures\CommitCountingRepoFixture.cs" />
Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,49 @@
11
namespace GitVersion
22
{
33
using System;
4+
using System.IO;
45

56
public class Jenkins : BuildServerBase
67
{
7-
// string _file = "gitversion.properties"
8+
string _file;
9+
10+
public Jenkins() : this("gitversion.properties")
11+
{
12+
}
13+
14+
public Jenkins(string propertiesFileName)
15+
{
16+
_file = propertiesFileName;
17+
}
818

919
public override bool CanApplyToCurrentContext()
1020
{
11-
return !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("JENKINS_URL__"));
21+
return !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("JENKINS_URL"));
1222
}
1323

1424
public override string GenerateSetVersionMessage(string versionToUseForBuildNumber)
1525
{
16-
throw new NotImplementedException();
17-
// tbd
26+
return versionToUseForBuildNumber;
1827
}
1928

2029
public override string[] GenerateSetParameterMessage(string name, string value)
2130
{
22-
throw new NotImplementedException();
23-
// this returns an array of property lines
31+
return new[]
32+
{
33+
string.Format("GitVersion_{0}={1}", name, value)
34+
};
2435
}
2536

2637
public override void WriteIntegration(Action<string> writer, VersionVariables variables)
2738
{
28-
// log message that we're running on jenkins and writing a properties file
29-
// foreach in GenerateSetParameterMessage write line to file
39+
base.WriteIntegration(writer, variables);
40+
writer(string.Format("Outputting variables to '{0}' ... ", _file));
41+
WriteVariablesFile(variables);
42+
}
43+
44+
void WriteVariablesFile(VersionVariables variables)
45+
{
46+
File.WriteAllLines(_file, BuildOutputFormatter.GenerateBuildLogOutput(this, variables));
3047
}
3148
}
3249
}

0 commit comments

Comments
 (0)