Skip to content

Commit f26baca

Browse files
authored
Handle temp race condition (#1650)
1 parent e99fcfc commit f26baca

File tree

8 files changed

+69
-20
lines changed

8 files changed

+69
-20
lines changed

src/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<Project>
33
<PropertyGroup>
44
<NoWarn>CA1822;CS1591;CS0649;xUnit1026;xUnit1013;CS1573;VerifyTestsProjectDir;VerifySetParameters;PolyFillTargetsForNuget;xUnit1051;NU1608;NU1109</NoWarn>
5-
<Version>31.7.2</Version>
5+
<Version>31.7.4</Version>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<LangVersion>preview</LangVersion>
88
<AssemblyVersion>1.0.0</AssemblyVersion>

src/RawTempUsage/GlobalUsings.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Global using directives
2+
3+
global using VerifyTests;
4+
global using Xunit;

src/RawTempUsage/RawTempUsage.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public class RawTempUsage
2+
{
3+
[Fact]
4+
public void Directory()
5+
{
6+
using var directory = new TempDirectory();
7+
}
8+
9+
[Fact]
10+
public void File()
11+
{
12+
using var file = new TempFile();
13+
}
14+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net10.0</TargetFramework>
5+
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
6+
<PlatformTarget>x64</PlatformTarget>
7+
<OutputType>Exe</OutputType>
8+
<NoWarn>$(NoWarn);CS8002</NoWarn>
9+
<RootNamespace>Fake</RootNamespace>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<ProjectReference Include="..\Verify\Verify.csproj" />
14+
<PackageReference Include="Microsoft.NET.Test.Sdk" />
15+
<PackageReference Include="ProjectDefaults" PrivateAssets="all" />
16+
<PackageReference Include="MarkdownSnippets.MsBuild" PrivateAssets="all" />
17+
<PackageReference Include="xunit.runner.visualstudio" PrivateAssets="all" />
18+
<PackageReference Include="xunit.v3" />
19+
</ItemGroup>
20+
21+
</Project>

src/Verify.slnx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616
<Project Path="Benchmarks/Benchmarks.csproj" />
1717
<Project Path="DeterministicTests/DeterministicTests.csproj" />
1818
<Project Path="DisableScrubbersTests/DisableScrubbersTests.csproj" />
19-
<Project Path="DisableVerifyFileNestingTests/DisableVerifyFileNestingTests.csproj" Type="C#" />
19+
<Project Path="DisableVerifyFileNestingTests/DisableVerifyFileNestingTests.csproj" />
2020
<Project Path="FakeDiffTool/FakeDiffTool.csproj" />
2121
<Project Path="FSharpTests/FSharpTests.fsproj" />
2222
<Project Path="ModuleInitDocs/ModuleInitDocs.csproj" />
2323
<Project Path="NullComparer/NullComparer.csproj" />
24+
<Project Path="RawTempUsage/RawTempUsage.csproj" />
2425
<Project Path="StaticSettingsTests/StaticSettingsTests.csproj" />
2526
<Project Path="StrictJsonTests/StrictJsonTests.csproj" />
2627
<Project Path="TargetLibrary/TargetLibrary.csproj" />

src/Verify/TempDirectory.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ namespace VerifyTests;
2828
public class TempDirectory :
2929
IDisposable
3030
{
31+
List<string> paths;
32+
3133
/// <summary>
3234
/// The full path to the directory.
3335
/// </summary>
@@ -58,16 +60,17 @@ public static void Init()
5860
before: () =>
5961
{
6062
},
61-
after: () => paths.Value = null);
63+
after: () => asyncPaths.Value = null);
6264

6365
VerifierSettings.GlobalScrubbers.Add((scrubber, _, _) =>
6466
{
65-
if (paths.Value == null)
67+
var pathsValue = asyncPaths.Value;
68+
if (pathsValue == null)
6669
{
6770
return;
6871
}
6972

70-
foreach (var path in paths.Value)
73+
foreach (var path in pathsValue)
7174
{
7275
scrubber.Replace(path, "{TempDirectory}");
7376
}
@@ -76,7 +79,7 @@ public static void Init()
7679
Cleanup();
7780
}
7881

79-
static AsyncLocal<List<string>?> paths = new();
82+
static AsyncLocal<List<string>?> asyncPaths = new();
8083

8184
internal static void Cleanup()
8285
{
@@ -118,13 +121,14 @@ public TempDirectory()
118121
Path = IoPath.Combine(RootDirectory, IoPath.GetRandomFileName());
119122
Directory.CreateDirectory(Path);
120123

121-
if (paths.Value == null)
124+
paths = asyncPaths.Value!;
125+
if (paths == null)
122126
{
123-
paths.Value = [Path];
127+
paths = asyncPaths.Value = [Path];
124128
}
125129
else
126130
{
127-
paths.Value!.Add(Path);
131+
paths.Add(Path);
128132
}
129133
}
130134

@@ -135,7 +139,7 @@ public void Dispose()
135139
Directory.Delete(Path, true);
136140
}
137141

138-
paths.Value!.Remove(Path);
142+
paths.Remove(Path);
139143
}
140144

