Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
102 changes: 56 additions & 46 deletions src/Platform/Microsoft.Testing.Platform/Telemetry/TelemetryManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,29 @@ public async Task<ITelemetryCollector> BuildAsync(ServiceProvider serviceProvide
await logger.LogDebugAsync($"{EnvironmentVariableConstants.DOTNET_CLI_TELEMETRY_OPTOUT} environment variable: '{cli_telemetryOptOut}'");
isTelemetryOptedOut = (cli_telemetryOptOut is "1" or "true") || isTelemetryOptedOut;

// NO_LOGO
await logger.LogDebugAsync($"Telemetry is '{(!isTelemetryOptedOut ? "ENABLED" : "DISABLED")}'");

if (!isTelemetryOptedOut && _telemetryFactory is not null)
{
await ShowTelemetryBannerFirstNoticeAsync(serviceProvider, logger, environment);
}

serviceProvider.TryAddService(new TelemetryInformation(!isTelemetryOptedOut, TelemetryProperties.VersionValue));

ITelemetryCollector telemetryCollector = _telemetryFactory is null || isTelemetryOptedOut
? new NopTelemetryService(!isTelemetryOptedOut)
: _telemetryFactory(serviceProvider);

if (!isTelemetryOptedOut)
{
await logger.LogDebugAsync($"Telemetry collector provider: '{telemetryCollector.GetType()}'");
}

return telemetryCollector;
}

private async Task ShowTelemetryBannerFirstNoticeAsync(ServiceProvider serviceProvider, ILogger<TelemetryManager> logger, IEnvironment environment)
{
// If the environment variable is not set or is set to 0, telemetry is opted in.
ICommandLineOptions commandLineOptions = serviceProvider.GetCommandLineOptions();
bool doNotShowLogo = commandLineOptions.IsOptionSet(PlatformCommandLineProvider.NoBannerOptionKey);
Expand All @@ -61,64 +82,53 @@ public async Task<ITelemetryCollector> BuildAsync(ServiceProvider serviceProvide
await logger.LogDebugAsync($"{EnvironmentVariableConstants.DOTNET_NOLOGO} environment variable: '{dotnetNoLogoEnvVar}'");
doNotShowLogo = (dotnetNoLogoEnvVar is "1" or "true") || doNotShowLogo;

await logger.LogDebugAsync($"Telemetry is '{(!isTelemetryOptedOut ? "ENABLED" : "DISABLED")}'");

if (!isTelemetryOptedOut && !doNotShowLogo)
if (doNotShowLogo)
{
ITestApplicationModuleInfo testApplicationModuleInfo = serviceProvider.GetTestApplicationModuleInfo();
return;
}

ITestApplicationModuleInfo testApplicationModuleInfo = serviceProvider.GetTestApplicationModuleInfo();
#pragma warning disable RS0030 // Do not use banned APIs - There is no easy way to disable it for all members
string? directory = environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData, Environment.SpecialFolderOption.Create);
string? directory = environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData, Environment.SpecialFolderOption.Create);
#pragma warning restore RS0030 // Do not use banned APIs
if (directory is not null)
{
directory = Path.Combine(directory, "Microsoft", "TestingPlatform");
}
if (directory is not null)
{
directory = Path.Combine(directory, "Microsoft", "TestingPlatform");
}

IFileSystem fileSystem = serviceProvider.GetFileSystem();
string fileName = Path.ChangeExtension(Path.GetFileName(testApplicationModuleInfo.GetCurrentTestApplicationFullPath()), "testingPlatformFirstTimeUseSentinel");
bool sentinelIsNotPresent =
RoslynString.IsNullOrWhiteSpace(directory)
|| !fileSystem.Exists(Path.Combine(directory, fileName));
IFileSystem fileSystem = serviceProvider.GetFileSystem();
string fileName = Path.ChangeExtension(Path.GetFileName(testApplicationModuleInfo.GetCurrentTestApplicationFullPath()), "testingPlatformFirstTimeUseSentinel");
bool sentinelIsNotPresent =
RoslynString.IsNullOrWhiteSpace(directory)
|| !fileSystem.Exists(Path.Combine(directory, fileName));

if (!doNotShowLogo && sentinelIsNotPresent)
if (!sentinelIsNotPresent)
{
return;
}

IOutputDevice outputDevice = serviceProvider.GetOutputDevice();
await outputDevice.DisplayAsync(this, new TextOutputDeviceData(PlatformResources.TelemetryNotice));

string? path = null;
try
{
// See if we should write the file, and write it.
if (!RoslynString.IsNullOrWhiteSpace(directory))
{
IOutputDevice outputDevice = serviceProvider.GetOutputDevice();
await outputDevice.DisplayAsync(this, new TextOutputDeviceData(PlatformResources.TelemetryNotice));
Directory.CreateDirectory(directory);

string? path = null;
try
{
// See if we should write the file, and write it.
if (!RoslynString.IsNullOrWhiteSpace(directory))
{
Directory.CreateDirectory(directory);

// Write empty file.
path = Path.Combine(directory, fileName);
using (fileSystem.NewFileStream(path, FileMode.Create, FileAccess.Write))
{
}
}
}
catch (Exception exception) when (exception is IOException or SystemException)
// Write empty file.
path = Path.Combine(directory, fileName);
using (fileSystem.NewFileStream(path, FileMode.Create, FileAccess.Write))
{
await logger.LogErrorAsync($"Could not write sentinel file for telemetry to path,'{path ?? "<unknown>"}'.", exception);
}
}
}

