From ff534e5a1f237676edb9eaa5bbd69dc5c9d0a526 Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Wed, 1 May 2024 12:20:12 -0400 Subject: [PATCH] - fixes empty log file issue --- CHANGELOG.md | 5 ++- src/kiota/Program.cs | 12 +++++- .../Logging/FileLoggerTests.cs | 40 +++++++++++++++++++ 3 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 tests/Kiota.Builder.Tests/Logging/FileLoggerTests.cs diff --git a/CHANGELOG.md b/CHANGELOG.md index e181101aeb..e1334709ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,10 +16,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - Fixed a bug where TypeScript deserialization would fail on Uppercase properties.[#4479](https://github.com/microsoft/kiota/issues/4479) +- Fixed a bug where clients and plugins generation would leave empty log files. [#4584](https://github.com/microsoft/kiota/issues/4584) - Changed URI template generation to reuse templates when required templates are absent across operations. -- Fixed path deduplication logic to avoid double `Id` suffixes in indexer names in scenarios where the `Id` suffix is already present.[#4519](https://github.com/microsoft/kiota/issues/4519) +- Fixed path deduplication logic to avoid double `Id` suffixes in indexer names in scenarios where the `Id` suffix is already present.[#4519](https://github.com/microsoft/kiota/issues/4519) - Updated reserved name providers for Java and Php so that "object" can be escaped. -- Fixes request builder disambiguation when child nodes had different prefixes for different paths with same parameters.[#4448](https://github.com/microsoft/kiota/issues/4448) +- Fixes request builder disambiguation when child nodes had different prefixes for different paths with same parameters.[#4448](https://github.com/microsoft/kiota/issues/4448) - Do not generate CS8603 warnings when enabling backing store in CSharp generation. - Fixed excluding operation. [#4399](https://github.com/microsoft/kiota/issues/4399) - Fixed plugin generation of `ApiManifest` type to not include the `x-ms-kiota-hash` in the generated plugin. [#4561](https://github.com/microsoft/kiota/issues/4561) diff --git a/src/kiota/Program.cs b/src/kiota/Program.cs index 1509b077a3..9ddc6255ba 100644 --- a/src/kiota/Program.cs +++ b/src/kiota/Program.cs @@ -7,8 +7,16 @@ static async Task Main(string[] args) { var rootCommand = KiotaHost.GetRootCommand(); var result = await rootCommand.InvokeAsync(args); - foreach (var subCommand in rootCommand.Subcommands.Select(static x => x.Handler).OfType()) - subCommand.Dispose(); + DisposeSubCommands(rootCommand); return result; } + private static void DisposeSubCommands(this Command command) + { + if (command.Handler is IDisposable disposableHandler) + disposableHandler.Dispose(); + foreach (var subCommand in command.Subcommands) + { + DisposeSubCommands(subCommand); + } + } } diff --git a/tests/Kiota.Builder.Tests/Logging/FileLoggerTests.cs b/tests/Kiota.Builder.Tests/Logging/FileLoggerTests.cs new file mode 100644 index 0000000000..7955674a9e --- /dev/null +++ b/tests/Kiota.Builder.Tests/Logging/FileLoggerTests.cs @@ -0,0 +1,40 @@ +using System; +using System.IO; +using Kiota.Builder.Logging; +using Microsoft.Extensions.Logging; +using Xunit; + +namespace Kiota.Builder.Tests.Logging; + +public sealed class FileLoggerTests : IDisposable +{ + private readonly string _logDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); + public FileLoggerTests() + { + Directory.CreateDirectory(_logDirectory); + } + [Fact] + public void CleansUpFileWhenNoLogs() + { + using (var logger = new FileLogLogger(_logDirectory, LogLevel.Warning, "test")) + { //using this format intentionally to ensure the dispose is called before the assert + logger.LogInformation("test"); + } + Assert.False(File.Exists(Path.Combine(_logDirectory, FileLogLogger.LogFileName))); + } + [Fact] + public void KeepsLogFileWhenLogs() + { + using (var logger = new FileLogLogger(_logDirectory, LogLevel.Warning, "test")) + { //using this format intentionally to ensure the dispose is called before the assert + logger.LogWarning("test"); + } + Assert.True(File.Exists(Path.Combine(_logDirectory, FileLogLogger.LogFileName))); + } + + public void Dispose() + { + if (Directory.Exists(_logDirectory)) + Directory.Delete(_logDirectory, true); + } +}