Skip to content

Commit

Permalink
Conventional ❤ Linux (#68)
Browse files Browse the repository at this point in the history
* Ensures KnownPaths operates in an OS independent way
* Adds database test category for filtering out db tests on linux build
* Fixes test assembly path specification
* Makes Directory extension methods to be OS agnostic
* Makes assertions for files OS agnostic
* Makes path separators in lots of places OS agnostic
* Makes project file inclusion inspection OS agnostic
* More project file inclusion fixes
* More test fixing
* Makes Cecil AssemblyResolver work for Linux
* Make test assertions more resilient to environment differences
* Fix ThisAssembly test assertion to be OS agnostic
  • Loading branch information
andrewabest committed Jan 10, 2020
1 parent 96fb2b9 commit e4e463d
Show file tree
Hide file tree
Showing 13 changed files with 109 additions and 87 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using Conventional.Extensions;
using FluentAssertions;
using NUnit.Framework;

Expand All @@ -15,7 +17,7 @@ public class AssemblyConventionSpecificationTests
public void Setup()
{
_testAssembly = Assembly.LoadFrom(KnownPaths.SolutionRoot +
"TestSolution/TestSolution.TestProject/bin/Debug/TestSolution.TestProject.dll");
$"TestSolution{Path.DirectorySeparatorChar}TestSolution.TestProject{Path.DirectorySeparatorChar}bin{Path.DirectorySeparatorChar}Debug{Path.DirectorySeparatorChar}TestSolution.TestProject.dll");
}

[Test]
Expand Down Expand Up @@ -51,6 +53,7 @@ public void MustHaveFilesWithACertainExtensionBeResources_Success_FileExtension(
.BeTrue();
}

