mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-14 01:25:40 -05:00
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:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user