Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Azure.ClientSdk.Analyzers to fix incorrect behaviors #6496

Closed
wants to merge 15 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -10,84 +10,88 @@ namespace Azure.ClientSdk.Analyzers.Tests
public class AZC0002Tests
{
[Fact]
public async Task AZC0002ProducedForMethodsWithoutCancellationToken()
public async Task AZC0002ProducedForMethodsWithoutCancellationTokenOrRequestContext()
{
const string code = @"
using Azure;
using System.Threading.Tasks;

namespace RandomNamespace
{
public class SomeClient
{
public virtual Task {|AZC0002:GetAsync|}()
public virtual Task<Response> {|AZC0002:GetAsync|}()
{
return null;
}

public virtual void {|AZC0002:Get|}()
public virtual Response {|AZC0002:Get|}()
{
return null;
}
}
}";
await Verifier.CreateAnalyzer(code)
.WithDisabledDiagnostics("AZC0015")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious: does this enable AZC0015 for these tests?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It just means we don't need to suppress AZC0015. That's it.

AZC0015 is not effective now.

.RunAsync();
}

[Fact]
public async Task AZC0002ProducedForMethodsWithNonOptionalCancellationToken()
public async Task AZC0002ProducedForMethodsWithWrongNameCancellationToken()
{
const string code = @"
using Azure;
using System.Threading;
using System.Threading.Tasks;

namespace RandomNamespace
{
public class SomeClient
{
public virtual Task {|AZC0002:GetAsync|}(CancellationToken cancellationToken)
public virtual Task<Response> {|AZC0002:GetAsync|}(CancellationToken cancellation = default)
{
return null;
}

public virtual void {|AZC0002:Get|}(CancellationToken cancellationToken)
public virtual Response {|AZC0002:Get|}(CancellationToken cancellation = default)
{
return null;
}
}
}";
await Verifier.CreateAnalyzer(code)
.WithDisabledDiagnostics("AZC0015")
.RunAsync();
}

[Fact]
public async Task AZC0002ProducedForMethodsWithWrongNameParameter()
public async Task AZC0002ProducedForMethodsWithWrongNameRequestContext()
{
const string code = @"
using Azure;
using Azure.Core;
using System.Threading;
using System.Threading.Tasks;

namespace RandomNamespace
{
public class SomeClient
{
public virtual Task {|AZC0002:GetAsync|}(CancellationToken cancellation = default)
public virtual Task<Response> {|AZC0002:GetAsync|}(RequestContext cancellation = default)
{
return null;
}

public virtual void {|AZC0002:Get|}(CancellationToken cancellation = default)
public virtual Response {|AZC0002:Get|}(RequestContext cancellation = default)
{
return null;
}
}
}";
await Verifier.CreateAnalyzer(code)
.WithDisabledDiagnostics("AZC0015")
.RunAsync();
}

[Fact]
public async Task AZC0002ProducedForMethodsWhereRequestContextIsNotLast()
public async Task AZC0002ProducedForMethodsWithNonOptionalCancellationToken()
{
const string code = @"
using Azure;
Expand All @@ -98,58 +102,50 @@ namespace RandomNamespace
{
public class SomeClient
{
public virtual Task {|AZC0002:GetAsync|}(RequestContext context = default, string text = default)
public virtual Task<Response> {|AZC0002:GetAsync|}(CancellationToken cancellationToken)
{
return null;
}

public virtual void {|AZC0002:Get|}(RequestContext context = default, string text = default)
public virtual Response {|AZC0002:Get|}(CancellationToken cancellationToken)
{
return null;
}
}
}";
await Verifier.CreateAnalyzer(code)
.WithDisabledDiagnostics("AZC0015")
.RunAsync();
}

[Fact]
public async Task AZC0002DoesntFireIfThereIsAnOverloadWithCancellationToken()
public async Task AZC0002ProducedForMethodsWhereRequestContextIsNotLast()
{
const string code = @"
using Azure;
using System.Threading;
using System.Threading.Tasks;

namespace RandomNamespace
{
public class SomeClient
{
public virtual Task GetAsync(string s)
public virtual Task<Response> {|AZC0002:GetAsync|}(RequestContext context = default, string text = default)
{
return null;
}

public virtual void Get(string s)
{
}

public virtual Task GetAsync(string s, CancellationToken cancellationToken)
public virtual Response {|AZC0002:Get|}(RequestContext context = default, string text = default)
{
return null;
}

public virtual void Get(string s, CancellationToken cancellationToken)
{
}
}
}";
await Verifier.CreateAnalyzer(code)
.WithDisabledDiagnostics("AZC0015")
.RunAsync();
}
}

