Skip to content

Commit

Permalink
test: TestSingleFileFalseOverwriteDuplicateVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
netpyoung committed Dec 1, 2024
1 parent 72c12b6 commit 13f5e28
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,18 +164,28 @@ public override async Task<int> 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);
Expand All @@ -184,7 +194,7 @@ public override async Task<int> 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)
Expand Down Expand Up @@ -231,7 +241,7 @@ public override async Task<int> ExecuteAsync(CommandContext context, Settings se

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);
ExtractBaseHeaderAndContent(newsfileFpath, config, out string baseHeader, out string baseContent);
if (!string.IsNullOrEmpty(topLine)
&& baseContent.Contains(topLine))
{
Expand Down Expand Up @@ -272,15 +282,23 @@ 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;
baseContent = string.Empty;
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public sealed class ReleaseNoteConfigMaker
public string StartString { get; set; } = "<!-- release notes start -->\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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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 = [
Expand All @@ -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);
}
}
}

0 comments on commit 13f5e28

Please sign in to comment.