Skip to content

Commit 9649b90

Browse files
committed
Workaround sporadic error "IOException: The process cannot access the file" when running locally through 'dotnet test'
1 parent b7745ae commit 9649b90

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

test/OpenApiTests/OpenApiTestContext.cs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,25 @@ private static JsonElement ParseSwaggerDocument(string content)
8686

8787
private static async Task WriteToDiskAsync(string path, JsonElement jsonElement)
8888
{
89-
string directory = Path.GetDirectoryName(path)!;
90-
Directory.CreateDirectory(directory);
91-
92-
string contents = jsonElement.ToString();
93-
await File.WriteAllTextAsync(path, contents);
89+
while (true)
90+
{
91+
try
92+
{
93+
string directory = Path.GetDirectoryName(path)!;
94+
Directory.CreateDirectory(directory);
95+
96+
string contents = jsonElement.ToString();
97+
await File.WriteAllTextAsync(path, contents);
98+
99+
return;
100+
}
101+
catch (IOException)
102+
{
103+
// This sometimes happens when running tests locally.
104+
// Multi-targeted projects should not use the same output path.
105+
106+
await Task.Delay(TimeSpan.FromMilliseconds(50));
107+
}
108+
}
94109
}
95110
}

0 commit comments

Comments
 (0)