[Fact]
public async Task AZC0002DoesntFireIfThereIsAnOverloadWithRequestContext()
public async Task AZC0002ProducedForMethodsWhereCancellationTokenIsNotLast()
{
const string code = @"
using Azure;
Expand All @@ -160,91 +156,107 @@ namespace RandomNamespace
{
public class SomeClient
{
public virtual Task GetAsync(string s)
public virtual Task<Response> {|AZC0002:GetAsync|}(CancellationToken cancellationToken = default, string text = default)
{
return null;
}

public virtual void Get(string s)
{
}

public virtual Task GetAsync(string s, RequestContext context = default)
public virtual Response {|AZC0002:Get|}(CancellationToken cancellationToken = default, string text = default)
{
return null;
}

public virtual void Get(string s, RequestContext context = default)
{
}
}
}";
await Verifier.CreateAnalyzer(code)
.WithDisabledDiagnostics("AZC0015")
.RunAsync();
}

[Fact]
public async Task AZC0002ProducedWhenCancellationTokenOverloadsDontMatch()
public async Task AZC0002NotProducedForMethodsWithCancellationToken()
{
const string code = @"
using Azure;
using System.Threading;
using System.Threading.Tasks;

namespace RandomNamespace
{
public class SomeClient
{
public virtual Task {|AZC0002:GetAsync|}(string s)
public virtual Task<Response> GetAsync(CancellationToken cancellationToken = default)
{
return null;
}

public virtual void {|AZC0002:Get|}(string s)
{
}

public virtual Task {|AZC0002:GetAsync|}(CancellationToken cancellationToken)
public virtual Response Get(CancellationToken cancellationToken = default)
{
return null;
}

public virtual void {|AZC0002:Get|}(CancellationToken cancellationToken)
{
}
}
}";
await Verifier.CreateAnalyzer(code)
.WithDisabledDiagnostics("AZC0015")
.RunAsync();
}

}
[Fact]
public async Task AZC0002NotProducedForMethodsWithCancellationToken()
public async Task AZC0002NotProducedForMethodsWithRequestContextAndCancellationToken()
{
const string code = @"
using Azure;
using System.Threading;
using System.Threading.Tasks;

namespace RandomNamespace
{
public class SomeClient
{
public virtual Task GetAsync(CancellationToken cancellationToken = default)
public virtual Task<Response> Get1Async(string s, CancellationToken cancellationToken = default)
{
return null;
}

public virtual void Get(CancellationToken cancellationToken = default)
public virtual Response Get1(string s, CancellationToken cancellationToken = default)
{
return null;
}

public virtual Task<Response> Get1Async(string s, RequestContext context)
{
return null;
}

public virtual Response Get1(string s, RequestContext context)
{
return null;
}

public virtual Task<Response> Get2Async(CancellationToken cancellationToken = default)
{
return null;
}

public virtual Response Get2(CancellationToken cancellationToken = default)
{
return null;
}

public virtual Task<Response> Get2Async(RequestContext context)
{
return null;
}

public virtual Response Get2(RequestContext context)
{
return null;
}
}
}";
await Verifier.CreateAnalyzer(code)
.WithDisabledDiagnostics("AZC0015")
.WithDisabledDiagnostics("AZC0018")
.WithDisabledDiagnostics("AD0001")
.RunAsync();
}
}

[Fact]
public async Task AZC0002NotProducedForMethodsWithRequestContext()
{
Expand All @@ -257,19 +269,67 @@ namespace RandomNamespace
{
public class SomeClient
{
public virtual Task GetAsync(RequestContext context = default)
public virtual Task<Response> GetAsync(RequestContext context = default)
{
return null;
}

public virtual Response Get(RequestContext context = default)
{
return null;
}

public virtual void Get(RequestContext context = default)
public virtual Task<Response> Get2Async(RequestContext context)
{
return null;
}

public virtual Response Get2(RequestContext context)
{
return null;
}
}
}";
await Verifier.CreateAnalyzer(code)
.WithDisabledDiagnostics("AZC0015")
.WithDisabledDiagnostics("AZC0018")
.RunAsync();
}

[Fact]
public async Task AZC0002NotProducedIfThereIsAnOverloadWithCancellationToken()
{
const string code = @"
using Azure;
using System.Threading;
using System.Threading.Tasks;

namespace RandomNamespace
{
public class SomeClient
{
public virtual Task<Response> GetAsync(string s)
{
return null;
}

public virtual Response Get(string s)
{
return null;
}

public virtual Task<Response> GetAsync(string s, CancellationToken cancellationToken)
{
return null;
}

public virtual Response Get(string s, CancellationToken cancellationToken)
{
return null;
}
}
}";
await Verifier.CreateAnalyzer(code)
.RunAsync();
}
}
}
Loading