Skip to content

Commit

Permalink
Merge pull request #64 from sillsdev/FileUpdate-should-not-fail-if-li…
Browse files Browse the repository at this point in the history
…teral-replacementtext-is-found

FileUpdate: do not fail if literal ReplacementText is found
  • Loading branch information
tombogle committed Aug 11, 2023
2 parents 3d20b9a + a0e9cea commit de9a3fc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
14 changes: 14 additions & 0 deletions SIL.BuildTasks.Tests/FileUpdateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public class FileUpdateTests
ExpectedResult = "There were approximately 35 frog princes.")]
[TestCase("There were 35 frog princes.", "(?<number>\\d+)", "approximately ${number}",
ExpectedResult = "There were approximately 35 frog princes.")]
[TestCase("</TargetFrameworkProfile>\r\n <Version>0.0.0.0</Version> <!-- Replaced by FileUpdate in build/build.proj --> <Authors>SIL</Authors>", "<Version>0\\.0\\.0\\.0</Version>", "<Version>3.3.0</Version>",
ExpectedResult = "</TargetFrameworkProfile>\r\n <Version>3.3.0</Version> <!-- Replaced by FileUpdate in build/build.proj --> <Authors>SIL</Authors>")]
public string GetModifiedContents_RegexTextMatched_Succeeds(string origContents, string regex, string replacement)
{
var updater = new FileUpdate
Expand All @@ -32,6 +34,18 @@ public string GetModifiedContents_RegexTextMatched_Succeeds(string origContents,
return updater.GetModifiedContents(origContents);
}

[TestCase("</TargetFrameworkProfile>\r\n <Version>3.3.0</Version> <!-- Previously replaced by FileUpdate in build/build.proj --> <Authors>SIL</Authors>", "<Version>0\\.0\\.0\\.0</Version>", "<Version>3.3.0</Version>")]
public void GetModifiedContents_RegexTextNotMatchedButReplacementTextFound_NoChangeOrError(string origContents, string regex, string replacement)
{
var updater = new FileUpdate
{
Regex = regex,
ReplacementText = replacement
};

Assert.That(updater.GetModifiedContents(origContents), NUnit.Framework.Is.EqualTo(origContents));
}

[TestCase("This is the story of the frog prince.", "princess", "soup")]
[TestCase("This is the story of the frog prince.", "\\d+", "14")]
public void GetModifiedContents_RegexTextNotMatched_Throws(string origContents, string regex, string replacement)
Expand Down
15 changes: 14 additions & 1 deletion SIL.BuildTasks/FileUpdate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,20 @@ internal string GetModifiedContents(string content)
{
var regex = new Regex(Regex);
if (!regex.IsMatch(content))
throw new Exception($"No replacements made. Regex: '{Regex}'; ReplacementText: '{ReplacementText}'");
{
// This check will generally only work if ReplacementText is just a literal string.
// If it contains references to regex match groups, and GetModifiedContents is run
// against a previously modified file with the same arguments, most likely no match
// will be found, and the "No replacements made" error will be displayed.
if (content.Contains(ReplacementText))
{
// Most likely, the replacement was already done an a previous build step.
return content;
}

throw new Exception($"No replacements made. Regex: '{Regex}'; " +
$"ReplacementText: '{ReplacementText}'");
}

var newContents = regex.Replace(content, ReplacementText);

Expand Down

0 comments on commit de9a3fc

Please sign in to comment.