Skip to content

Commit fefeb86

Browse files
committed
Copy platform examples to samples folders
1 parent 5c4e0e5 commit fefeb86

19 files changed

+876
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using Microsoft.Testing.Platform.Extensions.Messages;
5+
using Microsoft.Testing.Platform.Extensions.OutputDevice;
6+
using Microsoft.Testing.Platform.Extensions.TestHost;
7+
using Microsoft.Testing.Platform.OutputDevice;
8+
using Microsoft.Testing.Platform.TestHost;
9+
10+
namespace TestingPlatformExplorer.InProcess;
11+
12+
internal class DisplayCompositeExtensionFactorySample : ITestSessionLifetimeHandler, IDataConsumer, IOutputDeviceDataProducer
13+
{
14+
private readonly IOutputDevice _outputDevice;
15+
private int _testNodeUpdateMessageCount;
16+
17+
public Type[] DataTypesConsumed => new[] { typeof(TestNodeUpdateMessage) };
18+
19+
public string Uid => nameof(DisplayCompositeExtensionFactorySample);
20+
21+
public string Version => "1.0.0";
22+
23+
public string DisplayName => nameof(DisplayCompositeExtensionFactorySample);
24+
25+
public string Description => "";
26+
27+
public DisplayCompositeExtensionFactorySample(IOutputDevice outputDevice)
28+
{
29+
_outputDevice = outputDevice;
30+
}
31+
32+
public Task<bool> IsEnabledAsync() => Task.FromResult(true);
33+
34+
public async Task ConsumeAsync(IDataProducer dataProducer, IData value, CancellationToken cancellationToken)
35+
{
36+
_testNodeUpdateMessageCount++;
37+
var testNodeUpdateMessage = (TestNodeUpdateMessage)value;
38+
string testNodeDisplayName = testNodeUpdateMessage.TestNode.DisplayName;
39+
TestNodeUid testNodeId = testNodeUpdateMessage.TestNode.Uid;
40+
41+
TestNodeStateProperty nodeState = testNodeUpdateMessage.TestNode.Properties.Single<TestNodeStateProperty>();
42+
43+
switch (nodeState)
44+
{
45+
case InProgressTestNodeStateProperty _:
46+
{
47+
await _outputDevice.DisplayAsync(this, new FormattedTextOutputDeviceData($"[DisplayCompositeExtensionFactorySample]TestNode '{testNodeId}' with display name '{testNodeDisplayName}' is in progress")
48+
{
49+
ForegroundColor = new SystemConsoleColor() { ConsoleColor = ConsoleColor.Green }
50+
});
51+
break;
52+
}
53+
case PassedTestNodeStateProperty _:
54+
{
55+
await _outputDevice.DisplayAsync(this, new FormattedTextOutputDeviceData($"[DisplayCompositeExtensionFactorySample]TestNode '{testNodeId}' with display name '{testNodeDisplayName}' is completed")
56+
{
57+
ForegroundColor = new SystemConsoleColor() { ConsoleColor = ConsoleColor.Green }
58+
});
59+
break;
60+
}
61+
case FailedTestNodeStateProperty failedTestNodeStateProperty:
62+
{
63+
await _outputDevice.DisplayAsync(this, new FormattedTextOutputDeviceData($"[DisplayCompositeExtensionFactorySample]TestNode '{testNodeId}' with display name '{testNodeDisplayName}' is failed with '{failedTestNodeStateProperty?.Exception?.Message}'")
64+
{
65+
ForegroundColor = new SystemConsoleColor() { ConsoleColor = ConsoleColor.Red }
66+
});
67+
break;
68+
}
69+
case SkippedTestNodeStateProperty _:
70+
{
71+
await _outputDevice.DisplayAsync(this, new FormattedTextOutputDeviceData($"[DisplayCompositeExtensionFactorySample]TestNode '{testNodeId}' with display name '{testNodeDisplayName}' is skipped")
72+
{
73+
ForegroundColor = new SystemConsoleColor() { ConsoleColor = ConsoleColor.White }
74+
});
75+
break;
76+
}
77+
default:
78+
break;
79+
}
80+
}
81+
82+
public async Task OnTestSessionStartingAsync(SessionUid sessionUid, CancellationToken cancellationToken)
83+
=> await _outputDevice.DisplayAsync(this, new FormattedTextOutputDeviceData("[DisplayCompositeExtensionFactorySample]Hello from OnTestSessionStartingAsync")
84+
{
85+
ForegroundColor = new SystemConsoleColor() { ConsoleColor = ConsoleColor.DarkGreen }
86+
});
87+
88+
public async Task OnTestSessionFinishingAsync(SessionUid sessionUid, CancellationToken cancellationToken)
89+
=> await _outputDevice.DisplayAsync(this, new FormattedTextOutputDeviceData($"[DisplayCompositeExtensionFactorySample]Total received 'TestNodeUpdateMessage': {_testNodeUpdateMessageCount}")
90+
{
91+
ForegroundColor = new SystemConsoleColor() { ConsoleColor = ConsoleColor.Green }
92+
});
93+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using Microsoft.Testing.Platform.Extensions.Messages;
5+
using Microsoft.Testing.Platform.Extensions.OutputDevice;
6+
using Microsoft.Testing.Platform.Extensions.TestHost;
7+
using Microsoft.Testing.Platform.OutputDevice;
8+
9+
namespace TestingPlatformExplorer.InProcess;
10+
internal class DisplayDataConsumer : IDataConsumer, IOutputDeviceDataProducer
11+
{
12+
private readonly IOutputDevice _outputDevice;
13+
14+
public Type[] DataTypesConsumed => new[] { typeof(TestNodeUpdateMessage) };
15+
16+
public string Uid => nameof(DisplayDataConsumer);
17+
18+
public string Version => "1.0.0";
19+
20+
public string DisplayName => nameof(DisplayDataConsumer);
21+
22+
public string Description => "This extension display in console the testnode id and display name of TestNodeUpdateMessage data type.";
23+
24+
public DisplayDataConsumer(IOutputDevice outputDevice)
25+
{
26+
_outputDevice = outputDevice;
27+
}
28+
29+
public async Task ConsumeAsync(IDataProducer dataProducer, IData value, CancellationToken cancellationToken)
30+
{
31+
var testNodeUpdateMessage = (TestNodeUpdateMessage)value;
32+
string testNodeDisplayName = testNodeUpdateMessage.TestNode.DisplayName;
33+
TestNodeUid testNodeId = testNodeUpdateMessage.TestNode.Uid;
34+
35+
TestNodeStateProperty nodeState = testNodeUpdateMessage.TestNode.Properties.Single<TestNodeStateProperty>();
36+
37+
switch (nodeState)
38+
{
39+
case InProgressTestNodeStateProperty _:
40+
{
41+
await _outputDevice.DisplayAsync(this, new FormattedTextOutputDeviceData($"[DisplayDataConsumer]TestNode '{testNodeId}' with display name '{testNodeDisplayName}' is in progress")
42+
{
43+
ForegroundColor = new SystemConsoleColor() { ConsoleColor = ConsoleColor.Green }
44+
});
45+
break;
46+
}
47+
case PassedTestNodeStateProperty _:
48+
{
49+
await _outputDevice.DisplayAsync(this, new FormattedTextOutputDeviceData($"[DisplayDataConsumer]TestNode '{testNodeId}' with display name '{testNodeDisplayName}' is completed")
50+
{
51+
ForegroundColor = new SystemConsoleColor() { ConsoleColor = ConsoleColor.Green }
52+
});
53+
break;
54+
}
55+
case FailedTestNodeStateProperty failedTestNodeStateProperty:
56+
{
57+
await _outputDevice.DisplayAsync(this, new FormattedTextOutputDeviceData($"[DisplayDataConsumer]TestNode '{testNodeId}' with display name '{testNodeDisplayName}' is failed with '{failedTestNodeStateProperty?.Exception?.Message}'")
58+
{
59+
ForegroundColor = new SystemConsoleColor() { ConsoleColor = ConsoleColor.Red }
60+
});
61+
break;
62+
}
63+
case SkippedTestNodeStateProperty _:
64+
{
65+
await _outputDevice.DisplayAsync(this, new FormattedTextOutputDeviceData($"[DisplayDataConsumer]TestNode '{testNodeId}' with display name '{testNodeDisplayName}' is skipped")
66+
{
67+
ForegroundColor = new SystemConsoleColor() { ConsoleColor = ConsoleColor.White }
68+
});
69+
break;
70+
}
71+
default:
72+
break;
73+
}
74+
}
75+
public Task<bool> IsEnabledAsync() => Task.FromResult(true);
76+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using Microsoft.Testing.Platform.Extensions.OutputDevice;
5+
using Microsoft.Testing.Platform.Extensions.TestHost;
6+
using Microsoft.Testing.Platform.OutputDevice;
7+
8+
namespace TestingPlatformExplorer.InProcess;
9+
internal class DisplayTestApplicationLifecycleCallbacks : ITestApplicationLifecycleCallbacks, IOutputDeviceDataProducer
10+
{
11+
private readonly IOutputDevice _outputDevice;
12+
13+
public string Uid => nameof(DisplayTestApplicationLifecycleCallbacks);
14+
15+
public string Version => "1.0.0";
16+
17+
public string DisplayName => nameof(DisplayTestApplicationLifecycleCallbacks);
18+
19+
public string Description => "This extension display in console the before/after run";
20+
21+
public DisplayTestApplicationLifecycleCallbacks(IOutputDevice outputDevice)
22+
{
23+
_outputDevice = outputDevice;
24+
}
25+
26+
public async Task AfterRunAsync(int exitCode, CancellationToken cancellation)
27+
=> await _outputDevice.DisplayAsync(this, new FormattedTextOutputDeviceData($"Hello from AfterRunAsync, exit code: {exitCode}")
28+
{
29+
ForegroundColor = new SystemConsoleColor() { ConsoleColor = ConsoleColor.DarkGreen }
30+
});
31+
32+
public async Task BeforeRunAsync(CancellationToken cancellationToken)
33+
=> await _outputDevice.DisplayAsync(this, new FormattedTextOutputDeviceData("Hello from BeforeRunAsync")
34+
{
35+
ForegroundColor = new SystemConsoleColor() { ConsoleColor = ConsoleColor.DarkGreen }
36+
});
37+
38+
public Task<bool> IsEnabledAsync() => Task.FromResult(true);
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using Microsoft.Testing.Platform.Extensions;
5+
using Microsoft.Testing.Platform.Extensions.OutputDevice;
6+
using Microsoft.Testing.Platform.Extensions.TestHost;
7+
using Microsoft.Testing.Platform.OutputDevice;
8+
using Microsoft.Testing.Platform.TestHost;
9+
10+
namespace TestingPlatformExplorer.InProcess;
11+
internal class DisplayTestSessionLifeTimeHandler : ITestSessionLifetimeHandler,
12+
IOutputDeviceDataProducer,
13+
IAsyncInitializableExtension,
14+
IAsyncCleanableExtension,
15+
IAsyncDisposable
16+
{
17+
private readonly IOutputDevice _outputDevice;
18+
19+
public string Uid => "This extension display in console the session start/end";
20+
21+
public string Version => "1.0.0";
22+
23+
public string DisplayName => nameof(DisplayTestSessionLifeTimeHandler);
24+
25+
public string Description => "This extension display in console the session start/end";
26+
27+
public DisplayTestSessionLifeTimeHandler(IOutputDevice outputDevice)
28+
{
29+
_outputDevice = outputDevice;
30+
}
31+
32+
public Task<bool> IsEnabledAsync() => Task.FromResult(true);
33+
34+
public async Task OnTestSessionStartingAsync(SessionUid sessionUid, CancellationToken cancellationToken)
35+
=> await _outputDevice.DisplayAsync(this, new FormattedTextOutputDeviceData("Hello from OnTestSessionStartingAsync")
36+
{
37+
ForegroundColor = new SystemConsoleColor() { ConsoleColor = ConsoleColor.DarkGreen }
38+
});
39+
40+
public async Task OnTestSessionFinishingAsync(SessionUid sessionUid, CancellationToken cancellationToken)
41+
=> await _outputDevice.DisplayAsync(this, new FormattedTextOutputDeviceData("Hello from OnTestSessionFinishingAsync")
42+
{
43+
ForegroundColor = new SystemConsoleColor() { ConsoleColor = ConsoleColor.DarkGreen }
44+
});
45+
public async Task InitializeAsync()
46+
=> await _outputDevice.DisplayAsync(this, new FormattedTextOutputDeviceData("Hello from InitializeAsync")
47+
{
48+
ForegroundColor = new SystemConsoleColor() { ConsoleColor = ConsoleColor.DarkGreen }
49+
});
50+
51+
public async Task CleanupAsync()
52+
=> await _outputDevice.DisplayAsync(this, new FormattedTextOutputDeviceData("Hello from CleanupAsync")
53+
{
54+
ForegroundColor = new SystemConsoleColor() { ConsoleColor = ConsoleColor.DarkGreen }
55+
});
56+
57+
public async ValueTask DisposeAsync()
58+
=> await _outputDevice.DisplayAsync(this, new FormattedTextOutputDeviceData("Hello from DisposeAsync")
59+
{
60+
ForegroundColor = new SystemConsoleColor() { ConsoleColor = ConsoleColor.DarkGreen }
61+
});
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using Microsoft.Testing.Platform.Extensions.OutputDevice;
5+
using Microsoft.Testing.Platform.Extensions.TestHostControllers;
6+
using Microsoft.Testing.Platform.OutputDevice;
7+
8+
namespace TestingPlatformExplorer.OutOfProcess;
9+
10+
internal class MonitorTestHost : ITestHostProcessLifetimeHandler, IOutputDeviceDataProducer
11+
{
12+
private readonly IOutputDevice _outputDevice;
13+
14+
public MonitorTestHost(IOutputDevice outputDevice)
15+
{
16+
_outputDevice = outputDevice;
17+
}
18+
19+
public string Uid => nameof(MonitorTestHost);
20+
21+
public string Version => "1.0.0";
22+
23+
public string DisplayName => nameof(MonitorTestHost);
24+
25+
public string Description => "Example of monitoring the test host process.";
26+
27+
public Task<bool> IsEnabledAsync() => Task.FromResult(true);
28+
29+
public async Task BeforeTestHostProcessStartAsync(CancellationToken cancellationToken)
30+
=> await _outputDevice.DisplayAsync(this, new FormattedTextOutputDeviceData("BeforeTestHostProcessStartAsync")
31+
{
32+
ForegroundColor = new SystemConsoleColor() { ConsoleColor = ConsoleColor.Green }
33+
});
34+
35+
public async Task OnTestHostProcessExitedAsync(ITestHostProcessInformation testHostProcessInformation, CancellationToken cancellation)
36+
=> await _outputDevice.DisplayAsync(this, new FormattedTextOutputDeviceData($"OnTestHostProcessExitedAsync, test host exited with exit code {testHostProcessInformation.ExitCode}")
37+
{
38+
ForegroundColor = new SystemConsoleColor() { ConsoleColor = ConsoleColor.Green }
39+
});
40+
41+
public async Task OnTestHostProcessStartedAsync(ITestHostProcessInformation testHostProcessInformation, CancellationToken cancellation)
42+
=> await _outputDevice.DisplayAsync(this, new FormattedTextOutputDeviceData($"OnTestHostProcessStartedAsync, test host started with PID {testHostProcessInformation.PID}")
43+
{
44+
ForegroundColor = new SystemConsoleColor() { ConsoleColor = ConsoleColor.Green }
45+
});
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
5+
using Microsoft.Testing.Platform.Extensions;
6+
using Microsoft.Testing.Platform.Extensions.TestHostControllers;
7+
8+
namespace TestingPlatformExplorer.OutOfProcess;
9+
10+
internal class SetEnvironmentVariableForTestHost : ITestHostEnvironmentVariableProvider
11+
{
12+
public string Uid => nameof(SetEnvironmentVariableForTestHost);
13+
14+
public string Version => "1.0.0";
15+
16+
public string DisplayName => nameof(SetEnvironmentVariableForTestHost);
17+
18+
public string Description => "Example of setting environment variables for the test host.";
19+
20+
public Task<bool> IsEnabledAsync() => Task.FromResult(true);
21+
22+
public Task UpdateAsync(IEnvironmentVariables environmentVariables)
23+
{
24+
environmentVariables.SetVariable(new EnvironmentVariable("SAMPLE", "SAMPLE_VALUE", false, true));
25+
return Task.CompletedTask;
26+
}
27+
28+
public Task<ValidationResult> ValidateTestHostEnvironmentVariablesAsync(IReadOnlyEnvironmentVariables environmentVariables)
29+
=> environmentVariables.TryGetVariable("SAMPLE", out OwnedEnvironmentVariable? value) && value.Value == "SAMPLE_VALUE"
30+
? ValidationResult.ValidTask
31+
: ValidationResult.InvalidTask("The environment variable 'SAMPLE' is not set to 'SAMPLE_VALUE'.");
32+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System.Reflection;
5+
6+
using Microsoft.Testing.Platform.Builder;
7+
using Microsoft.Testing.Platform.Extensions;
8+
using Microsoft.Testing.Platform.Services;
9+
10+
using TestingPlatformExplorer.InProcess;
11+
using TestingPlatformExplorer.OutOfProcess;
12+
using TestingPlatformExplorer.TestingFramework;
13+
14+
// Create the test application builder
15+
ITestApplicationBuilder testApplicationBuilder = await TestApplication.CreateBuilderAsync(args);
16+
17+
// Register the testing framework
18+
testApplicationBuilder.AddTestingFramework(() => new[] { Assembly.GetExecutingAssembly() });
19+
20+
// In-process & out-of-process extensions
21+
// Register the testing framework command line options
22+
testApplicationBuilder.CommandLine.AddProvider(() => new TestingFrameworkCommandLineOptions());
23+
24+
// In-process extensions
25+
testApplicationBuilder.TestHost.AddTestApplicationLifecycleCallbacks(serviceProvider
26+
=> new DisplayTestApplicationLifecycleCallbacks(serviceProvider.GetOutputDevice()));
27+
testApplicationBuilder.TestHost.AddTestSessionLifetimeHandle(serviceProvider
28+
=> new DisplayTestSessionLifeTimeHandler(serviceProvider.GetOutputDevice()));
29+
testApplicationBuilder.TestHost.AddDataConsumer(serviceProvider
30+
=> new DisplayDataConsumer(serviceProvider.GetOutputDevice()));
31+
32+
// Out-of-process extensions
33+
testApplicationBuilder.TestHostControllers.AddEnvironmentVariableProvider(_
34+
=> new SetEnvironmentVariableForTestHost());
35+
testApplicationBuilder.TestHostControllers.AddProcessLifetimeHandler(serviceProvider =>
36+
new MonitorTestHost(serviceProvider.GetOutputDevice()));
37+
38+
// In-process composite extension SessionLifeTimeHandler+DataConsumer
39+
CompositeExtensionFactory<DisplayCompositeExtensionFactorySample> compositeExtensionFactory = new(serviceProvider => new DisplayCompositeExtensionFactorySample(serviceProvider.GetOutputDevice()));
40+
testApplicationBuilder.TestHost.AddTestSessionLifetimeHandle(compositeExtensionFactory);
41+
testApplicationBuilder.TestHost.AddDataConsumer(compositeExtensionFactory);
42+
43+
using ITestApplication testApplication = await testApplicationBuilder.BuildAsync();
44+
return await testApplication.RunAsync();

0 commit comments

Comments
 (0)