Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SNOW-1545648: Fix PUT command error if file path contains spaces and single quotes #1066

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,33 @@ public class FileUploadDownloadLargeFilesIT : SFBaseTest
private static readonly string s_fullFileName = Path.Combine(s_localFolderName, FileName);
private static readonly string s_fullDownloadedFileName = Path.Combine(s_downloadFolderName, FileName);
private static readonly MD5 s_md5 = MD5.Create();

[OneTimeSetUp]
public static void GenerateLargeFileForTests()
{
CreateLocalDirectory(s_localFolderName);
GenerateLargeFile(s_fullFileName);
}

[OneTimeTearDown]
public static void DeleteGeneratedLargeFile()
{
RemoveLocalFile(s_fullFileName);
RemoveDirectory(s_localFolderName);
}

[Test]
public void TestThatUploadsAndDownloadsTheSameFile()
{
// act
UploadFile(s_fullFileName, s_remoteFolderName);
DownloadFile(s_remoteFolderName, s_downloadFolderName, FileName);

// assert
Assert.AreEqual(
CalcualteMD5(s_fullFileName),
CalcualteMD5(s_fullDownloadedFileName));

// cleanup
RemoveFilesFromServer(s_remoteFolderName);
RemoveLocalFile(s_fullDownloadedFileName);
Expand Down Expand Up @@ -85,7 +85,7 @@ private void DownloadFile(string remoteFolderName, string downloadFolderName, st
command.ExecuteNonQuery();
}
}

private void RemoveFilesFromServer(string remoteFolderName)
{
using (var conn = new SnowflakeDbConnection())
Expand All @@ -108,7 +108,7 @@ private static string CalcualteMD5(string fullFileName)
}

private static void RemoveLocalFile(string fullFileName) => File.Delete(fullFileName);

private static void CreateLocalDirectory(string path) => Directory.CreateDirectory(path);

private static void RemoveDirectory(string path) => Directory.Delete(path, true);
Expand Down
49 changes: 43 additions & 6 deletions Snowflake.Data.Tests/IntegrationTests/SFPutGetTest.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 @@ -560,6 +560,29 @@
}
}

[Test]
public void TestPutFileWithSpaceAndSingleQuote()
sfc-gh-knozderko marked this conversation as resolved.
Show resolved Hide resolved
{
var absolutePathPrefix = Path.Combine($"{Path.GetTempPath()}{Guid.NewGuid()} file path with space");
sfc-gh-knozderko marked this conversation as resolved.
Show resolved Hide resolved
var files = new List<string> {
$"{absolutePathPrefix}_one.csv",
$"{absolutePathPrefix}_two.csv",
$"{absolutePathPrefix}_three.csv"
};
PrepareFileData(files);

// Set the PUT query variables
t_inputFilePath = $"{absolutePathPrefix}*";
t_internalStagePath = $"@{t_schemaName}.{t_stageName}";

using (var conn = new SnowflakeDbConnection(ConnectionString))
{
conn.Open();
PutFile(conn, "", ResultStatus.UPLOADED, true);
VerifyFilesAreUploaded(conn, files, t_internalStagePath);
}
}

