Skip to content

Commit 13f5e28

Browse files
committed
test: TestSingleFileFalseOverwriteDuplicateVersion
1 parent 72c12b6 commit 13f5e28

File tree

3 files changed

+109
-14
lines changed

3 files changed

+109
-14
lines changed

NF.Tool.ReleaseNoteMaker/NF.Tool.ReleaseNoteMaker.CLI/Commands/Command_Build.cs

+25-7
Original file line numberDiff line numberDiff line change
@@ -164,18 +164,28 @@ public override async Task<int> ExecuteAsync(CommandContext context, Settings se
164164
return 0;
165165
}
166166

167-
string newsfileFpath = Path.Combine(baseDirectory, config.Maker.OutputFileName);
167+
string newsFileName;
168+
if (config.Maker.IsSingleFile)
169+
{
170+
newsFileName = config.Maker.OutputFileName;
171+
}
172+
else
173+
{
174+
newsFileName = string.Format(config.Maker.OutputFileName, versionData.Name, versionData.Version, versionData.Date);
175+
}
176+
177+
string newsFileFpath = Path.Combine(baseDirectory, newsFileName);
168178
AnsiConsole.MarkupLine($"[green]*[/] Writing to newsfile...");
169179
{
170-
TextPath txtPath = new TextPath(newsfileFpath)
180+
TextPath txtPath = new TextPath(newsFileFpath)
171181
.RootColor(Color.Red)
172182
.SeparatorColor(Color.Green)
173183
.StemColor(Color.Blue)
174184
.LeafColor(Color.Yellow);
175-
AnsiConsole.Write($"{nameof(newsfileFpath)}: ");
185+
AnsiConsole.Write($"{nameof(newsFileFpath)}: ");
176186
AnsiConsole.Write(txtPath);
177187
AnsiConsole.WriteLine();
178-
Exception? appendToNewsFileExOrNull = await AppendToNewsFile(config, topLine, content, newsfileFpath);
188+
Exception? appendToNewsFileExOrNull = await AppendToNewsFile(config, topLine, content, newsFileFpath);
179189
if (appendToNewsFileExOrNull != null)
180190
{
181191
AnsiConsole.WriteException(appendToNewsFileExOrNull);
@@ -184,7 +194,7 @@ public override async Task<int> ExecuteAsync(CommandContext context, Settings se
184194
}
185195

186196
AnsiConsole.MarkupLine("[green]*[/] Staging newsfile...");
187-
GitHelper.StageNewsfile(newsfileFpath);
197+
GitHelper.StageNewsfile(newsFileFpath);
188198

189199
string[] fragmentFpaths = fragmentResult.FragmentFiles.Select(x => x.FileName).ToArray();
190200
if (fragmentFpaths.Length == 0)
@@ -231,7 +241,7 @@ public override async Task<int> ExecuteAsync(CommandContext context, Settings se
231241

232242
internal static async Task<Exception?> AppendToNewsFile(ReleaseNoteConfig config, string topLine, string content, string newsfileFpath)
233243
{
234-
ExtractBaseHeaderAndContent(newsfileFpath, config.Maker.StartString, out string baseHeader, out string baseContent);
244+
ExtractBaseHeaderAndContent(newsfileFpath, config, out string baseHeader, out string baseContent);
235245
if (!string.IsNullOrEmpty(topLine)
236246
&& baseContent.Contains(topLine))
237247
{
@@ -272,15 +282,23 @@ private static string NormalizeEndOfLine(string content, ReleaseNoteConfigMaker.
272282
}
273283
}
274284

275-
private static void ExtractBaseHeaderAndContent(string path, string startString, out string baseHeader, out string baseContent)
285+
private static void ExtractBaseHeaderAndContent(string path, ReleaseNoteConfig config, out string baseHeader, out string baseContent)
276286
{
287+
if (!config.Maker.IsSingleFile)
288+
{
289+
baseHeader = string.Empty;
290+
baseContent = string.Empty;
291+
return;
292+
}
293+
277294
if (!File.Exists(path))
278295
{
279296
baseHeader = string.Empty;
280297
baseContent = string.Empty;
281298
return;
282299
}
283300

301+
string startString = config.Maker.StartString;
284302
string txt = NormalizeEndOfLine(File.ReadAllText(path), ReleaseNoteConfigMaker.E_END_OF_LINE.LF);
285303
int index = txt.IndexOf(startString);
286304
if (index == -1)

NF.Tool.ReleaseNoteMaker/NF.Tool.ReleaseNoteMaker.Common/Config/ReleaseNoteConfig.cs

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public sealed class ReleaseNoteConfigMaker
3232
public string StartString { get; set; } = "<!-- release notes start -->\n";
3333
public bool IsWrap { get; set; } = false;
3434
public bool IsAllBullets { get; set; } = false;
35+
public bool IsSingleFile { get; set; } = true;
3536
public E_END_OF_LINE EndOfLine { get; set; } = E_END_OF_LINE.LF;
3637

3738
// config.package_dir

NF.Tool.ReleaseNoteMaker/NF.Tool.ReleaseNoteMaker.Tests/TestWrite.cs

+83-7
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,23 @@ namespace NF.Tool.ReleaseNoteMaker.Tests
1212
[DoNotParallelize]
1313
public class TestWrite
1414
{
15-
[TestInitialize()]
16-
public void Init()
15+
public required TestContext TestContext { get; set; }
16+
17+
[TestInitialize]
18+
public void TestInitialize()
19+
{
20+
string testName = TestContext.TestName!;
21+
string testDirectory = Path.Combine(TestContext.DeploymentDirectory!, testName);
22+
Directory.CreateDirectory(testDirectory);
23+
File.Copy("Template.tt", $"{testDirectory}/Template.tt");
24+
File.Copy("ReleaseNote.config.toml", $"{testDirectory}/ReleaseNote.config.toml");
25+
Directory.SetCurrentDirectory(testDirectory);
26+
}
27+
28+
[TestCleanup]
29+
public void TestCleanup()
1730
{
18-
File.Delete("ChangeLog.md");
31+
Directory.SetCurrentDirectory(TestContext.DeploymentDirectory!);
1932
}
2033

2134
[TestMethod]
@@ -230,17 +243,21 @@ public async Task TestMultipleFileNoStartString()
230243
[TestMethod]
231244
[DeploymentItem("Template.tt")]
232245
[DeploymentItem("ReleaseNote.config.toml")]
233-
[DeploymentItem("SampleData/Case001/123.feature", "ChangeLog.d/")]
234246
public async Task TestWithTitleFormatDuplicateVersionRaise()
235247
{
236-
Assert.IsTrue(File.Exists("ChangeLog.d/123.feature"));
237-
248+
Directory.CreateDirectory("ChangeLog.d");
249+
File.WriteAllText("ChangeLog.d/123.feature", "Adds levitation");
238250
File.WriteAllText("ReleaseNote.config.toml", """
239251
[ReleaseNote.Maker]
240252
Directory = "ChangeLog.d"
241253
OutputFileName = "{0}-notes.md"
242254
TemplateFilePath = "Template.tt"
243255
TitleFormat = "{0} {1} ({2})"
256+
257+
[[ReleaseNote.Type]]
258+
Category = "feature"
259+
DisplayName = "Features"
260+
IsShowContent = true
244261
""");
245262

246263
string[] args = [
@@ -254,17 +271,76 @@ public async Task TestWithTitleFormatDuplicateVersionRaise()
254271
int result = await Program.Main(args);
255272
Assert.AreEqual(0, result);
256273
Console.Write(File.ReadAllText("{0}-notes.md"));
257-
Assert.IsTrue(File.Exists("ChangeLog.d/123.feature"));
274+
Assert.IsTrue(!File.Exists("ChangeLog.d/123.feature"));
258275

259276

260277

261278
TestConsole c = new TestConsole();
262279
AnsiConsole.Console = c;
280+
281+
File.WriteAllText("ChangeLog.d/123.feature", "Adds levitation");
263282
result = await Program.Main(args);
264283
Assert.AreEqual(1, result);
265284

266285
string expected = "already produced newsfiles for this version";
267286
Assert.IsTrue(c.Output.Contains(expected));
268287
}
288+
289+
[TestMethod]
290+
[DeploymentItem("Template.tt")]
291+
[DeploymentItem("ReleaseNote.config.toml")]
292+
public async Task TestSingleFileFalseOverwriteDuplicateVersion()
293+
{
294+
Directory.CreateDirectory("ChangeLog.d");
295+
File.WriteAllText("ChangeLog.d/123.feature", "Adds levitation");
296+
File.WriteAllText("ReleaseNote.config.toml", """
297+
[ReleaseNote.Maker]
298+
Directory = "ChangeLog.d"
299+
OutputFileName = "{1}-notes.md"
300+
TemplateFilePath = "Template.tt"
301+
TitleFormat = "# {0} {1} ({2})"
302+
IsSingleFile = false
303+
304+
[[ReleaseNote.Type]]
305+
Category = "feature"
306+
DisplayName = "Features"
307+
IsShowContent = true
308+
""");
309+
310+
string[] args = [
311+
"build",
312+
"--version", "7.8.9",
313+
"--name", "foo",
314+
"--date", "01-01-2001",
315+
"--yes",
316+
];
317+
318+
int result = await Program.Main(args);
319+
Assert.AreEqual(0, result);
320+
Assert.IsTrue(!File.Exists("ChangeLog.d/123.feature"));
321+
322+
File.WriteAllText("ChangeLog.d/123.feature", "Adds levitation");
323+
324+
result = await Program.Main(args);
325+
Assert.AreEqual(0, result);
326+
Assert.IsTrue(!File.Exists("ChangeLog.d/123.feature"));
327+
328+
329+
string[] notes = Directory.GetFiles(".", "*-notes.md", SearchOption.TopDirectoryOnly).Select(x => Path.GetRelativePath(".", x)).ToArray();
330+
Assert.AreEqual(1, notes.Length);
331+
Assert.AreEqual("7.8.9-notes.md", notes[0]);
332+
333+
string actual = File.ReadAllText(notes[0]);
334+
335+
string expected = """
336+
# foo 7.8.9 (01-01-2001)
337+
338+
### Features
339+
340+
- Adds levitation (#123)
341+
342+
""".Replace("\r\n", "\n");
343+
Assert.AreEqual(expected, actual);
344+
}
269345
}
270346
}

0 commit comments

Comments
 (0)