Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: output redirected to StandardOut #18

Merged
merged 1 commit into from
Oct 20, 2024
Merged
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
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