Skip to content

Commit e4938be

Browse files
committed
tests: use unique file names to avoid race conditions
Signed-off-by: Vincent Biret <vibiret@microsoft.com>
1 parent 9b68313 commit e4938be

1 file changed

Lines changed: 18 additions & 12 deletions

File tree

test/Microsoft.OpenApi.Hidi.Tests/Services/OpenApiServiceTests.cs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public async Task ShowCommandGeneratesMermaidMarkdownFileWithMermaidDiagramAsync
135135
var options = new HidiOptions
136136
{
137137
OpenApi = Path.Combine("UtilityFiles", "SampleOpenApi.yml"),
138-
Output = new("sample.md")
138+
Output = new($"{nameof(ShowCommandGeneratesMermaidMarkdownFileWithMermaidDiagramAsync)}.md")
139139
};
140140

141141
await OpenApiService.ShowOpenApiDocumentAsync(options, _logger, TestContext.Current.CancellationToken);
@@ -217,7 +217,7 @@ public async Task TransformCommandConvertsOpenApiAsync()
217217
var options = new HidiOptions
218218
{
219219
OpenApi = Path.Combine("UtilityFiles", "SampleOpenApi.yml"),
220-
Output = new("sample.json"),
220+
Output = new($"{nameof(TransformCommandConvertsOpenApiAsync)}.json"),
221221
CleanOutput = true,
222222
TerseOutput = false,
223223
InlineLocal = false,
@@ -226,7 +226,7 @@ public async Task TransformCommandConvertsOpenApiAsync()
226226
// create a dummy ILogger instance for testing
227227
await OpenApiService.TransformOpenApiDocumentAsync(options, _logger, TestContext.Current.CancellationToken);
228228

229-
var output = await File.ReadAllTextAsync("sample.json", TestContext.Current.CancellationToken);
229+
var output = await File.ReadAllTextAsync(options.Output.FullName, TestContext.Current.CancellationToken);
230230
Assert.NotEmpty(output);
231231
}
232232

@@ -241,11 +241,12 @@ public async Task TransformCommandConvertsOpenApiWithDefaultOutputNameAsync()
241241
TerseOutput = false,
242242
InlineLocal = false,
243243
InlineExternal = false,
244+
Output = new FileInfo($"{nameof(TransformCommandConvertsOpenApiWithDefaultOutputNameAsync)}.yml")
244245
};
245246
// create a dummy ILogger instance for testing
246247
await OpenApiService.TransformOpenApiDocumentAsync(options, _logger, TestContext.Current.CancellationToken);
247248

248-
var output = await File.ReadAllTextAsync("output.yml", TestContext.Current.CancellationToken);
249+
var output = await File.ReadAllTextAsync(options.Output.FullName, TestContext.Current.CancellationToken);
249250
Assert.NotEmpty(output);
250251
}
251252

@@ -255,6 +256,7 @@ public async Task TransformCommandConvertsOpenApiWithDefaultOutputNameAndSwitchF
255256
var options = new HidiOptions
256257
{
257258
OpenApi = Path.Combine("UtilityFiles", "SampleOpenApi.yml"),
259+
Output = new($"{nameof(TransformCommandConvertsOpenApiWithDefaultOutputNameAndSwitchFormatAsync)}.yml"),
258260
CleanOutput = true,
259261
Version = "3.0",
260262
OpenApiFormat = OpenApiConstants.Yaml,
@@ -265,7 +267,7 @@ public async Task TransformCommandConvertsOpenApiWithDefaultOutputNameAndSwitchF
265267
// create a dummy ILogger instance for testing
266268
await OpenApiService.TransformOpenApiDocumentAsync(options, _logger, TestContext.Current.CancellationToken);
267269

268-
var output = await File.ReadAllTextAsync("output.yml", TestContext.Current.CancellationToken);
270+
var output = await File.ReadAllTextAsync(options.Output.FullName, TestContext.Current.CancellationToken);
269271
Assert.NotEmpty(output);
270272
}
271273

