Skip to content

Commit 955ff5e

Browse files
authored
Merge pull request #2150 from KiLLeRRaT/EOFRegex
Add handling of Assembly attributes that do not end on a new line
2 parents 7c4b172 + 9411d6f commit 955ff5e

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/GitVersionTask.Tests/InvalidFileCheckerTests.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,21 @@ public void VerifyCommentWorksCSharp([Values("AssemblyVersion", "AssemblyFileVer
9898
FileHelper.CheckForInvalidFiles(new ITaskItem[] { new MockTaskItem { ItemSpec = "AssemblyInfo.cs" } }, projectFile);
9999
}
100100

101+
[Test]
102+
public void VerifyCommentWithNoNewLineAtEndWorksCSharp([Values("AssemblyVersion", "AssemblyFileVersion", "AssemblyInformationalVersion")]string attribute)
103+
{
104+
using (var writer = File.CreateText(Path.Combine(projectDirectory, "AssemblyInfo.cs")))
105+
{
106+
writer.Write(@"
107+
using System;
108+
using System.Reflection;
109+
110+
//[assembly: {0}(""1.0.0.0"")]", attribute);
111+
}
112+
113+
FileHelper.CheckForInvalidFiles(new ITaskItem[] { new MockTaskItem { ItemSpec = "AssemblyInfo.cs" } }, projectFile);
114+
}
115+
101116
[Test]
102117
public void VerifyStringWorksCSharp([Values("AssemblyVersion", "AssemblyFileVersion", "AssemblyInformationalVersion")]string attribute)
103118
{
@@ -186,6 +201,21 @@ Imports System.Reflection
186201
FileHelper.CheckForInvalidFiles(new ITaskItem[] { new MockTaskItem { ItemSpec = "AssemblyInfo.vb" } }, projectFile);
187202
}
188203

204+
[Test]
205+
public void VerifyCommentWithNoNewLineAtEndWorksVisualBasic([Values("AssemblyVersion", "AssemblyFileVersion", "AssemblyInformationalVersion")]string attribute)
206+
{
207+
using (var writer = File.CreateText(Path.Combine(projectDirectory, "AssemblyInfo.vb")))
208+
{
209+
writer.Write(@"
210+
Imports System
211+
Imports System.Reflection
212+
213+
'<Assembly: {0}(""1.0.0.0"")>", attribute);
214+
}
215+
216+
FileHelper.CheckForInvalidFiles(new ITaskItem[] { new MockTaskItem { ItemSpec = "AssemblyInfo.vb" } }, projectFile);
217+
}
218+
189219
[Test]
190220
public void VerifyStringWorksVisualBasic([Values("AssemblyVersion", "AssemblyFileVersion", "AssemblyInformationalVersion")]string attribute)
191221
{

src/GitVersionTask/FileHelper.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ private static bool CSharpFileContainsVersionAttribute(string compileFile, strin
9797
var combine = Path.Combine(Path.GetDirectoryName(projectFile), compileFile);
9898
var allText = File.ReadAllText(combine);
9999

100+
allText += System.Environment.NewLine; // Always add a new line, this handles the case for when a file ends with the EOF marker and no new line. If you don't have this newline, the regex will match commented out Assembly*Version tags on the last line.
101+
100102
var blockComments = @"/\*(.*?)\*/";
101103
var lineComments = @"//(.*?)\r?\n";
102104
var strings = @"""((\\[^\n]|[^""\n])*)""";
@@ -123,6 +125,8 @@ private static bool VisualBasicFileContainsVersionAttribute(string compileFile,
123125
var combine = Path.Combine(Path.GetDirectoryName(projectFile), compileFile);
124126
var allText = File.ReadAllText(combine);
125127

128+
allText += System.Environment.NewLine; // Always add a new line, this handles the case for when a file ends with the EOF marker and no new line. If you don't have this newline, the regex will match commented out Assembly*Version tags on the last line.
129+
126130
var lineComments = @"'(.*?)\r?\n";
127131
var strings = @"""((\\[^\n]|[^""\n])*)""";
128132

0 commit comments

Comments
 (0)