serviceProvider.TryAddService(new TelemetryInformation(!isTelemetryOptedOut, TelemetryProperties.VersionValue));

ITelemetryCollector telemetryCollector = _telemetryFactory is null || isTelemetryOptedOut
? new NopTelemetryService(!isTelemetryOptedOut)
: _telemetryFactory(serviceProvider);

if (!isTelemetryOptedOut)
catch (Exception exception) when (exception is IOException or SystemException)
{
await logger.LogDebugAsync($"Telemetry collector provider: '{telemetryCollector.GetType()}'");
await logger.LogErrorAsync($"Could not write sentinel file for telemetry to path,'{path ?? "<unknown>"}'.", exception);
}

return telemetryCollector;
}

public Task<bool> IsEnabledAsync() => throw new NotImplementedException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ public async Task Telemetry_ByDefault_TelemetryIsEnabled(string tfm)
.+ Microsoft.Testing.Platform.Telemetry.TelemetryManager DEBUG TestApplicationOptions.EnableTelemetry: True
.+ Microsoft.Testing.Platform.Telemetry.TelemetryManager DEBUG TESTINGPLATFORM_TELEMETRY_OPTOUT environment variable: ''
.+ Microsoft.Testing.Platform.Telemetry.TelemetryManager DEBUG DOTNET_CLI_TELEMETRY_OPTOUT environment variable: ''
.+ Microsoft.Testing.Platform.Telemetry.TelemetryManager DEBUG TESTINGPLATFORM_NOBANNER environment variable: ''
.+ Microsoft.Testing.Platform.Telemetry.TelemetryManager DEBUG DOTNET_NOLOGO environment variable: ''
.+ Microsoft.Testing.Platform.Telemetry.TelemetryManager DEBUG Telemetry is 'ENABLED'
""";
await AssertDiagnosticReportAsync(testHostResult, diagPathPattern, diagContentsPattern);
Expand Down Expand Up @@ -64,8 +62,6 @@ public async Task Telemetry_WhenOptingOutTelemetry_WithEnvironmentVariable_Telem
.+ Microsoft.Testing.Platform.Telemetry.TelemetryManager DEBUG TestApplicationOptions.EnableTelemetry: True
.+ Microsoft.Testing.Platform.Telemetry.TelemetryManager DEBUG TESTINGPLATFORM_TELEMETRY_OPTOUT environment variable: '1'
.+ Microsoft.Testing.Platform.Telemetry.TelemetryManager DEBUG DOTNET_CLI_TELEMETRY_OPTOUT environment variable: ''
.+ Microsoft.Testing.Platform.Telemetry.TelemetryManager DEBUG TESTINGPLATFORM_NOBANNER environment variable: ''
.+ Microsoft.Testing.Platform.Telemetry.TelemetryManager DEBUG DOTNET_NOLOGO environment variable: ''
.+ Microsoft.Testing.Platform.Telemetry.TelemetryManager DEBUG Telemetry is 'DISABLED'
""";
await AssertDiagnosticReportAsync(testHostResult, diagPathPattern, diagContentsPattern);
Expand Down Expand Up @@ -93,8 +89,6 @@ public async Task Telemetry_WhenOptingOutTelemetry_With_DOTNET_CLI_EnvironmentVa
.+ Microsoft.Testing.Platform.Telemetry.TelemetryManager DEBUG TestApplicationOptions.EnableTelemetry: True
.+ Microsoft.Testing.Platform.Telemetry.TelemetryManager DEBUG TESTINGPLATFORM_TELEMETRY_OPTOUT environment variable: ''
.+ Microsoft.Testing.Platform.Telemetry.TelemetryManager DEBUG DOTNET_CLI_TELEMETRY_OPTOUT environment variable: '1'
.+ Microsoft.Testing.Platform.Telemetry.TelemetryManager DEBUG TESTINGPLATFORM_NOBANNER environment variable: ''
.+ Microsoft.Testing.Platform.Telemetry.TelemetryManager DEBUG DOTNET_NOLOGO environment variable: ''
.+ Microsoft.Testing.Platform.Telemetry.TelemetryManager DEBUG Telemetry is 'DISABLED'
""";
await AssertDiagnosticReportAsync(testHostResult, diagPathPattern, diagContentsPattern);
Expand All @@ -116,8 +110,6 @@ public async Task Telemetry_WhenEnableTelemetryIsFalse_WithTestApplicationOption
.+ Microsoft.Testing.Platform.Telemetry.TelemetryManager DEBUG TestApplicationOptions.EnableTelemetry: False
.+ Microsoft.Testing.Platform.Telemetry.TelemetryManager DEBUG TESTINGPLATFORM_TELEMETRY_OPTOUT environment variable: ''
.+ Microsoft.Testing.Platform.Telemetry.TelemetryManager DEBUG DOTNET_CLI_TELEMETRY_OPTOUT environment variable: ''
.+ Microsoft.Testing.Platform.Telemetry.TelemetryManager DEBUG TESTINGPLATFORM_NOBANNER environment variable: ''
.+ Microsoft.Testing.Platform.Telemetry.TelemetryManager DEBUG DOTNET_NOLOGO environment variable: ''
.+ Microsoft.Testing.Platform.Telemetry.TelemetryManager DEBUG Telemetry is 'DISABLED'
""";
await AssertDiagnosticReportAsync(testHostResult, diagPathPattern, diagContentsPattern);
Expand Down