Skip to content

Commit

Permalink
SNOW-723810: Fix case where root and second directory contain a wildcard
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-ext-simba-lf committed Sep 27, 2023
1 parent c73e9f6 commit e0eef6c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
47 changes: 47 additions & 0 deletions Snowflake.Data.Tests/UnitTests/SFFileTransferAgentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,53 @@ public void TestUploadWithWildcardInTheDirectoryPath()
Directory.Delete(tempUploadRootDirectory, true);
}

[Test]
public void TestUploadThrowsArgumentExceptionForMissingRootDirectoryWithWildcard()
{
// Arrange
UploadSetUpFile();

// Create the mock directory and files
string mockFileName = "testUploadWithMultipleDirectory.txt";
string tempUploadRootDirectory = "mockRootDirectoryWithWildcard";
string tempUploadSecondDirectory = "secondDirectoryWithWilcard";
int numberOfDirectories = 3;

// Do not create the root directory

// Create the second directory and write to file but the test should still fail
for (int i = 0; i < numberOfDirectories; i++)
{
Directory.CreateDirectory($"{tempUploadSecondDirectory}{i}");
File.WriteAllText($"{tempUploadSecondDirectory}{i}/{mockFileName}", FileContent);
}

// Create source location with wildcard in its filename
_responseData.src_locations = new List<string>()
{
// Add wildcard in the source location
$"{tempUploadRootDirectory}*/{tempUploadSecondDirectory}*/{mockFileName}",
};

// Set command to upload
_responseData.command = CommandTypes.UPLOAD.ToString();
_fileTransferAgent = new SFFileTransferAgent(_putQuery,
_session,
_responseData,
_cancellationToken);

// Act
Exception ex = Assert.Throws<ArgumentException>(() => _fileTransferAgent.execute());

// Assert
Assert.That(ex.Message, Does.Match($"No file found for: {tempUploadRootDirectory}\\*/{tempUploadSecondDirectory}\\*/{mockFileName}"));

for (int i = 0; i < numberOfDirectories; i++)
{
Directory.Delete($"{tempUploadSecondDirectory}{i}", true);
}
}

private void DownloadSetUpFile()
{
// Download setup
Expand Down
8 changes: 6 additions & 2 deletions Snowflake.Data/Core/FileTransfer/SFFileTransferAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -790,14 +790,16 @@ private static IEnumerable<string> ExpandDirectories(string directoryPath)
var pathParts = directoryPath.Split(Path.DirectorySeparatorChar);
var resolvedPaths = new List<string>();

bool firstPass = true;

foreach (var part in pathParts)
{
if (ContainsWildcard(part))
{
// Directory containing the wildcard is the first one in the path
if (resolvedPaths.Count == 0)
if (firstPass)
{
resolvedPaths.Add("./");
resolvedPaths.Add(Directory.GetCurrentDirectory());
}

var tempPaths = new List<string>();
Expand Down Expand Up @@ -830,6 +832,8 @@ private static IEnumerable<string> ExpandDirectories(string directoryPath)
resolvedPaths = resolvedPaths.Select(s => s + (part + Path.DirectorySeparatorChar)).ToList();
}
}

firstPass = false;
}

return resolvedPaths;
Expand Down

0 comments on commit e0eef6c

Please sign in to comment.