Invoke-SqlCmd handles ":!!if" in inconsistent way (PS5 vs PS6) (#806)

* Invoke-SqlCmd handles ":!!if" in inconsistent way (PS5 vs PS6)

Fix: Set wordBounday to false for "!!" in Lexer.cs

* Issue : Invoke-SqlCmd handles ":!!if" in inconsistent way (PS5 vs PS6)
TFS: http://sqlbuvsts01:8080/Main/SQL%20Server/_workitems#id=12817630&triage=true&_a=edit
Fix:
1) Wrote two test cases in BatchParserTests.cs
This commit is contained in:
NiranjanVirtuosity
2019-05-14 03:39:21 +05:30
committed by Karl Burtram
parent 5392f81d54
commit 2e2b764c6d
2 changed files with 142 additions and 90 deletions

View File

@@ -43,7 +43,8 @@ namespace Microsoft.SqlTools.ServiceLayer.BatchParser
/// </summary>
public Token CurrentToken
{
get {
get
{
return currentToken;
}
}
@@ -83,7 +84,7 @@ namespace Microsoft.SqlTools.ServiceLayer.BatchParser
if (CurrentTokenType == LexerTokenType.Eof)
{
popInputAtNextConsume = true;
if(inputStack.Count > 0)
if (inputStack.Count > 0)
{
// report as empty NewLine token
currentToken = new Token(
@@ -645,91 +646,108 @@ namespace Microsoft.SqlTools.ServiceLayer.BatchParser
{
Consume(); // colon
if (TryAccept("reset", true))
{
SetToken(LexerTokenType.Reset);
return true;
}
else if (TryAccept("ed", true))
{
SetToken(LexerTokenType.Ed);
return true;
}
else if (TryAccept("!!", true))
/*
TryAccept(text: "!!", wordBoundary: false)
----------------------
Reason:
* The behavior is differ from SqlCmd and current implementation.
* Example:
* !!Dir (without whitespace)
* Command: Invoke-Sqlcmd -Query ':!!if exist foo.txt del foo.txt'
* Result : Incorrect syntax near ':'. (InCorrect)
* !! DIR (with whitespace)
* Command: Invoke-Sqlcmd -Query ':!! if exist foo.txt del foo.txt'
* Result : Command Execute is not supported. (Correct)
* TryAccept return false , if we pass true to parameter wordBoundary and LexerTokenType will set to Text instead of Execute.
* So by passing wordBoundary as false, I will get TryAccept as true and LexerTokenType will set to Execute.
*/
if (TryAccept(text: "!!", wordBoundary: false))
{
SetToken(LexerTokenType.Execute);
return true;
}
else if (TryAccept("quit", true))
{
SetToken(LexerTokenType.Quit);
return true;
}
else if (TryAccept("exit", true))
{
SetToken(LexerTokenType.Exit);
return true;
}
else if (TryAccept("r", true))
{
SetToken(LexerTokenType.Include);
return true;
}
else if (TryAccept("serverlist", true))
{
SetToken(LexerTokenType.Serverlist);
return true;
}
else if (TryAccept("setvar", true))
{
SetToken(LexerTokenType.Setvar);
return true;
}
else if (TryAccept("list", true))
{
SetToken(LexerTokenType.List);
return true;
}
else if (TryAccept("error", true))
{
SetToken(LexerTokenType.ErrorCommand);
return true;
}
else if (TryAccept("out", true))
{
SetToken(LexerTokenType.Out);
return true;
}
else if (TryAccept("perftrace", true))
{
SetToken(LexerTokenType.Perftrace);
return true;
}
else if (TryAccept("connect", true))
else if (TryAccept(text: "connect", wordBoundary: true))
{
SetToken(LexerTokenType.Connect);
return true;
}
else if (TryAccept("on error", true))
else if (TryAccept(text: "ed", wordBoundary: true))
{
SetToken(LexerTokenType.OnError);
SetToken(LexerTokenType.Ed);
return true;
}
else if (TryAccept("help", true))
else if (TryAccept(text: "error", wordBoundary: true))
{
SetToken(LexerTokenType.ErrorCommand);
return true;
}
else if (TryAccept(text: "exit", wordBoundary: true))
{
SetToken(LexerTokenType.Exit);
return true;
}
else if (TryAccept(text: "help", wordBoundary: true))
{
SetToken(LexerTokenType.Help);
return true;
}
else if (TryAccept("xml", true))
else if (TryAccept(text: "list", wordBoundary: true))
{
SetToken(LexerTokenType.Xml);
SetToken(LexerTokenType.List);
return true;
}
else if (TryAccept("listvar", true))
else if (TryAccept(text: "listvar", wordBoundary: true))
{
SetToken(LexerTokenType.ListVar);
return true;
}
else if (TryAccept(text: "on error", wordBoundary: true))
{
SetToken(LexerTokenType.OnError);
return true;
}
else if (TryAccept(text: "out", wordBoundary: true))
{
SetToken(LexerTokenType.Out);
return true;
}
else if (TryAccept(text: "perftrace", wordBoundary: true))
{
SetToken(LexerTokenType.Perftrace);
return true;
}
else if (TryAccept(text: "quit", wordBoundary: true))
{
SetToken(LexerTokenType.Quit);
return true;
}
else if (TryAccept(text: "r", wordBoundary: true))
{
SetToken(LexerTokenType.Include);
return true;
}
else if (TryAccept(text: "reset", wordBoundary: true))
{
SetToken(LexerTokenType.Reset);
return true;
}
else if (TryAccept(text: "serverlist", wordBoundary: true))
{
SetToken(LexerTokenType.Serverlist);
return true;
}
else if (TryAccept(text: "setvar", wordBoundary: true))
{
SetToken(LexerTokenType.Setvar);
return true;
}
else if (TryAccept(text: "xml", wordBoundary: true))
{
SetToken(LexerTokenType.Xml);
return true;
}
return false;
}

View File

@@ -3,17 +3,17 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using System;
using System.Data.SqlClient;
using System.Globalization;
using System.IO;
using System.Text;
using Microsoft.SqlTools.ManagedBatchParser.IntegrationTests.TSQLExecutionEngine;
using Microsoft.SqlTools.ManagedBatchParser.IntegrationTests.Utility;
using Microsoft.SqlTools.ServiceLayer.BatchParser;
using Microsoft.SqlTools.ServiceLayer.BatchParser.ExecutionEngineCode;
using Microsoft.SqlTools.ServiceLayer.Test.Common;
using Microsoft.SqlTools.ServiceLayer.Test.Common.Baselined;
using System;
using System.Data.SqlClient;
using System.Globalization;
using System.IO;
using System.Text;
using Xunit;
namespace Microsoft.SqlTools.ManagedBatchParser.UnitTests.BatchParser
@@ -122,7 +122,6 @@ namespace Microsoft.SqlTools.ManagedBatchParser.UnitTests.BatchParser
}
}
// Verify the execution by passing long value , Except a exception.
[Fact]
public void VerifyInvalidNumber()
@@ -223,7 +222,6 @@ namespace Microsoft.SqlTools.ManagedBatchParser.UnitTests.BatchParser
{
try
{
string query = ":SETVAR a 10";
var inputStream = GenerateStreamFromString(query);
using (Lexer lexer = new Lexer(new StreamReader(inputStream), "Test.sql"))
@@ -232,15 +230,52 @@ namespace Microsoft.SqlTools.ManagedBatchParser.UnitTests.BatchParser
}
executionResult = ScriptExecutionResult.Success;
}
catch (Exception ex)
catch (Exception)
{
executionResult = ScriptExecutionResult.Failure;
}
// we doesn't expect any exception or testCase failures
Assert.Equal<ScriptExecutionResult>(ScriptExecutionResult.Success, executionResult);
}
// This test case is to verify that, Powershell's Invoke-SqlCmd handles ":!!if" in an inconsistent way
// Inconsistent way means, instead of throwing an exception as "Command Execute is not supported." it was throwing "Incorrect syntax near ':'."
[Fact]
public void VerifySqlCmdExecute()
{
string query = ":!!if exist foo.txt del foo.txt";
var inputStream = GenerateStreamFromString(query);
TestCommandHandler handler = new TestCommandHandler(new StringBuilder());
IVariableResolver resolver = new TestVariableResolver(new StringBuilder());
using (Parser p = new Parser(
handler,
resolver,
new StringReader(query),
"test"))
{
p.ThrowOnUnresolvedVariable = true;
handler.SetParser(p);
var exception = Assert.Throws<BatchParserException>(() => p.Parse());
// Verify the message should be "Command Execute is not supported."
Assert.Equal("Command Execute is not supported.", exception.Message);
}
}
// This test case is to verify that, Lexer type for :!!If was set to "Text" instead of "Execute"
[Fact]
public void VerifyLexerTypeOfSqlCmdIFisExecute()
{
string query = ":!!if exist foo.txt del foo.txt";
var inputStream = GenerateStreamFromString(query);
LexerTokenType type = LexerTokenType.None;
using (Lexer lexer = new Lexer(new StreamReader(inputStream), "Test.sql"))
{
lexer.ConsumeToken();
type = lexer.CurrentTokenType;
}
// we are expecting the lexer type should to be Execute.
Assert.Equal("Execute", type.ToString());
}
// Verify the custom exception functionality by raising user defined error.
@@ -299,7 +334,6 @@ namespace Microsoft.SqlTools.ManagedBatchParser.UnitTests.BatchParser
TestExecutor testExecutor = new TestExecutor(query, con, new ExecutionEngineConditions());
testExecutor.Run();
Assert.True(testExecutor.ResultCountQueue.Count >= 1);
}
}
}
@@ -318,10 +352,10 @@ namespace Microsoft.SqlTools.ManagedBatchParser.UnitTests.BatchParser
executionEngine.BatchParserExecutionFinished += OnBatchParserExecutionFinished;
executionEngine.ExecuteBatch(new ScriptExecutionArgs(query, con, 15, new ExecutionEngineConditions(), new BatchParserMockEventHandler()));
Assert.Equal(ScriptExecutionResult.Success, executionResult);
}
}
}
// Capture the event once batch finish execution.
private void OnBatchParserExecutionFinished(object sender, BatchParserExecutionFinishedEventArgs e)
{