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

Commit c380e48

Browse files
Add tests report
1 parent 63b54ab commit c380e48

27 files changed

+287
-132
lines changed

CSharpInteractive.HostApi/TestState.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ namespace HostApi;
33
public enum TestState
44
{
55
Finished,
6-
Failed,
7-
Ignored
6+
Ignored,
7+
Failed
88
}

CSharpInteractive.Tests/BuildRunnerTests.cs

+10-6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class BuildRunnerTests
2020
private readonly Mock<ICommandLineResult> _commandLineResult = new();
2121
private readonly Mock<IStartInfo> _startInfo = new();
2222
private readonly Mock<IStartInfoDescription> _startInfoDescription = new();
23+
private readonly Mock<ICommandLineStatistics> _statistics = new();
2324
private readonly Mock<ICommandLine> _process = new();
2425
private readonly Mock<IProcessResultHandler> _processResultHandler = new();
2526
private readonly ProcessResult _processResult;
@@ -59,7 +60,7 @@ public void ShouldRunBuildWhenHasHandler(bool handled)
5960
.Callback<Output, IReadOnlyCollection<BuildMessage>, Action<BuildMessage>>((o, _, _) => { o.Handled = handled; });
6061

6162
// When
62-
buildService.Run(_process.Object, customHandler, TimeSpan.FromSeconds(1));
63+
var result = buildService.Run(_process.Object, customHandler, TimeSpan.FromSeconds(1));
6364

6465
// Then
6566
output.Handled.ShouldBeTrue();
@@ -68,6 +69,7 @@ public void ShouldRunBuildWhenHasHandler(bool handled)
6869
_teamCityContext.VerifySet(i => i.TeamCityIntegration = true);
6970
_teamCityContext.VerifySet(i => i.TeamCityIntegration = false);
7071
_processResultHandler.Verify(i => i.Handle(_processResult, customHandler));
72+
_statistics.Verify(i => i.Register(new CommandLineInfo(result, _processResult)));
7173
}
7274

7375
[Fact]
@@ -89,14 +91,15 @@ public void ShouldRunBuildWhenHasNoHandler()
8991
.Returns(_processResult);
9092

9193
// When
92-
buildService.Run(_process.Object, default, TimeSpan.FromSeconds(1));
94+
var result = buildService.Run(_process.Object, default, TimeSpan.FromSeconds(1));
9395

9496
// Then
9597
output.Handled.ShouldBeTrue();
9698
_defaultBuildMessagesProcessor.Verify(i => i.ProcessMessages(output, buildMessages, It.IsAny<Action<BuildMessage>>()));
9799
_teamCityContext.VerifySet(i => i.TeamCityIntegration = true);
98100
_teamCityContext.VerifySet(i => i.TeamCityIntegration = false);
99101
_processResultHandler.Verify(i => i.Handle(_processResult, default(Action<BuildMessage>)));
102+
_statistics.Verify(i => i.Register(new CommandLineInfo(result, _processResult)));
100103
}
101104

102105
[Fact]
@@ -110,17 +113,17 @@ public async Task ShouldRunBuildAsync()
110113
var handler = Mock.Of<Action<BuildMessage>>();
111114

112115
// When
113-
await buildService.RunAsync(_process.Object, handler, token);
116+
var result = await buildService.RunAsync(_process.Object, handler, token);
114117

115118
// Then
116119
_teamCityContext.VerifySet(i => i.TeamCityIntegration = true);
117120
_teamCityContext.VerifySet(i => i.TeamCityIntegration = false);
118121
_processResultHandler.Verify(i => i.Handle(_processResult, handler));
122+
_statistics.Verify(i => i.Register(new CommandLineInfo(result, _processResult)));
119123
}
120124

