Skip to content

Commit

Permalink
asyncapi#196 GetStreamFor logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Senn Geerts authored and Senn Geerts committed Jul 13, 2024
1 parent 07b4273 commit e846dd9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
8 changes: 6 additions & 2 deletions src/AsyncAPI.Saunter.Generator.Cli/ToFile/StreamProvider.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
namespace AsyncAPI.Saunter.Generator.Cli.ToFile;
using Microsoft.Extensions.Logging;

namespace AsyncAPI.Saunter.Generator.Cli.ToFile;

internal interface IStreamProvider
{
Stream GetStreamFor(string path);
}

internal class StreamProvider : IStreamProvider
internal class StreamProvider(ILogger<StreamProvider> logger) : IStreamProvider
{
public Stream GetStreamFor(string path)
{
logger.LogDebug($"GetStreamFor(path: {path})");

if (!string.IsNullOrEmpty(path))
{
Directory.CreateDirectory(Path.GetDirectoryName(path));
Expand Down
14 changes: 7 additions & 7 deletions test/AsyncAPI.Saunter.Generator.Cli.Tests/IntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Retrieves AsyncAPI spec from a startup assembly and writes to file.
[InlineData("StreetlightsAPI.TopLevelStatement", "net8.0")]
public void Streetlights_ExportSpecTest(string csprojName, string targetFramework)
{
var path = Path.Combine(Directory.GetCurrentDirectory(), csprojName);
var path = Path.Combine(Directory.GetCurrentDirectory(), csprojName, "specs");
output.WriteLine($"Output path: {path}");
var stdOut = RunTool($"tofile ../../../../../examples/{csprojName}/bin/Debug/{targetFramework}/{csprojName}.dll --output {path} --format json,yml,yaml");

Expand All @@ -69,17 +69,17 @@ public void Streetlights_ExportSpecTest(string csprojName, string targetFramewor
stdOut.ShouldContain($"AsyncAPI yml successfully written to {Path.Combine(path, "asyncapi.yml")}");
stdOut.ShouldContain($"AsyncAPI json successfully written to {Path.Combine(path, "asyncapi.json")}");

File.Exists(Path.Combine(csprojName, "asyncapi.yml")).ShouldBeTrue("asyncapi.yml");
File.Exists(Path.Combine(csprojName, "asyncapi.yaml")).ShouldBeTrue("asyncapi.yaml");
File.Exists(Path.Combine(csprojName, "asyncapi.json")).ShouldBeTrue("asyncapi.json");
File.Exists(Path.Combine(path, "asyncapi.yml")).ShouldBeTrue("asyncapi.yml");
File.Exists(Path.Combine(path, "asyncapi.yaml")).ShouldBeTrue("asyncapi.yaml");
File.Exists(Path.Combine(path, "asyncapi.json")).ShouldBeTrue("asyncapi.json");

var yml = File.ReadAllText(Path.Combine(csprojName, "asyncapi.yml"));
var yml = File.ReadAllText(Path.Combine(path, "asyncapi.yml"));
yml.ShouldBe(ExpectedSpecFiles.Yml_v2_6, "yml");

var yaml = File.ReadAllText(Path.Combine(csprojName, "asyncapi.yaml"));
var yaml = File.ReadAllText(Path.Combine(path, "asyncapi.yaml"));
yaml.ShouldBe(yml, "yaml");

var json = File.ReadAllText(Path.Combine(csprojName, "asyncapi.json"));
var json = File.ReadAllText(Path.Combine(path, "asyncapi.json"));
json.ShouldBe(ExpectedSpecFiles.Json_v2_6, "json");
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
using AsyncAPI.Saunter.Generator.Cli.ToFile;
using Microsoft.Extensions.Logging;
using NSubstitute;
using NSubstitute.Community.Logging;
using Shouldly;

namespace AsyncAPI.Saunter.Generator.Cli.Tests.ToFile;

public class StreamProviderTests
{
private readonly IStreamProvider _streamProvider = new StreamProvider();
private readonly IStreamProvider _streamProvider;
private readonly ILogger<StreamProvider> _logger;

public StreamProviderTests()
{
this._logger = Substitute.For<ILogger<StreamProvider>>();
this._streamProvider = new StreamProvider(this._logger);
}

[Fact]
public void NullPathIsStdOut()
Expand All @@ -14,6 +24,7 @@ public void NullPathIsStdOut()

stream.ShouldNotBeNull();
Assert.False(stream is FileStream);
this._logger.Received(1).CallToLog(LogLevel.Debug);
}

[Fact]
Expand All @@ -33,5 +44,7 @@ public void StringPathIsFileStream()
{
File.Delete(path);
}

this._logger.Received(1).CallToLog(LogLevel.Debug);
}
}

0 comments on commit e846dd9

Please sign in to comment.