Support for folders in the projects service (#1834)

* Delete folder support + tests

* updating casing

* cleanup
This commit is contained in:
Benjin Dubishar
2023-02-06 11:30:03 -08:00
committed by GitHub
parent f288bee294
commit 4eac02453b
4 changed files with 90 additions and 0 deletions

View File

@@ -0,0 +1,26 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using Microsoft.SqlTools.Hosting.Protocol.Contracts;
using Microsoft.SqlTools.ServiceLayer.Utility;
namespace Microsoft.SqlTools.ServiceLayer.SqlProjects.Contracts
{
/// <summary>
/// Parameters for adding, deleting, or excluding a folder
/// </summary>
public class FolderParams : SqlProjectParams
{
/// <summary>
/// Path of the folder, typically relative to the .sqlproj file
/// </summary>
public string Path { get; set; }
}
public class AddFolderRequest
{
public static readonly RequestType<FolderParams, ResultStatus> Type = RequestType<FolderParams, ResultStatus>.Create("sqlProjects/addFolder");
}
}

View File

@@ -0,0 +1,15 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using Microsoft.SqlTools.Hosting.Protocol.Contracts;
using Microsoft.SqlTools.ServiceLayer.Utility;
namespace Microsoft.SqlTools.ServiceLayer.SqlProjects.Contracts
{
public class DeleteFolderRequest
{
public static readonly RequestType<FolderParams, ResultStatus> Type = RequestType<FolderParams, ResultStatus>.Create("sqlProjects/deleteFolder");
}
}

View File

@@ -50,6 +50,10 @@ namespace Microsoft.SqlTools.ServiceLayer.SqlProjects
serviceHost.SetRequestHandler(DeleteSqlObjectScriptRequest.Type, HandleDeleteSqlObjectScriptRequest, isParallelProcessingSupported: false);
serviceHost.SetRequestHandler(ExcludeSqlObjectScriptRequest.Type, HandleExcludeSqlObjectScriptRequest, isParallelProcessingSupported: false);
// Folder functions
serviceHost.SetRequestHandler(AddFolderRequest.Type, HandleAddFolderRequest, isParallelProcessingSupported: false);
serviceHost.SetRequestHandler(DeleteFolderRequest.Type, HandleDeleteFolderRequest, isParallelProcessingSupported: false);
// SQLCMD variable functions
serviceHost.SetRequestHandler(AddSqlCmdVariableRequest.Type, HandleAddSqlCmdVariableRequest, isParallelProcessingSupported: false);
serviceHost.SetRequestHandler(DeleteSqlCmdVariableRequest.Type, HandleDeleteSqlCmdVariableRequest, isParallelProcessingSupported: false);
@@ -143,6 +147,20 @@ namespace Microsoft.SqlTools.ServiceLayer.SqlProjects
#endregion
#region Folder functions
internal async Task HandleAddFolderRequest(FolderParams requestParams, RequestContext<ResultStatus> requestContext)
{
await RunWithErrorHandling(() => GetProject(requestParams.ProjectUri).Folders.Add(new Folder(requestParams.Path)), requestContext);
}
internal async Task HandleDeleteFolderRequest(FolderParams requestParams, RequestContext<ResultStatus> requestContext)
{
await RunWithErrorHandling(() => GetProject(requestParams.ProjectUri).Folders.Delete(requestParams.Path), requestContext);
}
#endregion
#region SQLCMD variable functions
internal async Task HandleAddSqlCmdVariableRequest(AddSqlCmdVariableParams requestParams, RequestContext<ResultStatus> requestContext)

View File

@@ -252,6 +252,37 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.SqlProjects
Assert.IsFalse(service.Projects[projectUri].DatabaseReferences.Any(x => x is SqlProjectReference), "Database references list expected to not contain the SQL Project reference");
}
[Test]
public async Task TestFolderAddDelete()
{
// Setup
SqlProjectsService service = new();
string projectUri = await service.CreateSqlProject();
Assert.AreEqual(0, service.Projects[projectUri].Folders.Count, "Baseline number of folders");
// Validate adding a folder
MockRequest<ResultStatus> requestMock = new();
FolderParams folderParams = new FolderParams()
{
ProjectUri = projectUri,
Path = "TestFolder"
};
await service.HandleAddFolderRequest(folderParams, requestMock.Object);
requestMock.AssertSuccess(nameof(service.HandleAddFolderRequest));
Assert.AreEqual(1, service.Projects[projectUri].Folders.Count, "Folder count after add");
Assert.IsTrue(Directory.Exists(Path.Join(Path.GetDirectoryName(projectUri), folderParams.Path)), $"Subfolder '{folderParams.Path}' expected to exist on disk");
Assert.IsTrue(service.Projects[projectUri].Folders.Contains(folderParams.Path), $"SqlObjectScripts expected to contain {folderParams.Path}");
// Validate deleting a folder
requestMock = new();
await service.HandleDeleteFolderRequest(folderParams, requestMock.Object);
requestMock.AssertSuccess(nameof(service.HandleDeleteFolderRequest));
Assert.AreEqual(0, service.Projects[projectUri].Folders.Count, "Folder count after delete");
}
[Test]
public async Task TestSqlCmdVariablesAddDelete()
{