From 2429a5383de01d3622d4205369144c9723fb76d4 Mon Sep 17 00:00:00 2001 From: Tom Hvitved Date: Tue, 22 Aug 2023 11:44:55 +0200 Subject: [PATCH] C#: Move `NestPaths` to `Semmle.Util` --- .../Semmle.Extraction.Tests/TrapWriter.cs | 52 ------------------- .../extractor/Semmle.Extraction/TrapWriter.cs | 36 +------------ .../extractor/Semmle.Util.Tests/FileUtils.cs | 43 +++++++++++++++ csharp/extractor/Semmle.Util/FileUtils.cs | 33 ++++++++++++ 4 files changed, 78 insertions(+), 86 deletions(-) delete mode 100644 csharp/extractor/Semmle.Extraction.Tests/TrapWriter.cs diff --git a/csharp/extractor/Semmle.Extraction.Tests/TrapWriter.cs b/csharp/extractor/Semmle.Extraction.Tests/TrapWriter.cs deleted file mode 100644 index 54e0a9db25a..00000000000 --- a/csharp/extractor/Semmle.Extraction.Tests/TrapWriter.cs +++ /dev/null @@ -1,52 +0,0 @@ -using Xunit; -using Semmle.Util.Logging; -using Semmle.Util; - -namespace Semmle.Extraction.Tests -{ - public class TrapWriterTests - { - [Fact] - public void NestedPaths() - { - string tempDir = System.IO.Path.GetTempPath(); - string root1, root2, root3; - - if (Win32.IsWindows()) - { - root1 = "E:"; - root2 = "e:"; - root3 = @"\"; - } - else - { - root1 = "/E_"; - root2 = "/e_"; - root3 = "/"; - } - - using var logger = new LoggerMock(); - - Assert.Equal($@"C:\Temp\source_archive\def.cs", TrapWriter.NestPaths(logger, @"C:\Temp\source_archive", "def.cs").Replace('/', '\\')); - - Assert.Equal(@"C:\Temp\source_archive\def.cs", TrapWriter.NestPaths(logger, @"C:\Temp\source_archive", "def.cs").Replace('/', '\\')); - - Assert.Equal(@"C:\Temp\source_archive\E_\source\def.cs", TrapWriter.NestPaths(logger, @"C:\Temp\source_archive", $@"{root1}\source\def.cs").Replace('/', '\\')); - - Assert.Equal(@"C:\Temp\source_archive\e_\source\def.cs", TrapWriter.NestPaths(logger, @"C:\Temp\source_archive", $@"{root2}\source\def.cs").Replace('/', '\\')); - - Assert.Equal(@"C:\Temp\source_archive\source\def.cs", TrapWriter.NestPaths(logger, @"C:\Temp\source_archive", $@"{root3}source\def.cs").Replace('/', '\\')); - - Assert.Equal(@"C:\Temp\source_archive\source\def.cs", TrapWriter.NestPaths(logger, @"C:\Temp\source_archive", $@"{root3}source\def.cs").Replace('/', '\\')); - - Assert.Equal(@"C:\Temp\source_archive\diskstation\share\source\def.cs", TrapWriter.NestPaths(logger, @"C:\Temp\source_archive", $@"{root3}{root3}diskstation\share\source\def.cs").Replace('/', '\\')); - } - - private sealed class LoggerMock : ILogger - { - public void Dispose() { } - - public void Log(Severity s, string text) { } - } - } -} diff --git a/csharp/extractor/Semmle.Extraction/TrapWriter.cs b/csharp/extractor/Semmle.Extraction/TrapWriter.cs index 3757e632e72..58d71ccaf38 100644 --- a/csharp/extractor/Semmle.Extraction/TrapWriter.cs +++ b/csharp/extractor/Semmle.Extraction/TrapWriter.cs @@ -216,7 +216,7 @@ namespace Semmle.Extraction private void ArchiveContents(PathTransformer.ITransformedPath transformedPath, string contents) { - var dest = NestPaths(logger, archive, transformedPath.Value); + var dest = FileUtils.NestPaths(logger, archive, transformedPath.Value); var tmpSrcFile = Path.GetTempFileName(); File.WriteAllText(tmpSrcFile, contents, utf8); try @@ -231,38 +231,6 @@ namespace Semmle.Extraction } } - public static string NestPaths(ILogger logger, string? outerpath, string innerpath) - { - var nested = innerpath; - if (!string.IsNullOrEmpty(outerpath)) - { - // Remove all leading path separators / or \ - // For example, UNC paths have two leading \\ - innerpath = innerpath.TrimStart(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); - - if (innerpath.Length > 1 && innerpath[1] == ':') - innerpath = innerpath[0] + "_" + innerpath.Substring(2); - - nested = Path.Combine(outerpath, innerpath); - } - try - { - var directoryName = Path.GetDirectoryName(nested); - if (directoryName is null) - { - logger.Log(Severity.Warning, "Failed to get directory name from path '" + nested + "'."); - throw new InvalidOperationException(); - } - Directory.CreateDirectory(directoryName); - } - catch (PathTooLongException) - { - logger.Log(Severity.Warning, "Failed to create parent directory of '" + nested + "': Path too long."); - throw; - } - return nested; - } - private static string TrapExtension(CompressionMode compression) { switch (compression) @@ -280,7 +248,7 @@ namespace Semmle.Extraction if (string.IsNullOrEmpty(folder)) folder = Directory.GetCurrentDirectory(); - return NestPaths(logger, folder, filename); + return FileUtils.NestPaths(logger, folder, filename); } } } diff --git a/csharp/extractor/Semmle.Util.Tests/FileUtils.cs b/csharp/extractor/Semmle.Util.Tests/FileUtils.cs index b3feedde436..cbc82d4b814 100644 --- a/csharp/extractor/Semmle.Util.Tests/FileUtils.cs +++ b/csharp/extractor/Semmle.Util.Tests/FileUtils.cs @@ -1,5 +1,6 @@ using Xunit; using Semmle.Util; +using Semmle.Util.Logging; namespace SemmleTests.Semmle.Util { @@ -16,5 +17,47 @@ namespace SemmleTests.Semmle.Util Assert.Equal(Win32.IsWindows() ? @"foo\bar" : "foo/bar", FileUtils.ConvertToNative("foo/bar")); } + + [Fact] + public void NestedPaths() + { + string root1, root2, root3; + + if (Win32.IsWindows()) + { + root1 = "E:"; + root2 = "e:"; + root3 = @"\"; + } + else + { + root1 = "/E_"; + root2 = "/e_"; + root3 = "/"; + } + + using var logger = new LoggerMock(); + + Assert.Equal($@"C:\Temp\source_archive\def.cs", FileUtils.NestPaths(logger, @"C:\Temp\source_archive", "def.cs").Replace('/', '\\')); + + Assert.Equal(@"C:\Temp\source_archive\def.cs", FileUtils.NestPaths(logger, @"C:\Temp\source_archive", "def.cs").Replace('/', '\\')); + + Assert.Equal(@"C:\Temp\source_archive\E_\source\def.cs", FileUtils.NestPaths(logger, @"C:\Temp\source_archive", $@"{root1}\source\def.cs").Replace('/', '\\')); + + Assert.Equal(@"C:\Temp\source_archive\e_\source\def.cs", FileUtils.NestPaths(logger, @"C:\Temp\source_archive", $@"{root2}\source\def.cs").Replace('/', '\\')); + + Assert.Equal(@"C:\Temp\source_archive\source\def.cs", FileUtils.NestPaths(logger, @"C:\Temp\source_archive", $@"{root3}source\def.cs").Replace('/', '\\')); + + Assert.Equal(@"C:\Temp\source_archive\source\def.cs", FileUtils.NestPaths(logger, @"C:\Temp\source_archive", $@"{root3}source\def.cs").Replace('/', '\\')); + + Assert.Equal(@"C:\Temp\source_archive\diskstation\share\source\def.cs", FileUtils.NestPaths(logger, @"C:\Temp\source_archive", $@"{root3}{root3}diskstation\share\source\def.cs").Replace('/', '\\')); + } + + private sealed class LoggerMock : ILogger + { + public void Dispose() { } + + public void Log(Severity s, string text) { } + } } } diff --git a/csharp/extractor/Semmle.Util/FileUtils.cs b/csharp/extractor/Semmle.Util/FileUtils.cs index 90d0cc0a635..ad8cb6cbec3 100644 --- a/csharp/extractor/Semmle.Util/FileUtils.cs +++ b/csharp/extractor/Semmle.Util/FileUtils.cs @@ -5,6 +5,7 @@ using System.Net.Http; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; +using Semmle.Util.Logging; namespace Semmle.Util { @@ -110,5 +111,37 @@ namespace Semmle.Util /// public static void DownloadFile(string address, string fileName) => DownloadFileAsync(address, fileName).Wait(); + + public static string NestPaths(ILogger logger, string? outerpath, string innerpath) + { + var nested = innerpath; + if (!string.IsNullOrEmpty(outerpath)) + { + // Remove all leading path separators / or \ + // For example, UNC paths have two leading \\ + innerpath = innerpath.TrimStart(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); + + if (innerpath.Length > 1 && innerpath[1] == ':') + innerpath = innerpath[0] + "_" + innerpath.Substring(2); + + nested = Path.Combine(outerpath, innerpath); + } + try + { + var directoryName = Path.GetDirectoryName(nested); + if (directoryName is null) + { + logger.Log(Severity.Warning, "Failed to get directory name from path '" + nested + "'."); + throw new InvalidOperationException(); + } + Directory.CreateDirectory(directoryName); + } + catch (PathTooLongException) + { + logger.Log(Severity.Warning, "Failed to create parent directory of '" + nested + "': Path too long."); + throw; + } + return nested; + } } }