[Test]
public void MustHaveFilesWithACertainExtensionBeResources_Success_RegEx()
{
var matchResxFiles = new Regex(@"\.RESX$", RegexOptions.IgnoreCase);
Expand All @@ -65,36 +68,24 @@ public void MustHaveFilesWithACertainExtensionBeResources_Success_RegEx()
[Test]
public void MustHaveFilesWithACertainExtensionBeResources_FailsWhenFilesAreNotResources_Regex()
{
var expectedFailureMessage =
@"All files matching '.\.txt' within assembly 'Conventional.Tests' must have their build action set to 'Embedded Resource'"
+ Environment.NewLine
+ @"- Conventional\Conventions\Assemblies\non_embedded_text_file_first.txt"
+ Environment.NewLine
+ @"- Conventional\Conventions\Assemblies\non_embedded_text_file_second.txt";

var result = typeof(AssemblyConventionSpecificationTests).Assembly
.MustConformTo(Convention.MustHaveFilesBeResources(new Regex(@".\.txt")));

result.IsSatisfied.Should().BeFalse();
result.Failures.Single().Should().Be(expectedFailureMessage);
result.Failures.Single().Should().Contain("non_embedded_text_file_first.txt");
result.Failures.Single().Should().Contain("non_embedded_text_file_second.txt");
}


[Test]
public void MustHaveFilesWithACertainExtensionBeResources_FailsWhenFilesAreNotResources_FileExtension()
{
var expectedFailureMessage =
@"All files matching '*.txt' within assembly 'Conventional.Tests' must have their build action set to 'Embedded Resource'"
+ Environment.NewLine
+ @"- Conventional\Conventions\Assemblies\non_embedded_text_file_first.txt"
+ Environment.NewLine
+ @"- Conventional\Conventions\Assemblies\non_embedded_text_file_second.txt";

var result = typeof(AssemblyConventionSpecificationTests).Assembly
.MustConformTo(Convention.MustHaveFilesBeResources("*.txt"));

result.IsSatisfied.Should().BeFalse();
result.Failures.Single().Should().Be(expectedFailureMessage);
result.Failures.Single().Should().Contain("non_embedded_text_file_first.txt");
result.Failures.Single().Should().Contain("non_embedded_text_file_second.txt");
}


Expand Down Expand Up @@ -135,38 +126,26 @@ public void MustHaveFilesWithACertainExtensionBeEmbeddedResources_Success_RegEx(
public void
MustHaveFilesWithACertainExtensionBeEmbeddedResources_FailsWhenFilesAreNotEmbeddedResources_FileExtension()
{
var expectedFailureMessage =
"All files matching '*.txt' within assembly 'Conventional.Tests' must have their build action set to 'Embedded Resource'"
+ Environment.NewLine
+ @"- Conventional\Conventions\Assemblies\non_embedded_text_file_first.txt"
+ Environment.NewLine
+ @"- Conventional\Conventions\Assemblies\non_embedded_text_file_second.txt";

var result = typeof(AssemblyConventionSpecificationTests).Assembly
.MustConformTo(Convention.MustHaveFilesBeEmbeddedResources("*.txt"));

result.IsSatisfied.Should().BeFalse();
result.Failures.Single().Should().Be(expectedFailureMessage);
result.Failures.Single().Should().Contain("non_embedded_text_file_first.txt");
result.Failures.Single().Should().Contain("non_embedded_text_file_second.txt");
}


[Test]
public void MustHaveFilesWithACertainExtensionBeEmbeddedResources_FailsWhenFilesAreNotEmbeddedResources_RegEx()
{
var expectedFailureMessage =
"All files matching '.*NON_EMBEDDED.*' within assembly 'Conventional.Tests' must have their build action set to 'Embedded Resource'"
+ Environment.NewLine
+ @"- Conventional\Conventions\Assemblies\non_embedded_text_file_first.txt"
+ Environment.NewLine
+ @"- Conventional\Conventions\Assemblies\non_embedded_text_file_second.txt";

var matchNonEmbeddedRegEx = new Regex(".*NON_EMBEDDED.*", RegexOptions.IgnoreCase);

var result = typeof(AssemblyConventionSpecificationTests).Assembly
.MustConformTo(Convention.MustHaveFilesBeEmbeddedResources(matchNonEmbeddedRegEx));

result.IsSatisfied.Should().BeFalse();
result.Failures.Single().Should().Be(expectedFailureMessage);
result.Failures.Single().Should().Contain("non_embedded_text_file_first.txt");
result.Failures.Single().Should().Contain("non_embedded_text_file_second.txt");
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using Conventional.Conventions.Assemblies;
Expand All @@ -16,7 +17,7 @@ public void Setup()
{
_testAssembly =
Assembly.LoadFrom(KnownPaths.SolutionRoot +
"TestSolution/TestSolution.TestProject/bin/Debug/TestSolution.TestProject.dll");
$"TestSolution{Path.DirectorySeparatorChar}TestSolution.TestProject{Path.DirectorySeparatorChar}bin{Path.DirectorySeparatorChar}Debug{Path.DirectorySeparatorChar}TestSolution.TestProject.dll");
}

[Test]
Expand Down Expand Up @@ -56,9 +57,8 @@ public void MustIncludeAllMatchingFilesInFolder_ProducesAppropriateErrorMessage(
{
var result = _testAssembly.MustConformTo(Convention.MustIncludeAllMatchingFilesInFolder("*.js"));
var failureText = result.Failures.Single();
failureText.Should().Contain(@"All files matching '*.js' within ");
failureText.Should().Contain(@"\TestSolution\TestSolution.TestProject' must be included in the project.");
failureText.Should().Contain(@"\TestSolution\TestSolution.TestProject\Scripts\unincludedJsFile.js");
failureText.Should().Contain("TestSolution.TestProject");
failureText.Should().Contain("unincludedJsFile.js");
failureText.Split(new[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries).Length.Should().Be(2);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public void Teardown()
}

[Test]
[Category("Database")]
public void AllCheckConstraintsMustBeNamedConventionalSpecification_Success()
{
ExecuteSqlScriptFromResource("AllCheckConstraintsMustBeNamedConventionalSpecification_Success.sql");
Expand All @@ -53,6 +54,7 @@ public void AllCheckConstraintsMustBeNamedConventionalSpecification_Success()
}

[Test]
[Category("Database")]
public void AllCheckConstraintsMustBeNamedConventionalSpecification_Fail()
{
ExecuteSqlScriptFromResource("AllCheckConstraintsMustBeNamedConventionalSpecification_Fail.sql");
Expand All @@ -66,6 +68,7 @@ public void AllCheckConstraintsMustBeNamedConventionalSpecification_Fail()
}

[Test]
[Category("Database")]
public void AllDefaultConstraintsMustBeNamedConventionalSpecification_Success()
{
ExecuteSqlScriptFromResource("AllDefaultConstraintsMustBeNamedConventionalSpecification_Success.sql");
Expand All @@ -79,6 +82,7 @@ public void AllDefaultConstraintsMustBeNamedConventionalSpecification_Success()
}

[Test]
[Category("Database")]
public void AllDefaultConstraintsMustBeNamedConventionalSpecification_Fail()
{
ExecuteSqlScriptFromResource("AllDefaultConstraintsMustBeNamedConventionalSpecification_Fail.sql");
Expand All @@ -92,6 +96,7 @@ public void AllDefaultConstraintsMustBeNamedConventionalSpecification_Fail()
}

[Test]
[Category("Database")]
public void AllPrimaryKeyConstraintsMustBeNamedConventionalSpecification_Success()
{
ExecuteSqlScriptFromResource("AllPrimaryKeyConstraintsMustBeNamedConventionalSpecification_Success.sql");
Expand All @@ -105,6 +110,7 @@ public void AllPrimaryKeyConstraintsMustBeNamedConventionalSpecification_Success
}

[Test]
[Category("Database")]
public void AllPrimaryKeyConstraintsMustBeNamedConventionalSpecification_Fail()
{
ExecuteSqlScriptFromResource("AllPrimaryKeyConstraintsMustBeNamedConventionalSpecification_Fail.sql");
Expand All @@ -118,6 +124,7 @@ public void AllPrimaryKeyConstraintsMustBeNamedConventionalSpecification_Fail()
}

[Test]
[Category("Database")]
public void AllReferenceConstraintsMustBeNamedConventionalSpecification_Success()
{
ExecuteSqlScriptFromResource("AllReferenceConstraintsMustBeNamedConventionalSpecification_Success.sql");
Expand All @@ -131,6 +138,7 @@ public void AllReferenceConstraintsMustBeNamedConventionalSpecification_Success(
}

[Test]
[Category("Database")]
public void AllReferenceConstraintsMustBeNamedConventionalSpecification_Fail()
{
ExecuteSqlScriptFromResource("AllReferenceConstraintsMustBeNamedConventionalSpecification_Fail.sql");
Expand All @@ -144,6 +152,7 @@ public void AllReferenceConstraintsMustBeNamedConventionalSpecification_Fail()
}

[Test]
[Category("Database")]
public void AllUniqueConstraintsMustBeNamedConventionalSpecification_Success()
{
ExecuteSqlScriptFromResource("AllUniqueConstraintsMustBeNamedConventionalSpecification_Success.sql");
Expand All @@ -157,6 +166,7 @@ public void AllUniqueConstraintsMustBeNamedConventionalSpecification_Success()
}

[Test]
[Category("Database")]
public void AllUniqueConstraintsMustBeNamedConventionalSpecification_Fail()
{
ExecuteSqlScriptFromResource("AllUniqueConstraintsMustBeNamedConventionalSpecification_Fail.sql");
Expand All @@ -170,6 +180,7 @@ public void AllUniqueConstraintsMustBeNamedConventionalSpecification_Fail()
}

[Test]
[Category("Database")]
public void AllNamedColumnsMustBeNullableConventionSpecification_Success()
{
ExecuteSqlScriptFromResource("AllNamedColumnsMustBeNullableConventionalSpecification_Success.sql");
Expand All @@ -183,6 +194,7 @@ public void AllNamedColumnsMustBeNullableConventionSpecification_Success()
}

[Test]
[Category("Database")]
public void AllNamedColumnsMustBeNonNullableConventionSpecification_Fails()
{
ExecuteSqlScriptFromResource("AllNamedColumnsMustBeNullableConventionalSpecification_Fail.sql");
Expand All @@ -196,6 +208,7 @@ public void AllNamedColumnsMustBeNonNullableConventionSpecification_Fails()
}

[Test]
[Category("Database")]
public void AllNamedColumnsMustBeNonNullableConventionSpecification_Success()
{
ExecuteSqlScriptFromResource("AllNamedColumnsMustBeNullableConventionalSpecification_Success.sql");
Expand All @@ -209,6 +222,7 @@ public void AllNamedColumnsMustBeNonNullableConventionSpecification_Success()
}

[Test]
[Category("Database")]
public void AllNamedColumnsMustBeNullableConventionSpecification_Fails()
{
ExecuteSqlScriptFromResource("AllNamedColumnsMustBeNullableConventionalSpecification_Fail.sql");
Expand All @@ -222,6 +236,7 @@ public void AllNamedColumnsMustBeNullableConventionSpecification_Fails()
}

[Test]
[Category("Database")]
public void AllIdentityColumnsMustBeNamedTableNameIdConventionSpecification_Success()
{
ExecuteSqlScriptFromResource("AllIdentityColumnsMustBeNamedTableNameIdConventionSpecificationSuccess.sql");
Expand All @@ -235,6 +250,7 @@ public void AllIdentityColumnsMustBeNamedTableNameIdConventionSpecification_Succ
}

[Test]
[Category("Database")]
public void AllIdentityColumnsMustBeNamedTableNameIdConventionSpecification_FailsWhenIdentityColumnIsNotNamedId()
{
ExecuteSqlScriptFromResource("AllIdentityColumnsMustBeNamedTableNameIdConventionSpecificationFailure.sql");
Expand All @@ -248,6 +264,7 @@ public void AllIdentityColumnsMustBeNamedTableNameIdConventionSpecification_Fail
}

[Test]
[Category("Database")]
public void AllTablesMustHaveAClusteredIndex_Success()
{
ExecuteSqlScriptFromResource("TablesWithoutClusteredIndexSuccess.sql");
Expand All @@ -261,6 +278,7 @@ public void AllTablesMustHaveAClusteredIndex_Success()
}

[Test]
[Category("Database")]
public void AllTablesMustHaveAClusteredIndex_Failure()
{
ExecuteSqlScriptFromResource("TablesWithoutClusteredIndexFailure.sql");
Expand All @@ -274,6 +292,7 @@ public void AllTablesMustHaveAClusteredIndex_Failure()
}

[Test]
[Category("Database")]
public void EachRowMustHaveACorrespondingEnum_Success()
{
ExecuteSqlScriptFromResource("EachRowMustHaveACorrespondingEnum_Success.sql");
Expand All @@ -287,6 +306,7 @@ public void EachRowMustHaveACorrespondingEnum_Success()
}

[Test]
[Category("Database")]
public void EachRowMustHaveACorrespondingEnum_Fail()
{
ExecuteSqlScriptFromResource("EachRowMustHaveACorrespondingEnum_Fail.sql");
Expand All @@ -308,19 +328,15 @@ private void ExecuteSqlScriptFromResource(string resourceName)
{
if (stream == null) { throw new MissingManifestResourceException(fullResourceName); }

using (var reader = new StreamReader(stream))
{
script = reader.ReadToEnd();
}
using var reader = new StreamReader(stream);
script = reader.ReadToEnd();
}

using (IDbConnection dbConnection = new SqlConnection(_settings.ConnectionString))
{
dbConnection.Open();
var command = dbConnection.CreateCommand();
command.CommandText = script;
command.ExecuteNonQuery();
}
using var dbConnection = new SqlConnection(_settings.ConnectionString);
dbConnection.Open();
var command = dbConnection.CreateCommand();
command.CommandText = script;
command.ExecuteNonQuery();
}

private void CreateDatabase()
Expand All @@ -329,13 +345,11 @@ private void CreateDatabase()
var dbName = sb.InitialCatalog;
sb.InitialCatalog = "master";

using (IDbConnection dbConnection = new SqlConnection(sb.ConnectionString))
{
dbConnection.Open();
var command = dbConnection.CreateCommand();
command.CommandText = SqlScripts.CreateDb(dbName);
command.ExecuteNonQuery();
}
using var dbConnection = new SqlConnection(sb.ConnectionString);
dbConnection.Open();
var command = dbConnection.CreateCommand();
command.CommandText = SqlScripts.CreateDb(dbName);
command.ExecuteNonQuery();
}

private void DropDatabase()
Expand All @@ -344,13 +358,11 @@ private void DropDatabase()
var dbName = sb.InitialCatalog;
sb.InitialCatalog = "master";

using (IDbConnection dbConnection = new SqlConnection(sb.ConnectionString))
{
dbConnection.Open();
var command = dbConnection.CreateCommand();
command.CommandText = SqlScripts.DropDb(dbName);
command.ExecuteNonQuery();
}
using var dbConnection = new SqlConnection(sb.ConnectionString);
dbConnection.Open();
var command = dbConnection.CreateCommand();
command.CommandText = SqlScripts.DropDb(dbName);
command.ExecuteNonQuery();
}
}
}
Loading

0 comments on commit e4e463d

Please sign in to comment.