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

Disable cake diagnostics endpoint for requests that don't specify a file #1107

Merged
merged 6 commits into from
Feb 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# Changelog
All changes to the project will be documented in this file.

## [1.29.1] - Not Yet Released
* Fixed duplicate diagnostics in C# ([omnisharp-vscode#1830](https://github.com/OmniSharp/omnisharp-vscode/issues/1830), PR: [#1107](https://github.com/OmniSharp/omnisharp-roslyn/pull/1107))

## [1.29.0] - 2018-1-29
* Updated to Roslyn 2.6.1 packages - C# 7.2 support, (PR: [#1055](https://github.com/OmniSharp/omnisharp-roslyn/pull/1055))
* Updated to Roslyn 2.6.1 packages - C# 7.2 support (PR: [#1055](https://github.com/OmniSharp/omnisharp-roslyn/pull/1055))
* Shipped Language Server Protocol support in box. (PR: [#969](https://github.com/OmniSharp/omnisharp-roslyn/pull/969))
- Additional information and features tracked at [#968](https://github.com/OmniSharp/omnisharp-roslyn/issues/968)
* Fixed locating Visual Studio with more than one installation (PR: [#1063](https://github.com/OmniSharp/omnisharp-roslyn/pull/1063))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,15 @@ public virtual async Task<TResponse> Handle(TRequest request)

request = await TranslateRequestAsync(request);

var response = await service.Handle(request);
var response = IsValid(request)
Copy link
Member

Choose a reason for hiding this comment

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

Maybe check validity before TranslateRequest?

? await service.Handle(request)
: default(TResponse);

return await TranslateResponse(response, request);
}

public virtual bool IsValid(TRequest request) => true;

protected virtual async Task<TRequest> TranslateRequestAsync(TRequest req)
{
var request = req as Request;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Composition;
using System.Threading.Tasks;
using OmniSharp.Mef;
using OmniSharp.Models;
using OmniSharp.Models.CodeCheck;
Expand All @@ -14,5 +15,8 @@ public CodeCheckHandler(
: base(workspace)
{
}

public override bool IsValid(CodeCheckRequest request) =>
!string.IsNullOrEmpty(request.FileName);
}
}
72 changes: 72 additions & 0 deletions tests/OmniSharp.Cake.Tests/CodeCheckFacts.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System.IO;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using OmniSharp.Cake.Services.RequestHandlers.Diagnostics;
using OmniSharp.Models;
using OmniSharp.Models.CodeCheck;
using OmniSharp.Models.UpdateBuffer;
using TestUtility;
using Xunit;
using Xunit.Abstractions;

namespace OmniSharp.Cake.Tests
{
public class CodeCheckFacts : CakeSingleRequestHandlerTestFixture<CodeCheckHandler>
{
private readonly ILogger _logger;

public CodeCheckFacts(ITestOutputHelper testOutput) : base(testOutput)
{
_logger = LoggerFactory.CreateLogger<AutoCompleteFacts>();
}

protected override string EndpointName => OmniSharpEndpoints.CodeCheck;

[Fact]
public async Task ShouldProvideDiagnosticsIfRequestContainsCakeFileName()
{
const string input = @"zzz";

var diagnostics = await FindDiagnostics(input, includeFileName: true);
Assert.NotEmpty(diagnostics.QuickFixes);
}

[Fact]
public async Task ShouldNotCallCodeCheckServiceIfRequestDoesNotSpecifyFileName()
{
const string input = @"zzz$$";

var diagnostics = await FindDiagnostics(input, includeFileName: false);
Assert.Null(diagnostics);
}

private async Task<QuickFixResponse> FindDiagnostics(string contents, bool includeFileName)
{
using (var testProject = await TestAssets.Instance.GetTestProjectAsync("CakeProject", shadowCopy : false))
using (var host = CreateOmniSharpHost(testProject.Directory))
{
var testFile = new TestFile(Path.Combine(testProject.Directory, "build.cake"), contents);

var request = new CodeCheckRequest
{
FileName = includeFileName ? testFile.FileName : string.Empty,
};

var updateBufferRequest = new UpdateBufferRequest
{
Buffer = testFile.Content.Code,
Column = request.Column,
FileName = testFile.FileName,
Line = request.Line,
FromDisk = false
};

await GetUpdateBufferHandler(host).Handle(updateBufferRequest);

var requestHandler = GetRequestHandler(host);

return await requestHandler.Handle(request);
}
}
}
}