Skip to content

Commit c31b062

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

File tree

1 file changed

+118
-81
lines changed

1 file changed

+118
-81
lines changed

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

+118-81
Original file line numberDiff line numberDiff line change
@@ -10,39 +10,52 @@ namespace SemmleTests.Semmle.Util
1010
/// Ensure that the Extractor works with long paths.
1111
/// These should be handled by .NET Core.
1212
/// </summary>
13-
public sealed class LongPaths : IDisposable
13+
public sealed class LongPaths
1414
{
1515
private static readonly string tmpDir = Environment.GetEnvironmentVariable("TEST_TMPDIR") ?? Path.GetTempPath();
1616
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(int i)
2222
{
23-
CleanUp();
23+
return Path.Combine(longPathDir, $"iiiiiiiiiiiiiiii{i}.txt");
2424
}
2525

26-
public void Dispose()
26+
private static string MakeShortPath(int i)
27+
{
28+
return Path.Combine(tmpDir, $"test{i}.txt");
29+
}
30+
public LongPaths()
2731
{
28-
CleanUp();
32+
Directory.CreateDirectory(longPathDir);
2933
}
3034

31-
private static void CleanUp()
35+
private static void WithSetUpAndTearDown(int i, Action<string, string> test)
3236
{
37+
var longPath = MakeLongPath(i);
38+
var shortPath = MakeShortPath(i);
3339
try
3440
{
35-
File.Delete(shortPath);
36-
}
37-
catch (DirectoryNotFoundException)
38-
{
39-
}
40-
try
41-
{
42-
File.Delete(longPath);
41+
test(longPath, shortPath);
4342
}
44-
catch (DirectoryNotFoundException)
43+
finally
4544
{
45+
try
46+
{
47+
File.Delete(longPath);
48+
}
49+
catch (DirectoryNotFoundException)
50+
{
51+
}
52+
try
53+
{
54+
File.Delete(shortPath);
55+
}
56+
catch (DirectoryNotFoundException)
57+
{
58+
}
4659
}
4760
}
4861

@@ -63,122 +76,146 @@ public void ParentDirectory()
6376
[Fact]
6477
public void Delete()
6578
{
66-
// OK Do not exist.
67-
File.Delete(shortPath);
68-
File.Delete(longPath);
79+
WithSetUpAndTearDown(1, (longPath, shortPath) =>
80+
{
81+
// OK Do not exist.
82+
File.Delete(shortPath);
83+
File.Delete(longPath);
84+
});
6985
}
7086

7187
[Fact]
7288
public void Move()
7389
{
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));
90+
WithSetUpAndTearDown(2, (longPath, shortPath) =>
91+
{
92+
File.WriteAllText(shortPath, "abc");
93+
Directory.CreateDirectory(Path.GetDirectoryName(longPath)!);
94+
File.Delete(longPath);
95+
File.Move(shortPath, longPath);
96+
File.Move(longPath, shortPath);
97+
Assert.Equal("abc", File.ReadAllText(shortPath));
98+
});
8099
}
81100

82101
[Fact]
83102
public void Replace()
84103
{
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));
104+
WithSetUpAndTearDown(3, (longPath, shortPath) =>
105+
{
106+
File.WriteAllText(shortPath, "abc");
107+
File.Delete(longPath);
108+
Directory.CreateDirectory(Path.GetDirectoryName(longPath)!);
109+
File.Move(shortPath, longPath);
110+
File.WriteAllText(shortPath, "def");
111+
FileUtils.MoveOrReplace(shortPath, longPath);
112+
File.WriteAllText(shortPath, "abc");
113+
FileUtils.MoveOrReplace(longPath, shortPath);
114+
Assert.Equal("def", File.ReadAllText(shortPath));
115+
});
94116
}
95117

96118
private readonly byte[] buffer1 = new byte[10] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
97119

98120
[Fact]
99121
public void CreateShortStream()
100122
{
101-
var buffer2 = new byte[10];
102-
103-
using (var s1 = new FileStream(shortPath, FileMode.Create, FileAccess.Write, FileShare.None))
123+
WithSetUpAndTearDown(4, (_, shortPath) =>
104124
{
105-
s1.Write(buffer1, 0, 10);
106-
}
125+
var buffer2 = new byte[10];
107126

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-
}
127+
using (var s1 = new FileStream(shortPath, FileMode.Create, FileAccess.Write, FileShare.None))
128+
{
129+
s1.Write(buffer1, 0, 10);
130+
}
131+
132+
using (var s2 = new FileStream(shortPath, FileMode.Open, FileAccess.Read, FileShare.None))
133+
{
134+
Assert.Equal(10, s2.Read(buffer2, 0, 10));
135+
Assert.True(Enumerable.SequenceEqual(buffer1, buffer2));
136+
}
137+
});
113138
}
114139