private void PrepareTest(string sourceFileCompressionType, StageType stageType, string stagePath,
bool autoCompress, bool clientEncryption = true)
{
Expand Down Expand Up @@ -610,16 +633,30 @@
string PutFile(
SnowflakeDbConnection conn,
String additionalAttribute = "",
ResultStatus expectedStatus = ResultStatus.UPLOADED)
ResultStatus expectedStatus = ResultStatus.UPLOADED,
bool encloseInSingleQuotes = false)
{
string queryId;
using (var command = conn.CreateCommand())
{
// Prepare PUT query
string putQuery =
$"PUT file://{t_inputFilePath} {t_internalStagePath}" +
$" AUTO_COMPRESS={(t_autoCompress ? "TRUE" : "FALSE")}" +
$" {additionalAttribute}";
string putQuery;
if (encloseInSingleQuotes)
{
// Enclosing the file path in single quotes require forward slash
t_inputFilePath = t_inputFilePath.Replace("\\", "/");
putQuery =
$"PUT 'file://{t_inputFilePath}' {t_internalStagePath}" +
$" AUTO_COMPRESS={(t_autoCompress ? "TRUE" : "FALSE")}";
}
else
{
putQuery =
$"PUT file://{t_inputFilePath} {t_internalStagePath}" +
$" AUTO_COMPRESS={(t_autoCompress ? "TRUE" : "FALSE")}" +
$" {additionalAttribute}";
}

// Upload file
command.CommandText = putQuery;
var reader = command.ExecuteReader();
Expand Down Expand Up @@ -764,7 +801,7 @@
{
var bytes = new byte[stream.Length];
stream.Position = 0;
stream.Read(bytes, 0, (int) stream.Length);

Check warning on line 804 in Snowflake.Data.Tests/IntegrationTests/SFPutGetTest.cs

View workflow job for this annotation

GitHub Actions / Tests on Linux (net6.0, AZURE)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 804 in Snowflake.Data.Tests/IntegrationTests/SFPutGetTest.cs

View workflow job for this annotation

GitHub Actions / Tests on Linux (net6.0, AZURE)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 804 in Snowflake.Data.Tests/IntegrationTests/SFPutGetTest.cs

View workflow job for this annotation

GitHub Actions / Tests on Linux (net6.0, GCP)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 804 in Snowflake.Data.Tests/IntegrationTests/SFPutGetTest.cs

View workflow job for this annotation

GitHub Actions / Tests on Linux (net6.0, GCP)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 804 in Snowflake.Data.Tests/IntegrationTests/SFPutGetTest.cs

View workflow job for this annotation

GitHub Actions / Tests on Linux (net6.0, AWS)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 804 in Snowflake.Data.Tests/IntegrationTests/SFPutGetTest.cs

View workflow job for this annotation

GitHub Actions / Tests on Linux (net7.0, AZURE)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 804 in Snowflake.Data.Tests/IntegrationTests/SFPutGetTest.cs

View workflow job for this annotation

GitHub Actions / Tests on Linux (net6.0, AWS)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 804 in Snowflake.Data.Tests/IntegrationTests/SFPutGetTest.cs

View workflow job for this annotation

GitHub Actions / Tests on Linux (net7.0, AZURE)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 804 in Snowflake.Data.Tests/IntegrationTests/SFPutGetTest.cs

View workflow job for this annotation

GitHub Actions / Tests on Linux (net8.0, AZURE)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 804 in Snowflake.Data.Tests/IntegrationTests/SFPutGetTest.cs

View workflow job for this annotation

GitHub Actions / Tests on Linux (net8.0, AZURE)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 804 in Snowflake.Data.Tests/IntegrationTests/SFPutGetTest.cs

View workflow job for this annotation

GitHub Actions / Tests on Linux (net7.0, AWS)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 804 in Snowflake.Data.Tests/IntegrationTests/SFPutGetTest.cs

View workflow job for this annotation

GitHub Actions / Tests on MAC (net6.0, AZURE)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 804 in Snowflake.Data.Tests/IntegrationTests/SFPutGetTest.cs

View workflow job for this annotation

GitHub Actions / Tests on Linux (net7.0, AWS)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 804 in Snowflake.Data.Tests/IntegrationTests/SFPutGetTest.cs

View workflow job for this annotation

GitHub Actions / Tests on Linux (net8.0, AWS)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 804 in Snowflake.Data.Tests/IntegrationTests/SFPutGetTest.cs

View workflow job for this annotation

GitHub Actions / Tests on MAC (net6.0, AZURE)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 804 in Snowflake.Data.Tests/IntegrationTests/SFPutGetTest.cs

View workflow job for this annotation

GitHub Actions / Tests on Linux (net8.0, AWS)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 804 in Snowflake.Data.Tests/IntegrationTests/SFPutGetTest.cs

View workflow job for this annotation

GitHub Actions / Tests on Linux (net9.0, AWS)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 804 in Snowflake.Data.Tests/IntegrationTests/SFPutGetTest.cs

View workflow job for this annotation

GitHub Actions / Tests on Linux (net9.0, AWS)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 804 in Snowflake.Data.Tests/IntegrationTests/SFPutGetTest.cs

View workflow job for this annotation

GitHub Actions / Tests on Linux (net8.0, GCP)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 804 in Snowflake.Data.Tests/IntegrationTests/SFPutGetTest.cs

View workflow job for this annotation

GitHub Actions / Tests on Linux (net8.0, GCP)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 804 in Snowflake.Data.Tests/IntegrationTests/SFPutGetTest.cs

View workflow job for this annotation

GitHub Actions / Tests on MAC (net6.0, AWS)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 804 in Snowflake.Data.Tests/IntegrationTests/SFPutGetTest.cs

View workflow job for this annotation

GitHub Actions / Tests on Linux (net7.0, GCP)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 804 in Snowflake.Data.Tests/IntegrationTests/SFPutGetTest.cs

View workflow job for this annotation

GitHub Actions / Tests on Linux (net7.0, GCP)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 804 in Snowflake.Data.Tests/IntegrationTests/SFPutGetTest.cs

View workflow job for this annotation

GitHub Actions / Tests on MAC (net6.0, AWS)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 804 in Snowflake.Data.Tests/IntegrationTests/SFPutGetTest.cs

View workflow job for this annotation

GitHub Actions / Tests on Linux (net9.0, AZURE)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 804 in Snowflake.Data.Tests/IntegrationTests/SFPutGetTest.cs

View workflow job for this annotation

GitHub Actions / Tests on Linux (net9.0, AZURE)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 804 in Snowflake.Data.Tests/IntegrationTests/SFPutGetTest.cs

View workflow job for this annotation

GitHub Actions / Tests on MAC (net6.0, GCP)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 804 in Snowflake.Data.Tests/IntegrationTests/SFPutGetTest.cs

View workflow job for this annotation

GitHub Actions / Tests on MAC (net6.0, GCP)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 804 in Snowflake.Data.Tests/IntegrationTests/SFPutGetTest.cs

View workflow job for this annotation

GitHub Actions / Tests on MAC (net7.0, GCP)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 804 in Snowflake.Data.Tests/IntegrationTests/SFPutGetTest.cs

View workflow job for this annotation

GitHub Actions / Tests on MAC (net7.0, AZURE)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 804 in Snowflake.Data.Tests/IntegrationTests/SFPutGetTest.cs

View workflow job for this annotation

GitHub Actions / Tests on MAC (net7.0, GCP)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 804 in Snowflake.Data.Tests/IntegrationTests/SFPutGetTest.cs

View workflow job for this annotation

GitHub Actions / Tests on MAC (net7.0, AZURE)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 804 in Snowflake.Data.Tests/IntegrationTests/SFPutGetTest.cs

View workflow job for this annotation

GitHub Actions / Tests on MAC (net8.0, AZURE)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 804 in Snowflake.Data.Tests/IntegrationTests/SFPutGetTest.cs

View workflow job for this annotation

GitHub Actions / Tests on MAC (net8.0, AZURE)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 804 in Snowflake.Data.Tests/IntegrationTests/SFPutGetTest.cs

View workflow job for this annotation

GitHub Actions / Tests on MAC (net9.0, GCP)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 804 in Snowflake.Data.Tests/IntegrationTests/SFPutGetTest.cs

View workflow job for this annotation

GitHub Actions / Tests on MAC (net9.0, GCP)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 804 in Snowflake.Data.Tests/IntegrationTests/SFPutGetTest.cs

View workflow job for this annotation

GitHub Actions / Tests on MAC (net8.0, AWS)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 804 in Snowflake.Data.Tests/IntegrationTests/SFPutGetTest.cs

View workflow job for this annotation

GitHub Actions / Tests on MAC (net8.0, GCP)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 804 in Snowflake.Data.Tests/IntegrationTests/SFPutGetTest.cs

View workflow job for this annotation

GitHub Actions / Tests on MAC (net8.0, AWS)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 804 in Snowflake.Data.Tests/IntegrationTests/SFPutGetTest.cs

View workflow job for this annotation

GitHub Actions / Tests on MAC (net8.0, GCP)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 804 in Snowflake.Data.Tests/IntegrationTests/SFPutGetTest.cs

View workflow job for this annotation

GitHub Actions / Tests on MAC (net9.0, AWS)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 804 in Snowflake.Data.Tests/IntegrationTests/SFPutGetTest.cs

View workflow job for this annotation

GitHub Actions / Tests on MAC (net9.0, AWS)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 804 in Snowflake.Data.Tests/IntegrationTests/SFPutGetTest.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net9.0, GCP)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 804 in Snowflake.Data.Tests/IntegrationTests/SFPutGetTest.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net9.0, GCP)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 804 in Snowflake.Data.Tests/IntegrationTests/SFPutGetTest.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net9.0, AZURE)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 804 in Snowflake.Data.Tests/IntegrationTests/SFPutGetTest.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net9.0, AZURE)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 804 in Snowflake.Data.Tests/IntegrationTests/SFPutGetTest.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net9.0, AWS)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 804 in Snowflake.Data.Tests/IntegrationTests/SFPutGetTest.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net9.0, AWS)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 804 in Snowflake.Data.Tests/IntegrationTests/SFPutGetTest.cs