121125
private BuildRunner CreateInstance() =>
122-
new(
123-
_processRunner.Object,
126+
new(_processRunner.Object,
124127
_host.Object,
125128
_teamCityContext.Object,
126129
_resultFactory,
@@ -129,5 +132,6 @@ private BuildRunner CreateInstance() =>
129132
_defaultBuildMessagesProcessor.Object,
130133
_customBuildMessagesProcessor.Object,
131134
_processResultHandler.Object,
132-
_startInfoDescription.Object);
135+
_startInfoDescription.Object,
136+
_statistics.Object);
133137
}

CSharpInteractive.Tests/CommandLineRunnerTests.cs

+13-5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public class CommandLineRunnerTests
1010
private readonly Mock<IProcessResultHandler> _processResultHandler = new();
1111
private readonly Mock<IStartInfo> _startInfo = new();
1212
private readonly Mock<IStartInfoDescription> _startInfoDescription = new();
13+
private readonly Mock<ICommandLineStatistics> _statistics = new();
1314
private readonly ProcessResult _processResult;
1415

1516
public CommandLineRunnerTests() =>
@@ -25,11 +26,12 @@ public void ShouldRunProcess()
2526
_processRunner.Setup(i => i.Run(It.IsAny<ProcessInfo>(), TimeSpan.FromSeconds(1))).Returns(_processResult);
2627

2728
// When
28-
var exitCode = cmdService.Run(process.Object, Handler, TimeSpan.FromSeconds(1)).ExitCode;
29+
var result = cmdService.Run(process.Object, Handler, TimeSpan.FromSeconds(1));
2930

3031
// Then
31-
exitCode.ShouldBe(33);
32+
result.ExitCode.ShouldBe(33);
3233
_processResultHandler.Verify(i => i.Handle<Output>(_processResult, Handler));
34+
_statistics.Verify(i => i.Register(new CommandLineInfo(result, _processResult)));
3335
}
3436

3537
[Fact]
@@ -44,15 +46,21 @@ public async Task ShouldRunProcessAsync()
4446
_processRunner.Setup(i => i.RunAsync(It.IsAny<ProcessInfo>(), token)).Returns(Task.FromResult(_processResult));
4547

4648
// When
47-
var exitCode = (await cmdService.RunAsync(process.Object, Handler, token)).ExitCode;
49+
var result = await cmdService.RunAsync(process.Object, Handler, token);
4850

4951
// Then
50-
exitCode.ShouldBe(33);
52+
result.ExitCode.ShouldBe(33);
5153
_processResultHandler.Verify(i => i.Handle<Output>(_processResult, Handler));
54+
_statistics.Verify(i => i.Register(new CommandLineInfo(result, _processResult)));
5255
}
5356

5457
private static void Handler(Output obj) { }
5558

5659
private CommandLineRunner CreateInstance() =>
57-
new(_host.Object, _processRunner.Object, Mock.Of<IProcessMonitor>, _processResultHandler.Object, _startInfoDescription.Object);
60+
new(_host.Object,
61+
_processRunner.Object,
62+
Mock.Of<IProcessMonitor>,
63+
_processResultHandler.Object,
64+
_startInfoDescription.Object,
65+
_statistics.Object);
5866
}

CSharpInteractive.Tests/Integration/ScriptRunTests.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace CSharpInteractive.Tests.Integration;
1111
[SuppressMessage("Performance", "CA1861:Avoid constant arrays as arguments")]
1212
public class ScriptRunTests
1313
{
14-
private const int InitialLinesCount = 4;
14+
private const int InitialLinesCount = 3;
1515

1616
[Fact]
1717
public void ShouldRunEmptyScript()
@@ -166,7 +166,7 @@ public void ShouldSupportWarning()
166166
// Then
167167
result.ExitCode.ShouldBe(0, result.ToString());
168168
result.StdErr.ShouldBeEmpty(result.ToString());
169-
result.StdOut.Count.ShouldBe(InitialLinesCount + 4, result.ToString());
169+
result.StdOut.Count.ShouldBe(InitialLinesCount + 5, result.ToString());
170170
result.StdOut.Contains("My warning").ShouldBeTrue(result.ToString());
171171
}
172172

