From baa248ce652dda9d6d06d3e272bdd998fdf0ae98 Mon Sep 17 00:00:00 2001 From: Michael Nebel Date: Mon, 9 Dec 2024 13:00:52 +0100 Subject: [PATCH 1/3] C#: Enable Semmle.Util.Tests. --- csharp/BUILD.bazel | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/csharp/BUILD.bazel b/csharp/BUILD.bazel index 49293c27095e..8aaa0d492ef0 100644 --- a/csharp/BUILD.bazel +++ b/csharp/BUILD.bazel @@ -77,7 +77,6 @@ test_suite( "//csharp/autobuilder/Semmle.Autobuild.CSharp.Tests:acst", "//csharp/autobuilder/Semmle.Autobuild.Cpp.Tests:acpt", "//csharp/extractor/Semmle.Extraction.Tests:et", - # this test suite currently fails, disable for now - # "//csharp/extractor/Semmle.Util.Tests:ut", + "//csharp/extractor/Semmle.Util.Tests:ut", ], ) From 5624a77176db4fe7d36038d7ce6204970d2c5337 Mon Sep 17 00:00:00 2001 From: Michael Nebel Date: Mon, 9 Dec 2024 13:59:01 +0100 Subject: [PATCH 2/3] C#: Use TEST_TEMPDIR when set for test files. --- csharp/extractor/Semmle.Util.Tests/LongPaths.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/csharp/extractor/Semmle.Util.Tests/LongPaths.cs b/csharp/extractor/Semmle.Util.Tests/LongPaths.cs index 1c0d5e2ce139..c1583e275036 100644 --- a/csharp/extractor/Semmle.Util.Tests/LongPaths.cs +++ b/csharp/extractor/Semmle.Util.Tests/LongPaths.cs @@ -12,7 +12,7 @@ namespace SemmleTests.Semmle.Util /// public sealed class LongPaths : IDisposable { - private static readonly string tmpDir = Path.GetTempPath(); + private static readonly string tmpDir = Environment.GetEnvironmentVariable("TEST_TMPDIR") ?? Path.GetTempPath(); private static readonly string shortPath = Path.Combine(tmpDir, "test.txt"); private static readonly string longPath = Path.Combine(tmpDir, "aaaaaaaaaaaaaaaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbbbbbbbbbbbbbbb", "ccccccccccccccccccccccccccccccc", "ddddddddddddddddddddddddddddddddddddd", "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", "fffffffffffffffffffffffffffffffff", From 4275813b87f5d1cba5d5f2694544579538a9a01c Mon Sep 17 00:00:00 2001 From: Michael Nebel Date: Mon, 9 Dec 2024 16:31:11 +0100 Subject: [PATCH 3/3] C#: Make the path tests independent. --- .../extractor/Semmle.Util.Tests/LongPaths.cs | 196 ++++++++++-------- 1 file changed, 115 insertions(+), 81 deletions(-) diff --git a/csharp/extractor/Semmle.Util.Tests/LongPaths.cs b/csharp/extractor/Semmle.Util.Tests/LongPaths.cs index c1583e275036..90607bc8f02d 100644 --- a/csharp/extractor/Semmle.Util.Tests/LongPaths.cs +++ b/csharp/extractor/Semmle.Util.Tests/LongPaths.cs @@ -1,5 +1,6 @@ using Xunit; using System; +using System.Collections.Generic; using System.IO; using System.Linq; using Semmle.Util; @@ -10,39 +11,51 @@ namespace SemmleTests.Semmle.Util /// Ensure that the Extractor works with long paths. /// These should be handled by .NET Core. /// - public sealed class LongPaths : IDisposable + public sealed class LongPaths { private static readonly string tmpDir = Environment.GetEnvironmentVariable("TEST_TMPDIR") ?? Path.GetTempPath(); - private static readonly string shortPath = Path.Combine(tmpDir, "test.txt"); - private static readonly string longPath = Path.Combine(tmpDir, "aaaaaaaaaaaaaaaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbbbbbbbbbbbbbbb", + private static readonly string longPathDir = Path.Combine(tmpDir, "aaaaaaaaaaaaaaaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbbbbbbbbbbbbbbb", "ccccccccccccccccccccccccccccccc", "ddddddddddddddddddddddddddddddddddddd", "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", "fffffffffffffffffffffffffffffffff", - "ggggggggggggggggggggggggggggggggggg", "hhhhhhhhhhhhhhhhhhhhhhhhhhhhhh", "iiiiiiiiiiiiiiii.txt"); + "ggggggggggggggggggggggggggggggggggg", "hhhhhhhhhhhhhhhhhhhhhhhhhhhhhh"); - public LongPaths() + private static string MakeLongPath() { - CleanUp(); + var uniquePostfix = Guid.NewGuid().ToString("N"); + return Path.Combine(longPathDir, $"iiiiiiiiiiiiiiii{uniquePostfix}.txt"); } - public void Dispose() + private static string MakeShortPath() { - CleanUp(); + var uniquePostfix = Guid.NewGuid().ToString("N"); + return Path.Combine(tmpDir, $"test{uniquePostfix}.txt"); } - private static void CleanUp() + public LongPaths() { - try - { - File.Delete(shortPath); - } - catch (DirectoryNotFoundException) + // Create directory to avoid directory not found exceptions when deleting files + Directory.CreateDirectory(longPathDir); + } + + private static void Cleanup(params IEnumerable paths) + { + foreach (var path in paths) { + File.Delete(path); } + } + + private static void WithSetUpAndTearDown(Action test) + { + var longPath = MakeLongPath(); + var shortPath = MakeShortPath(); + Cleanup(longPath, shortPath); try { - File.Delete(longPath); + test(longPath, shortPath); } - catch (DirectoryNotFoundException) + finally { + Cleanup(longPath, shortPath); } } @@ -63,122 +76,143 @@ public void ParentDirectory() [Fact] public void Delete() { - // OK Do not exist. - File.Delete(shortPath); - File.Delete(longPath); + WithSetUpAndTearDown((longPath, shortPath) => + { + // OK Do not exist. + File.Delete(shortPath); + File.Delete(longPath); + }); } [Fact] public void Move() { - File.WriteAllText(shortPath, "abc"); - Directory.CreateDirectory(Path.GetDirectoryName(longPath)!); - File.Delete(longPath); - File.Move(shortPath, longPath); - File.Move(longPath, shortPath); - Assert.Equal("abc", File.ReadAllText(shortPath)); + WithSetUpAndTearDown((longPath, shortPath) => + { + File.WriteAllText(shortPath, "abc"); + File.Delete(longPath); + File.Move(shortPath, longPath); + File.Move(longPath, shortPath); + Assert.Equal("abc", File.ReadAllText(shortPath)); + }); } [Fact] public void Replace() { - File.WriteAllText(shortPath, "abc"); - File.Delete(longPath); - Directory.CreateDirectory(Path.GetDirectoryName(longPath)!); - File.Move(shortPath, longPath); - File.WriteAllText(shortPath, "def"); - FileUtils.MoveOrReplace(shortPath, longPath); - File.WriteAllText(shortPath, "abc"); - FileUtils.MoveOrReplace(longPath, shortPath); - Assert.Equal("def", File.ReadAllText(shortPath)); + WithSetUpAndTearDown((longPath, shortPath) => + { + File.WriteAllText(shortPath, "abc"); + File.Move(shortPath, longPath); + File.WriteAllText(shortPath, "def"); + FileUtils.MoveOrReplace(shortPath, longPath); + File.WriteAllText(shortPath, "abc"); + FileUtils.MoveOrReplace(longPath, shortPath); + Assert.Equal("def", File.ReadAllText(shortPath)); + }); } - private readonly byte[] buffer1 = new byte[10] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + private readonly byte[] buffer1 = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; [Fact] public void CreateShortStream() { - var buffer2 = new byte[10]; - - using (var s1 = new FileStream(shortPath, FileMode.Create, FileAccess.Write, FileShare.None)) + WithSetUpAndTearDown((_, shortPath) => { - s1.Write(buffer1, 0, 10); - } + var buffer2 = new byte[10]; - using (var s2 = new FileStream(shortPath, FileMode.Open, FileAccess.Read, FileShare.None)) - { - Assert.Equal(10, s2.Read(buffer2, 0, 10)); - Assert.True(Enumerable.SequenceEqual(buffer1, buffer2)); - } + using (var s1 = new FileStream(shortPath, FileMode.Create, FileAccess.Write, FileShare.None)) + { + s1.Write(buffer1, 0, 10); + } + + using (var s2 = new FileStream(shortPath, FileMode.Open, FileAccess.Read, FileShare.None)) + { + Assert.Equal(10, s2.Read(buffer2, 0, 10)); + Assert.True(Enumerable.SequenceEqual(buffer1, buffer2)); + } + }); } [Fact] public void CreateLongStream() { - var buffer2 = new byte[10]; + WithSetUpAndTearDown((longPath, _) => + { + var buffer2 = new byte[10]; - Directory.CreateDirectory(Path.GetDirectoryName(longPath)!); + Directory.CreateDirectory(Path.GetDirectoryName(longPath)!); - using (var s3 = new FileStream(longPath, FileMode.Create, FileAccess.Write, FileShare.None)) - { - s3.Write(buffer1, 0, 10); - } + using (var s3 = new FileStream(longPath, FileMode.Create, FileAccess.Write, FileShare.None)) + { + s3.Write(buffer1, 0, 10); + } - using (var s4 = new FileStream(longPath, FileMode.Open, FileAccess.Read, FileShare.None)) - { - Assert.Equal(10, s4.Read(buffer2, 0, 10)); - Assert.True(Enumerable.SequenceEqual(buffer1, buffer2)); - } + using (var s4 = new FileStream(longPath, FileMode.Open, FileAccess.Read, FileShare.None)) + { + Assert.Equal(10, s4.Read(buffer2, 0, 10)); + Assert.True(Enumerable.SequenceEqual(buffer1, buffer2)); + } + }); } [Fact] public void FileDoesNotExist() { - // File does not exist - Assert.Throws(() => + WithSetUpAndTearDown((longPath, _) => { - using (new FileStream(longPath, FileMode.Open, FileAccess.Read, FileShare.None)) + // File does not exist + Assert.Throws(() => { - // - } + using (new FileStream(longPath, FileMode.Open, FileAccess.Read, FileShare.None)) + { + // + } + }); }); } [Fact] public void OverwriteFile() { - using (var s1 = new FileStream(longPath, FileMode.Create, FileAccess.Write, FileShare.None)) + WithSetUpAndTearDown((longPath, _) => { - s1.Write(buffer1, 0, 10); - } + using (var s1 = new FileStream(longPath, FileMode.Create, FileAccess.Write, FileShare.None)) + { + s1.Write(buffer1, 0, 10); + } - byte[] buffer2 = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }; + byte[] buffer2 = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }; - using (var s2 = new FileStream(longPath, FileMode.Create, FileAccess.Write, FileShare.None)) - { - s2.Write(buffer2, 0, 10); - } + using (var s2 = new FileStream(longPath, FileMode.Create, FileAccess.Write, FileShare.None)) + { + s2.Write(buffer2, 0, 10); + } - byte[] buffer3 = new byte[10]; + byte[] buffer3 = new byte[10]; - using (var s3 = new FileStream(longPath, FileMode.Open, FileAccess.Read, FileShare.None)) - { - Assert.Equal(10, s3.Read(buffer3, 0, 10)); - } + using (var s3 = new FileStream(longPath, FileMode.Open, FileAccess.Read, FileShare.None)) + { + Assert.Equal(10, s3.Read(buffer3, 0, 10)); + } - Assert.True(Enumerable.SequenceEqual(buffer2, buffer3)); + Assert.True(Enumerable.SequenceEqual(buffer2, buffer3)); + }); } [Fact] public void LongFileExists() { - Assert.False(File.Exists("no such file")); - Assert.False(File.Exists("\":")); - Assert.False(File.Exists(@"C:\")); // A directory + WithSetUpAndTearDown((longPath, _) => + { + Assert.False(File.Exists("no such file")); + Assert.False(File.Exists("\":")); + Assert.False(File.Exists(@"C:\")); // A directory - Assert.False(File.Exists(longPath)); - new FileStream(longPath, FileMode.Create, FileAccess.Write, FileShare.None).Close(); - Assert.True(File.Exists(longPath)); + Assert.False(File.Exists(longPath)); + new FileStream(longPath, FileMode.Create, FileAccess.Write, FileShare.None).Close(); + Assert.True(File.Exists(longPath)); + }); } } }