@@ -10,39 +10,52 @@ namespace SemmleTests.Semmle.Util
10
10
/// Ensure that the Extractor works with long paths.
11
11
/// These should be handled by .NET Core.
12
12
/// </summary>
13
- public sealed class LongPaths : IDisposable
13
+ public sealed class LongPaths
14
14
{
15
15
private static readonly string tmpDir = Environment . GetEnvironmentVariable ( "TEST_TMPDIR" ) ?? Path . GetTempPath ( ) ;
16
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" ,
18
18
"ccccccccccccccccccccccccccccccc" , "ddddddddddddddddddddddddddddddddddddd" , "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" , "fffffffffffffffffffffffffffffffff" ,
19
- "ggggggggggggggggggggggggggggggggggg" , "hhhhhhhhhhhhhhhhhhhhhhhhhhhhhh" , "iiiiiiiiiiiiiiii.txt" ) ;
19
+ "ggggggggggggggggggggggggggggggggggg" , "hhhhhhhhhhhhhhhhhhhhhhhhhhhhhh" ) ;
20
20
21
- public LongPaths ( )
21
+ private static string MakeLongPath ( int i )
22
22
{
23
- CleanUp ( ) ;
23
+ return Path . Combine ( longPathDir , $ "iiiiiiiiiiiiiiii { i } .txt" ) ;
24
24
}
25
25
26
- public void Dispose ( )
26
+ private static string MakeShortPath ( int i )
27
+ {
28
+ return Path . Combine ( tmpDir , $ "test{ i } .txt") ;
29
+ }
30
+ public LongPaths ( )
27
31
{
28
- CleanUp ( ) ;
32
+ Directory . CreateDirectory ( longPathDir ) ;
29
33
}
30
34
31
- private static void CleanUp ( )
35
+ private static void WithSetUpAndTearDown ( int i , Action < string , string > test )
32
36
{
37
+ var longPath = MakeLongPath ( i ) ;
38
+ var shortPath = MakeShortPath ( i ) ;
33
39
try
34
40
{
35
- File . Delete ( shortPath ) ;
36
- }
37
- catch ( DirectoryNotFoundException )
38
- {
39
- }
40
- try
41
- {
42
- File . Delete ( longPath ) ;
41
+ test ( longPath , shortPath ) ;
43
42
}
44
- catch ( DirectoryNotFoundException )
43
+ finally
45
44
{
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
+ }
46
59
}
47
60
}
48
61
@@ -63,122 +76,146 @@ public void ParentDirectory()
63
76
[ Fact ]
64
77
public void Delete ( )
65
78
{
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
+ } ) ;
69
85
}
70
86
71
87
[ Fact ]
72
88
public void Move ( )
73
89
{
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
+ } ) ;
80
99
}
81
100
82
101
[ Fact ]
83
102
public void Replace ( )
84
103
{
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
+ } ) ;
94
116
}
95
117
96
118
private readonly byte [ ] buffer1 = new byte [ 10 ] { 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } ;
97
119
98
120
[ Fact ]
99
121
public void CreateShortStream ( )
100
122
{
101
- var buffer2 = new byte [ 10 ] ;
102
-
103
- using ( var s1 = new FileStream ( shortPath , FileMode . Create , FileAccess . Write , FileShare . None ) )
123
+ WithSetUpAndTearDown ( 4 , ( _ , shortPath ) =>
104
124
{
105
- s1 . Write ( buffer1 , 0 , 10 ) ;
106
- }
125
+ var buffer2 = new byte [ 10 ] ;
107
126
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
+ } ) ;
113
138
}
114
139
115
140
[ Fact ]
116
141
public void CreateLongStream ( )
117
142
{
118
- var buffer2 = new byte [ 10 ] ;
143
+ WithSetUpAndTearDown ( 5 , ( longPath , _ ) =>
144
+ {
145
+ var buffer2 = new byte [ 10 ] ;
119
146
120
- Directory . CreateDirectory ( Path . GetDirectoryName ( longPath ) ! ) ;
147
+ Directory . CreateDirectory ( Path . GetDirectoryName ( longPath ) ! ) ;
121
148
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
+ }
126
153
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
+ } ) ;
132
160
}
133
161
134
162
[ Fact ]
135
163
public void FileDoesNotExist ( )
136
164
{
137
- // File does not exist
138
- Assert . Throws < System . IO . FileNotFoundException > ( ( ) =>
165
+ WithSetUpAndTearDown ( 6 , ( longPath , _ ) =>
139
166
{
140
- using ( new FileStream ( longPath , FileMode . Open , FileAccess . Read , FileShare . None ) )
167
+ // File does not exist
168
+ Assert . Throws < System . IO . FileNotFoundException > ( ( ) =>
141
169
{
142
- //
143
- }
170
+ using ( new FileStream ( longPath , FileMode . Open , FileAccess . Read , FileShare . None ) )
171
+ {
172
+ //
173
+ }
174
+ } ) ;
144
175
} ) ;
145
176
}
146
177
147
178
[ Fact ]
148
179
public void OverwriteFile ( )
149
180
{
150
- using ( var s1 = new FileStream ( longPath , FileMode . Create , FileAccess . Write , FileShare . None ) )
181
+ WithSetUpAndTearDown ( 7 , ( longPath , _ ) =>
151
182
{
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
+ }
154
187
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 } ;
156
189
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
+ }
161
194
162
- byte [ ] buffer3 = new byte [ 10 ] ;
195
+ byte [ ] buffer3 = new byte [ 10 ] ;
163
196
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
+ }
168
201
169
- Assert . True ( Enumerable . SequenceEqual ( buffer2 , buffer3 ) ) ;
202
+ Assert . True ( Enumerable . SequenceEqual ( buffer2 , buffer3 ) ) ;
203
+ } ) ;
170
204
}
171
205
172
206
[ Fact ]
173
207
public void LongFileExists ( )
174
208
{
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
178
214
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
+ } ) ;
182
219
}
183
220
}
184
221
}
0 commit comments