From a51a373a10440411efe23ceabaeef7a79acaa1b6 Mon Sep 17 00:00:00 2001 From: Edward Wilde Date: Tue, 28 Oct 2014 09:18:10 +0000 Subject: [PATCH 1/3] Implement configuration convention Closes #58 --- .gitignore | 3 +- .../ProjectConfigurationTests.cs | 55 ++++++++++++++ .../SampleApp.Tests/SampleApp.Tests.csproj | 17 +++++ Samples/SampleApp.Tests/packages.config | 2 + .../ProjectPropertyGroupsTests.cs | 37 ++++++++++ ...mize_true_if_property_defined.approved.txt | 4 ++ ...s_should_have_platform_AnyCPU.approved.txt | 4 ++ .../ProjectBasedConventions.cs | 48 +++++++++++++ ...debug_type_should_be_pdb_only.approved.txt | 9 +++ .../Properties/Resources.Designer.cs | 16 ++++- .../Properties/Resources.resx | 3 + .../ProjectFileWithReleaseDebugTypeFull.txt | 15 ++++ .../TestStack.ConventionTests.Tests.csproj | 4 ++ .../ConventionData/ProjectPropertyGroup.cs | 72 +++++++++++++++++++ .../ConventionData/ProjectPropertyGroups.cs | 36 ++++++++++ .../ConfigurationHasSpecificValue.cs | 71 ++++++++++++++++++ .../TestStack.ConventionTests.csproj | 5 ++ 17 files changed, 399 insertions(+), 2 deletions(-) create mode 100644 Samples/SampleApp.Tests/ProjectConfigurationTests.cs create mode 100644 TestStack.ConventionTests.Tests/ConventionData/ProjectPropertyGroupsTests.cs create mode 100644 TestStack.ConventionTests.Tests/ProjectBasedConventions.all_configuration_groups_should_have_optimize_true_if_property_defined.approved.txt create mode 100644 TestStack.ConventionTests.Tests/ProjectBasedConventions.all_configuration_groups_should_have_platform_AnyCPU.approved.txt create mode 100644 TestStack.ConventionTests.Tests/ProjectBasedConventions.release_debug_type_should_be_pdb_only.approved.txt create mode 100644 TestStack.ConventionTests.Tests/Resources/ProjectFileWithReleaseDebugTypeFull.txt create mode 100644 TestStack.ConventionTests/ConventionData/ProjectPropertyGroup.cs create mode 100644 TestStack.ConventionTests/ConventionData/ProjectPropertyGroups.cs create mode 100644 TestStack.ConventionTests/Conventions/ConfigurationHasSpecificValue.cs diff --git a/.gitignore b/.gitignore index 20ccecc..20aa641 100644 --- a/.gitignore +++ b/.gitignore @@ -14,4 +14,5 @@ _ReSharper.* ConventionTests*.nupkg _NCrunch* *.received.txt -*.orig \ No newline at end of file +*.orig +packages \ No newline at end of file diff --git a/Samples/SampleApp.Tests/ProjectConfigurationTests.cs b/Samples/SampleApp.Tests/ProjectConfigurationTests.cs new file mode 100644 index 0000000..1564718 --- /dev/null +++ b/Samples/SampleApp.Tests/ProjectConfigurationTests.cs @@ -0,0 +1,55 @@ +namespace SampleApp.Tests +{ + using System; + using System.IO; + using System.Xml.Linq; + using NSubstitute; + using NUnit.Framework; + using SampleApp.Domain; + using SampleApp.Dtos; + using TestStack.ConventionTests; + using TestStack.ConventionTests.ConventionData; + using TestStack.ConventionTests.Conventions; + using TestStack.ConventionTests.Internal; + + [TestFixture] + public class ProjectConfigurationTests + { + IProjectLocator projectLocator; + IProjectProvider projectProvider; + + [SetUp] + public void Setup() + { + projectLocator = Substitute.For(); + projectProvider = Substitute.For(); + projectProvider + .LoadProjectDocument(Arg.Any()) + .Returns(XDocument.Parse(File.ReadAllText(Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, @"..\..\..\SampleApp\SampleApp.csproj"))))); + } + + [Test] + public void debug_configurations_should_have_debug_type_pdb_only() + { + Convention.Is(new ConfigurationHasSpecificValue(ConfigurationType.Debug, "DebugType", "full"), new ProjectPropertyGroups(typeof(DomainClass).Assembly, projectProvider, projectLocator)); + } + + [Test] + public void debug_configurations_should_have_optimize_false() + { + Convention.Is(new ConfigurationHasSpecificValue(ConfigurationType.Debug, "Optimize", "false"), new ProjectPropertyGroups(typeof(DomainClass).Assembly, projectProvider, projectLocator)); + } + + [Test] + public void release_configurations_should_have_debug_type_pdb_only() + { + Convention.Is(new ConfigurationHasSpecificValue(ConfigurationType.Release, "DebugType", "pdbonly"), new ProjectPropertyGroups(typeof(DomainClass).Assembly, projectProvider, projectLocator)); + } + + [Test] + public void release_configurations_should_have_optimize_true() + { + Convention.Is(new ConfigurationHasSpecificValue(ConfigurationType.Release, "Optimize", "true"), new ProjectPropertyGroups(typeof(DomainClass).Assembly, projectProvider, projectLocator)); + } + } +} \ No newline at end of file diff --git a/Samples/SampleApp.Tests/SampleApp.Tests.csproj b/Samples/SampleApp.Tests/SampleApp.Tests.csproj index fd7a560..ebfe7b5 100644 --- a/Samples/SampleApp.Tests/SampleApp.Tests.csproj +++ b/Samples/SampleApp.Tests/SampleApp.Tests.csproj @@ -31,10 +31,26 @@ 4 + + False + ..\..\packages\Mono.Cecil.0.9.5.4\lib\net40\Mono.Cecil.dll + + + ..\..\packages\Mono.Cecil.0.9.5.4\lib\net40\Mono.Cecil.Mdb.dll + + + ..\..\packages\Mono.Cecil.0.9.5.4\lib\net40\Mono.Cecil.Pdb.dll + + + ..\..\packages\Mono.Cecil.0.9.5.4\lib\net40\Mono.Cecil.Rocks.dll + False ..\..\packages\Newtonsoft.Json.4.5.11\lib\net40\Newtonsoft.Json.dll + + ..\..\packages\NSubstitute.1.6.1.0\lib\NET40\NSubstitute.dll + ..\..\packages\NUnit.2.6.2\lib\nunit.framework.dll @@ -63,6 +79,7 @@ + diff --git a/Samples/SampleApp.Tests/packages.config b/Samples/SampleApp.Tests/packages.config index 17c4ae1..ac48445 100644 --- a/Samples/SampleApp.Tests/packages.config +++ b/Samples/SampleApp.Tests/packages.config @@ -3,6 +3,8 @@ + + \ No newline at end of file diff --git a/TestStack.ConventionTests.Tests/ConventionData/ProjectPropertyGroupsTests.cs b/TestStack.ConventionTests.Tests/ConventionData/ProjectPropertyGroupsTests.cs new file mode 100644 index 0000000..6843a8d --- /dev/null +++ b/TestStack.ConventionTests.Tests/ConventionData/ProjectPropertyGroupsTests.cs @@ -0,0 +1,37 @@ +namespace TestStack.ConventionTests.Tests.ConventionData +{ + using System.Linq; + using System.Xml.Linq; + using NSubstitute; + using NUnit.Framework; + using TestStack.ConventionTests.ConventionData; + using TestStack.ConventionTests.Internal; + using TestStack.ConventionTests.Tests.Properties; + + [TestFixture] + public class ProjectPropertyGroupsTests + { + IProjectProvider projectProvider; + + [SetUp] + public void Setup() + { + projectProvider = Substitute.For(); + } + + [Test] + public void can_parse_a_normal_project_file_to_read_global_debug_and_release_property_groups() + { + projectProvider + .LoadProjectDocument(Arg.Any()) + .Returns(XDocument.Parse(Resources.ProjectFileWithBinReference)); + + var projectGroups = new ProjectPropertyGroups(typeof(ProjectPropertyGroups).Assembly, projectProvider, Substitute.For()); + + Assert.That(projectGroups.PropertyGroups.Length, Is.EqualTo(3)); + Assert.That(projectGroups.PropertyGroups.Any(item => item.Debug)); + Assert.That(projectGroups.PropertyGroups.Any(item => item.Release)); + Assert.That(projectGroups.PropertyGroups.Any(item => item.Global)); + } + } +} \ No newline at end of file diff --git a/TestStack.ConventionTests.Tests/ProjectBasedConventions.all_configuration_groups_should_have_optimize_true_if_property_defined.approved.txt b/TestStack.ConventionTests.Tests/ProjectBasedConventions.all_configuration_groups_should_have_optimize_true_if_property_defined.approved.txt new file mode 100644 index 0000000..183a444 --- /dev/null +++ b/TestStack.ConventionTests.Tests/ProjectBasedConventions.all_configuration_groups_should_have_optimize_true_if_property_defined.approved.txt @@ -0,0 +1,4 @@ +'Optimize property in Release|AnyCPU must have a value of true' for 'Project property groups in TestStack.ConventionTests.Tests' +-------------------------------------------------------------------------------------------------------------------------------- + + Optimize:false diff --git a/TestStack.ConventionTests.Tests/ProjectBasedConventions.all_configuration_groups_should_have_platform_AnyCPU.approved.txt b/TestStack.ConventionTests.Tests/ProjectBasedConventions.all_configuration_groups_should_have_platform_AnyCPU.approved.txt new file mode 100644 index 0000000..f8ebe7a --- /dev/null +++ b/TestStack.ConventionTests.Tests/ProjectBasedConventions.all_configuration_groups_should_have_platform_AnyCPU.approved.txt @@ -0,0 +1,4 @@ +'Platform property in Release|x86 must have a value of AnyCPU' for 'Project property groups in TestStack.ConventionTests.Tests' +------------------------------------------------------------------------------------------------------------------------------- + + Platform:x86 diff --git a/TestStack.ConventionTests.Tests/ProjectBasedConventions.cs b/TestStack.ConventionTests.Tests/ProjectBasedConventions.cs index 8775ee9..b2734fd 100644 --- a/TestStack.ConventionTests.Tests/ProjectBasedConventions.cs +++ b/TestStack.ConventionTests.Tests/ProjectBasedConventions.cs @@ -74,5 +74,53 @@ public void scripts_not_embedded_resources_with_approved_exceptions() Convention.IsWithApprovedExeptions(new FilesAreEmbeddedResources(".sql"), project); } + + [Test] + public void release_debug_type_should_be_pdb_only() + { + projectProvider + .LoadProjectDocument(Arg.Any()) + .Returns(XDocument.Parse(Resources.ProjectFileWithReleaseDebugTypeFull)); + + var projectLocator = Substitute.For(); + var propertyGroups = new ProjectPropertyGroups(typeof(ProjectBasedConventions).Assembly, projectProvider, projectLocator); + var ex = + Assert.Throws( + () => Convention.Is(new ConfigurationHasSpecificValue(ConfigurationType.Release, "DebugType", "pdbonly"), propertyGroups)); + + Approvals.Verify(ex.Message); + } + + [Test] + public void all_configuration_groups_should_have_platform_AnyCPU() + { + projectProvider + .LoadProjectDocument(Arg.Any()) + .Returns(XDocument.Parse(Resources.ProjectFileWithReleaseDebugTypeFull)); + + var projectLocator = Substitute.For(); + var propertyGroups = new ProjectPropertyGroups(typeof(ProjectBasedConventions).Assembly, projectProvider, projectLocator); + var ex = + Assert.Throws( + () => Convention.Is(new ConfigurationHasSpecificValue(ConfigurationType.All, "Platform", "AnyCPU"), propertyGroups)); + + Approvals.Verify(ex.Message); + } + + [Test] + public void all_configuration_groups_should_have_optimize_true_if_property_defined() + { + projectProvider + .LoadProjectDocument(Arg.Any()) + .Returns(XDocument.Parse(Resources.ProjectFileWithReleaseDebugTypeFull)); + + var projectLocator = Substitute.For(); + var propertyGroups = new ProjectPropertyGroups(typeof(ProjectBasedConventions).Assembly, projectProvider, projectLocator); + var ex = + Assert.Throws( + () => Convention.Is(new ConfigurationHasSpecificValue(ConfigurationType.All, "Optimize", "true"), propertyGroups)); + + Approvals.Verify(ex.Message); + } } } \ No newline at end of file diff --git a/TestStack.ConventionTests.Tests/ProjectBasedConventions.release_debug_type_should_be_pdb_only.approved.txt b/TestStack.ConventionTests.Tests/ProjectBasedConventions.release_debug_type_should_be_pdb_only.approved.txt new file mode 100644 index 0000000..525c95e --- /dev/null +++ b/TestStack.ConventionTests.Tests/ProjectBasedConventions.release_debug_type_should_be_pdb_only.approved.txt @@ -0,0 +1,9 @@ +'DebugType property in Release|AnyCPU must have a value of pdbonly' for 'Project property groups in TestStack.ConventionTests.Tests' +------------------------------------------------------------------------------------------------------------------------------------ + + DebugType:full + +'DebugType property in Release|x86 must have a value of pdbonly' for 'Project property groups in TestStack.ConventionTests.Tests' +--------------------------------------------------------------------------------------------------------------------------------- + + DebugType:full diff --git a/TestStack.ConventionTests.Tests/Properties/Resources.Designer.cs b/TestStack.ConventionTests.Tests/Properties/Resources.Designer.cs index 37c497c..625521b 100644 --- a/TestStack.ConventionTests.Tests/Properties/Resources.Designer.cs +++ b/TestStack.ConventionTests.Tests/Properties/Resources.Designer.cs @@ -1,7 +1,7 @@ //------------------------------------------------------------------------------ // // This code was generated by a tool. -// Runtime Version:4.0.30319.32559 +// Runtime Version:4.0.30319.34014 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. @@ -94,5 +94,19 @@ internal static string ProjectFileWithInvalidSqlScriptFile { return ResourceManager.GetString("ProjectFileWithInvalidSqlScriptFile", resourceCulture); } } + + /// + /// Looks up a localized string similar to <?xml version="1.0" encoding="utf-8"?> + ///<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + /// <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> + /// <DebugType>full</DebugType> + /// </PropertyGroup> + ///</Project>. + /// + internal static string ProjectFileWithReleaseDebugTypeFull { + get { + return ResourceManager.GetString("ProjectFileWithReleaseDebugTypeFull", resourceCulture); + } + } } } diff --git a/TestStack.ConventionTests.Tests/Properties/Resources.resx b/TestStack.ConventionTests.Tests/Properties/Resources.resx index 74f2e24..1f3e8ab 100644 --- a/TestStack.ConventionTests.Tests/Properties/Resources.resx +++ b/TestStack.ConventionTests.Tests/Properties/Resources.resx @@ -124,4 +124,7 @@ ..\Resources\ProjectFileWithInvalidSqlScriptFile.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252 + + ..\resources\projectfilewithreleasedebugtypefull.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8 + \ No newline at end of file diff --git a/TestStack.ConventionTests.Tests/Resources/ProjectFileWithReleaseDebugTypeFull.txt b/TestStack.ConventionTests.Tests/Resources/ProjectFileWithReleaseDebugTypeFull.txt new file mode 100644 index 0000000..126a645 --- /dev/null +++ b/TestStack.ConventionTests.Tests/Resources/ProjectFileWithReleaseDebugTypeFull.txt @@ -0,0 +1,15 @@ + + + + AnyCPU + + + AnyCPU + full + false + + + x86 + full + + \ No newline at end of file diff --git a/TestStack.ConventionTests.Tests/TestStack.ConventionTests.Tests.csproj b/TestStack.ConventionTests.Tests/TestStack.ConventionTests.Tests.csproj index 09a9d04..fd63170 100644 --- a/TestStack.ConventionTests.Tests/TestStack.ConventionTests.Tests.csproj +++ b/TestStack.ConventionTests.Tests/TestStack.ConventionTests.Tests.csproj @@ -62,6 +62,7 @@ + @@ -104,6 +105,9 @@ + + +