CSharpInteractive.Tests/Integration/TeamCityScriptRunTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace CSharpInteractive.Tests.Integration;
44
[Trait("Integration", "true")]
55
public class ScriptRunTeamCityScriptRunTests
66
{
7-
private const int InitialMessagesCount = 4;
7+
private const int InitialMessagesCount = 3;
88

99
[Fact]
1010
public void ShouldAddSystemNamespace()

CSharpInteractive.Tests/ProcessMonitorTests.cs

-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ public class ProcessMonitorTests
1010
private static readonly Text Description = new("My process description", Color.Details);
1111
private readonly Mock<ILog<ProcessMonitor>> _log = new();
1212
private readonly Mock<IEnvironment> _environment = new();
13-
private readonly Mock<IStatistics> _statistics = new();
1413
private readonly Mock<IStartInfo> _startInfo = new();
1514
private readonly Mock<IStartInfoDescription> _startInfoDescription = new();
1615

@@ -75,7 +74,6 @@ internal void ShouldCreateResultWhenFinishedWithSuccess(ProcessState state, stri
7574

7675
// Then
7776
result.Description.ShouldBe([Description, Text.Space, new Text(stateDescription, Color.Success), new Text(" (in 22 ms)"), new Text(" with exit code 33"), new Text(".")]);
78-
_statistics.Verify(i => i.RegisterProcessResult(result));
7977
}
8078

8179
[Fact]
@@ -91,7 +89,6 @@ public void ShouldCreateResultWhenFailed()
9189

9290
// Then
9391
result.Description.ShouldBe([Description, Text.Space, new Text("failed to start", Color.Error), new Text(" (in 22 ms)"), new Text(" with exit code 33"), new Text(".")]);
94-
_statistics.Verify(i => i.RegisterProcessResult(result));
9592
}
9693

9794
[Fact]
@@ -106,7 +103,6 @@ public void ShouldCreateResultWhenFailedToStart()
106103

107104
// Then
108105
result.Description.ShouldBe([Description, Text.Space, new Text("failed to start", Color.Error), new Text(" (in 22 ms)"), new Text(".")]);
109-
_statistics.Verify(i => i.RegisterProcessResult(result));
110106
}
111107

112108
[Fact]
@@ -122,12 +118,10 @@ public void ShouldCreateResultWhenCanceled()
122118

123119
// Then
124120
result.Description.ShouldBe([Description, Text.Space, new Text("canceled", Color.Warning), new Text(" (in 22 ms)"), new Text(".")]);
125-
_statistics.Verify(i => i.RegisterProcessResult(result));
126121
}
127122

128123
private ProcessMonitor CreateInstance() =>
129124
new(_log.Object,
130125
_environment.Object,
131-
_statistics.Object,
132126
_startInfoDescription.Object);
133127
}

CSharpInteractive.Tests/SummaryPresenterTests.cs

+8-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ namespace CSharpInteractive.Tests;
22

33
using System.Diagnostics.CodeAnalysis;
44
using Core;
5-
using CSharpInteractive;
65
using HostApi;
76

87
[SuppressMessage("Performance", "CA1861:Avoid constant arrays as arguments")]
98
public class SummaryPresenterTests
109
{
1110
private readonly Mock<ILog<SummaryPresenter>> _log = new();
11+
private readonly Mock<ICommandLineStatistics> _commandLineStatistics = new();
12+
private readonly Mock<IPresenter<ICommandLineStatistics>> _commandLineStatisticsPresenter = new();
1213
private readonly Mock<IStatistics> _statistics = new();
1314
private readonly Mock<IPresenter<IStatistics>> _statisticsPresenter = new();
1415

@@ -48,10 +49,15 @@ public void ShouldSummary(bool? success, bool hasError, bool hasWarning, string
4849
presenter.Show(new Summary(success));
4950

5051
// Then
52+
_commandLineStatisticsPresenter.Verify(i => i.Show(_commandLineStatistics.Object));
5153
_statisticsPresenter.Verify(i => i.Show(_statistics.Object));
5254
_log.Verify(i => i.Info(new Text(message, color)));
5355
}
5456

5557
private SummaryPresenter CreateInstance() =>
56-
new(_log.Object, _statistics.Object, _statisticsPresenter.Object);
58+
new(_log.Object,
59+
_commandLineStatistics.Object,
60+
_commandLineStatisticsPresenter.Object,
61+
_statistics.Object,
62+
_statisticsPresenter.Object);
5763
}

