Skip to content
This repository was archived by the owner on Feb 12, 2025. It is now read-only.

Commit 8fd4f43

Browse files
author
Sergii Lutai
committed
added CustomLabeller with tests
CustomLabeller allows generate label with plain cs code
1 parent bba79ac commit 8fd4f43

File tree

4 files changed

+185
-0
lines changed

4 files changed

+185
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
using Exortech.NetReflector;
2+
using NUnit.Framework;
3+
using ThoughtWorks.CruiseControl.Core.Label;
4+
5+
namespace ThoughtWorks.CruiseControl.UnitTests.Core.Label
6+
{
7+
[TestFixture]
8+
public class CustomLabellerTest : IntegrationFixture
9+
{
10+
private CustomLabeller labeller;
11+
12+
private string exampleCode = @"
13+
14+
if (
15+
integrationResult != null
16+
&& integrationResult.LastIntegration != null
17+
&& !integrationResult.LastIntegration.IsInitial()
18+
)
19+
{
20+
if (integrationResult.LastIntegration.Status == ThoughtWorks.CruiseControl.Remote.IntegrationStatus.Success)
21+
{
22+
System.Version lastVersion = System.Version.Parse(integrationResult.LastIntegration.Label);
23+
System.Version nextVersion = new System.Version(lastVersion.Major, lastVersion.Minor, lastVersion.Build, lastVersion.Revision + 1);
24+
ret = nextVersion.ToString();
25+
}
26+
else
27+
{
28+
ret = integrationResult.LastIntegration.Label;
29+
}
30+
}
31+
else
32+
{
33+
ret = new System.Version(1, 1, 1, 1).ToString();
34+
}
35+
36+
37+
";
38+
39+
[SetUp]
40+
public void SetUp()
41+
{
42+
labeller = new CustomLabeller();
43+
}
44+
45+
[Test]
46+
public void GenerateInitialLabelWoCode()
47+
{
48+
Assert.AreEqual("0.0.0.0", labeller.Generate(InitialIntegrationResult()));
49+
}
50+
51+
[Test]
52+
public void GenerateLabelWoCode()
53+
{
54+
Assert.AreEqual("0.0.0.0", labeller.Generate(SuccessfulResult("35")));
55+
}
56+
57+
[Test]
58+
public void GenerateNextLabel()
59+
{
60+
labeller.CsCode = @"ret = ""somelabel"";";
61+
Assert.AreEqual("somelabel", labeller.Generate(SuccessfulResult("previouslabel")));
62+
}
63+
64+
[Test]
65+
public void GenerateInitialLabel()
66+
{
67+
labeller.CsCode = @"ret = ""somelabel"";";
68+
Assert.AreEqual("somelabel", labeller.Generate(SuccessfulResult("previouslabel")));
69+
}
70+
71+
[Test]
72+
public void GenerateInitialLabelWithExampleCode()
73+
{
74+
labeller.CsCode = exampleCode;
75+
Assert.AreEqual("1.1.1.1", labeller.Generate(InitialIntegrationResult()));
76+
}
77+
78+
[Test]
79+
public void GenerateNextLabelWithExampleCode()
80+
{
81+
labeller.CsCode = exampleCode;
82+
Assert.AreEqual("1.2.3.5", labeller.Generate(SuccessfulResult("1.2.3.4")));
83+
}
84+
85+
[Test]
86+
public void GenerateSameLabelWithExampleCode()
87+
{
88+
labeller.CsCode = exampleCode;
89+
Assert.AreEqual("1.2.3.4", labeller.Generate(FailedResult("1.2.3.4")));
90+
}
91+
}
92+
}

project/UnitTests/UnitTests.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,7 @@
376376
<Compile Include="Core\Extensions\IntegrationPerformanceCountersExtensionTests.cs" />
377377
<Compile Include="Core\Extensions\DiskSpaceMonitorExtensionTests.cs" />
378378
<Compile Include="Core\Extensions\IntegrationRequestThrottleExtensionTests.cs" />
379+
<Compile Include="Core\Label\CustomLabellerTest.cs" />
379380
<Compile Include="Core\Publishers\ConditionalPublisherTests.cs" />
380381
<Compile Include="Core\Publishers\ManifestGeneratorTests.cs" />
381382
<Compile Include="Core\Publishers\ManifestImporterTests.cs" />

