Skip to content

Commit 4a2d3a2

Browse files
committed
C#: Make the path tests independent.
1 parent 5624a77 commit 4a2d3a2

File tree

1 file changed

+123
-81
lines changed

1 file changed

+123
-81
lines changed

csharp/extractor/Semmle.Util.Tests/LongPaths.cs

+123-81
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,64 @@
33
using System.IO;
44
using System.Linq;
55
using Semmle.Util;
6+
using System.Collections.Generic;
67

78
namespace SemmleTests.Semmle.Util
89
{
910
/// <summary>
1011
/// Ensure that the Extractor works with long paths.
1112
/// These should be handled by .NET Core.
1213
/// </summary>
13-
public sealed class LongPaths : IDisposable
14+
public sealed class LongPaths
1415
{
1516
private static readonly string tmpDir = Environment.GetEnvironmentVariable("TEST_TMPDIR") ?? Path.GetTempPath();
16-
private static readonly string shortPath = Path.Combine(tmpDir, "test.txt");
17-
private static readonly string longPath = Path.Combine(tmpDir, "aaaaaaaaaaaaaaaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
17+
private static readonly string longPathDir = Path.Combine(tmpDir, "aaaaaaaaaaaaaaaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
1818
"ccccccccccccccccccccccccccccccc", "ddddddddddddddddddddddddddddddddddddd", "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", "fffffffffffffffffffffffffffffffff",
19-
"ggggggggggggggggggggggggggggggggggg", "hhhhhhhhhhhhhhhhhhhhhhhhhhhhhh", "iiiiiiiiiiiiiiii.txt");
19+
"ggggggggggggggggggggggggggggggggggg", "hhhhhhhhhhhhhhhhhhhhhhhhhhhhhh");
2020

21-
public LongPaths()
21+
private static string MakeLongPath()
2222
{
23-
CleanUp();
23+
var uniquePostfix = Guid.NewGuid().ToString("N");
24+
return Path.Combine(longPathDir, $"iiiiiiiiiiiiiiii{uniquePostfix}.txt");
2425
}
2526

26-
public void Dispose()
27+
private static string MakeShortPath()
2728
{
28-
CleanUp();
29+
var uniquePostfix = Guid.NewGuid().ToString("N");
30+
return Path.Combine(tmpDir, $"test{uniquePostfix}.txt");
2931
}
3032

31-
private static void CleanUp()
33+
public LongPaths()
3234
{
33-
try
34-
{
35-
File.Delete(shortPath);
36-
}
37-
catch (DirectoryNotFoundException)
35+
Directory.CreateDirectory(longPathDir);
36+
}
37+
38+
private static void Cleanup(params IEnumerable<string> paths)
39+
{
40+
foreach (var path in paths)
3841
{
42+
try
43+
{
44+
File.Delete(path);
45+
}
46+
catch (DirectoryNotFoundException)
47+
{
48+
}
3949
}
50+
}
51+
52+
private static void WithSetUpAndTearDown(Action<string, string> test)
53+
{
54+
var longPath = MakeLongPath();
55+
var shortPath = MakeShortPath();
56+
Cleanup(longPath, shortPath);
4057
try
4158
{
42-
File.Delete(longPath);
59+
test(longPath, shortPath);
4360
}
44-
catch (DirectoryNotFoundException)
61+
finally
4562
{
63+
Cleanup(longPath, shortPath);
4664
}
4765
}
4866

@@ -63,122 +81,146 @@ public void ParentDirectory()
6381
[Fact]
6482
public void Delete()
6583
{
66-
// OK Do not exist.
67-
File.Delete(shortPath);
68-
File.Delete(longPath);
84+
WithSetUpAndTearDown((longPath, shortPath) =>
85+
{
86+
// OK Do not exist.
87+
File.Delete(shortPath);
88+
File.Delete(longPath);
89+
});
6990
}
7091

7192
[Fact]
7293
public void Move()
7394
{
74-
File.WriteAllText(shortPath, "abc");
75-
Directory.CreateDirectory(Path.GetDirectoryName(longPath)!);
76-
File.Delete(longPath);
77-
File.Move(shortPath, longPath);
78-
File.Move(longPath, shortPath);
79-
Assert.Equal("abc", File.ReadAllText(shortPath));
95+
WithSetUpAndTearDown((longPath, shortPath) =>
96+
{
97+
File.WriteAllText(shortPath, "abc");
98+
Directory.CreateDirectory(Path.GetDirectoryName(longPath)!);
99+
File.Delete(longPath);
100+
File.Move(shortPath, longPath);
101+
File.Move(longPath, shortPath);
102+
Assert.Equal("abc", File.ReadAllText(shortPath));
103+
});
80104
}
81105

82106
[Fact]
83107
public void Replace()
84108
{
85-
File.WriteAllText(shortPath, "abc");
86-
File.Delete(longPath);
87-
Directory.CreateDirectory(Path.GetDirectoryName(longPath)!);
88-
File.Move(shortPath, longPath);
89-
File.WriteAllText(shortPath, "def");
90-
FileUtils.MoveOrReplace(shortPath, longPath);
91-
File.WriteAllText(shortPath, "abc");
92-
FileUtils.MoveOrReplace(longPath, shortPath);
93-
Assert.Equal("def", File.ReadAllText(shortPath));
109+
WithSetUpAndTearDown((longPath, shortPath) =>
110+
{
111+
File.WriteAllText(shortPath, "abc");
112+
File.Delete(longPath);
113+
Directory.CreateDirectory(Path.GetDirectoryName(longPath)!);
114+
File.Move(shortPath, longPath);
115+
File.WriteAllText(shortPath, "def");
116+
FileUtils.MoveOrReplace(shortPath, longPath);
117+
File.WriteAllText(shortPath, "abc");
118+
FileUtils.MoveOrReplace(longPath, shortPath);
119+
Assert.Equal("def", File.ReadAllText(shortPath));
120+
});
94121
}
95122

96-
private readonly byte[] buffer1 = new byte[10] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
123+
private readonly byte[] buffer1 = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
97124

98125
[Fact]
99126
public void CreateShortStream()
100127
{
101-
var buffer2 = new byte[10];
102-
103-
using (var s1 = new FileStream(shortPath, FileMode.Create, FileAccess.Write, FileShare.None))
128+
WithSetUpAndTearDown((_, shortPath) =>
104129
{
105-
s1.Write(buffer1, 0, 10);
106-
}
130+
var buffer2 = new byte[10];
107131

108-
using (var s2 = new FileStream(shortPath, FileMode.Open, FileAccess.Read, FileShare.None))
109-
{
110-
Assert.Equal(10, s2.Read(buffer2, 0, 10));
111-
Assert.True(Enumerable.SequenceEqual(buffer1, buffer2));
112-
}
132+
using (var s1 = new FileStream(shortPath, FileMode.Create, FileAccess.Write, FileShare.None))
133+
{
134+
s1.Write(buffer1, 0, 10);
135+
}
136+
137+
using (var s2 = new FileStream(shortPath, FileMode.Open, FileAccess.Read, FileShare.None))
138+
{
139+
Assert.Equal(10, s2.Read(buffer2, 0, 10));
140+
Assert.True(Enumerable.SequenceEqual(buffer1, buffer2));
141+
}
142+
});
113143
}
114144

115145
[Fact]
116146
public void CreateLongStream()
117147
{
118-
var buffer2 = new byte[10];
148+
WithSetUpAndTearDown((longPath, _) =>
149+
{
150+
var buffer2 = new byte[10];
119151

120-
Directory.CreateDirectory(Path.GetDirectoryName(longPath)!);
152+
Directory.CreateDirectory(Path.GetDirectoryName(longPath)!);
121153

122-
using (var s3 = new FileStream(longPath, FileMode.Create, FileAccess.Write, FileShare.None))
123-
{
124-
s3.Write(buffer1, 0, 10);
125-
}
154+
using (var s3 = new FileStream(longPath, FileMode.Create, FileAccess.Write, FileShare.None))
155+
{
156+
s3.Write(buffer1, 0, 10);
157+
}
126158

127-
using (var s4 = new FileStream(longPath, FileMode.Open, FileAccess.Read, FileShare.None))
128-
{
129-
Assert.Equal(10, s4.Read(buffer2, 0, 10));
130-
Assert.True(Enumerable.SequenceEqual(buffer1, buffer2));
131-
}
159+
using (var s4 = new FileStream(longPath, FileMode.Open, FileAccess.Read, FileShare.None))
160+
{
161+
Assert.Equal(10, s4.Read(buffer2, 0, 10));
162+
Assert.True(Enumerable.SequenceEqual(buffer1, buffer2));
163+
}
164+
});
132165
}
133166

134167
[Fact]
135168
public void FileDoesNotExist()
136169
{
137-
// File does not exist
138-
Assert.Throws<System.IO.FileNotFoundException>(() =>
170+
WithSetUpAndTearDown((longPath, _) =>
139171
{
140-
using (new FileStream(longPath, FileMode.Open, FileAccess.Read, FileShare.None))
172+
// File does not exist
173+
Assert.Throws<System.IO.FileNotFoundException>(() =>
141174
{
142-
//
143-
}
175+
using (new FileStream(longPath, FileMode.Open, FileAccess.Read, FileShare.None))
176+
{
177+
//
178+
}
179+
});
144180
});
145181
}
146182

147183
[Fact]
148184
public void OverwriteFile()
149185
{
150-
using (var s1 = new FileStream(longPath, FileMode.Create, FileAccess.Write, FileShare.None))
186+
WithSetUpAndTearDown((longPath, _) =>
151187
{
152-
s1.Write(buffer1, 0, 10);
153-
}
188+
using (var s1 = new FileStream(longPath, FileMode.Create, FileAccess.Write, FileShare.None))
189+
{
190+
s1.Write(buffer1, 0, 10);
191+
}
154192

155-
byte[] buffer2 = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
193+
byte[] buffer2 = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
156194

157-
using (var s2 = new FileStream(longPath, FileMode.Create, FileAccess.Write, FileShare.None))
158-
{
159-
s2.Write(buffer2, 0, 10);
160-
}
195+
using (var s2 = new FileStream(longPath, FileMode.Create, FileAccess.Write, FileShare.None))
196+
{
197+
s2.Write(buffer2, 0, 10);
198+
}
161199

162-
byte[] buffer3 = new byte[10];
200+
byte[] buffer3 = new byte[10];
163201

164-
using (var s3 = new FileStream(longPath, FileMode.Open, FileAccess.Read, FileShare.None))
165-
{
166-
Assert.Equal(10, s3.Read(buffer3, 0, 10));
167-
}
202+
using (var s3 = new FileStream(longPath, FileMode.Open, FileAccess.Read, FileShare.None))
203+
{
204+
Assert.Equal(10, s3.Read(buffer3, 0, 10));
205+
}
168206

169-
Assert.True(Enumerable.SequenceEqual(buffer2, buffer3));
207+
Assert.True(Enumerable.SequenceEqual(buffer2, buffer3));
208+
});
170209
}
171210

172211
[Fact]
173212
public void LongFileExists()
174213
{
175-
Assert.False(File.Exists("no such file"));
176-
Assert.False(File.Exists("\":"));
177-
Assert.False(File.Exists(@"C:\")); // A directory
214+
WithSetUpAndTearDown((longPath, _) =>
215+
{
216+
Assert.False(File.Exists("no such file"));
217+
Assert.False(File.Exists("\":"));
218+
Assert.False(File.Exists(@"C:\")); // A directory
178219

179-
Assert.False(File.Exists(longPath));
180-
new FileStream(longPath, FileMode.Create, FileAccess.Write, FileShare.None).Close();
181-
Assert.True(File.Exists(longPath));
220+
Assert.False(File.Exists(longPath));
221+
new FileStream(longPath, FileMode.Create, FileAccess.Write, FileShare.None).Close();
222+
Assert.True(File.Exists(longPath));
223+
});
182224
}
183225
}
184226
}

0 commit comments

Comments
 (0)