Skip to content

Commit c447240

Browse files
committed
Improve the language discovery heuristics of AssemblyInfoBuilder
Improve how the AssemblyInfoBuilder discovers what language is being compiled by not looking for an `AssemblyInfo.*` file, but instead finding the first *.vb og *.cs file and deducing the langauge from there. Also adds a few more guards and tests for AssemblyInfoBuilder to make it more robust and easier to understand if it breaks.
1 parent 98f8b97 commit c447240

File tree

4 files changed

+57
-12
lines changed

4 files changed

+57
-12
lines changed

src/GitVersionTask.Tests/AssemblyInfoBuilderTests.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.ComponentModel;
34
using System.IO;
45
using System.Linq;
56
using System.Reflection;
67
using System.Runtime.CompilerServices;
78
using GitVersion;
89
using GitVersionCore.Tests;
10+
using Microsoft.Build.Framework;
911
using Microsoft.CodeAnalysis;
1012
using Microsoft.CodeAnalysis.CSharp;
1113
using Microsoft.CodeAnalysis.VisualBasic;
14+
using NSubstitute;
1215
using NUnit.Framework;
1316
using Shouldly;
1417

@@ -212,6 +215,36 @@ public void VerifyAssemblyVersion_MajorMinorPatchTag_NugetAssemblyInfo([ValueSou
212215
VerifyAssemblyVersion(compiler, AssemblyVersioningScheme.MajorMinorPatchTag, "{NugetVersion}");
213216
}
214217

218+
[Test]
219+
public void GetAssemblyInfoBuilder_Empty_ThrowsWarningException()
220+
{
221+
var taskItems = Substitute.For<IEnumerable<ITaskItem>>();
222+
var exception = Assert.Throws<GitTools.WarningException>(() => AssemblyInfoBuilder.GetAssemblyInfoBuilder(taskItems));
223+
exception.Message.ShouldBe("Unable to determine which AssemblyBuilder required to generate GitVersion assembly information");
224+
}
225+
226+
[Test]
227+
public void GetAssemblyInfoBuilder_Null_ThrowsArgumentNullException()
228+
{
229+
var exception = Assert.Throws<ArgumentNullException>(() => AssemblyInfoBuilder.GetAssemblyInfoBuilder(null));
230+
exception.ParamName.ShouldBe("compileFiles");
231+
}
232+
233+
[TestCase("Class1.cs", typeof(CSharpAssemblyInfoBuilder))]
234+
[TestCase("Class1.vb", typeof(VisualBasicAssemblyInfoBuilder))]
235+
[TestCase("AssemblyInfo.cs", typeof(CSharpAssemblyInfoBuilder))]
236+
[TestCase("AssemblyInfo.vb", typeof(VisualBasicAssemblyInfoBuilder))]
237+
public void GetAssemblyInfoBuilder_ShouldReturnAppropriateAssemblyInfoBuilder(string fileName, Type assemblyInfoBuilderType)
238+
{
239+
var taskItem = Substitute.For<ITaskItem>();
240+
taskItem.ItemSpec.Returns(fileName);
241+
242+
var assemblyInfoBuilder = AssemblyInfoBuilder.GetAssemblyInfoBuilder(new[] { taskItem });
243+
244+
assemblyInfoBuilder.ShouldNotBeNull();
245+
assemblyInfoBuilder.ShouldBeOfType(assemblyInfoBuilderType);
246+
}
247+
215248
static void VerifyAssemblyVersion(ICompiler compiler, AssemblyVersioningScheme avs, string assemblyInformationalFormat = null)
216249
{
217250
var semanticVersion = new SemanticVersion

src/GitVersionTask.Tests/GitVersionTask.Tests.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@
8787
<HintPath>..\packages\Newtonsoft.Json.8.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
8888
<Private>True</Private>
8989
</Reference>
90+
<Reference Include="NSubstitute, Version=1.10.0.0, Culture=neutral, PublicKeyToken=92dd2e9066daa5ca, processorArchitecture=MSIL">
91+
<HintPath>..\packages\NSubstitute.1.10.0.0\lib\net45\NSubstitute.dll</HintPath>
92+
<Private>True</Private>
93+
</Reference>
9094
<Reference Include="nunit.core, Version=2.6.4.14350, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
9195
<HintPath>..\packages\NUnitTestAdapter.2.0.0\lib\nunit.core.dll</HintPath>
9296
<Private>True</Private>

src/GitVersionTask.Tests/packages.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<package id="Microsoft.Web.Xdt" version="2.1.1" targetFramework="net45" />
1515
<package id="ModuleInit.Fody" version="1.5.9.0" targetFramework="net45" developmentDependency="true" />
1616
<package id="Newtonsoft.Json" version="8.0.3" targetFramework="net45" />
17+
<package id="NSubstitute" version="1.10.0.0" targetFramework="net45" />
1718
<package id="NUnit" version="3.2.1" targetFramework="net45" />
1819
<package id="NUnitTestAdapter" version="2.0.0" targetFramework="net45" />
1920
<package id="ObjectApproval" version="1.3.0" targetFramework="net45" />
Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,44 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.IO;
34
using System.Linq;
5+
using GitTools;
46
using GitVersion;
57
using Microsoft.Build.Framework;
6-
using System.IO;
7-
using GitTools;
88

99
public abstract class AssemblyInfoBuilder
1010
{
11-
private static readonly Dictionary<string, Type> assemblyInfoBuilders = new Dictionary<string, Type>()
11+
private static readonly Dictionary<string, Type> assemblyInfoBuilders = new Dictionary<string, Type>
1212
{
13-
{ ".cs", typeof(CSharpAssemblyInfoBuilder) },
14-
{ ".vb", typeof(VisualBasicAssemblyInfoBuilder) }
13+
{".cs", typeof(CSharpAssemblyInfoBuilder)},
14+
{".vb", typeof(VisualBasicAssemblyInfoBuilder)}
1515
// TODO: Missing FSharpAssemblyInfoBuilder
1616
};
1717

18+
public abstract string AssemblyInfoExtension { get; }
19+
1820
public static AssemblyInfoBuilder GetAssemblyInfoBuilder(IEnumerable<ITaskItem> compileFiles)
1921
{
22+
if (compileFiles == null)
23+
{
24+
throw new ArgumentNullException("compileFiles");
25+
}
26+
2027
Type builderType;
2128

22-
var assemblyInfoExtension = compileFiles.Select(x => x.ItemSpec)
23-
.Where(compileFile => compileFile.Contains("AssemblyInfo"))
24-
.Select(Path.GetExtension).FirstOrDefault();
29+
var assemblyInfoExtension = compileFiles
30+
.Select(x => x.ItemSpec)
31+
.Select(Path.GetExtension)
32+
// TODO: While it works, this seems like a bad way to discover the language is being compiled. @asbjornu
33+
.FirstOrDefault(extension => assemblyInfoBuilders.ContainsKey(extension.ToLowerInvariant()));
2534

26-
if (assemblyInfoBuilders.TryGetValue(assemblyInfoExtension, out builderType))
35+
if (assemblyInfoExtension != null && assemblyInfoBuilders.TryGetValue(assemblyInfoExtension, out builderType))
2736
{
2837
return Activator.CreateInstance(builderType) as AssemblyInfoBuilder;
2938
}
3039

3140
throw new WarningException("Unable to determine which AssemblyBuilder required to generate GitVersion assembly information");
3241
}
3342

34-
public abstract string AssemblyInfoExtension { get; }
35-
3643
public abstract string GetAssemblyInfoText(VersionVariables vars, string rootNamespace);
37-
}
44+
}

0 commit comments

Comments
 (0)