diff --git a/src/Platform/Microsoft.Testing.Platform/Telemetry/TelemetryManager.cs b/src/Platform/Microsoft.Testing.Platform/Telemetry/TelemetryManager.cs index 2cc9e319db..3ac6bb615a 100644 --- a/src/Platform/Microsoft.Testing.Platform/Telemetry/TelemetryManager.cs +++ b/src/Platform/Microsoft.Testing.Platform/Telemetry/TelemetryManager.cs @@ -47,8 +47,29 @@ public async Task 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 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); @@ -61,64 +82,53 @@ public async Task 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 ?? ""}'.", 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 ?? ""}'.", exception); } - - return telemetryCollector; } public Task IsEnabledAsync() => throw new NotImplementedException(); diff --git a/test/IntegrationTests/Microsoft.Testing.Platform.Acceptance.IntegrationTests/TelemetryTests.cs b/test/IntegrationTests/Microsoft.Testing.Platform.Acceptance.IntegrationTests/TelemetryTests.cs index 73432d0ba4..c533062424 100644 --- a/test/IntegrationTests/Microsoft.Testing.Platform.Acceptance.IntegrationTests/TelemetryTests.cs +++ b/test/IntegrationTests/Microsoft.Testing.Platform.Acceptance.IntegrationTests/TelemetryTests.cs @@ -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); @@ -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); @@ -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); @@ -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); diff --git a/test/UnitTests/Microsoft.Testing.Platform.UnitTests/Telemetry/TelemetryManagerTests.cs b/test/UnitTests/Microsoft.Testing.Platform.UnitTests/Telemetry/TelemetryManagerTests.cs index 90fc478266..d66cd424f7 100644 --- a/test/UnitTests/Microsoft.Testing.Platform.UnitTests/Telemetry/TelemetryManagerTests.cs +++ b/test/UnitTests/Microsoft.Testing.Platform.UnitTests/Telemetry/TelemetryManagerTests.cs @@ -55,6 +55,7 @@ public async Task TelemetryManager_UsingNoLogoShouldSuppressTelemetryMessage(str loggerFactoryMock.Setup(f => f.CreateLogger(It.IsAny())).Returns(new Mock().Object); TelemetryManager telemetryManager = new(); + telemetryManager.AddTelemetryCollectorProvider(_ => new NopTelemetryService(false)); // Act environmentMock.Setup(e => e.GetEnvironmentVariable(variable)).Returns(value); @@ -107,6 +108,7 @@ public async Task TelemetryManager_UsingTelemetryOptOutShouldDisableTelemetry(st loggerFactoryMock.Setup(f => f.CreateLogger(It.IsAny())).Returns(new Mock().Object); TelemetryManager telemetryManager = new(); + telemetryManager.AddTelemetryCollectorProvider(_ => new NopTelemetryService(false)); // Act environmentMock.Setup(e => e.GetEnvironmentVariable(variable)).Returns(value); @@ -156,6 +158,7 @@ public async Task TelemetryManager_SentinelIsWrittenPerUserAndAvoidsShowingNotic loggerFactoryMock.Setup(f => f.CreateLogger(It.IsAny())).Returns(new Mock().Object); TelemetryManager telemetryManager = new(); + telemetryManager.AddTelemetryCollectorProvider(_ => new NopTelemetryService(false)); // Act await telemetryManager.BuildAsync(serviceProvider, loggerFactoryMock.Object, options); @@ -217,6 +220,7 @@ public async Task TelemetryManager_SentinelIsWrittenOnlyWhenUserWouldSeeTheMessa loggerFactoryMock.Setup(f => f.CreateLogger(It.IsAny())).Returns(new Mock().Object); TelemetryManager telemetryManager = new(); + telemetryManager.AddTelemetryCollectorProvider(_ => new NopTelemetryService(false)); // Act // Disable showing the telemetry message.