Skip to content

Commit

Permalink
feat: output redirected to StandardOut (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
Seddryck authored Oct 20, 2024
1 parent cce95e5 commit 9b5b31d
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
2 changes: 1 addition & 1 deletion src/Didot.Cli/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
6 changes: 5 additions & 1 deletion src/Didot.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Error> errs)
Expand Down
4 changes: 2 additions & 2 deletions src/Didot.Cli/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
}
52 changes: 40 additions & 12 deletions testing/Didot.Cli.Testing/ProgramTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
Expand All @@ -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;
Expand Down

0 comments on commit 9b5b31d

Please sign in to comment.