Пример #1
0
        public void DontBindToObjectExplorerConnectEvents()
        {
            // when I ask for a non-file object in the workspace, it should return null
            var        workspace = new ServiceLayer.Workspace.Workspace();
            ScriptFile file      = workspace.GetFile("objectexplorer://server;database=database;user=user");

            Assert.Null(file);

            // when I ask for a file, it should return the file
            string tempFile     = Path.GetTempFileName();
            string fileContents = "hello world";

            File.WriteAllText(tempFile, fileContents);

            file = workspace.GetFile(tempFile);
            Assert.Equal(fileContents, file.Contents);

            if (tempFile.StartsWith("/"))
            {
                tempFile = tempFile.Substring(1);
            }
            file = workspace.GetFile("file://" + tempFile);
            Assert.Equal(fileContents, file.Contents);

            file = workspace.GetFileBuffer("untitled://" + tempFile, fileContents);
            Assert.Equal(fileContents, file.Contents);

            // For windows files, just check scheme is null since it's hard to mock file contents in these
            Assert.Null(ServiceLayer.Workspace.Workspace.GetScheme(@"C:\myfile.sql"));
            Assert.Null(ServiceLayer.Workspace.Workspace.GetScheme(@"\\myfile.sql"));
        }
Пример #2
0
        private async Task VerifyFileIsNotAddedOnDocOpened(string filePath)
        {
            // setup test workspace
            var workspace        = new ServiceLayer.Workspace.Workspace();
            var workspaceService = new WorkspaceService <SqlToolsSettings> {
                Workspace = workspace
            };

            // send a document open event with git:/ prefix URI
            var openParams = new DidOpenTextDocumentNotification
            {
                TextDocument = new TextDocumentItem {
                    Uri = filePath
                }
            };

            await workspaceService.HandleDidOpenTextDocumentNotification(openParams, eventContext : null);

            // verify the file is not being tracked by workspace
            Assert.False(workspaceService.Workspace.ContainsFile(filePath));

            // send a close event with git:/ prefix URI
            var closeParams = new DidCloseTextDocumentParams
            {
                TextDocument = new TextDocumentItem {
                    Uri = filePath
                }
            };

            await workspaceService.HandleDidCloseTextDocumentNotification(closeParams, eventContext : null);

            // this is not that interesting validation since the open is ignored
            // the main validation is that close doesn't raise an exception
            Assert.False(workspaceService.Workspace.ContainsFile(filePath));
        }
Пример #3
0
        public void GetFileReturnsNullForPerforceFile()
        {
            // when I ask for a non-file object in the workspace, it should return null
            var        workspace = new ServiceLayer.Workspace.Workspace();
            ScriptFile file      = workspace.GetFile("perforce:myfile.sql");

            Assert.Null(file);
        }
Пример #4
0
 public void ResolveRelativeScriptPath()
 {
     TestUtils.RunIfWindows(() =>
     {
         var workspace = new ServiceLayer.Workspace.Workspace();
         Assert.NotNull(workspace.ResolveRelativeScriptPath(null, @"c:\path\file.sql"));
         Assert.NotNull(workspace.ResolveRelativeScriptPath(@"c:\path\", "file.sql"));
     });
 }
Пример #5
0
 public void GetBaseFilePath()
 {
     TestUtils.RunIfWindows(() =>
     {
         using (var workspace = new ServiceLayer.Workspace.Workspace())
         {
             Assert.Throws <InvalidOperationException>(() => workspace.GetBaseFilePath("path"));
             Assert.NotNull(workspace.GetBaseFilePath(@"c:\path\file.sql"));
             Assert.Equal(workspace.GetBaseFilePath("tsqloutput://c:/path/file.sql"), workspace.WorkspacePath);
         }
     });
 }
Пример #6
0
        public async Task FileClosedSuccessfully()
        {
            // Given:
            // ... A workspace that has a single file open
            var workspace        = new ServiceLayer.Workspace.Workspace();
            var workspaceService = new WorkspaceService <SqlToolsSettings> {
                Workspace = workspace
            };
            var openedFile = workspace.GetFileBuffer(TestObjects.ScriptUri, string.Empty);

            Assert.NotNull(openedFile);
            Assert.NotEmpty(workspace.GetOpenedFiles());

            // ... And there is a callback registered for the file closed event
            ScriptFile closedFile = null;
            string     closedUri  = null;

            workspaceService.RegisterTextDocCloseCallback((u, f, c) =>
            {
                closedUri  = u;
                closedFile = f;
                return(Task.FromResult(true));
            });

            // If:
            // ... An event to close the open file occurs
            var eventContext  = new Mock <EventContext>().Object;
            var requestParams = new DidCloseTextDocumentParams
            {
                TextDocument = new TextDocumentItem {
                    Uri = TestObjects.ScriptUri
                }
            };
            await workspaceService.HandleDidCloseTextDocumentNotification(requestParams, eventContext);

            // Then:
            // ... The file should no longer be in the open files
            Assert.Empty(workspace.GetOpenedFiles());

            // ... The callback should have been called
            // ... The provided script file should be the one we created
            Assert.NotNull(closedFile);
            Assert.Equal(openedFile, closedFile);
            Assert.Equal(TestObjects.ScriptUri, closedUri);
        }
Пример #7
0
        public async Task WorkspaceContainsFile()
        {
            var workspace        = new ServiceLayer.Workspace.Workspace();
            var workspaceService = new WorkspaceService <SqlToolsSettings> {
                Workspace = workspace
            };
            var openedFile = workspace.GetFileBuffer(TestObjects.ScriptUri, string.Empty);

            // send a document open event
            var openParams = new DidOpenTextDocumentNotification
            {
                TextDocument = new TextDocumentItem {
                    Uri = TestObjects.ScriptUri
                }
            };

            await workspaceService.HandleDidOpenTextDocumentNotification(openParams, eventContext : null);

            // verify the file is being tracked by workspace
            Assert.True(workspaceService.Workspace.ContainsFile(TestObjects.ScriptUri));
        }
Пример #8
0
        public async Task FileClosedNotOpen()
        {
            // Given:
            // ... A workspace that has no files open
            var workspace        = new ServiceLayer.Workspace.Workspace();
            var workspaceService = new WorkspaceService <SqlToolsSettings> {
                Workspace = workspace
            };

            Assert.Empty(workspace.GetOpenedFiles());

            // ... And there is a callback registered for the file closed event
            bool callbackCalled = false;

            workspaceService.RegisterTextDocCloseCallback((f, c) =>
            {
                callbackCalled = true;
                return(Task.FromResult(true));
            });

            // If:
            // ... An event to close the a file occurs
            var eventContext  = new Mock <EventContext>().Object;
            var requestParams = new DidCloseTextDocumentParams
            {
                TextDocument = new TextDocumentItem {
                    Uri = TestObjects.ScriptUri
                }
            };
            // Then:
            // ... There should be a file not found exception thrown
            // TODO: This logic should be changed to not create the ScriptFile
            await Assert.ThrowsAnyAsync <IOException>(
                () => workspaceService.HandleDidCloseTextDocumentNotification(requestParams, eventContext));

            // ... There should still be no open files
            // ... The callback should not have been called
            Assert.Empty(workspace.GetOpenedFiles());
            Assert.False(callbackCalled);
        }
        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);
                }
            }
        }