View workflow job for this annotation

GitHub Actions / Tests on MAC (net7.0, AWS)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 804 in Snowflake.Data.Tests/IntegrationTests/SFPutGetTest.cs

View workflow job for this annotation

GitHub Actions / Tests on MAC (net7.0, AWS)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 804 in Snowflake.Data.Tests/IntegrationTests/SFPutGetTest.cs

View workflow job for this annotation

GitHub Actions / Tests on MAC (net9.0, AZURE)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 804 in Snowflake.Data.Tests/IntegrationTests/SFPutGetTest.cs

View workflow job for this annotation

GitHub Actions / Tests on MAC (net9.0, AZURE)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 804 in Snowflake.Data.Tests/IntegrationTests/SFPutGetTest.cs

View workflow job for this annotation

GitHub Actions / Tests on Linux (net9.0, GCP)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 804 in Snowflake.Data.Tests/IntegrationTests/SFPutGetTest.cs

View workflow job for this annotation

GitHub Actions / Tests on Linux (net9.0, GCP)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)
return Encoding.UTF8.GetString(bytes).Split('\n');
}

Expand Down
36 changes: 35 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
static readonly string s_filePathWithoutSpaces = Path.Combine("C:\\Users\\Test\\", "folder_without_space", "*.*");
static readonly string s_filePathWithSpaces = Path.Combine("C:\\Users\\Test\\", "folder with space", "*.*");

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