141145
/// <summary>

src/Verify/TempFile.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ namespace VerifyTests;
3232
public class TempFile :
3333
IDisposable
3434
{
35+
List<string> paths;
36+
3537
/// <summary>
3638
/// The full path to the file.
3739
/// </summary>
@@ -61,16 +63,17 @@ public static void Init()
6163
before: () =>
6264
{
6365
},
64-
after: () => paths.Value = null);
66+
after: () => asyncPaths.Value = null);
6567

6668
VerifierSettings.GlobalScrubbers.Add((scrubber, _, _) =>
6769
{
68-
if (paths.Value == null)
70+
var pathsValue = asyncPaths.Value;
71+
if (pathsValue == null)
6972
{
7073
return;
7174
}
7275

73-
foreach (var path in paths.Value)
76+
foreach (var path in pathsValue)
7477
{
7578
scrubber.Replace(path, "{TempFile}");
7679
}
@@ -79,7 +82,7 @@ public static void Init()
7982
Cleanup();
8083
}
8184

82-
static AsyncLocal<List<string>?> paths = new();
85+
static AsyncLocal<List<string>?> asyncPaths = new();
8386

8487
internal static void Cleanup()
8588
{
@@ -132,13 +135,14 @@ public TempFile(string? extension = null)
132135

133136
Path = IoPath.Combine(RootDirectory, fileName);
134137

135-
if (paths.Value == null)
138+
paths = asyncPaths.Value!;
139+
if (paths == null)
136140
{
137-
paths.Value = [Path];
141+
paths = asyncPaths.Value = [Path];
138142
}
139143
else
140144
{
141-
paths.Value!.Add(Path);
145+
paths.Add(Path);
142146
}
143147
}
144148

@@ -162,7 +166,7 @@ public void Dispose()
162166
File.Delete(Path);
163167
}
164168

165-
paths.Value!.Remove(Path);
169+
paths.Remove(Path);
166170
}
167171

168172
/// <summary>

src/VerifyCore.slnf

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
{
1+
{
22
"solution": {
33
"path": "Verify.slnx",
44
"projects": [
5+
"DisableScrubbersTests\\DisableScrubbersTests.csproj",
6+
"RawTempUsage\\RawTempUsage.csproj",
57
"TargetLibrary\\TargetLibrary.csproj",
68
"Verify.NUnit\\Verify.NUnit.csproj",
79
"Verify.SamplePlugin\\Verify.SamplePlugin.csproj",
810
"Verify.Tests\\Verify.Tests.csproj",
911
"Verify.XunitV3\\Verify.XunitV3.csproj",
10-
"DisableScrubbersTests\\DisableScrubbersTests.csproj",
1112
"Verify\\Verify.csproj"
1213
]
1314
}

0 commit comments

Comments
 (0)