Skip to content
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
17 changes: 1 addition & 16 deletions eng/vendored-files.json
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@
{
"id": "platform-ci-environment-detector",
"local_path": "src/Platform/Microsoft.Testing.Platform/Helpers/CIEnvironmentDetector.cs",
"notes": "Adapted from dotnet CLI telemetry CI detection. Maintained per https://learn.microsoft.com/dotnet/core/tools/telemetry#continuous-integration-detection.",
"notes": "Adapted from dotnet CLI telemetry CI detection. Maintained per https://learn.microsoft.com/dotnet/core/tools/telemetry#continuous-integration-detection. This file is the single source of truth: it is linked into MSTest.TestFramework via src/TestFramework/TestFramework/TestFramework.csproj and toggled via the IS_CORE_MTP define.",

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[NIT] Documentation Accuracy

The notes field says "toggled via the IS_CORE_MTP define" but the actual define that gates the TestFramework variant is TESTFRAMEWORK_CI_DETECTOR. IS_CORE_MTP is a separate, unrelated symbol used only inside Microsoft.Testing.Platform.csproj to guard PlatformResources.cs.

A future maintainer searching for #if IS_CORE_MTP to understand how this file is toggled will come up empty.

Recommendation: Change IS_CORE_MTPTESTFRAMEWORK_CI_DETECTOR in this notes string to match the actual define in TestFramework.csproj and the comment at the top of the linked source file.

"sources": [
{
"repo": "dotnet/sdk",
Expand Down Expand Up @@ -294,21 +294,6 @@
}
]
},
{
"id": "testframework-ci-environment-detector",
"local_path": "src/TestFramework/TestFramework/Internal/CIEnvironmentDetector.cs",
"notes": "Independent in-tree copy of the CI detector (test-framework variant exposing an IEnvironment-backed Instance) sharing the rules with the platform copy.",
"sources": [
{
"repo": "dotnet/sdk",
"ref": "main",
"path": "src/Cli/Microsoft.DotNet.Cli.Definitions/Telemetry/CIEnvironmentDetectorForTelemetry.cs",
"baseline_ref_sha": "1ab3ae8e0338cdd06b3974959c5802ccc5a85978",
"baseline_blob_sha": "e10e1fc2cd973c6123fd983b16a8f0c1e9191c31",
"scope": "CI detection rules and environment variable lists"
}
]
},
{
"id": "source-gen-equatable-array",
"local_path": "src/Analyzers/MSTest.SourceGeneration/Helpers/EquatableArray{T}.cs",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

// NOTE: This file is the single source of truth for CI environment detection. It is the canonical
// copy for Microsoft.Testing.Platform and is also linked into Microsoft.Testing.Extensions.Telemetry
// (via Microsoft.Testing.Extensions.Telemetry.csproj) and MSTest.TestFramework (via
// src/TestFramework/TestFramework/TestFramework.csproj). The TESTFRAMEWORK_CI_DETECTOR define
// is set only in the MSTest.TestFramework project; the #if blocks below toggle namespace,
// attributes, constructor accessibility, the static Instance helper, and the IsNullOrEmpty
// implementation so the file fits both the Platform/Telemetry layer and the TestFramework layer.
#if TESTFRAMEWORK_CI_DETECTOR
namespace Microsoft.VisualStudio.TestTools.UnitTesting;
#else
using Microsoft.CodeAnalysis;

namespace Microsoft.Testing.Platform.Helpers;
#endif

// Detection of CI: https://learn.microsoft.com/dotnet/core/tools/telemetry#continuous-integration-detection
// Based on: https://github.com/dotnet/sdk/blob/main/src/Cli/Microsoft.DotNet.Cli.Definitions/Telemetry/CIEnvironmentDetectorForTelemetry.cs
#if !TESTFRAMEWORK_CI_DETECTOR
[Embedded]
[ExcludeFromCodeCoverage]
#endif
internal sealed class CIEnvironmentDetector
{
// Systems that provide boolean values only, so we can simply parse and check for true
Expand Down Expand Up @@ -58,12 +71,23 @@ internal sealed class CIEnvironmentDetector

private readonly IEnvironment _environment;

#if TESTFRAMEWORK_CI_DETECTOR
/// <summary>
/// Gets the default instance that uses the real environment.
/// </summary>
public static CIEnvironmentDetector Instance { get; } = new(EnvironmentWrapper.Instance);
#endif

/// <summary>
/// Initializes a new instance of the <see cref="CIEnvironmentDetector"/> class.
/// </summary>
/// <param name="environment">The environment abstraction to use for reading environment variables.</param>
#if TESTFRAMEWORK_CI_DETECTOR
internal /* for testing purposes */ CIEnvironmentDetector(IEnvironment environment) => _environment = environment;
#else
public CIEnvironmentDetector(IEnvironment environment)
=> _environment = environment;
#endif

/// <summary>
/// Detects if the current environment is a CI environment.
Expand All @@ -84,7 +108,7 @@ public bool IsCIEnvironment()
bool allVariablesPresent = true;
foreach (string variable in variables)
{
if (global::Microsoft.Testing.Platform.RoslynString.IsNullOrEmpty(_environment.GetEnvironmentVariable(variable)))
if (IsNullOrEmpty(_environment.GetEnvironmentVariable(variable)))
{
allVariablesPresent = false;
break;
Expand All @@ -99,12 +123,20 @@ public bool IsCIEnvironment()

foreach (string variable in IfNonNullVariables)
{
if (!global::Microsoft.Testing.Platform.RoslynString.IsNullOrEmpty(_environment.GetEnvironmentVariable(variable)))
if (!IsNullOrEmpty(_environment.GetEnvironmentVariable(variable)))
{
return true;
}
}

return false;
}

#if TESTFRAMEWORK_CI_DETECTOR
private static bool IsNullOrEmpty(string? value)
=> string.IsNullOrEmpty(value);
#else
private static bool IsNullOrEmpty(string? value)
=> global::Microsoft.Testing.Platform.RoslynString.IsNullOrEmpty(value);
#endif
}
115 changes: 0 additions & 115 deletions src/TestFramework/TestFramework/Internal/CIEnvironmentDetector.cs

This file was deleted.

8 changes: 8 additions & 0 deletions src/TestFramework/TestFramework/TestFramework.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,16 @@

<ItemGroup>
<Compile Include="..\..\Platform\Microsoft.Testing.Platform\Helpers\RoslynHashCode.cs" Link="Internal\RoslynHashCode.cs" />
<!-- Single source of truth for CI environment detection lives in Microsoft.Testing.Platform. The
TESTFRAMEWORK_CI_DETECTOR define selects the TestFramework variant of the class (different
namespace, internal ctor, static Instance helper, string.IsNullOrEmpty). -->
<Compile Include="..\..\Platform\Microsoft.Testing.Platform\Helpers\CIEnvironmentDetector.cs" Link="Internal\CIEnvironmentDetector.cs" />
</ItemGroup>

<PropertyGroup>
<DefineConstants>$(DefineConstants);TESTFRAMEWORK_CI_DETECTOR</DefineConstants>
</PropertyGroup>

<ItemGroup>
<Compile Include="$(RepoRoot)src/Polyfills/**/*.cs" Link="Polyfills\%(RecursiveDir)%(Filename)%(Extension)" />
</ItemGroup>
Expand Down
Loading