[Test]
public void TestGetFilePathWithoutSpacesFromPutCommand()
{
TestGetFilePathFromPutCommand("PUT file://" + s_filePathWithoutSpaces + " @TestStage", s_filePathWithoutSpaces);
}
sfc-gh-knozderko marked this conversation as resolved.
Show resolved Hide resolved

[Test]
public void TestGetFilePathWithSpacesFromPutCommand()
{
TestGetFilePathFromPutCommand("PUT file://" + s_filePathWithSpaces + " @TestStage", s_filePathWithSpaces);
}

[Test]
public void TestGetFilePathWithoutSpacesAndWithSingleQuotesFromPutCommand()
{
TestGetFilePathFromPutCommand("PUT 'file://" + s_filePathWithoutSpaces + "' @TestStage", s_filePathWithoutSpaces);
}

[Test]
public void TestGetFilePathWithSpacesAndWithSingleQuotesFromPutCommand()
{
TestGetFilePathFromPutCommand("PUT 'file://" + s_filePathWithSpaces + "' @TestStage", s_filePathWithSpaces);
}

public void TestGetFilePathFromPutCommand(string query, string expectedFilePath)
{
var actualFilePath = SFFileTransferAgent.getFilePathFromPutCommand(query);
Assert.AreEqual(expectedFilePath, actualFilePath);
}
}
sfc-gh-knozderko marked this conversation as resolved.
Show resolved Hide resolved
}
10 changes: 8 additions & 2 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 @@ -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)
{
sfc-gh-knozderko marked this conversation as resolved.
Show resolved Hide resolved
// 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
Loading