project/core/core.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@
372372
<Compile Include="label\DateLabeller.cs">
373373
<SubType>Code</SubType>
374374
</Compile>
375+
<Compile Include="label\CustomLabeller.cs" />
375376
<Compile Include="label\DefaultLabeller.cs">
376377
<SubType>Code</SubType>
377378
</Compile>

project/core/label/CustomLabeller.cs

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
using System;
2+
using System.Text.RegularExpressions;
3+
using Exortech.NetReflector;
4+
using ThoughtWorks.CruiseControl.Remote;
5+
using System.Globalization;
6+
using Microsoft.CSharp;
7+
using System.CodeDom.Compiler;
8+
using System.Reflection;
9+
10+
namespace ThoughtWorks.CruiseControl.Core.Label
11+
{
12+
/// <summary>
13+
/// <para>
14+
/// Allows CCNet create custom code-generated labels
15+
/// </para>
16+
/// <para>
17+
/// You can do this by specifying your own configuration of the default labeller in your project.
18+
/// </para>
19+
/// </summary>
20+
/// <title>Custom Labeller</title>
21+
/// <version>1.0</version>
22+
/// <example>
23+
/// <code>
24+
/// &lt;labeller type="customlabeller"&gt;
25+
/// &lt;cscode&gt;1&lt;/cscode&gt;
26+
/// &lt;/labeller&gt;
27+
/// </code>
28+
/// </example>
29+
[ReflectorType("customlabeller")]
30+
public class CustomLabeller
31+
: LabellerBase
32+
{
33+
/// <summary>
34+
/// Generates the specified integration result.
35+
/// </summary>
36+
/// <param name="integrationResult">The integration result.</param>
37+
/// <returns></returns>
38+
/// <remarks></remarks>
39+
public override string Generate(IIntegrationResult integrationResult)
40+
{
41+
MethodInfo method = this.CreateFunction(this.CsCode);
42+
string ret = (string)method.Invoke(null, new object[1] { integrationResult });
43+
return ret;
44+
}
45+
46+
[ReflectorProperty("cscode", Required = true)]
47+
public string CsCode { get; set; }
48+
49+
private string CSCodeWrapper
50+
{
51+
get
52+
{
53+
return @"
54+
55+
using System;
56+
using ThoughtWorks.CruiseControl.Core;
57+
using ThoughtWorks.CruiseControl.Remote;
58+
59+
namespace CustomLabelerGeneratorUserFunctions
60+
{
61+
public class CustomLabelerGenerator
62+
{
63+
public static string Generate(ThoughtWorks.CruiseControl.Core.IIntegrationResult integrationResult)
64+
{
65+
string ret = ""0.0.0.0"";
66+
<customCodeForReplace>
67+
return ret;
68+
}
69+
}
70+
}
71+
";
72+
}
73+
}
74+
75+
public MethodInfo CreateFunction(string function)
76+
{
77+
string finalCode = CSCodeWrapper.Replace("<customCodeForReplace>", function);
78+
79+
CSharpCodeProvider provider = new CSharpCodeProvider();
80+
var parameters = new CompilerParameters();
81+
parameters.ReferencedAssemblies.Add(Assembly.GetExecutingAssembly().Location);
82+
parameters.ReferencedAssemblies.Add(typeof(ThoughtWorks.CruiseControl.Remote.IntegrationStatus).Assembly.Location);
83+
parameters.GenerateInMemory = true;
84+
parameters.GenerateExecutable = false;
85+
CompilerResults results = provider.CompileAssemblyFromSource(parameters, finalCode);
86+
87+
Type binaryFunction = results.CompiledAssembly.GetType("CustomLabelerGeneratorUserFunctions.CustomLabelerGenerator");
88+
return binaryFunction.GetMethod("Generate");
89+
}
90+
}
91+
}

0 commit comments

Comments
 (0)