From 13f5e288aaed14c38d545ee9af2a7484e105d082 Mon Sep 17 00:00:00 2001 From: netpyoung Date: Mon, 2 Dec 2024 03:22:05 +0900 Subject: [PATCH] test: TestSingleFileFalseOverwriteDuplicateVersion --- .../Commands/Command_Build.cs | 32 +++++-- .../Config/ReleaseNoteConfig.cs | 1 + .../TestWrite.cs | 90 +++++++++++++++++-- 3 files changed, 109 insertions(+), 14 deletions(-) diff --git a/NF.Tool.ReleaseNoteMaker/NF.Tool.ReleaseNoteMaker.CLI/Commands/Command_Build.cs b/NF.Tool.ReleaseNoteMaker/NF.Tool.ReleaseNoteMaker.CLI/Commands/Command_Build.cs index 4077004..39953d4 100644 --- a/NF.Tool.ReleaseNoteMaker/NF.Tool.ReleaseNoteMaker.CLI/Commands/Command_Build.cs +++ b/NF.Tool.ReleaseNoteMaker/NF.Tool.ReleaseNoteMaker.CLI/Commands/Command_Build.cs @@ -164,18 +164,28 @@ public override async Task ExecuteAsync(CommandContext context, Settings se return 0; } - string newsfileFpath = Path.Combine(baseDirectory, config.Maker.OutputFileName); + string newsFileName; + if (config.Maker.IsSingleFile) + { + newsFileName = config.Maker.OutputFileName; + } + else + { + newsFileName = string.Format(config.Maker.OutputFileName, versionData.Name, versionData.Version, versionData.Date); + } + + string newsFileFpath = Path.Combine(baseDirectory, newsFileName); AnsiConsole.MarkupLine($"[green]*[/] Writing to newsfile..."); { - TextPath txtPath = new TextPath(newsfileFpath) + TextPath txtPath = new TextPath(newsFileFpath) .RootColor(Color.Red) .SeparatorColor(Color.Green) .StemColor(Color.Blue) .LeafColor(Color.Yellow); - AnsiConsole.Write($"{nameof(newsfileFpath)}: "); + AnsiConsole.Write($"{nameof(newsFileFpath)}: "); AnsiConsole.Write(txtPath); AnsiConsole.WriteLine(); - Exception? appendToNewsFileExOrNull = await AppendToNewsFile(config, topLine, content, newsfileFpath); + Exception? appendToNewsFileExOrNull = await AppendToNewsFile(config, topLine, content, newsFileFpath); if (appendToNewsFileExOrNull != null) { AnsiConsole.WriteException(appendToNewsFileExOrNull); @@ -184,7 +194,7 @@ public override async Task ExecuteAsync(CommandContext context, Settings se } AnsiConsole.MarkupLine("[green]*[/] Staging newsfile..."); - GitHelper.StageNewsfile(newsfileFpath); + GitHelper.StageNewsfile(newsFileFpath); string[] fragmentFpaths = fragmentResult.FragmentFiles.Select(x => x.FileName).ToArray(); if (fragmentFpaths.Length == 0) @@ -231,7 +241,7 @@ public override async Task ExecuteAsync(CommandContext context, Settings se internal static async Task AppendToNewsFile(ReleaseNoteConfig config, string topLine, string content, string newsfileFpath) { - ExtractBaseHeaderAndContent(newsfileFpath, config.Maker.StartString, out string baseHeader, out string baseContent); + ExtractBaseHeaderAndContent(newsfileFpath, config, out string baseHeader, out string baseContent); if (!string.IsNullOrEmpty(topLine) && baseContent.Contains(topLine)) { @@ -272,8 +282,15 @@ private static string NormalizeEndOfLine(string content, ReleaseNoteConfigMaker. } } - private static void ExtractBaseHeaderAndContent(string path, string startString, out string baseHeader, out string baseContent) + private static void ExtractBaseHeaderAndContent(string path, ReleaseNoteConfig config, out string baseHeader, out string baseContent) { + if (!config.Maker.IsSingleFile) + { + baseHeader = string.Empty; + baseContent = string.Empty; + return; + } + if (!File.Exists(path)) { baseHeader = string.Empty; @@ -281,6 +298,7 @@ private static void ExtractBaseHeaderAndContent(string path, string startString, return; } + string startString = config.Maker.StartString; string txt = NormalizeEndOfLine(File.ReadAllText(path), ReleaseNoteConfigMaker.E_END_OF_LINE.LF); int index = txt.IndexOf(startString); if (index == -1) diff --git a/NF.Tool.ReleaseNoteMaker/NF.Tool.ReleaseNoteMaker.Common/Config/ReleaseNoteConfig.cs b/NF.Tool.ReleaseNoteMaker/NF.Tool.ReleaseNoteMaker.Common/Config/ReleaseNoteConfig.cs index 81c5290..12e756a 100644 --- a/NF.Tool.ReleaseNoteMaker/NF.Tool.ReleaseNoteMaker.Common/Config/ReleaseNoteConfig.cs +++ b/NF.Tool.ReleaseNoteMaker/NF.Tool.ReleaseNoteMaker.Common/Config/ReleaseNoteConfig.cs @@ -32,6 +32,7 @@ public sealed class ReleaseNoteConfigMaker public string StartString { get; set; } = "\n"; public bool IsWrap { get; set; } = false; public bool IsAllBullets { get; set; } = false; + public bool IsSingleFile { get; set; } = true; public E_END_OF_LINE EndOfLine { get; set; } = E_END_OF_LINE.LF; // config.package_dir diff --git a/NF.Tool.ReleaseNoteMaker/NF.Tool.ReleaseNoteMaker.Tests/TestWrite.cs b/NF.Tool.ReleaseNoteMaker/NF.Tool.ReleaseNoteMaker.Tests/TestWrite.cs index 923bd2e..e4c6b73 100644 --- a/NF.Tool.ReleaseNoteMaker/NF.Tool.ReleaseNoteMaker.Tests/TestWrite.cs +++ b/NF.Tool.ReleaseNoteMaker/NF.Tool.ReleaseNoteMaker.Tests/TestWrite.cs @@ -12,10 +12,23 @@ namespace NF.Tool.ReleaseNoteMaker.Tests [DoNotParallelize] public class TestWrite { - [TestInitialize()] - public void Init() + public required TestContext TestContext { get; set; } + + [TestInitialize] + public void TestInitialize() + { + string testName = TestContext.TestName!; + string testDirectory = Path.Combine(TestContext.DeploymentDirectory!, testName); + Directory.CreateDirectory(testDirectory); + File.Copy("Template.tt", $"{testDirectory}/Template.tt"); + File.Copy("ReleaseNote.config.toml", $"{testDirectory}/ReleaseNote.config.toml"); + Directory.SetCurrentDirectory(testDirectory); + } + + [TestCleanup] + public void TestCleanup() { - File.Delete("ChangeLog.md"); + Directory.SetCurrentDirectory(TestContext.DeploymentDirectory!); } [TestMethod] @@ -230,17 +243,21 @@ public async Task TestMultipleFileNoStartString() [TestMethod] [DeploymentItem("Template.tt")] [DeploymentItem("ReleaseNote.config.toml")] - [DeploymentItem("SampleData/Case001/123.feature", "ChangeLog.d/")] public async Task TestWithTitleFormatDuplicateVersionRaise() { - Assert.IsTrue(File.Exists("ChangeLog.d/123.feature")); - + Directory.CreateDirectory("ChangeLog.d"); + File.WriteAllText("ChangeLog.d/123.feature", "Adds levitation"); File.WriteAllText("ReleaseNote.config.toml", """ [ReleaseNote.Maker] Directory = "ChangeLog.d" OutputFileName = "{0}-notes.md" TemplateFilePath = "Template.tt" TitleFormat = "{0} {1} ({2})" + +[[ReleaseNote.Type]] +Category = "feature" +DisplayName = "Features" +IsShowContent = true """); string[] args = [ @@ -254,17 +271,76 @@ public async Task TestWithTitleFormatDuplicateVersionRaise() int result = await Program.Main(args); Assert.AreEqual(0, result); Console.Write(File.ReadAllText("{0}-notes.md")); - Assert.IsTrue(File.Exists("ChangeLog.d/123.feature")); + Assert.IsTrue(!File.Exists("ChangeLog.d/123.feature")); TestConsole c = new TestConsole(); AnsiConsole.Console = c; + + File.WriteAllText("ChangeLog.d/123.feature", "Adds levitation"); result = await Program.Main(args); Assert.AreEqual(1, result); string expected = "already produced newsfiles for this version"; Assert.IsTrue(c.Output.Contains(expected)); } + + [TestMethod] + [DeploymentItem("Template.tt")] + [DeploymentItem("ReleaseNote.config.toml")] + public async Task TestSingleFileFalseOverwriteDuplicateVersion() + { + Directory.CreateDirectory("ChangeLog.d"); + File.WriteAllText("ChangeLog.d/123.feature", "Adds levitation"); + File.WriteAllText("ReleaseNote.config.toml", """ +[ReleaseNote.Maker] +Directory = "ChangeLog.d" +OutputFileName = "{1}-notes.md" +TemplateFilePath = "Template.tt" +TitleFormat = "# {0} {1} ({2})" +IsSingleFile = false + +[[ReleaseNote.Type]] +Category = "feature" +DisplayName = "Features" +IsShowContent = true +"""); + + string[] args = [ + "build", + "--version", "7.8.9", + "--name", "foo", + "--date", "01-01-2001", + "--yes", + ]; + + int result = await Program.Main(args); + Assert.AreEqual(0, result); + Assert.IsTrue(!File.Exists("ChangeLog.d/123.feature")); + + File.WriteAllText("ChangeLog.d/123.feature", "Adds levitation"); + + result = await Program.Main(args); + Assert.AreEqual(0, result); + Assert.IsTrue(!File.Exists("ChangeLog.d/123.feature")); + + + string[] notes = Directory.GetFiles(".", "*-notes.md", SearchOption.TopDirectoryOnly).Select(x => Path.GetRelativePath(".", x)).ToArray(); + Assert.AreEqual(1, notes.Length); + Assert.AreEqual("7.8.9-notes.md", notes[0]); + + string actual = File.ReadAllText(notes[0]); + + string expected = """ +# foo 7.8.9 (01-01-2001) + +### Features + +- Adds levitation (#123) + +""".Replace("\r\n", "\n"); + Assert.AreEqual(expected, actual); + } } } \ No newline at end of file