Skip to content

Commit

Permalink
test: add TestWrite.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
netpyoung committed Nov 30, 2024
1 parent 75abee6 commit a468279
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,26 +175,12 @@ public override async Task<int> ExecuteAsync(CommandContext context, Settings se
AnsiConsole.Write($"{nameof(newsfileFpath)}: ");
AnsiConsole.Write(txtPath);
AnsiConsole.WriteLine();
ExtractBaseHeaderAndContent(newsfileFpath, config.Maker.StartString, out string baseHeader, out string baseContent);
if (!string.IsNullOrEmpty(topLine)
&& baseContent.Contains(topLine))
Exception? appendToNewsFileExOrNull = await AppendToNewsFile(config, topLine, content, newsfileFpath);
if (appendToNewsFileExOrNull != null)
{
AnsiConsole.MarkupLine("It seems you've already produced newsfiles for this version?");
AnsiConsole.WriteException(appendToNewsFileExOrNull);
return 1;
}

StringBuilder sb = new StringBuilder();
_ = sb.Append(baseHeader);
if (!string.IsNullOrEmpty(baseContent))
{
_ = sb.AppendLine(baseContent);
}
else
{
_ = sb.AppendLine(content.TrimEnd());
}
string newContent = sb.ToString();
await File.WriteAllTextAsync(newsfileFpath, newContent);
}

AnsiConsole.MarkupLine("[green]*[/] Staging newsfile...");
Expand Down Expand Up @@ -243,6 +229,31 @@ public override async Task<int> ExecuteAsync(CommandContext context, Settings se
return 0;
}

internal static async Task<Exception?> AppendToNewsFile(ReleaseNoteConfig config, string topLine, string content, string newsfileFpath)
{
ExtractBaseHeaderAndContent(newsfileFpath, config.Maker.StartString, out string baseHeader, out string baseContent);
if (!string.IsNullOrEmpty(topLine)
&& baseContent.Contains(topLine))
{
return new ReleaseNoteMakerException("It seems you've already produced newsfiles for this version?");
}

StringBuilder sb = new StringBuilder();
_ = sb.Append(baseHeader);
if (!string.IsNullOrEmpty(baseContent))
{
_ = sb.Append(content);
_ = sb.Append(baseContent);
}
else
{
_ = sb.AppendLine(content.TrimEnd());
}
string newContent = sb.ToString();
await File.WriteAllTextAsync(newsfileFpath, newContent);
return null;
}

private static void ExtractBaseHeaderAndContent(string path, string startString, out string baseHeader, out string baseContent)
{
if (!File.Exists(path))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using NF.Tool.ReleaseNoteMaker.CLI.Commands;
using NF.Tool.ReleaseNoteMaker.Common.Config;
using NF.Tool.ReleaseNoteMaker.Common.Fragments;
using NF.Tool.ReleaseNoteMaker.Common.Template;

namespace NF.Tool.ReleaseNoteMaker.Tests
{
[TestClass]
[DoNotParallelize]
public class TestWrite
{

[TestMethod]
[DeploymentItem("Template.tt")]
[DeploymentItem("ReleaseNote.config.toml")]
public async Task TestAppendAtTop()
{

List<FragmentContent> fragments = new List<FragmentContent>
{
new FragmentContent("", new FragmentBasename("142", "misc", 0), ""),
new FragmentContent("", new FragmentBasename("1", "misc", 0), ""),
new FragmentContent("", new FragmentBasename ("4", "feature", 0), "Stuff!"),
new FragmentContent("", new FragmentBasename("4", "feature", 1), "Second Stuff!"),
new FragmentContent("", new FragmentBasename("2", "feature", 0), "Foo added."),
new FragmentContent("", new FragmentBasename("72", "feature", 0), "Foo added."),
new FragmentContent("Names", FragmentBasename.Empty, string.Empty),
new FragmentContent("Web", new FragmentBasename("3", "bugfix", 0), "Web fixed."),
};
List<ReleaseNoteType> definitions = new List<ReleaseNoteType>
{
new ReleaseNoteType{ DisplayName= "Features", Category= "feature", IsShowContent=true},
new ReleaseNoteType{ DisplayName= "Bugfixes", Category= "bugfix", IsShowContent=true},
new ReleaseNoteType{ DisplayName= "Misc", Category= "misc", IsShowContent=true},
};

ReleaseNoteConfig config = new ReleaseNoteConfig();
config.Maker.IsAllBullets = true;
config.Maker.IsWrap = true;
config.Types.AddRange(definitions);

List<FragmentContent> splitted = FragmentFinder.SplitFragments(fragments, config);

string templatePath = "Template.tt";
VersionData versionData = new VersionData("MyProject", "1.0", "never");
(Exception? renderExOrNull, string text) = await TemplateRenderer.RenderFragments(templatePath, config, versionData, splitted);
Assert.IsNull(renderExOrNull);

await File.WriteAllTextAsync("ChangeLog.md", $"Old text.{Environment.NewLine}");

Exception? ex = await Command_Build.AppendToNewsFile(config, "release notes start", text, "ChangeLog.md");
Assert.IsNull(ex);

string expected = @"# MyProject 1.0 (never)
### Features
- Foo added. (#2, #72)
- Stuff! (#4)
- Second Stuff! (#4)
### Misc
- #1, #142
## Names
No significant changes.
## Web
### Bugfixes
- Web fixed. (#3)
Old text.
";

string actual = await File.ReadAllTextAsync("ChangeLog.md");
Assert.AreEqual(expected, actual);
}
}
}

0 comments on commit a468279

Please sign in to comment.