Skip to content
Merged
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
45 changes: 45 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ DiffEngine manages launching and cleanup of diff tools. It is designed to be use
* [Closing a tool](#closing-a-tool)
* [File type detection](#file-type-detection)
* [BuildServerDetector](#buildserverdetector)
* [Override in tests](#override-in-tests)
* [AiCliDetector](#aiclidetector)
* [Disable for a machine/process](#disable-for-a-machineprocess)
* [Disable in code](#disable-in-code)
Expand Down Expand Up @@ -162,6 +163,50 @@ var isAppVeyor = BuildServerDetector.IsAppVeyor;
<!-- endSnippet -->


### Override in tests

`BuildServerDetector.Detected` can be set at test time. The value is stored in an `AsyncLocal`, so it is scoped to the current async context and does not leak to other threads or tests running in parallel.

<!-- snippet: BuildServerDetectorDetectedOverride -->
<a id='snippet-BuildServerDetectorDetectedOverride'></a>
```cs
[Fact]
public async Task SetDetectedPersistsInAsyncContext()
{
var original = BuildServerDetector.Detected;
try
{
BuildServerDetector.Detected = true;
Assert.True(BuildServerDetector.Detected);

await Task.Delay(1);

Assert.True(BuildServerDetector.Detected);
}
finally
{
BuildServerDetector.Detected = original;
}
}

[Fact]
public async Task SetDetectedDoesNotLeakToOtherContexts()
{
var parentValue = BuildServerDetector.Detected;

await Task.Run(() =>
{
BuildServerDetector.Detected = true;
Assert.True(BuildServerDetector.Detected);
});

Assert.Equal(parentValue, BuildServerDetector.Detected);
}
```
<sup><a href='/src/DiffEngine.Tests/BuildServerDetectorTest.cs#L28-L63' title='Snippet source file'>snippet source</a> | <a href='#snippet-BuildServerDetectorDetectedOverride' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


## AiCliDetector

`AiCliDetector.Detected` returns true if the current code is running in an AI-powered CLI environment.
Expand Down
7 changes: 7 additions & 0 deletions readme.source.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ There are also individual properties to check for each specific build system
snippet: BuildServerDetectorProps


### Override in tests

`BuildServerDetector.Detected` can be set at test time. The value is stored in an `AsyncLocal`, so it is scoped to the current async context and does not leak to other threads or tests running in parallel.

snippet: BuildServerDetectorDetectedOverride


## AiCliDetector

`AiCliDetector.Detected` returns true if the current code is running in an AI-powered CLI environment.
Expand Down
37 changes: 37 additions & 0 deletions src/DiffEngine.Tests/BuildServerDetectorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,41 @@ public void Props()

// ReSharper restore UnusedVariable
}

#region BuildServerDetectorDetectedOverride

[Fact]
public async Task SetDetectedPersistsInAsyncContext()
{
var original = BuildServerDetector.Detected;
try
{
BuildServerDetector.Detected = true;
Assert.True(BuildServerDetector.Detected);

await Task.Delay(1);

Assert.True(BuildServerDetector.Detected);
}
finally
{
BuildServerDetector.Detected = original;
}
}

[Fact]
public async Task SetDetectedDoesNotLeakToOtherContexts()
{
var parentValue = BuildServerDetector.Detected;

await Task.Run(() =>
{
BuildServerDetector.Detected = true;
Assert.True(BuildServerDetector.Detected);
});

Assert.Equal(parentValue, BuildServerDetector.Detected);
}

#endregion
}
11 changes: 9 additions & 2 deletions src/DiffEngine/BuildServerDetector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ static BuildServerDetector()
// https://docs.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch#access-variables-through-the-environment
IsAzureDevops = ValueEquals(variables, "TF_BUILD", "True");

Detected = IsTravis ||
detected = IsTravis ||
IsJenkins ||
IsGithubAction ||
IsAzureDevops ||
Expand All @@ -65,6 +65,9 @@ static BuildServerDetector()
IsAppVeyor;
}

static bool detected;
static AsyncLocal<bool?> overrideDetected = new();

static bool ValueEquals(IDictionary variables, string key, string value)
{
var variable = variables[key];
Expand Down Expand Up @@ -98,5 +101,9 @@ static bool ValueEquals(IDictionary variables, string key, string value)

public static bool IsJenkins { get; }

public static bool Detected { get; set; }
public static bool Detected
{
get => overrideDetected.Value ?? detected;
set => overrideDetected.Value = value;
}
}
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project>
<PropertyGroup>
<NoWarn>CS1591;CS0649;NU1608;NU1109</NoWarn>
<Version>18.3.0</Version>
<Version>18.4.0</Version>
<AssemblyVersion>1.0.0</AssemblyVersion>
<PackageTags>Testing, Snapshot, Diff, Compare</PackageTags>
<Description>Launches diff tools based on file extensions. Designed to be consumed by snapshot testing libraries.</Description>
Expand Down