3
3
using System . IO ;
4
4
using System . Linq ;
5
5
using Semmle . Util ;
6
+ using System . Collections . Generic ;
6
7
7
8
namespace SemmleTests . Semmle . Util
8
9
{
9
10
/// <summary>
10
11
/// Ensure that the Extractor works with long paths.
11
12
/// These should be handled by .NET Core.
12
13
/// </summary>
13
- public sealed class LongPaths : IDisposable
14
+ public sealed class LongPaths
14
15
{
15
16
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" ,
18
18
"ccccccccccccccccccccccccccccccc" , "ddddddddddddddddddddddddddddddddddddd" , "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" , "fffffffffffffffffffffffffffffffff" ,
19
- "ggggggggggggggggggggggggggggggggggg" , "hhhhhhhhhhhhhhhhhhhhhhhhhhhhhh" , "iiiiiiiiiiiiiiii.txt" ) ;
19
+ "ggggggggggggggggggggggggggggggggggg" , "hhhhhhhhhhhhhhhhhhhhhhhhhhhhhh" ) ;
20
20
21
- public LongPaths ( )
21
+ private static string MakeLongPath ( )
22
22
{
23
- CleanUp ( ) ;
23
+ var uniquePostfix = Guid . NewGuid ( ) . ToString ( "N" ) ;
24
+ return Path . Combine ( longPathDir , $ "iiiiiiiiiiiiiiii{ uniquePostfix } .txt") ;
24
25
}
25
26
26
- public void Dispose ( )
27
+ private static string MakeShortPath ( )
27
28
{
28
- CleanUp ( ) ;
29
+ var uniquePostfix = Guid . NewGuid ( ) . ToString ( "N" ) ;
30
+ return Path . Combine ( tmpDir , $ "test{ uniquePostfix } .txt") ;
29
31
}
30
32
31
- private static void CleanUp ( )
33
+ public LongPaths ( )
32
34
{
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 )
38
41
{
42
+ try
43
+ {
44
+ File . Delete ( path ) ;
45
+ }
46
+ catch ( DirectoryNotFoundException )
47
+ {
48
+ }
39
49
}
50
+ }
51
+
52
+ private static void WithSetUpAndTearDown ( Action < string , string > test )
53
+ {
54
+ var longPath = MakeLongPath ( ) ;
55
+ var shortPath = MakeShortPath ( ) ;
56
+ Cleanup ( longPath , shortPath ) ;
40
57
try
41
58
{
42
- File . Delete ( longPath ) ;
59
+ test ( longPath , shortPath ) ;
43
60
}
44
- catch ( DirectoryNotFoundException )
61
+ finally
45
62
{
63
+ Cleanup ( longPath , shortPath ) ;
46
64
}
47
65
}
48
66
@@ -63,122 +81,146 @@ public void ParentDirectory()
63
81
[ Fact ]
64
82
public void Delete ( )
65
83
{
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
+ } ) ;
69
90
}
70
91
71
92
[ Fact ]
72
93
public void Move ( )
73
94
{
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
+ } ) ;
80
104
}
81
105
82
106
[ Fact ]
83
107
public void Replace ( )
84
108
{
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
+ } ) ;
94
121
}
95
122
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 } ;
97
124
98
125
[ Fact ]
99
126
public void CreateShortStream ( )
100
127
{
101
- var buffer2 = new byte [ 10 ] ;
102
-
103
- using ( var s1 = new FileStream ( shortPath , FileMode . Create , FileAccess . Write , FileShare . None ) )
128
+ WithSetUpAndTearDown ( ( _ , shortPath ) =>
104
129
{
105
- s1 . Write ( buffer1 , 0 , 10 ) ;
106
- }
130
+ var buffer2 = new byte [ 10 ] ;
107
131
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
+ } ) ;
113
143
}
114
144
115
145
[ Fact ]
116
146
public void CreateLongStream ( )
117
147
{
118
- var buffer2 = new byte [ 10 ] ;
148
+ WithSetUpAndTearDown ( ( longPath , _ ) =>
149
+ {
150
+ var buffer2 = new byte [ 10 ] ;
119
151
120
- Directory . CreateDirectory ( Path . GetDirectoryName ( longPath ) ! ) ;
152
+ Directory . CreateDirectory ( Path . GetDirectoryName ( longPath ) ! ) ;
121
153
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
+ }
126
158
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
+ } ) ;
132
165
}
133
166
134
167
[ Fact ]
135
168
public void FileDoesNotExist ( )
136
169
{
137
- // File does not exist
138
- Assert . Throws < System . IO . FileNotFoundException > ( ( ) =>
170
+ WithSetUpAndTearDown ( ( longPath , _ ) =>
139
171
{
140
- using ( new FileStream ( longPath , FileMode . Open , FileAccess . Read , FileShare . None ) )
172
+ // File does not exist
173
+ Assert . Throws < System . IO . FileNotFoundException > ( ( ) =>
141
174
{
142
- //
143
- }
175
+ using ( new FileStream ( longPath , FileMode . Open , FileAccess . Read , FileShare . None ) )
176
+ {
177
+ //
178
+ }
179
+ } ) ;
144
180
} ) ;
145
181
}
146
182
147
183
[ Fact ]
148
184
public void OverwriteFile ( )
149
185
{
150
- using ( var s1 = new FileStream ( longPath , FileMode . Create , FileAccess . Write , FileShare . None ) )
186
+ WithSetUpAndTearDown ( ( longPath , _ ) =>
151
187
{
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
+ }
154
192
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 } ;
156
194
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
+ }
161
199
162
- byte [ ] buffer3 = new byte [ 10 ] ;
200
+ byte [ ] buffer3 = new byte [ 10 ] ;
163
201
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
+ }
168
206
169
- Assert . True ( Enumerable . SequenceEqual ( buffer2 , buffer3 ) ) ;
207
+ Assert . True ( Enumerable . SequenceEqual ( buffer2 , buffer3 ) ) ;
208
+ } ) ;
170
209
}
171
210
172
211
[ Fact ]
173
212
public void LongFileExists ( )
174
213
{
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
178
219
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
+ } ) ;
182
224
}
183
225
}
184
226
}
0 commit comments