Feature/sqlcmd : Enable running scripts with SQLCMD variables - Part 1 (#839)

* Part1 : Changes to make cmdcmd script to work with parameters in script

* Stop SQL intellisense for SQLCMD

* Adding test for Intellisense handling of SQLCMD page

* Removing unintentional spacing changes caused by formatting

* Updating with smaller CR comments. Will discuss regarding script vs other options in batch info

* Removing unintentional change

* Adding latest PR comments
This commit is contained in:
Udeesha Gautam
2019-08-09 14:25:47 -07:00
committed by GitHub
parent d42e3626cb
commit 68145d5e7c
9 changed files with 177 additions and 24 deletions

View File

@@ -17,6 +17,10 @@ using Microsoft.SqlTools.ServiceLayer.LanguageServices.Contracts;
using Microsoft.SqlTools.ServiceLayer.Test.Common;
using Microsoft.SqlTools.ServiceLayer.UnitTests.ServiceHost;
using Microsoft.SqlTools.ServiceLayer.Workspace.Contracts;
using Microsoft.SqlTools.ServiceLayer.Workspace;
using Microsoft.SqlTools.ServiceLayer.SqlContext;
using Microsoft.SqlTools.ServiceLayer.Connection;
using Microsoft.SqlTools.ServiceLayer.Connection.Contracts;
using Moq;
using Xunit;
@@ -329,5 +333,73 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.LanguageServer
testDb.Cleanup();
}
}
/// <summary>
// This test validates switching off editor intellisesnse for now.
// Will change to better handling once we have specific SQLCMD intellisense in Language Service
/// </summary>
[Fact]
public async Task HandleRequestToChangeToSqlcmdFile()
{
var scriptFile = new ScriptFile() { ClientFilePath = "HandleRequestToChangeToSqlcmdFile_" + DateTime.Now.ToLongDateString() + "_.sql" };
try
{
// Prepare a script file
scriptFile.SetFileContents("koko wants a bananas");
File.WriteAllText(scriptFile.ClientFilePath, scriptFile.Contents);
// Create a workspace and add file to it so that its found for intellense building
var workspace = new ServiceLayer.Workspace.Workspace();
var workspaceService = new WorkspaceService<SqlToolsSettings> { Workspace = workspace };
var langService = new LanguageService() { WorkspaceServiceInstance = workspaceService };
langService.CurrentWorkspace.GetFile(scriptFile.ClientFilePath);
langService.CurrentWorkspaceSettings.SqlTools.IntelliSense.EnableIntellisense = true;
// Add a connection to ensure the intellisense building works
ConnectionInfo connectionInfo = GetLiveAutoCompleteTestObjects().ConnectionInfo;
langService.ConnectionServiceInstance.OwnerToConnectionMap.Add(scriptFile.ClientFilePath, connectionInfo);
// Test SQL
int countOfValidationCalls = 0;
var eventContextSql = new Mock<SqlTools.Hosting.Protocol.EventContext>();
eventContextSql.Setup(x => x.SendEvent(PublishDiagnosticsNotification.Type, It.Is<PublishDiagnosticsNotification>((notif) => ValidateNotification(notif, 2, ref countOfValidationCalls)))).Returns(Task.FromResult(new object()));
await langService.HandleDidChangeLanguageFlavorNotification(new LanguageFlavorChangeParams
{
Uri = scriptFile.ClientFilePath,
Language = LanguageService.SQL_LANG.ToLower(),
Flavor = "MSSQL"
}, eventContextSql.Object);
await langService.DelayedDiagnosticsTask; // to ensure completion and validation before moveing to next step
// Test SQL CMD
var eventContextSqlCmd = new Mock<SqlTools.Hosting.Protocol.EventContext>();
eventContextSqlCmd.Setup(x => x.SendEvent(PublishDiagnosticsNotification.Type, It.Is<PublishDiagnosticsNotification>((notif) => ValidateNotification(notif, 0, ref countOfValidationCalls)))).Returns(Task.FromResult(new object()));
await langService.HandleDidChangeLanguageFlavorNotification(new LanguageFlavorChangeParams
{
Uri = scriptFile.ClientFilePath,
Language = LanguageService.SQL_CMD_LANG.ToLower(),
Flavor = "MSSQL"
}, eventContextSqlCmd.Object);
await langService.DelayedDiagnosticsTask;
Assert.True(countOfValidationCalls == 2, $"Validation should be called 2 time but is called {countOfValidationCalls} times");
}
finally
{
if (File.Exists(scriptFile.ClientFilePath))
{
File.Delete(scriptFile.ClientFilePath);
}
}
}
private bool ValidateNotification(PublishDiagnosticsNotification notif, int errors, ref int countOfValidationCalls)
{
countOfValidationCalls++;
Assert.True(notif.Diagnostics.Length == errors, $"Notification errors {notif.Diagnostics.Length} are not as expected {errors}");
return true;
}
}
}