Skip to content

Commit

Permalink
Fix for 20741. Allow older versions of tools to pass null args.
Browse files Browse the repository at this point in the history
  • Loading branch information
lajones committed May 1, 2020
1 parent 5ba415e commit c591da4
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 6 deletions.
5 changes: 3 additions & 2 deletions src/EFCore.Design/Design/Internal/DatabaseOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,14 @@ public DatabaseOperations(
Check.NotNull(startupAssembly, nameof(startupAssembly));
Check.NotNull(projectDir, nameof(projectDir));
Check.NotNull(rootNamespace, nameof(rootNamespace));
Check.NotNull(args, nameof(args));
// Note: cannot assert that args is not null - as old versions of
// tools can still pass null.

_reporter = reporter;
_projectDir = projectDir;
_rootNamespace = rootNamespace;
_language = language;
_args = args;
_args = args ?? Array.Empty<string>();

_servicesBuilder = new DesignTimeServicesBuilder(assembly, startupAssembly, reporter, _args);
}
Expand Down
5 changes: 3 additions & 2 deletions src/EFCore.Design/Design/Internal/DbContextOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,13 @@ protected DbContextOperations(
Check.NotNull(reporter, nameof(reporter));
Check.NotNull(assembly, nameof(assembly));
Check.NotNull(startupAssembly, nameof(startupAssembly));
Check.NotNull(args, nameof(args));
// Note: cannot assert that args is not null - as old versions of
// tools can still pass null.

_reporter = reporter;
_assembly = assembly;
_startupAssembly = startupAssembly;
_args = args;
_args = args ?? Array.Empty<string>();
_appServicesFactory = appServicesFactory;
}

Expand Down
5 changes: 3 additions & 2 deletions src/EFCore.Design/Design/Internal/MigrationsOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,15 @@ public MigrationsOperations(
Check.NotNull(startupAssembly, nameof(startupAssembly));
Check.NotNull(projectDir, nameof(projectDir));
Check.NotNull(rootNamespace, nameof(rootNamespace));
Check.NotNull(args, nameof(args));
// Note: cannot assert that args is not null - as old versions of
// tools can still pass null.

_reporter = reporter;
_assembly = assembly;
_projectDir = projectDir;
_rootNamespace = rootNamespace;
_language = language;
_args = args;
_args = args ?? Array.Empty<string>();
_contextOperations = new DbContextOperations(
reporter,
assembly,
Expand Down
41 changes: 41 additions & 0 deletions test/EFCore.Design.Tests/Design/Internal/DatabaseOperationsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using Microsoft.EntityFrameworkCore.TestUtilities;
using Xunit;

namespace Microsoft.EntityFrameworkCore.Design.Internal
{
public class DatabaseOperationsTest
{
[ConditionalFact]
public void Can_pass_null_args()
{
// Even though newer versions of the tools will pass an empty array
// older versions of the tools can pass null args.
var assembly = MockAssembly.Create(typeof(TestContext));
_ = new TestDatabaseOperations(
new TestOperationReporter(),
assembly,
assembly,
"projectDir",
"RootNamespace",
"C#",
args: null);
}

private class TestContext : DbContext
{
public TestContext()
{
throw new Exception("This isn't the constructor you're looking for.");
}

public TestContext(DbContextOptions<TestContext> options)
: base(options)
{
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ public void CreateContext_gets_service_without_AddDbContext()
CreateOperations(typeof(TestProgramWithoutAddDbContext)).CreateContext(typeof(TestContext).FullName);
}

[ConditionalFact]
public void Can_pass_null_args()
{
// Even though newer versions of the tools will pass an empty array
// older versions of the tools can pass null args.
var assembly = MockAssembly.Create(typeof(BaseContext), typeof(DerivedContext), typeof(HierarchyContextFactory));
_ = new TestDbContextOperations(
new TestOperationReporter(),
assembly,
assembly,
args: null,
new TestAppServiceProviderFactory(assembly));
}

[ConditionalFact]
public void CreateContext_uses_exact_factory_method()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using Microsoft.EntityFrameworkCore.TestUtilities;
using Xunit;

namespace Microsoft.EntityFrameworkCore.Design.Internal
{
public class MigrationsOperationsTest
{
[ConditionalFact]
public void Can_pass_null_args()
{
// Even though newer versions of the tools will pass an empty array
// older versions of the tools can pass null args.
var assembly = MockAssembly.Create(typeof(TestContext));
_ = new TestMigrationsOperations(
new TestOperationReporter(),
assembly,
assembly,
"projectDir",
"RootNamespace",
"C#",
args: null);
}

private class TestContext : DbContext
{
public TestContext()
{
throw new Exception("This isn't the constructor you're looking for.");
}

public TestContext(DbContextOptions<TestContext> options)
: base(options)
{
}
}
}
}
23 changes: 23 additions & 0 deletions test/EFCore.Design.Tests/TestUtilities/TestDatabaseOperations.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Reflection;
using Microsoft.EntityFrameworkCore.Design.Internal;

namespace Microsoft.EntityFrameworkCore.TestUtilities
{
public class TestDatabaseOperations : DatabaseOperations
{
public TestDatabaseOperations(
IOperationReporter reporter,
Assembly assembly,
Assembly startupAssembly,
string projectDir,
string rootNamespace,
string language,
string[] args)
: base(reporter, assembly, startupAssembly, projectDir,rootNamespace, language, args)
{
}
}
}
23 changes: 23 additions & 0 deletions test/EFCore.Design.Tests/TestUtilities/TestMigrationsOperations.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Reflection;
using Microsoft.EntityFrameworkCore.Design.Internal;

namespace Microsoft.EntityFrameworkCore.TestUtilities
{
public class TestMigrationsOperations : MigrationsOperations
{
public TestMigrationsOperations(
IOperationReporter reporter,
Assembly assembly,
Assembly startupAssembly,
string projectDir,
string rootNamespace,
string language,
string[] args)
: base(reporter, assembly, startupAssembly, projectDir,rootNamespace, language, args)
{
}
}
}

0 comments on commit c591da4

Please sign in to comment.