Skip to content
This repository was archived by the owner on Dec 5, 2022. It is now read-only.

Passing test sources parameter to data collectors #176

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions RFCs/0026-DataCollector-Extensibility-Sources-Parameter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# 0026 DataCollector Extensibility - Adding testSources parameter

## Summary
Exposing `testSources` parameter to datacollector extensions. List of test sources can be used by datacollectors for processing before test run start.

## Motivation
Data collector might need test sources list while initialization. Example: Static code coverage data collector needs to instrument the test sources before test run start.

## Using testSources parameter in DataCollector

Refer [datacollector](https://github.com/Microsoft/vstest/tree/master/test/TestAssets/OutOfProcDataCollector) doc for more details on how to write a datacollector.
To use `testSources` parameter in `DataCollector`:

```csharp
using Microsoft.VisualStudio.TestPlatform.ObjectModel.DataCollection;

[DataCollectorFriendlyName("NewDataCollector")]
[DataCollectorTypeUri("my://new/datacollector")]
public class NewDataCollector : DataCollector
{
private string logFileName;
private IEnumerable<string> testSources;
private DataCollectionEnvironmentContext context;

public override void Initialize(
System.Xml.XmlElement configurationElement,
DataCollectionEvents events,
DataCollectionSink dataSink,
DataCollectionLogger logger,
DataCollectionEnvironmentContext environmentContext)
{
events.SessionStart += this.SessionStarted_Handler;
events.TestCaseStart += this.Events_TestCaseStart;
this.logFileName = configurationElement["LogFileName"];
this.context = environmentContext;
}

public override void Initialize(
System.Xml.XmlElement configurationElement,
IEnumerable<string> testSources,
DataCollectionEvents events,
DataCollectionSink dataSink,
DataCollectionLogger logger,
DataCollectionEnvironmentContext environmentContext)
{
this.testSources = testSources;
base.Initialize(configurationElement, events, dataSink, logger, environmentContext);
}

private void SessionStarted_Handler(object sender, SessionStartEventArgs args)
{
var filename = Path.Combine(AppContext.BaseDirectory, logFileName);
File.WriteAllText(filename, "SessionStarted");
this.dataCollectionSink.SendFileAsync(this.context.SessionDataCollectionContext, filename, true);
this.logger.LogWarning(this.context.SessionDataCollectionContext, "SessionStarted");
}


private void Events_TestCaseStart(object sender, TestCaseStartEventArgs e)
{
this.logger.LogWarning(this.context.SessionDataCollectionContext, "TestCaseStarted " + e.TestCaseName);
}
}
```
DataCollector exposes

`Initialize(System.Xml.XmlElement configurationElement, IEnumerable<string> testSources, DataCollectionEvents events, DataCollectionSink dataSink, DataCollectionLogger logger, DataCollectionEnvironmentContext environmentContext)`

in addition to existing

`Initialize(System.Xml.XmlElement configurationElement, DataCollectionEvents events, DataCollectionSink dataSink, DataCollectionLogger logger, DataCollectionEnvironmentContext environmentContext)`

to expose `testSources` list to `DataCollector`.
`testSources` parameter is an IEnumerable of test source file path.
3 changes: 2 additions & 1 deletion docs/extensions/datacollector.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public class NewDataCollector : DataCollector
{
events.SessionStart += this.SessionStarted_Handler;
events.TestCaseStart += this.Events_TestCaseStart;
logFileName = configurationElement["LogFileName"];
this.logFileName = configurationElement["LogFileName"];
this.context = environmentContext;
}

private void SessionStarted_Handler(object sender, SessionStartEventArgs args)
Expand Down