Ejemplo n.º 1
0
    public async Task TwoInvocationsProduceTwoDiagnostic()
    {
        // Arrange
        var source      = @"
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
var builder = WebApplication.CreateBuilder(args);
builder.Host.{|#0:ConfigureHostConfiguration(builder => builder.AddJsonFile(""foo.json"", optional: true))|};
builder.WebHost.{|#1:ConfigureAppConfiguration(builder => builder.AddJsonFile(""foo.json"", optional: true))|};
";
        var fixedSource = @"
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
var builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddJsonFile(""foo.json"", optional: true);
builder.Configuration.AddJsonFile(""foo.json"", optional: true);
";

        var expectedDiagnostic = new[] {
            new DiagnosticResult(DiagnosticDescriptors.DisallowConfigureAppConfigureHostBuilder).WithArguments("ConfigureHostConfiguration").WithLocation(0),
            new DiagnosticResult(DiagnosticDescriptors.DisallowConfigureAppConfigureHostBuilder).WithArguments("ConfigureAppConfiguration").WithLocation(1)
        };

        // Assert
        await VerifyCS.VerifyCodeFixAsync(source, expectedDiagnostic, fixedSource);
    }
Ejemplo n.º 2
0
    public async Task ConfigureAppWebHostBuilderProducesDiagnosticInMain()
    {
        // Arrange
        var source      = @"
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
public static class Test
{
    public static void Main(string[]args) {
    var builder = WebApplication.CreateBuilder(args);
    builder.WebHost.{|#0:ConfigureAppConfiguration(builder => builder.AddJsonFile(""foo.json"", optional: true))|};
    }
}
";
        var fixedSource = @"
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
public static class Test
{
    public static void Main(string[]args) {
    var builder = WebApplication.CreateBuilder(args);
    builder.Configuration.AddJsonFile(""foo.json"", optional: true);
    }
}
";

        var expectedDiagnostic = new DiagnosticResult(DiagnosticDescriptors.DisallowConfigureAppConfigureHostBuilder).WithArguments("ConfigureAppConfiguration").WithLocation(0);

        // Assert
        await VerifyCS.VerifyCodeFixAsync(source, expectedDiagnostic, fixedSource);
    }
Ejemplo n.º 3
0
    public async Task ConfigurationBuilderRunsWithoutDiagnostic()
    {
        // Arrange
        var source = @"
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
var builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddJsonFile(""foo.json"", optional: true);
";

        // Assert
        await VerifyCS.VerifyCodeFixAsync(source, source);
    }
Ejemplo n.º 4
0
    public async Task WarnsWhenConfigureAppConfigurationIsCalledWhenChainedWithCreateBuilder()
    {
        //arrange
        var source            = @"
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
WebApplication.CreateBuilder(args).Host.{|#0:ConfigureAppConfiguration((context, builder) => builder.AddJsonFile(""foo.json"", optional: true))|};
";
        var fixedSource       = @"
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
WebApplication.CreateBuilder(args).Configuration.AddJsonFile(""foo.json"", optional: true);
";
        var expectedDiagnosis = new DiagnosticResult(DiagnosticDescriptors.DisallowConfigureAppConfigureHostBuilder).WithArguments("ConfigureAppConfiguration").WithLocation(0);
        await VerifyCS.VerifyCodeFixAsync(source, expectedDiagnosis, fixedSource);
    }
Ejemplo n.º 5
0
    public async Task TwoMethodsInArgumentsProducesTwoProperties()
    {
        // Arrange
        var source      = @"
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
var builder = WebApplication.CreateBuilder(args);
builder.Host.{|#0:ConfigureAppConfiguration((context, builder) => { builder.AddJsonFile(""foo.json"", optional: true); builder.AddEnvironmentVariables();})|};
";
        var fixedSource = @"
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
var builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddJsonFile(""foo.json"", optional: true).AddEnvironmentVariables();
";

        var expectedDiagnostic = new DiagnosticResult(DiagnosticDescriptors.DisallowConfigureAppConfigureHostBuilder).WithArguments("ConfigureAppConfiguration").WithLocation(0);

        // Assert
        await VerifyCS.VerifyCodeFixAsync(source, expectedDiagnostic, fixedSource);
    }