115140
[Fact]
116141
public void CreateLongStream()
117142
{
118-
var buffer2 = new byte[10];
143+
WithSetUpAndTearDown(5, (longPath, _) =>
144+
{
145+
var buffer2 = new byte[10];
119146

120-
Directory.CreateDirectory(Path.GetDirectoryName(longPath)!);
147+
Directory.CreateDirectory(Path.GetDirectoryName(longPath)!);
121148

122-
using (var s3 = new FileStream(longPath, FileMode.Create, FileAccess.Write, FileShare.None))
123-
{
124-
s3.Write(buffer1, 0, 10);
125-
}
149+
using (var s3 = new FileStream(longPath, FileMode.Create, FileAccess.Write, FileShare.None))
150+
{
151+
s3.Write(buffer1, 0, 10);
152+
}
126153

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-
}
154+
using (var s4 = new FileStream(longPath, FileMode.Open, FileAccess.Read, FileShare.None))
155+
{
156+
Assert.Equal(10, s4.Read(buffer2, 0, 10));
157+
Assert.True(Enumerable.SequenceEqual(buffer1, buffer2));
158+
}
159+
});
132160
}
133161

134162
[Fact]
135163
public void FileDoesNotExist()
136164
{
137-
// File does not exist
138-
Assert.Throws<System.IO.FileNotFoundException>(() =>
165+
WithSetUpAndTearDown(6, (longPath, _) =>
139166
{
140-
using (new FileStream(longPath, FileMode.Open, FileAccess.Read, FileShare.None))
167+
// File does not exist
168+
Assert.Throws<System.IO.FileNotFoundException>(() =>
141169
{
142-
//
143-
}
170+
using (new FileStream(longPath, FileMode.Open, FileAccess.Read, FileShare.None))
171+
{
172+
//
173+
}
174+
});
144175
});
145176
}
146177

147178
[Fact]
148179
public void OverwriteFile()
149180
{
150-
using (var s1 = new FileStream(longPath, FileMode.Create, FileAccess.Write, FileShare.None))
181+
WithSetUpAndTearDown(7, (longPath, _) =>
151182
{
152-
s1.Write(buffer1, 0, 10);
153-
}
183+
using (var s1 = new FileStream(longPath, FileMode.Create, FileAccess.Write, FileShare.None))
184+
{
185+
s1.Write(buffer1, 0, 10);
186+
}
154187

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

157-
using (var s2 = new FileStream(longPath, FileMode.Create, FileAccess.Write, FileShare.None))
158-
{
159-
s2.Write(buffer2, 0, 10);
160-
}
190+
using (var s2 = new FileStream(longPath, FileMode.Create, FileAccess.Write, FileShare.None))
191+
{
192+
s2.Write(buffer2, 0, 10);
193+
}
161194

162-
byte[] buffer3 = new byte[10];
195+
byte[] buffer3 = new byte[10];
163196

164-
using (var s3 = new FileStream(longPath, FileMode.Open, FileAccess.Read, FileShare.None))
165-
{
166-
Assert.Equal(10, s3.Read(buffer3, 0, 10));
167-
}
197+
using (var s3 = new FileStream(longPath, FileMode.Open, FileAccess.Read, FileShare.None))
198+
{
199+
Assert.Equal(10, s3.Read(buffer3, 0, 10));
200+
}
168201

169-
Assert.True(Enumerable.SequenceEqual(buffer2, buffer3));
202+
Assert.True(Enumerable.SequenceEqual(buffer2, buffer3));
203+
});
170204
}
171205

172206
[Fact]
173207
public void LongFileExists()
174208
{
175-
Assert.False(File.Exists("no such file"));
176-
Assert.False(File.Exists("\":"));
177-
Assert.False(File.Exists(@"C:\")); // A directory
209+
WithSetUpAndTearDown(8, (longPath, _) =>
210+
{
211+
Assert.False(File.Exists("no such file"));
212+
Assert.False(File.Exists("\":"));
213+
Assert.False(File.Exists(@"C:\")); // A directory
178214

179-
Assert.False(File.Exists(longPath));
180-
new FileStream(longPath, FileMode.Create, FileAccess.Write, FileShare.None).Close();
181-
Assert.True(File.Exists(longPath));
215+
Assert.False(File.Exists(longPath));
216+
new FileStream(longPath, FileMode.Create, FileAccess.Write, FileShare.None).Close();
217+
Assert.True(File.Exists(longPath));
218+
});
182219
}
183220
}
184221
}

0 commit comments

Comments
 (0)