From 32b325fcdf607d876b9ab15a41140e7abb45ae46 Mon Sep 17 00:00:00 2001 From: Bart Koelman Date: Thu, 13 Feb 2020 00:01:27 +0100 Subject: [PATCH 1/3] Fixed another cause for random test failures The problem: NoEntityFrameworkTests project has a reference to UnitTests project, which has a reference to JsonApiDotNetCoreExampleTests project, which has a reference to JsonApiDotNetCoreExample project. The last one contains an appsettings.json file with connection string to JsonApiDotNetCoreExample database. So when building, this file may end up in the output directory of NoEntityFrameworkTests project, overwriting the appsettings.json (containing the connection string to JsonApiDotNetCoreNoEFCoreExample database) from NoEntityFrameworkExample project. Next, if the NoEntityFrameworkTests testset runs first, it populates the JsonApiDotNetCoreExample database from models intended for the JsonApiDotNetCoreNoEFCoreExample database. And this causes subsequent test projects to fail. --- .../NoEntityFrameworkTests.csproj | 1 - .../TestScopedServiceProvider.cs | 28 +++++++++++++++++++ test/NoEntityFrameworkTests/TestStartup.cs | 2 -- 3 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 test/NoEntityFrameworkTests/TestScopedServiceProvider.cs diff --git a/test/NoEntityFrameworkTests/NoEntityFrameworkTests.csproj b/test/NoEntityFrameworkTests/NoEntityFrameworkTests.csproj index 548cb6b048..5b652cb098 100644 --- a/test/NoEntityFrameworkTests/NoEntityFrameworkTests.csproj +++ b/test/NoEntityFrameworkTests/NoEntityFrameworkTests.csproj @@ -15,7 +15,6 @@ - diff --git a/test/NoEntityFrameworkTests/TestScopedServiceProvider.cs b/test/NoEntityFrameworkTests/TestScopedServiceProvider.cs new file mode 100644 index 0000000000..69628ff8f1 --- /dev/null +++ b/test/NoEntityFrameworkTests/TestScopedServiceProvider.cs @@ -0,0 +1,28 @@ +using System; +using JsonApiDotNetCore.Services; +using Microsoft.AspNetCore.Http; +using Moq; + +namespace NoEntityFrameworkTests +{ + public class TestScopedServiceProvider : IScopedServiceProvider + { + private readonly IServiceProvider _serviceProvider; + private Mock _httpContextAccessorMock = new Mock(); + + public TestScopedServiceProvider(IServiceProvider serviceProvider) + { + _serviceProvider = serviceProvider; + } + + public object GetService(Type serviceType) + { + if (serviceType == typeof(IHttpContextAccessor)) + { + return _httpContextAccessorMock.Object; + } + + return _serviceProvider.GetService(serviceType); + } + } +} diff --git a/test/NoEntityFrameworkTests/TestStartup.cs b/test/NoEntityFrameworkTests/TestStartup.cs index 3f0cd1fc4c..ad0022a197 100644 --- a/test/NoEntityFrameworkTests/TestStartup.cs +++ b/test/NoEntityFrameworkTests/TestStartup.cs @@ -2,8 +2,6 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.DependencyInjection; using NoEntityFrameworkExample; -using System; -using UnitTests; namespace NoEntityFrameworkTests { From a94d1e1951b6abdc7b7b50818681b69df3617e7b Mon Sep 17 00:00:00 2001 From: Bart Koelman Date: Wed, 12 Feb 2020 23:05:20 +0100 Subject: [PATCH 2/3] Updated build scripts to use auto-discovery --- Build.ps1 | 15 ++++----------- build.sh | 6 ++---- .../NoEntityFrameworkExample/Startup.cs | 3 ++- src/JsonApiDotNetCore/JsonApiDotNetCore.sln | 17 ----------------- .../JsonApiDotNetCoreExampleTests.sln | 17 ----------------- 5 files changed, 8 insertions(+), 50 deletions(-) delete mode 100644 src/JsonApiDotNetCore/JsonApiDotNetCore.sln delete mode 100644 test/JsonApiDotNetCoreExampleTests/JsonApiDotNetCoreExampleTests.sln diff --git a/Build.ps1 b/Build.ps1 index 60006411a8..d789f62cd0 100644 --- a/Build.ps1 +++ b/Build.ps1 @@ -22,23 +22,16 @@ $revision = @{ $true = $env:APPVEYOR_BUILD_NUMBER; $false = 1 }[$env:APPVEYOR_BU $revision = "{0:D4}" -f [convert]::ToInt32($revision, 10) dotnet restore - -dotnet build ./src/Examples/GettingStarted/GettingStarted.csproj -CheckLastExitCode - -dotnet test ./test/UnitTests/UnitTests.csproj -CheckLastExitCode - -dotnet test ./test/JsonApiDotNetCoreExampleTests/JsonApiDotNetCoreExampleTests.csproj CheckLastExitCode -dotnet test ./test/NoEntityFrameworkTests/NoEntityFrameworkTests.csproj +dotnet build -c Release CheckLastExitCode -dotnet test ./test/DiscoveryTests/DiscoveryTests.csproj +# Workaround for random test failures, to be investigated later. +dotnet test ./test/JsonApiDotNetCoreExampleTests/JsonApiDotNetCoreExampleTests.csproj -c Release --no-build CheckLastExitCode -dotnet build ./src/JsonApiDotNetCore/JsonApiDotNetCore.csproj -c Release +dotnet test -c Release --no-build CheckLastExitCode Write-Output "APPVEYOR_REPO_TAG: $env:APPVEYOR_REPO_TAG" diff --git a/build.sh b/build.sh index 71989c80a7..1edc2ea9a4 100755 --- a/build.sh +++ b/build.sh @@ -4,7 +4,5 @@ set -e dotnet restore - -dotnet test ./test/UnitTests/UnitTests.csproj -dotnet test ./test/JsonApiDotNetCoreExampleTests/JsonApiDotNetCoreExampleTests.csproj -dotnet test ./test/NoEntityFrameworkTests/NoEntityFrameworkTests.csproj +dotnet build -c Release +dotnet test -c Release --no-build diff --git a/src/Examples/NoEntityFrameworkExample/Startup.cs b/src/Examples/NoEntityFrameworkExample/Startup.cs index f21f59c7ba..8f27ed1d7f 100644 --- a/src/Examples/NoEntityFrameworkExample/Startup.cs +++ b/src/Examples/NoEntityFrameworkExample/Startup.cs @@ -1,3 +1,4 @@ +using System; using JsonApiDotNetCore.Extensions; using JsonApiDotNetCore.Services; using Microsoft.AspNetCore.Builder; @@ -42,7 +43,7 @@ public virtual void ConfigureServices(IServiceCollection services) ); services.AddScoped, TodoItemService>(); var optionsBuilder = new DbContextOptionsBuilder(); - optionsBuilder.UseNpgsql(GetDbConnectionString()); + optionsBuilder.UseNpgsql(GetDbConnectionString(), options => options.SetPostgresVersion(new Version(9,6))); services.AddSingleton(Configuration); services.AddSingleton(optionsBuilder.Options); services.AddScoped(); diff --git a/src/JsonApiDotNetCore/JsonApiDotNetCore.sln b/src/JsonApiDotNetCore/JsonApiDotNetCore.sln deleted file mode 100644 index 58b08eb568..0000000000 --- a/src/JsonApiDotNetCore/JsonApiDotNetCore.sln +++ /dev/null @@ -1,17 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 15 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JsonApiDotNetCore", "JsonApiDotNetCore.csproj", "{C8E2AE2E-80E2-408E-89FF-F66BA720F879}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Release|Any CPU = Release|Any CPU - Debug|Any CPU = Debug|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {C8E2AE2E-80E2-408E-89FF-F66BA720F879}.Release|Any CPU.ActiveCfg = Release|Any CPU - {C8E2AE2E-80E2-408E-89FF-F66BA720F879}.Release|Any CPU.Build.0 = Release|Any CPU - {C8E2AE2E-80E2-408E-89FF-F66BA720F879}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {C8E2AE2E-80E2-408E-89FF-F66BA720F879}.Debug|Any CPU.Build.0 = Debug|Any CPU - EndGlobalSection -EndGlobal diff --git a/test/JsonApiDotNetCoreExampleTests/JsonApiDotNetCoreExampleTests.sln b/test/JsonApiDotNetCoreExampleTests/JsonApiDotNetCoreExampleTests.sln deleted file mode 100644 index 87d08fd486..0000000000 --- a/test/JsonApiDotNetCoreExampleTests/JsonApiDotNetCoreExampleTests.sln +++ /dev/null @@ -1,17 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 15.00 -# Visual Studio 2017 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JsonApiDotNetCoreExampleTests", "JsonApiDotNetCoreExampleTests.csproj", "{54F94127-E63E-4ADA-9B20-367EC2F6E3D6}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {54F94127-E63E-4ADA-9B20-367EC2F6E3D6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {54F94127-E63E-4ADA-9B20-367EC2F6E3D6}.Debug|Any CPU.Build.0 = Debug|Any CPU - {54F94127-E63E-4ADA-9B20-367EC2F6E3D6}.Release|Any CPU.ActiveCfg = Release|Any CPU - {54F94127-E63E-4ADA-9B20-367EC2F6E3D6}.Release|Any CPU.Build.0 = Release|Any CPU - EndGlobalSection -EndGlobal From 70574b793ec7d96b33028ceeeb7345eeafde29ce Mon Sep 17 00:00:00 2001 From: Bart Koelman Date: Thu, 13 Feb 2020 19:08:09 +0100 Subject: [PATCH 3/3] Tried another workaround --- Build.ps1 | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/Build.ps1 b/Build.ps1 index d789f62cd0..7cdc82e3d9 100644 --- a/Build.ps1 +++ b/Build.ps1 @@ -27,11 +27,21 @@ CheckLastExitCode dotnet build -c Release CheckLastExitCode -# Workaround for random test failures, to be investigated later. +# Workaround: running 'dotnet test -c Release' fails for yet unknown reasons on AppVeyor, so we run tests one by one. + dotnet test ./test/JsonApiDotNetCoreExampleTests/JsonApiDotNetCoreExampleTests.csproj -c Release --no-build CheckLastExitCode -dotnet test -c Release --no-build +dotnet test ./test/DiscoveryTests/DiscoveryTests.csproj -c Release --no-build +CheckLastExitCode + +dotnet test ./test/IntegrationTests/IntegrationTests.csproj -c Release --no-build +CheckLastExitCode + +dotnet test ./test/UnitTests/UnitTests.csproj -c Release --no-build +CheckLastExitCode + +dotnet test ./test/NoEntityFrameworkTests/NoEntityFrameworkTests.csproj -c Release --no-build CheckLastExitCode Write-Output "APPVEYOR_REPO_TAG: $env:APPVEYOR_REPO_TAG"