From 45590b5202e2658c25da4524f07efd91519474ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Asbj=C3=B8rn=20Ulsberg?= Date: Mon, 27 Jun 2016 12:56:38 +0200 Subject: [PATCH] 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. --- .../AssemblyInfoBuilderTests.cs | 33 +++++++++++++++++++ .../GitVersionTask.Tests.csproj | 4 +++ src/GitVersionTask.Tests/packages.config | 1 + .../AssemblyInfoBuilder.cs | 31 ++++++++++------- 4 files changed, 57 insertions(+), 12 deletions(-) diff --git a/src/GitVersionTask.Tests/AssemblyInfoBuilderTests.cs b/src/GitVersionTask.Tests/AssemblyInfoBuilderTests.cs index 40352406b9..075768c553 100644 --- a/src/GitVersionTask.Tests/AssemblyInfoBuilderTests.cs +++ b/src/GitVersionTask.Tests/AssemblyInfoBuilderTests.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.ComponentModel; using System.IO; using System.Linq; @@ -6,9 +7,11 @@ using System.Runtime.CompilerServices; using GitVersion; using GitVersionCore.Tests; +using Microsoft.Build.Framework; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.VisualBasic; +using NSubstitute; using NUnit.Framework; using Shouldly; @@ -212,6 +215,36 @@ public void VerifyAssemblyVersion_MajorMinorPatchTag_NugetAssemblyInfo([ValueSou VerifyAssemblyVersion(compiler, AssemblyVersioningScheme.MajorMinorPatchTag, "{NugetVersion}"); } + [Test] + public void GetAssemblyInfoBuilder_Empty_ThrowsWarningException() + { + var taskItems = Substitute.For>(); + var exception = Assert.Throws(() => AssemblyInfoBuilder.GetAssemblyInfoBuilder(taskItems)); + exception.Message.ShouldBe("Unable to determine which AssemblyBuilder required to generate GitVersion assembly information"); + } + + [Test] + public void GetAssemblyInfoBuilder_Null_ThrowsArgumentNullException() + { + var exception = Assert.Throws(() => AssemblyInfoBuilder.GetAssemblyInfoBuilder(null)); + exception.ParamName.ShouldBe("compileFiles"); + } + + [TestCase("Class1.cs", typeof(CSharpAssemblyInfoBuilder))] + [TestCase("Class1.vb", typeof(VisualBasicAssemblyInfoBuilder))] + [TestCase("AssemblyInfo.cs", typeof(CSharpAssemblyInfoBuilder))] + [TestCase("AssemblyInfo.vb", typeof(VisualBasicAssemblyInfoBuilder))] + public void GetAssemblyInfoBuilder_ShouldReturnAppropriateAssemblyInfoBuilder(string fileName, Type assemblyInfoBuilderType) + { + var taskItem = Substitute.For(); + taskItem.ItemSpec.Returns(fileName); + + var assemblyInfoBuilder = AssemblyInfoBuilder.GetAssemblyInfoBuilder(new[] { taskItem }); + + assemblyInfoBuilder.ShouldNotBeNull(); + assemblyInfoBuilder.ShouldBeOfType(assemblyInfoBuilderType); + } + static void VerifyAssemblyVersion(ICompiler compiler, AssemblyVersioningScheme avs, string assemblyInformationalFormat = null) { var semanticVersion = new SemanticVersion diff --git a/src/GitVersionTask.Tests/GitVersionTask.Tests.csproj b/src/GitVersionTask.Tests/GitVersionTask.Tests.csproj index b78faf0ff9..3bde0520a5 100644 --- a/src/GitVersionTask.Tests/GitVersionTask.Tests.csproj +++ b/src/GitVersionTask.Tests/GitVersionTask.Tests.csproj @@ -87,6 +87,10 @@ ..\packages\Newtonsoft.Json.8.0.3\lib\net45\Newtonsoft.Json.dll True + + ..\packages\NSubstitute.1.10.0.0\lib\net45\NSubstitute.dll + True + ..\packages\NUnitTestAdapter.2.0.0\lib\nunit.core.dll True diff --git a/src/GitVersionTask.Tests/packages.config b/src/GitVersionTask.Tests/packages.config index 296e4c0e55..f1eac1ccd9 100644 --- a/src/GitVersionTask.Tests/packages.config +++ b/src/GitVersionTask.Tests/packages.config @@ -14,6 +14,7 @@ + diff --git a/src/GitVersionTask/AssemblyInfoBuilder/AssemblyInfoBuilder.cs b/src/GitVersionTask/AssemblyInfoBuilder/AssemblyInfoBuilder.cs index a02db87c91..257fdabf56 100644 --- a/src/GitVersionTask/AssemblyInfoBuilder/AssemblyInfoBuilder.cs +++ b/src/GitVersionTask/AssemblyInfoBuilder/AssemblyInfoBuilder.cs @@ -1,29 +1,38 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; +using GitTools; using GitVersion; using Microsoft.Build.Framework; -using System.IO; -using GitTools; public abstract class AssemblyInfoBuilder { - private static readonly Dictionary assemblyInfoBuilders = new Dictionary() + private static readonly Dictionary assemblyInfoBuilders = new Dictionary { - { ".cs", typeof(CSharpAssemblyInfoBuilder) }, - { ".vb", typeof(VisualBasicAssemblyInfoBuilder) } + {".cs", typeof(CSharpAssemblyInfoBuilder)}, + {".vb", typeof(VisualBasicAssemblyInfoBuilder)} // TODO: Missing FSharpAssemblyInfoBuilder }; + public abstract string AssemblyInfoExtension { get; } + public static AssemblyInfoBuilder GetAssemblyInfoBuilder(IEnumerable compileFiles) { + if (compileFiles == null) + { + throw new ArgumentNullException("compileFiles"); + } + Type builderType; - var assemblyInfoExtension = compileFiles.Select(x => x.ItemSpec) - .Where(compileFile => compileFile.Contains("AssemblyInfo")) - .Select(Path.GetExtension).FirstOrDefault(); + var assemblyInfoExtension = compileFiles + .Select(x => x.ItemSpec) + .Select(Path.GetExtension) + // TODO: While it works, this seems like a bad way to discover the language is being compiled. @asbjornu + .FirstOrDefault(extension => assemblyInfoBuilders.ContainsKey(extension.ToLowerInvariant())); - if (assemblyInfoBuilders.TryGetValue(assemblyInfoExtension, out builderType)) + if (assemblyInfoExtension != null && assemblyInfoBuilders.TryGetValue(assemblyInfoExtension, out builderType)) { return Activator.CreateInstance(builderType) as AssemblyInfoBuilder; } @@ -31,7 +40,5 @@ public static AssemblyInfoBuilder GetAssemblyInfoBuilder(IEnumerable throw new WarningException("Unable to determine which AssemblyBuilder required to generate GitVersion assembly information"); } - public abstract string AssemblyInfoExtension { get; } - public abstract string GetAssemblyInfoText(VersionVariables vars, string rootNamespace); -} +} \ No newline at end of file