Skip to content

Commit

Permalink
C#: Make the path tests independent.
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelnebel committed Dec 9, 2024
1 parent 5624a77 commit c31b062
Showing 1 changed file with 118 additions and 81 deletions.
199 changes: 118 additions & 81 deletions csharp/extractor/Semmle.Util.Tests/LongPaths.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,52 @@ namespace SemmleTests.Semmle.Util
/// Ensure that the Extractor works with long paths.
/// These should be handled by .NET Core.
/// </summary>
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(int i)
{
CleanUp();
return Path.Combine(longPathDir, $"iiiiiiiiiiiiiiii{i}.txt");
}

public void Dispose()
private static string MakeShortPath(int i)
{
return Path.Combine(tmpDir, $"test{i}.txt");
}
public LongPaths()
{
CleanUp();
Directory.CreateDirectory(longPathDir);
}

private static void CleanUp()
private static void WithSetUpAndTearDown(int i, Action<string, string> test)
{
var longPath = MakeLongPath(i);
var shortPath = MakeShortPath(i);

Check notice

Code scanning / CodeQL

Local scope variable shadows member Note test

Local scope variable 'shortPath' shadows
LongPaths.shortPath
.
try
{
File.Delete(shortPath);
}
catch (DirectoryNotFoundException)
{
}
try
{
File.Delete(longPath);
test(longPath, shortPath);
}
catch (DirectoryNotFoundException)
finally
{
try
{
File.Delete(longPath);
}
catch (DirectoryNotFoundException)
{
}

Check notice

Code scanning / CodeQL

Poor error handling: empty catch block Note test

Poor error handling: empty catch block.
try
{
File.Delete(shortPath);
}
catch (DirectoryNotFoundException)
{
}

Check notice

Code scanning / CodeQL

Poor error handling: empty catch block Note test

Poor error handling: empty catch block.
}
}

Expand All @@ -63,122 +76,146 @@ public void ParentDirectory()
[Fact]
public void Delete()
{
// OK Do not exist.
File.Delete(shortPath);
File.Delete(longPath);
WithSetUpAndTearDown(1, (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(2, (longPath, shortPath) =>
{
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));
});
}

[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(3, (longPath, shortPath) =>
{
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));
});
}

private readonly byte[] buffer1 = new byte[10] { 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(4, (_, 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(5, (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<System.IO.FileNotFoundException>(() =>
WithSetUpAndTearDown(6, (longPath, _) =>
{
using (new FileStream(longPath, FileMode.Open, FileAccess.Read, FileShare.None))
// File does not exist
Assert.Throws<System.IO.FileNotFoundException>(() =>
{
//
}
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(7, (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(8, (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));
});
}
}
}

0 comments on commit c31b062

Please sign in to comment.