From 9b5b31d3190001fa2ad41f7ec2b430f181957fb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20L=2E=20Charlier?= Date: Sun, 20 Oct 2024 10:03:58 +0200 Subject: [PATCH] feat: output redirected to StandardOut (#18) --- README.md | 2 +- src/Didot.Cli/Options.cs | 2 +- src/Didot.Cli/Program.cs | 6 ++- src/Didot.Cli/Properties/launchSettings.json | 4 +- testing/Didot.Cli.Testing/ProgramTests.cs | 52 +++++++++++++++----- 5 files changed, 49 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index da72f60..52efaac 100644 --- a/README.md +++ b/README.md @@ -150,7 +150,7 @@ The command to run Didot is simply `didot`. When executing it, you need to provi - `-t, --Template`: Specifies the path to the Scriban template file. - `-s, --Source`: Specifies the path to the source data file, which can be in YAML or JSON format. -- `-o, --Output`: Specifies the path to the output file where the generated content will be saved. +- `-o, --Output`: Specifies the path to the output file where the generated content will be saved. If not specified the output is redirected to the console. #### Example: diff --git a/src/Didot.Cli/Options.cs b/src/Didot.Cli/Options.cs index c12926f..64bb7a7 100644 --- a/src/Didot.Cli/Options.cs +++ b/src/Didot.Cli/Options.cs @@ -14,6 +14,6 @@ public class Options [Option('s', "Source", Required = true, HelpText = "Path to the source file.")] public required string Source { get; set; } - [Option('o', "Output", Required = true, HelpText = "Path to the generated file.")] + [Option('o', "Output", Required = false, HelpText = "Path to the generated file.")] public required string Output { get; set; } } diff --git a/src/Didot.Cli/Program.cs b/src/Didot.Cli/Program.cs index 29b0b5c..603f989 100644 --- a/src/Didot.Cli/Program.cs +++ b/src/Didot.Cli/Program.cs @@ -28,7 +28,11 @@ static void RunWithOptions(Options opts) using var source = File.OpenRead(opts.Source); using var template = File.OpenRead(opts.Template); var output = printer.Render(template, source); - File.WriteAllText(opts.Output, output); + + if (string.IsNullOrEmpty(opts.Output)) + Console.Out.WriteLine(output); + else + File.WriteAllText(opts.Output, output); } static void HandleParseError(IEnumerable errs) diff --git a/src/Didot.Cli/Properties/launchSettings.json b/src/Didot.Cli/Properties/launchSettings.json index 7351433..03e9189 100644 --- a/src/Didot.Cli/Properties/launchSettings.json +++ b/src/Didot.Cli/Properties/launchSettings.json @@ -2,7 +2,7 @@ "profiles": { "Didot.Console": { "commandName": "Project", - "commandLineArgs": "--Template template.scriban --Source source.yaml --Output page.html" + "commandLineArgs": "--Template template.scriban --Source source.yaml" } } -} \ No newline at end of file +} diff --git a/testing/Didot.Cli.Testing/ProgramTests.cs b/testing/Didot.Cli.Testing/ProgramTests.cs index 8b67525..9fd8345 100644 --- a/testing/Didot.Cli.Testing/ProgramTests.cs +++ b/testing/Didot.Cli.Testing/ProgramTests.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Data; using System.IO; using System.Linq; using System.Text; @@ -10,31 +11,58 @@ namespace Didot.Cli.Testing; public class ProgramTests { - private static string[] Templates = { "scriban", "liquid", "hbs", "smart" }; - public static string[] DataSets = { "yaml", "json", "xml" }; + private static readonly string[] Templates = { "scriban", "liquid", "hbs", "smart" }; + private static readonly string[] DataSets = { "yaml", "json", "xml" }; + + private TextWriter OriginalOutput { get; set; } + private Stream MemoryStream { get; set; } + private StreamWriter Writer { get; set; } + + [SetUp] + public void SetUp() + { + OriginalOutput = Console.Out; + MemoryStream = new MemoryStream(); + Writer = new(MemoryStream) + { + AutoFlush = true + }; + Console.SetOut(Writer); + } + + [TearDown] + public void TearDown() + { + Writer.Dispose(); + MemoryStream.Dispose(); + Console.SetOut(OriginalOutput); + } [Test, Combinatorial] - public void Main_WithNoArguments_ShouldPrintHelp( - [ValueSource(nameof(Templates))] string engine, - [ValueSource(nameof(DataSets))] string data) + public void CaptureConsoleOutputTest( + [ValueSource(nameof(Templates))] string engine, + [ValueSource(nameof(DataSets))] string data) { var args = new string[] { - $"-ttemplate/template-01.{engine}", - $"-sdata/data-01.{data}", - $"-ooutput-01-{engine}-{data}.txt" + $"-ttemplate/template-01.{engine}", + $"-sdata/data-01.{data}" }; Program.Main(args); - var output = File.ReadAllText($"output-01-{engine}-{data}.txt").Uniform(); - var expected = File.ReadAllText($"Expectation/expectation-01.txt").Uniform(); - Assert.That(output, Is.EqualTo(expected)); + MemoryStream.Position = 0; + using (var reader = new StreamReader(MemoryStream)) + { + var consoleOutput = reader.ReadToEnd().Standardize(); + var expected = File.ReadAllText($"Expectation/expectation-01.txt").Standardize(); + Assert.That(consoleOutput, Is.EqualTo(expected)); + } } } public static class StringExtensions { - public static string Uniform(this string value) + public static string Standardize(this string value) { var result = value.Trim().Replace("\r\n", "\n").Trim(); return result;