@@ -290,6 +292,7 @@ public async Task TransformToPowerShellCompliantOpenApiAsync()
290292
var options = new HidiOptions
291293
{
292294
OpenApi = Path.Combine("UtilityFiles", "SampleOpenApi.yml"),
295+
Output = new($"{nameof(TransformToPowerShellCompliantOpenApiAsync)}.yaml"),
293296
CleanOutput = true,
294297
Version = "3.0",
295298
OpenApiFormat = OpenApiConstants.Yaml,
@@ -301,7 +304,7 @@ public async Task TransformToPowerShellCompliantOpenApiAsync()
301304
// create a dummy ILogger instance for testing
302305
await OpenApiService.TransformOpenApiDocumentAsync(options, _logger, TestContext.Current.CancellationToken);
303306

304-
var output = await File.ReadAllTextAsync("output.yaml", TestContext.Current.CancellationToken);
307+
var output = await File.ReadAllTextAsync(options.Output.FullName, TestContext.Current.CancellationToken);
305308
Assert.NotEmpty(output);
306309
}
307310

@@ -310,13 +313,14 @@ public async Task InvokeTransformCommandAsync()
310313
{
311314
var rootCommand = Program.CreateRootCommand();
312315
var openapi = Path.Combine(".", "UtilityFiles", "SampleOpenApi.yml");
313-
var args = new[] { "transform", "-d", openapi, "-o", "sample.json", "--co" };
316+
var outputPath = $"{nameof(InvokeTransformCommandAsync)}.json";
317+
var args = new[] { "transform", "-d", openapi, "-o", outputPath, "--co" };
314318
var parseResult = rootCommand.Parse(args);
315319
var handler = Assert.IsType<AsynchronousCommandLineAction>(rootCommand.Subcommands.First(c => c.Name == "transform").Action, exactMatch: false);
316320

317321
await handler.InvokeAsync(parseResult, TestContext.Current.CancellationToken);
318322

319-
var output = await File.ReadAllTextAsync("sample.json", TestContext.Current.CancellationToken);
323+
var output = await File.ReadAllTextAsync(outputPath, TestContext.Current.CancellationToken);
320324
Assert.NotEmpty(output);
321325
}
322326

@@ -326,13 +330,14 @@ public async Task InvokeShowCommandAsync()
326330
{
327331
var rootCommand = Program.CreateRootCommand();
328332
var openApi = Path.Combine(".", "UtilityFiles", "SampleOpenApi.yml");
329-
var args = new[] { "show", "-d", openApi, "-o", "sample.md" };
333+
var outputPath = $"{nameof(InvokeShowCommandAsync)}.md";
334+
var args = new[] { "show", "-d", openApi, "-o", outputPath };
330335
var parseResult = rootCommand.Parse(args);
331336
var handler = Assert.IsType<AsynchronousCommandLineAction>(rootCommand.Subcommands.First(c => c.Name == "show").Action, exactMatch: false);
332337

333338
await handler.InvokeAsync(parseResult, TestContext.Current.CancellationToken);
334339

335-
var output = await File.ReadAllTextAsync("sample.md", TestContext.Current.CancellationToken);
340+
var output = await File.ReadAllTextAsync(outputPath, TestContext.Current.CancellationToken);
336341
Assert.Contains("graph LR", output, StringComparison.Ordinal);
337342
}
338343

@@ -341,13 +346,14 @@ public async Task InvokePluginCommandAsync()
341346
{
342347
var rootCommand = Program.CreateRootCommand();
343348
var manifest = Path.Combine(".", "UtilityFiles", "exampleapimanifest.json");
344-
var args = new[] { "plugin", "-m", manifest, "--of", AppDomain.CurrentDomain.BaseDirectory };
349+
var outputPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, nameof(InvokePluginCommandAsync));
350+
var args = new[] { "plugin", "-m", manifest, "--of", outputPath };
345351
var parseResult = rootCommand.Parse(args);
346352
var handler = Assert.IsType<AsynchronousCommandLineAction>(rootCommand.Subcommands.First(c => c.Name == "plugin").Action, exactMatch: false);
347353

348354
await handler.InvokeAsync(parseResult, TestContext.Current.CancellationToken);
349355

350-
using var jsDoc = JsonDocument.Parse(await File.ReadAllTextAsync("ai-plugin.json", TestContext.Current.CancellationToken));
356+
using var jsDoc = JsonDocument.Parse(await File.ReadAllTextAsync(Path.Combine(outputPath, "ai-plugin.json"), TestContext.Current.CancellationToken));
351357
var openAiManifest = OpenAIPluginManifest.Load(jsDoc.RootElement);
352358

353359
Assert.NotNull(openAiManifest);

0 commit comments

Comments
 (0)