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
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using DevProxy.Abstractions.Proxy.Http;
using DevProxy.Abstractions.Utils;
using DevProxy.Plugins.Generation;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Xunit;

Expand Down Expand Up @@ -217,9 +218,10 @@ public async Task OpenApiSpecGenerator_WritesSpec()
[Fact]
public async Task TypeSpecGenerator_WritesSpec()
{
var logger = new MessageCapturingLogger<TypeSpecGeneratorPlugin>();
var plugin = new TypeSpecGeneratorPlugin(
TestDefaults.HttpClient,
NullLogger<TypeSpecGeneratorPlugin>.Instance,
logger,
Watch,
new DisabledLanguageModelClient(),
new TestProxyConfiguration(),
Expand All @@ -233,10 +235,30 @@ public async Task TypeSpecGenerator_WritesSpec()
((Dictionary<string, object>)args.GlobalData[ProxyUtils.ReportsKey])[plugin.Name]);
Assert.NotEmpty(report);
Assert.NotEmpty(dir.GetFiles("*.tsp"));
Assert.Contains(logger.Messages, m => m.StartsWith("Writing TypeSpec file to ", StringComparison.Ordinal));
Assert.Contains(logger.Messages, m => m.StartsWith("Created TypeSpec file ", StringComparison.Ordinal));
Assert.DoesNotContain(logger.Messages, m => m.Contains("OpenAPI", StringComparison.Ordinal));
}
finally
{
dir.Delete(recursive: true);
}
}

private sealed class MessageCapturingLogger<T> : ILogger<T>
{
public List<string> Messages { get; } = [];

public IDisposable? BeginScope<TState>(TState state) where TState : notnull => null;

public bool IsEnabled(LogLevel logLevel) => true;

public void Log<TState>(
LogLevel logLevel,
EventId eventId,
TState state,
Exception? exception,
Func<TState, Exception?, string> formatter) =>
Messages.Add(formatter(state, exception));
}
}
4 changes: 2 additions & 2 deletions DevProxy.Plugins/Generation/TypeSpecGeneratorPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,12 @@ request.Method is null ||
cancellationToken.ThrowIfCancellationRequested();

var fileName = $"{typeSpecFile.Name}-{DateTime.Now:yyyyMMddHHmmss}.tsp";
Logger.LogDebug("Writing OpenAPI spec to {FileName}...", fileName);
Logger.LogDebug("Writing TypeSpec file to {FileName}...", fileName);
await File.WriteAllTextAsync(fileName, typeSpecFile.ToString(), cancellationToken);

generatedTypeSpecFiles.Add(typeSpecFile.Service.Servers.First().Url, fileName);

Logger.LogInformation("Created OpenAPI spec file {FileName}", fileName);
Logger.LogInformation("Created TypeSpec file {FileName}", fileName);
}

StoreReport(new TypeSpecGeneratorPluginReport(
Expand Down
Loading