Skip to content
This repository was archived by the owner on Jul 18, 2024. It is now read-only.

Commit 53e7f29

Browse files
NikolayPianikovNikolayPianikov
NikolayPianikov
authored and
NikolayPianikov
committed
Add docs for TeamCity service messages API
1 parent b322ed6 commit 53e7f29

12 files changed

+181
-92
lines changed

README.md

+34
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ Please use our YouTrack to [report](https://youtrack.jetbrains.com/newIssue?proj
7777
- [Run asynchronously in parallel](#run-asynchronously-in-parallel)
7878
- [Cancellation of asynchronous run](#cancellation-of-asynchronous-run)
7979
- [Run timeout](#run-timeout)
80+
- TeamCity Service Messages API
81+
- [TeamCity integration via service messages](#teamcity-integration-via-service-messages)
8082

8183
### Restore NuGet a package of newest version
8284

@@ -216,3 +218,35 @@ exitCode.HasValue.ShouldBeFalse();
216218

217219

218220

221+
### TeamCity integration via service messages
222+
223+
For more details how to use TeamCity service message API please see [this](https://github.com/JetBrains/TeamCity.ServiceMessages) page. Instead of creating a root message writer like in the following example:
224+
``` CSharp
225+
using var writer = new TeamCityServiceMessages().CreateWriter(Console.WriteLine);
226+
```
227+
use this statement:
228+
``` CSharp
229+
using var writer = GetService<ITeamCityWriter>();
230+
```
231+
This sample opens a block _My Tests_ and reports about two tests:
232+
233+
``` CSharp
234+
using var writer = GetService<ITeamCityWriter>();
235+
using (var tests = writer.OpenBlock("My Tests"))
236+
{
237+
using (var test = tests.OpenTest("Test1"))
238+
{
239+
test.WriteStdOutput("Hello");
240+
test.WriteImage("TestsResults/Test1Screenshot.jpg", "Screenshot");
241+
test.WriteDuration(TimeSpan.FromMilliseconds(10));
242+
}
243+
244+
using (var test = tests.OpenTest("Test2"))
245+
{
246+
test.WriteFailed("Some error", "Error details");
247+
}
248+
}
249+
```
250+
251+
For more information on TeamCity Service Messages, see [this](https://www.jetbrains.com/help/teamcity/service-messages.html) page.
252+

TeamCity.CSharpInteractive.Tests/Integration/ScriptRunTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ public void ShouldSupportTeamCityServiceMessages()
366366
// When
367367
var result = TestTool.Run(
368368
"using JetBrains.TeamCity.ServiceMessages.Write.Special;",
369-
"GetService<ITeamCityBuildStatusWriter>().WriteBuildParameter(\"system.hello\", \"Abc\");");
369+
"GetService<ITeamCityWriter>().WriteBuildParameter(\"system.hello\", \"Abc\");");
370370

371371
// Then
372372
result.StdErr.ShouldBeEmpty(result.ToString());

TeamCity.CSharpInteractive.Tests/README_TEMPLATE.md

+34
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
- [Run asynchronously in parallel](#run-asynchronously-in-parallel)
1313
- [Cancellation of asynchronous run](#cancellation-of-asynchronous-run)
1414
- [Run timeout](#run-timeout)
15+
- TeamCity Service Messages API
16+
- [TeamCity integration via service messages](#teamcity-integration-via-service-messages)
1517

1618
### Restore NuGet a package of newest version
1719

@@ -151,3 +153,35 @@ exitCode.HasValue.ShouldBeFalse();
151153

152154

153155

156+
### TeamCity integration via service messages
157+
158+
For more details how to use TeamCity service message API please see [this](https://github.com/JetBrains/TeamCity.ServiceMessages) page. Instead of creating a root message writer like in the following example:
159+
``` CSharp
160+
using var writer = new TeamCityServiceMessages().CreateWriter(Console.WriteLine);
161+
```
162+
use this statement:
163+
``` CSharp
164+
using var writer = GetService<ITeamCityWriter>();
165+
```
166+
This sample opens a block _My Tests_ and reports about two tests:
167+
168+
``` CSharp
169+
using var writer = GetService<ITeamCityWriter>();
170+
using (var tests = writer.OpenBlock("My Tests"))
171+
{
172+
using (var test = tests.OpenTest("Test1"))
173+
{
174+
test.WriteStdOutput("Hello");
175+
test.WriteImage("TestsResults/Test1Screenshot.jpg", "Screenshot");
176+
test.WriteDuration(TimeSpan.FromMilliseconds(10));
177+
}
178+
179+
using (var test = tests.OpenTest("Test2"))
180+
{
181+
test.WriteFailed("Some error", "Error details");
182+
}
183+
}
184+
```
185+
186+
For more information on TeamCity Service Messages, see [this](https://www.jetbrains.com/help/teamcity/service-messages.html) page.
187+

TeamCity.CSharpInteractive.Tests/TeamCityLogTests.cs

+10-16
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,16 @@ public class TeamCityLogTests
99
{
1010
private readonly Mock<ISettings> _settings;
1111
private readonly Mock<ITeamCityLineFormatter> _lineFormatter;
12-
private readonly Mock<ITeamCityBlockWriter<ITeamCityWriter>> _blockWriter;
13-
private readonly Mock<ITeamCityMessageWriter> _teamCityMessageWriter;
12+
private readonly Mock<ITeamCityWriter> _teamCityWriter;
1413
private readonly Text[] _text = {new("line1"), new("line2")};
1514
private readonly Mock<IStatistics> _statistics;
16-
private readonly Mock<ITeamCityBuildStatusWriter> _teamCityBuildStatusWriter;
17-
15+
1816
public TeamCityLogTests()
1917
{
2018
_settings = new Mock<ISettings>();
2119
_lineFormatter = new Mock<ITeamCityLineFormatter>();
2220
_lineFormatter.Setup(i => i.Format(It.IsAny<Text[]>())).Returns<Text[]>(i => "F_" + i.ToSimpleString());
23-
_blockWriter = new Mock<ITeamCityBlockWriter<ITeamCityWriter>>();
24-
_teamCityMessageWriter = new Mock<ITeamCityMessageWriter>();
25-
_teamCityBuildStatusWriter = new Mock<ITeamCityBuildStatusWriter>();
21+
_teamCityWriter = new Mock<ITeamCityWriter>();
2622
_statistics = new Mock<IStatistics>();
2723
}
2824

@@ -32,13 +28,13 @@ public void ShouldSupportBlock()
3228
// Given
3329
var log = CreateInstance();
3430
var blockToken = Mock.Of<ITeamCityWriter>();
35-
_blockWriter.Setup(i => i.OpenBlock("line1line2")).Returns(blockToken);
31+
_teamCityWriter.Setup(i => i.OpenBlock("line1line2")).Returns(blockToken);
3632

3733
// When
3834
var actualBlockToken = log.Block(_text);
3935

4036
// Then
41-
_blockWriter.Verify(i => i.OpenBlock("line1line2"));
37+
_teamCityWriter.Verify(i => i.OpenBlock("line1line2"));
4238
actualBlockToken.ShouldBe(blockToken);
4339
}
4440

@@ -56,7 +52,7 @@ internal void ShouldSupportError(VerbosityLevel verbosityLevel)
5652
log.Error(new ErrorId("id"), _text);
5753

5854
// Then
59-
_teamCityBuildStatusWriter.Verify(i => i.WriteBuildProblem("id", "line1line2"));
55+
_teamCityWriter.Verify(i => i.WriteBuildProblem("id", "line1line2"));
6056
_statistics.Verify(i => i.RegisterError("line1line2"));
6157
}
6258

@@ -74,7 +70,7 @@ internal void ShouldSupportWarning(VerbosityLevel verbosityLevel)
7470
log.Warning(_text);
7571

7672
// Then
77-
_teamCityMessageWriter.Verify(i => i.WriteWarning("line1line2"));
73+
_teamCityWriter.Verify(i => i.WriteWarning("line1line2"));
7874
_statistics.Verify(i => i.RegisterWarning("line1line2"));
7975
}
8076

@@ -93,7 +89,7 @@ internal void ShouldSupportInfo(VerbosityLevel verbosityLevel, bool enabled)
9389
log.Info(_text);
9490

9591
// Then
96-
_teamCityMessageWriter.Verify(i => i.WriteMessage("F_line1line2"), times);
92+
_teamCityWriter.Verify(i => i.WriteMessage("F_line1line2"), times);
9793
}
9894

9995
[Theory]
@@ -111,16 +107,14 @@ internal void ShouldSupportTrace(VerbosityLevel verbosityLevel, bool enabled)
111107
log.Trace("Orig", _text);
112108

113109
// Then
114-
_teamCityMessageWriter.Verify(i => i.WriteMessage($"F_{"Orig", -40}line1line2"), times);
110+
_teamCityWriter.Verify(i => i.WriteMessage($"F_{"Orig", -40}line1line2"), times);
115111
}
116112

117113
private TeamCityLog<string> CreateInstance() =>
118114
new(
119115
_settings.Object,
116+
_teamCityWriter.Object,
120117
_lineFormatter.Object,
121-
_blockWriter.Object,
122-
_teamCityMessageWriter.Object,
123-
_teamCityBuildStatusWriter.Object,
124118
_statistics.Object);
125119
}
126120
}

TeamCity.CSharpInteractive.Tests/TeamCityOutputTests.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ namespace TeamCity.CSharpInteractive.Tests
77
public class TeamCityOutputTests
88
{
99
private readonly Mock<ITeamCityLineFormatter> _lineFormatter;
10-
private readonly Mock<ITeamCityMessageWriter> _teamCityMessageWriter;
10+
private readonly Mock<ITeamCityWriter> _teamCityWriter;
1111

1212
public TeamCityOutputTests()
1313
{
1414
_lineFormatter = new Mock<ITeamCityLineFormatter>();
1515
_lineFormatter.Setup(i => i.Format(It.IsAny<Text[]>())).Returns<Text[]>(i => "F_" + i.ToSimpleString());
16-
_teamCityMessageWriter = new Mock<ITeamCityMessageWriter>();
16+
_teamCityWriter = new Mock<ITeamCityWriter>();
1717
}
1818

1919
[Fact]
@@ -26,7 +26,7 @@ public void ShouldWriteError()
2626
output.WriteLine(new[] {new Text("err")});
2727

2828
// Then
29-
_teamCityMessageWriter.Verify(i => i.WriteError("F_err", null));
29+
_teamCityWriter.Verify(i => i.WriteError("F_err", null));
3030
}
3131

3232
[Fact]
@@ -39,12 +39,12 @@ public void ShouldWriteMessage()
3939
output.WriteLine(new[] {new Text("message")});
4040

4141
// Then
42-
_teamCityMessageWriter.Verify(i => i.WriteMessage("F_message"));
42+
_teamCityWriter.Verify(i => i.WriteMessage("F_message"));
4343
}
4444

4545
private TeamCityOutput CreateInstance() =>
4646
new(
4747
_lineFormatter.Object,
48-
_teamCityMessageWriter.Object);
48+
_teamCityWriter.Object);
4949
}
5050
}

TeamCity.CSharpInteractive.Tests/TeamCityPropertiesTests.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ namespace TeamCity.CSharpInteractive.Tests
88
public class TeamCityPropertiesTests
99
{
1010
private readonly Mock<IProperties> _properties;
11-
private readonly Mock<ITeamCityBuildStatusWriter> _teamCityBuildStatusWriter;
11+
private readonly Mock<ITeamCityWriter> _teamCityWriter;
1212

1313
public TeamCityPropertiesTests()
1414
{
1515
_properties = new Mock<IProperties>();
16-
_teamCityBuildStatusWriter = new Mock<ITeamCityBuildStatusWriter>();
16+
_teamCityWriter = new Mock<ITeamCityWriter>();
1717
}
1818

1919
[Fact]
@@ -27,10 +27,10 @@ public void ShouldSetProperty()
2727

2828
// Then
2929
_properties.VerifySet(i => i["Abc"] = "Xyz");
30-
_teamCityBuildStatusWriter.Verify(i => i.WriteBuildParameter("system.Abc", "Xyz"));
30+
_teamCityWriter.Verify(i => i.WriteBuildParameter("system.Abc", "Xyz"));
3131
}
3232

3333
private TeamCityProperties CreateInstance() =>
34-
new(_properties.Object, _teamCityBuildStatusWriter.Object);
34+
new(_properties.Object, _teamCityWriter.Object);
3535
}
3636
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// ReSharper disable StringLiteralTypo
2+
// ReSharper disable ConvertToUsingDeclaration
3+
namespace TeamCity.CSharpInteractive.Tests.UsageScenarios
4+
{
5+
using System;
6+
using JetBrains.TeamCity.ServiceMessages.Write.Special;
7+
using Xunit;
8+
9+
public class TeamCityServiceMessages: Scenario
10+
{
11+
[SkippableFact]
12+
public void Run()
13+
{
14+
// $visible=true
15+
// $tag=3 TeamCity Service Messages API
16+
// $priority=00
17+
// $description=TeamCity integration via service messages
18+
// $header=For more details how to use TeamCity service message API please see [this](https://github.com/JetBrains/TeamCity.ServiceMessages) page. Instead of creating a root message writer like in the following example:
19+
// $header=``` CSharp
20+
// $header=using var writer = new TeamCityServiceMessages().CreateWriter(Console.WriteLine);
21+
// $header=```
22+
// $header=use this statement:
23+
// $header=``` CSharp
24+
// $header=using var writer = GetService<ITeamCityWriter>();
25+
// $header=```
26+
// $header=This sample opens a block _My Tests_ and reports about two tests:
27+
// $footer=For more information on TeamCity Service Messages, see [this](https://www.jetbrains.com/help/teamcity/service-messages.html) page.
28+
// {
29+
using var writer = GetService<ITeamCityWriter>();
30+
using (var tests = writer.OpenBlock("My Tests"))
31+
{
32+
using (var test = tests.OpenTest("Test1"))
33+
{
34+
test.WriteStdOutput("Hello");
35+
test.WriteImage("TestsResults/Test1Screenshot.jpg", "Screenshot");
36+
test.WriteDuration(TimeSpan.FromMilliseconds(10));
37+
}
38+
39+
using (var test = tests.OpenTest("Test2"))
40+
{
41+
test.WriteFailed("Some error", "Error details");
42+
}
43+
}
44+
// }
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)