Skip to content

Commit c85699d

Browse files
committed
Merge pull request #527 from distantcam/BetterDupAttributeDetection
Used regex to determine if version attributes are still there.
2 parents b99b936 + 66299f3 commit c85699d

File tree

4 files changed

+205
-5
lines changed

4 files changed

+205
-5
lines changed

src/GitVersionTask.Tests/GitVersionTask.Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,14 @@
102102
<Compile Include="AssemblyInfoBuilderTests.cs" />
103103
<Compile Include="AssemblyLocation.cs" />
104104
<Compile Include="GitVersionTaskDirectoryTests.cs" />
105+
<Compile Include="InvalidFileCheckerTests.cs" />
105106
<Compile Include="Mocks\MockBuildEngine.cs" />
106107
<Compile Include="GetVersionTaskTests.cs" />
107108
<Compile Include="Helpers\IPostTestDirectoryRemover.cs" />
108109
<Compile Include="Helpers\LibGit2SharpExtensions.cs" />
109110
<Compile Include="Helpers\Scrubbers.cs" />
110111
<Compile Include="Helpers\SelfCleaningDirectory.cs" />
112+
<Compile Include="Mocks\MockTaskItem.cs" />
111113
<Compile Include="ModuleInitializer.cs" />
112114
</ItemGroup>
113115
<ItemGroup>
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
using System;
2+
using System.IO;
3+
using GitVersion;
4+
using GitVersionTask.Tests.Mocks;
5+
using Microsoft.Build.Framework;
6+
using NUnit.Framework;
7+
8+
[TestFixture]
9+
public class InvalidFileCheckerTests
10+
{
11+
string projectDirectory;
12+
string projectFile;
13+
14+
[SetUp]
15+
public void CreateTemporaryProject()
16+
{
17+
projectDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
18+
projectFile = Path.Combine(projectDirectory, "Fake.csproj");
19+
20+
Directory.CreateDirectory(projectDirectory);
21+
22+
File.Create(projectFile).Close();
23+
}
24+
25+
[TearDown]
26+
public void Cleanup()
27+
{
28+
Directory.Delete(projectDirectory, true);
29+
}
30+
31+
[Test]
32+
public void VerifyIgnoreNonAssemblyInfoFile()
33+
{
34+
using (var writer = File.CreateText(Path.Combine(projectDirectory, "SomeOtherFile.cs")))
35+
{
36+
writer.Write(@"
37+
using System;
38+
using System.Reflection;
39+
40+
[assembly: AssemblyVersion(""1.0.0.0"")]
41+
");
42+
}
43+
44+
InvalidFileChecker.CheckForInvalidFiles(new ITaskItem[] { new MockTaskItem { ItemSpec = "SomeOtherFile.cs" } }, projectFile);
45+
}
46+
47+
[Test]
48+
public void VerifyAttributeFound([Values("AssemblyVersion", "AssemblyFileVersion", "AssemblyInformationalVersion", "System.Reflection.AssemblyVersion")]string attribute)
49+
{
50+
using (var writer = File.CreateText(Path.Combine(projectDirectory, "AssemblyInfo.cs")))
51+
{
52+
writer.Write(@"
53+
using System;
54+
using System.Reflection;
55+
56+
[assembly:{0}(""1.0.0.0"")]
57+
", attribute);
58+
}
59+
60+
var ex = Assert.Throws<WarningException>(() => InvalidFileChecker.CheckForInvalidFiles(new ITaskItem[] { new MockTaskItem { ItemSpec = "AssemblyInfo.cs" } }, projectFile), attribute);
61+
Assert.That(ex.Message, Is.EqualTo("File contains assembly version attributes with conflict with the attributes generated by GitVersion AssemblyInfo.cs"));
62+
}
63+
64+
[Test]
65+
public void VerifyUnformattedAttributeFound([Values("AssemblyVersion", "AssemblyFileVersion", "AssemblyInformationalVersion", "System . Reflection . AssemblyVersion")]string attribute)
66+
{
67+
using (var writer = File.CreateText(Path.Combine(projectDirectory, "AssemblyInfo.cs")))
68+
{
69+
writer.Write(@"
70+
using System;
71+
using System.Reflection;
72+
73+
[ assembly :
74+
{0} ( ""1.0.0.0"")]
75+
", attribute);
76+
}
77+
78+
var ex = Assert.Throws<WarningException>(() => InvalidFileChecker.CheckForInvalidFiles(new ITaskItem[] { new MockTaskItem { ItemSpec = "AssemblyInfo.cs" } }, projectFile), attribute);
79+
Assert.That(ex.Message, Is.EqualTo("File contains assembly version attributes with conflict with the attributes generated by GitVersion AssemblyInfo.cs"));
80+
}
81+
82+
[Test]
83+
public void VerifyCommentWorks([Values("AssemblyVersion", "AssemblyFileVersion", "AssemblyInformationalVersion")]string attribute)
84+
{
85+
using (var writer = File.CreateText(Path.Combine(projectDirectory, "AssemblyInfo.cs")))
86+
{
87+
writer.Write(@"
88+
using System;
89+
using System.Reflection;
90+
91+
//[assembly: {0}(""1.0.0.0"")]
92+
", attribute);
93+
}
94+
95+
InvalidFileChecker.CheckForInvalidFiles(new ITaskItem[] { new MockTaskItem { ItemSpec = "AssemblyInfo.cs" } }, projectFile);
96+
}
97+
98+
[Test]
99+
public void VerifyStringWorks([Values("AssemblyVersion", "AssemblyFileVersion", "AssemblyInformationalVersion")]string attribute)
100+
{
101+
using (var writer = File.CreateText(Path.Combine(projectDirectory, "AssemblyInfo.cs")))
102+
{
103+
writer.Write(@"
104+
using System;
105+
using System.Reflection;
106+
107+
public class Temp
108+
{{
109+
static const string Foo = ""[assembly: {0}(""""1.0.0.0"""")]"";
110+
}}
111+
", attribute);
112+
}
113+
114+
InvalidFileChecker.CheckForInvalidFiles(new ITaskItem[] { new MockTaskItem { ItemSpec = "AssemblyInfo.cs" } }, projectFile);
115+
}
116+
117+
[Test]
118+
public void VerifyIdentifierWorks([Values("AssemblyVersion", "AssemblyFileVersion", "AssemblyInformationalVersion")]string attribute)
119+
{
120+
using (var writer = File.CreateText(Path.Combine(projectDirectory, "AssemblyInfo.cs")))
121+
{
122+
writer.Write(@"
123+
using System;
124+
using System.Reflection;
125+
126+
public class {0}
127+
{{
128+
}}
129+
", attribute);
130+
}
131+
132+
InvalidFileChecker.CheckForInvalidFiles(new ITaskItem[] { new MockTaskItem { ItemSpec = "AssemblyInfo.cs" } }, projectFile);
133+
}
134+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using System.Collections;
3+
using Microsoft.Build.Framework;
4+
5+
namespace GitVersionTask.Tests.Mocks
6+
{
7+
class MockTaskItem : ITaskItem
8+
{
9+
public string ItemSpec { get; set; }
10+
11+
public int MetadataCount { get; private set; }
12+
13+
public ICollection MetadataNames { get; private set; }
14+
15+
public IDictionary CloneCustomMetadata()
16+
{
17+
throw new NotImplementedException();
18+
}
19+
20+
public void CopyMetadataTo(ITaskItem destinationItem)
21+
{
22+
throw new NotImplementedException();
23+
}
24+
25+
public string GetMetadata(string metadataName)
26+
{
27+
throw new NotImplementedException();
28+
}
29+
30+
public void RemoveMetadata(string metadataName)
31+
{
32+
throw new NotImplementedException();
33+
}
34+
35+
public void SetMetadata(string metadataName, string metadataValue)
36+
{
37+
throw new NotImplementedException();
38+
}
39+
}
40+
}
Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.IO;
34
using System.Linq;
5+
using System.Text.RegularExpressions;
46
using GitVersion;
57
using Microsoft.Build.Framework;
68

@@ -10,17 +12,40 @@ public static void CheckForInvalidFiles(IEnumerable<ITaskItem> compileFiles, str
1012
{
1113
foreach (var compileFile in GetInvalidFiles(compileFiles, projectFile))
1214
{
13-
throw new WarningException("File contains assembly version attributes with conflict with the attributes generated by GitVersion" + compileFile);
15+
throw new WarningException("File contains assembly version attributes with conflict with the attributes generated by GitVersion " + compileFile);
1416
}
1517
}
1618

1719
static bool FileContainsVersionAttribute(string compileFile, string projectFile)
1820
{
1921
var combine = Path.Combine(Path.GetDirectoryName(projectFile), compileFile);
2022
var allText = File.ReadAllText(combine);
21-
return allText.Contains("AssemblyVersion") ||
22-
allText.Contains("AssemblyFileVersion") ||
23-
allText.Contains("AssemblyInformationalVersion");
23+
24+
var blockComments = @"/\*(.*?)\*/";
25+
var lineComments = @"//(.*?)\r?\n";
26+
var strings = @"""((\\[^\n]|[^""\n])*)""";
27+
var verbatimStrings = @"@(""[^""]*"")+";
28+
29+
string noCommentsOrStrings = Regex.Replace(allText,
30+
blockComments + "|" + lineComments + "|" + strings + "|" + verbatimStrings,
31+
me =>
32+
{
33+
if (me.Value.StartsWith("//"))
34+
return Environment.NewLine;
35+
36+
return "";
37+
},
38+
RegexOptions.Singleline);
39+
40+
return Regex.IsMatch(noCommentsOrStrings, @"(?x) # IgnorePatternWhitespace
41+
42+
\[\s*assembly\s*:\s* # The [assembly: part
43+
44+
(System\s*\.\s*Reflection\s*\.\s*)? # The System.Reflection. part (optional)
45+
46+
Assembly(File|Informational)?Version # The attribute AssemblyVersion, AssemblyFileVersion, or AssemblyInformationalVersion
47+
48+
\s*\(\s*\)\s*\] # End brackets ()]");
2449
}
2550

2651
static IEnumerable<string> GetInvalidFiles(IEnumerable<ITaskItem> compileFiles, string projectFile)
@@ -29,5 +54,4 @@ static IEnumerable<string> GetInvalidFiles(IEnumerable<ITaskItem> compileFiles,
2954
.Where(compileFile => compileFile.Contains("AssemblyInfo"))
3055
.Where(s => FileContainsVersionAttribute(s, projectFile));
3156
}
32-
3357
}

0 commit comments

Comments
 (0)