Skip to content

Commit

Permalink
RavenDB-23351 - handle long paths in EmbeddedTestBase
Browse files Browse the repository at this point in the history
  • Loading branch information
garayx committed Dec 19, 2024
1 parent 53a03f2 commit c3d447f
Showing 1 changed file with 61 additions and 11 deletions.
72 changes: 61 additions & 11 deletions test/EmbeddedTests/EmbeddedTestBase.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
using System;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;
using EmbeddedTests.Platform;
using Raven.Embedded;
using Sparrow.Collections;
using Sparrow.Platform;

#pragma warning disable LOCAL0003

namespace EmbeddedTests
{
Expand All @@ -20,7 +25,7 @@ protected string NewDataPath([CallerMemberName] string caller = null)
if (PosixHelper.RunningOnPosix)
path = PosixHelper.FixLinuxPath(path);

path = Path.GetFullPath(path);
path = ToFullPath(Path.GetFullPath(path));
_localPathsToDelete.Add(path);

return path;
Expand All @@ -29,8 +34,8 @@ protected string NewDataPath([CallerMemberName] string caller = null)
protected (string ServerDirectory, string DataDirectory) CopyServer()
{
var baseDirectory = NewDataPath();
var serverDirectory = Path.Combine(baseDirectory, "RavenDBServer");
var dataDirectory = Path.Combine(baseDirectory, "RavenDB");
var serverDirectory = ToFullPath(Path.Combine(baseDirectory, "RavenDBServer"));
var dataDirectory = ToFullPath(Path.Combine(baseDirectory, "RavenDB"));

if (Directory.Exists(serverDirectory) == false)
Directory.CreateDirectory(serverDirectory);
Expand All @@ -52,14 +57,14 @@ protected string NewDataPath([CallerMemberName] string caller = null)
if (runtimeConfigFileInfo.Exists == false)
throw new FileNotFoundException("Could not find runtime config", runtimeConfigPath);

File.Copy(runtimeConfigPath, Path.Combine(serverDirectory, runtimeConfigFileInfo.Name), true);
File.Copy(runtimeConfigPath, ToFullPath(Path.Combine(serverDirectory, runtimeConfigFileInfo.Name)), true);

foreach (var extension in new[] { "*.dll", "*.so", "*.dylib", "*.deps.json" })
{
foreach (var file in Directory.GetFiles(runtimeConfigFileInfo.DirectoryName, extension))
foreach (var file in Directory.GetFiles(runtimeConfigFileInfo.DirectoryName, extension).Select(x => ToFullPath(x)))
{
var fileInfo = new FileInfo(file);
File.Copy(file, Path.Combine(serverDirectory, fileInfo.Name), true);
File.Copy(file, ToFullPath(Path.Combine(serverDirectory, fileInfo.Name)), true);
}
}

Expand All @@ -68,11 +73,11 @@ protected string NewDataPath([CallerMemberName] string caller = null)

foreach (string dirPath in Directory.GetDirectories(runtimesSource, "*",
SearchOption.AllDirectories))
Directory.CreateDirectory(dirPath.Replace(runtimesSource, runtimesDestination));
Directory.CreateDirectory(ToFullPath(dirPath.Replace(runtimesSource, runtimesDestination)));

foreach (string newPath in Directory.GetFiles(runtimesSource, "*.*",
SearchOption.AllDirectories))
File.Copy(newPath, newPath.Replace(runtimesSource, runtimesDestination), true);
SearchOption.AllDirectories).Select(x => ToFullPath(x)))
File.Copy(newPath, ToFullPath(newPath.Replace(runtimesSource, runtimesDestination)), true);

return (serverDirectory, dataDirectory);
}
Expand All @@ -82,7 +87,7 @@ protected ServerOptions CopyServerAndCreateOptions()
var (severDirectory, dataDirectory) = CopyServer();
return new ServerOptions { ServerDirectory = severDirectory, DataDirectory = dataDirectory, LogsPath = Path.Combine(severDirectory, "Logs") };
}

public virtual void Dispose()
{
foreach (var path in _localPathsToDelete)
Expand All @@ -91,8 +96,53 @@ public virtual void Dispose()
if (directoryInfo.Exists == false)
continue;

directoryInfo.Delete(recursive: true);
DeleteAllFilesAndSubfolders(directoryInfo);
}
}

private void DeleteAllFilesAndSubfolders(DirectoryInfo directoryInfo)
{
// Delete all files in the current directory
foreach (var p in directoryInfo.GetFiles().Select(x => ToFullPath(x.FullName)))
{
File.Delete(p);
}

// Recursively delete all subdirectories
foreach (var subdirectory in directoryInfo.GetDirectories())
{
DeleteAllFilesAndSubfolders(subdirectory);
Directory.Delete(ToFullPath(subdirectory.FullName));
}
}

private static string ToFullPath(string inputPath, string baseDataDirFullPath = null)
{
var path = Environment.ExpandEnvironmentVariables(inputPath);

if (PlatformDetails.RunningOnPosix == false && path.StartsWith(@"\") == false ||
PlatformDetails.RunningOnPosix && path.StartsWith(@"/") == false) // if relative path
path = Path.Combine(baseDataDirFullPath ?? AppContext.BaseDirectory, path);

var result = Path.IsPathRooted(path)
? path
: Path.Combine(baseDataDirFullPath ?? AppContext.BaseDirectory, path);

if (result.Length >= 260 &&
RuntimeInformation.IsOSPlatform(OSPlatform.Windows) &&
result.StartsWith(@"\\?\") == false)
result = @"\\?\" + result;

var resultRoot = Path.GetPathRoot(result);
if (resultRoot != result && (result.EndsWith(@"\") || result.EndsWith("/")))
result = result.TrimEnd('\\', '/');

if (PlatformDetails.RunningOnPosix)
result = PosixHelper.FixLinuxPath(result);

return result != string.Empty || resultRoot == null
? Path.GetFullPath(result)
: Path.GetFullPath(resultRoot); // it will unify directory separators and sort out parent directories
}
}
}

0 comments on commit c3d447f

Please sign in to comment.