CSharpInteractive/Composition.cs

+2
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ private void Setup()
104104
return lineCodeSource;
105105
}))
106106
.Bind().To<Statistics>()
107+
.Bind().To<CommandLineStatistics>()
107108
.Bind().To<CommandsRunner>()
108109
.Bind().To<CodeSourceCommandFactory>()
109110
.Bind().To<CSharpScriptRunner>()
@@ -133,6 +134,7 @@ private void Setup()
133134
.Bind().To<StringService>()
134135
.Bind().To<TracePresenter>()
135136
.Bind().To<StatisticsPresenter>()
137+
.Bind().To<CommandLineStatisticsPresenter>()
136138
.Bind().To<DiagnosticsPresenter>()
137139
.Bind().To<ScriptStatePresenter>()
138140
.Bind().To<BuildEngine>()

CSharpInteractive/Core/BuildRunner.cs

+14-9
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ internal class BuildRunner(
1919
[Tag("default")] IBuildMessagesProcessor defaultBuildMessagesProcessor,
2020
[Tag("custom")] IBuildMessagesProcessor customBuildMessagesProcessor,
2121
IProcessResultHandler processResultHandler,
22-
IStartInfoDescription startInfoDescription)
22+
IStartInfoDescription startInfoDescription,
23+
ICommandLineStatistics statistics)
2324
: IBuildRunner
2425
{
2526
public IBuildResult Run(ICommandLine commandLine, Action<BuildMessage>? handler = default, TimeSpan timeout = default)
@@ -28,10 +29,12 @@ public IBuildResult Run(ICommandLine commandLine, Action<BuildMessage>? handler
2829
var buildContext = buildContextFactory();
2930
var startInfo = CreateStartInfo(commandLine);
3031
var processInfo = new ProcessInfo(startInfo, monitorFactory(), output => Handle(handler, output, buildContext));
31-
var result = processRunner.Run(processInfo, timeout);
32-
processResultHandler.Handle(result, handler);
33-
return buildContext.Create(
34-
new CommandLineResult(startInfoDescription, startInfo, result.State, result.ElapsedMilliseconds, result.ExitCode, result.Error));
32+
var processResult = processRunner.Run(processInfo, timeout);
33+
processResultHandler.Handle(processResult, handler);
34+
var buildResult = buildContext.Create(
35+
new CommandLineResult(startInfoDescription, startInfo, processResult.State, processResult.ElapsedMilliseconds, processResult.ExitCode, processResult.Error));
36+
statistics.Register(new CommandLineInfo(buildResult, processResult));
37+
return buildResult;
3538
}
3639

3740
public async Task<IBuildResult> RunAsync(ICommandLine commandLine, Action<BuildMessage>? handler = default, CancellationToken cancellationToken = default)
@@ -40,10 +43,12 @@ public async Task<IBuildResult> RunAsync(ICommandLine commandLine, Action<BuildM
4043
var buildContext = buildContextFactory();
4144
var startInfo = CreateStartInfo(commandLine);
4245
var processInfo = new ProcessInfo(startInfo, monitorFactory(), output => Handle(handler, output, buildContext));
43-
var result = await processRunner.RunAsync(processInfo, cancellationToken);
44-
processResultHandler.Handle(result, handler);
45-
return buildContext.Create(
46-
new CommandLineResult(startInfoDescription, startInfo, result.State, result.ElapsedMilliseconds, result.ExitCode, result.Error));
46+
var processResult = await processRunner.RunAsync(processInfo, cancellationToken);
47+
processResultHandler.Handle(processResult, handler);
48+
var buildResult = buildContext.Create(
49+
new CommandLineResult(startInfoDescription, startInfo, processResult.State, processResult.ElapsedMilliseconds, processResult.ExitCode, processResult.Error));
50+
statistics.Register(new CommandLineInfo(buildResult, processResult));
51+
return buildResult;
4752
}
4853

4954
private IStartInfo CreateStartInfo(ICommandLine commandLine)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace CSharpInteractive.Core;
2+
3+
using HostApi;
4+
5+
internal record CommandLineInfo(
6+
ICommandLineResult CommandLineResult,
7+
ProcessResult ProcessResult);

CSharpInteractive/Core/CommandLineRunner.cs

+12-7
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,27 @@ internal class CommandLineRunner(
88
IProcessRunner processRunner,
99
Func<IProcessMonitor> monitorFactory,
1010
IProcessResultHandler processResultHandler,
11-
IStartInfoDescription startInfoDescription)
11+
IStartInfoDescription startInfoDescription,
12+
ICommandLineStatistics statistics)
1213
: ICommandLineRunner
1314
{
1415
public ICommandLineResult Run(ICommandLine commandLine, Action<Output>? handler = default, TimeSpan timeout = default)
1516
{
1617
ArgumentNullException.ThrowIfNull(commandLine);
17-
var result = processRunner.Run(new ProcessInfo(commandLine.GetStartInfo(host), monitorFactory(), handler), timeout);
18-
processResultHandler.Handle(result, handler);
19-
return new CommandLineResult(startInfoDescription, result.StartInfo, result.State, result.ElapsedMilliseconds, result.ExitCode, result.Error);
18+
var processResult = processRunner.Run(new ProcessInfo(commandLine.GetStartInfo(host), monitorFactory(), handler), timeout);
19+
processResultHandler.Handle(processResult, handler);
20+
var commandLineResult = new CommandLineResult(startInfoDescription, processResult.StartInfo, processResult.State, processResult.ElapsedMilliseconds, processResult.ExitCode, processResult.Error);
21+
statistics.Register(new CommandLineInfo(commandLineResult, processResult));
22+
return commandLineResult;
2023
}
2124

2225
public async Task<ICommandLineResult> RunAsync(ICommandLine commandLine, Action<Output>? handler = default, CancellationToken cancellationToken = default)
2326
{
2427
ArgumentNullException.ThrowIfNull(commandLine);
25-
var result = await processRunner.RunAsync(new ProcessInfo(commandLine.GetStartInfo(host), monitorFactory(), handler), cancellationToken);
26-
processResultHandler.Handle(result, handler);
27-
return new CommandLineResult(startInfoDescription, result.StartInfo, result.State, result.ElapsedMilliseconds, result.ExitCode, result.Error);
28+
var processResult = await processRunner.RunAsync(new ProcessInfo(commandLine.GetStartInfo(host), monitorFactory(), handler), cancellationToken);
29+
processResultHandler.Handle(processResult, handler);
30+
var commandLineResult = new CommandLineResult(startInfoDescription, processResult.StartInfo, processResult.State, processResult.ElapsedMilliseconds, processResult.ExitCode, processResult.Error);
31+
statistics.Register(new CommandLineInfo(commandLineResult, processResult));
32+
return commandLineResult;
2833
}
2934
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
namespace CSharpInteractive.Core;
2+
3+
using System.Collections.ObjectModel;
4+
5+
internal class CommandLineStatistics : ICommandLineStatistics
6+
{
7+
private readonly List<CommandLineInfo> _info = [];
8+
9+
public bool IsEmpty => CommandLines.Count == 0;
10+
11+
public IReadOnlyCollection<CommandLineInfo> CommandLines
12+
{
13+
get
14+
{
15+
lock (_info)
16+
{
17+
return new ReadOnlyCollection<CommandLineInfo>(_info);
18+
}
19+
}
20+
}
21+
22+
public void Register(CommandLineInfo info)
23+
{
24+
lock (_info)
25+
{
26+
_info.Add(info);
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)