Skip to content

Commit

Permalink
SNOW-1545648: Fix PUT command error if file path contains spaces and …
Browse files Browse the repository at this point in the history
…single quotes
  • Loading branch information
sfc-gh-ext-simba-lf committed Nov 28, 2024
1 parent 296a281 commit 3feb537
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
17 changes: 16 additions & 1 deletion Snowflake.Data.Tests/UnitTests/SFFileTransferAgentTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* Copyright (c) 2012-2023 Snowflake Computing Inc. All rights reserved.
*/

Expand Down Expand Up @@ -69,6 +69,10 @@ class SFFileTransferAgentTest : SFBaseTest
// Mock file content
const string FileContent = "FTAFileContent";

// Mock file paths
const string FilePathWithoutSpaces = "C:/Users/Test/folder_without_space/*.*";
const string FilePathWithSpaces = "C:/Users/Test/folder with space/*.*";

[SetUp]
public void BeforeEachTest()
{
Expand Down Expand Up @@ -634,5 +638,16 @@ public void TestDownloadThrowsErrorDirectoryNotFound()
Assert.IsInstanceOf<DirectoryNotFoundException>(innerException);
Assert.That(innerException?.Message, Does.Match("Could not find a part of the path .*"));
}

[Test]
[TestCase("PUT file://" + FilePathWithoutSpaces + " @TestStage", FilePathWithoutSpaces)]
[TestCase("PUT file://" + FilePathWithSpaces + " @TestStage", FilePathWithSpaces)]
[TestCase("PUT 'file://" + FilePathWithoutSpaces + "' @TestStage", FilePathWithoutSpaces)]
[TestCase("PUT 'file://" + FilePathWithSpaces + "' @TestStage", FilePathWithSpaces)]
public void TestGetFilePathFromPutCommand(string query, string expectedFilePath)
{
var actualFilePath = SFFileTransferAgent.GetFilePathFromPutCommand(query);
Assert.AreEqual(expectedFilePath, actualFilePath);
}
}
}
14 changes: 10 additions & 4 deletions Snowflake.Data/Core/FileTransfer/SFFileTransferAgent.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* Copyright (c) 2021 Snowflake Computing Inc. All rights reserved.
*/

Expand Down Expand Up @@ -451,7 +451,7 @@ private void updatePresignedUrl()
{
foreach (SFFileMetadata fileMeta in FilesMetas)
{
string filePathToReplace = getFilePathFromPutCommand(Query);
string filePathToReplace = GetFilePathFromPutCommand(Query);
string fileNameToReplaceWith = fileMeta.destFileName;
string queryWithSingleFile = Query;
queryWithSingleFile = queryWithSingleFile.Replace(filePathToReplace, fileNameToReplaceWith);
Expand Down Expand Up @@ -492,7 +492,7 @@ internal async Task updatePresignedUrlAsync(CancellationToken cancellationToken)
{
foreach (SFFileMetadata fileMeta in FilesMetas)
{
string filePathToReplace = getFilePathFromPutCommand(Query);
string filePathToReplace = GetFilePathFromPutCommand(Query);

Check warning on line 495 in Snowflake.Data/Core/FileTransfer/SFFileTransferAgent.cs

View check run for this annotation

Codecov / codecov/patch

Snowflake.Data/Core/FileTransfer/SFFileTransferAgent.cs#L495

Added line #L495 was not covered by tests
string fileNameToReplaceWith = fileMeta.destFileName;
string queryWithSingleFile = Query;
queryWithSingleFile = queryWithSingleFile.Replace(filePathToReplace, fileNameToReplaceWith);
Expand Down Expand Up @@ -527,13 +527,19 @@ internal async Task updatePresignedUrlAsync(CancellationToken cancellationToken)
/// </summary>
/// <param name="query">The query containing the file path</param>
/// <returns>The file path contained by the query</returns>
private string getFilePathFromPutCommand(string query)
internal static string GetFilePathFromPutCommand(string query)
{
// Extract file path from PUT command:
// E.g. "PUT file://C:<path-to-file> @DB.SCHEMA.%TABLE;"
int startIndex = query.IndexOf("file://") + "file://".Length;
int endIndex = query.Substring(startIndex).IndexOf('@') - 1;
string filePath = query.Substring(startIndex, endIndex);

// Check if file path contains an enclosing (') char
if (filePath[filePath.Length - 1] == '\'')
{
filePath = filePath.Substring(0, filePath.Length - 1);
}
return filePath;
}

Expand Down

0 comments on commit 3feb537

Please sign in to comment.