1
1
using Xunit ;
2
2
using System ;
3
+ using System . Collections . Generic ;
3
4
using System . IO ;
4
5
using System . Linq ;
5
6
using Semmle . Util ;
@@ -10,39 +11,51 @@ namespace SemmleTests.Semmle.Util
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
+ // Create directory to avoid directory not found exceptions when deleting files
36
+ Directory . CreateDirectory ( longPathDir ) ;
37
+ }
38
+
39
+ private static void Cleanup ( params IEnumerable < string > paths )
40
+ {
41
+ foreach ( var path in paths )
38
42
{
43
+ File . Delete ( path ) ;
39
44
}
45
+ }
46
+
47
+ private static void WithSetUpAndTearDown ( Action < string , string > test )
48
+ {
49
+ var longPath = MakeLongPath ( ) ;
50
+ var shortPath = MakeShortPath ( ) ;
51
+ Cleanup ( longPath , shortPath ) ;
40
52
try
41
53
{
42
- File . Delete ( longPath ) ;
54
+ test ( longPath , shortPath ) ;
43
55
}
44
- catch ( DirectoryNotFoundException )
56
+ finally
45
57
{
58
+ Cleanup ( longPath , shortPath ) ;
46
59
}
47
60
}
48
61
@@ -63,122 +76,143 @@ 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 ( ( 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 ( ( longPath , shortPath ) =>
91
+ {
92
+ File . WriteAllText ( shortPath , "abc" ) ;
93
+ File . Delete ( longPath ) ;
94
+ File . Move ( shortPath , longPath ) ;
95
+ File . Move ( longPath , shortPath ) ;
96
+ Assert . Equal ( "abc" , File . ReadAllText ( shortPath ) ) ;
97
+ } ) ;
80
98
}
81
99
82
100
[ Fact ]
83
101
public void Replace ( )
84
102
{
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 ) ) ;
103
+ WithSetUpAndTearDown ( ( longPath , shortPath ) =>
104
+ {
105
+ File . WriteAllText ( shortPath , "abc" ) ;
106
+ File . Move ( shortPath , longPath ) ;
107
+ File . WriteAllText ( shortPath , "def" ) ;
108
+ FileUtils . MoveOrReplace ( shortPath , longPath ) ;
109
+ File . WriteAllText ( shortPath , "abc" ) ;
110
+ FileUtils . MoveOrReplace ( longPath , shortPath ) ;
111
+ Assert . Equal ( "def" , File . ReadAllText ( shortPath ) ) ;
112
+ } ) ;
94
113
}
95
114
96
- private readonly byte [ ] buffer1 = new byte [ 10 ] { 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } ;
115
+ private readonly byte [ ] buffer1 = { 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } ;
97
116
98
117
[ Fact ]
99
118
public void CreateShortStream ( )
100
119
{
101
- var buffer2 = new byte [ 10 ] ;
102
-
103
- using ( var s1 = new FileStream ( shortPath , FileMode . Create , FileAccess . Write , FileShare . None ) )
120
+ WithSetUpAndTearDown ( ( _ , shortPath ) =>
104
121
{
105
- s1 . Write ( buffer1 , 0 , 10 ) ;
106
- }
122
+ var buffer2 = new byte [ 10 ] ;
107
123
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
- }
124
+ using ( var s1 = new FileStream ( shortPath , FileMode . Create , FileAccess . Write , FileShare . None ) )
125
+ {
126
+ s1 . Write ( buffer1 , 0 , 10 ) ;
127
+ }
128
+
129
+ using ( var s2 = new FileStream ( shortPath , FileMode . Open , FileAccess . Read , FileShare . None ) )
130
+ {
131
+ Assert . Equal ( 10 , s2 . Read ( buffer2 , 0 , 10 ) ) ;
132
+ Assert . True ( Enumerable . SequenceEqual ( buffer1 , buffer2 ) ) ;
133
+ }
134
+ } ) ;
113
135
}
114
136
115
137
[ Fact ]
116
138
public void CreateLongStream ( )
117
139
{
118
- var buffer2 = new byte [ 10 ] ;
140
+ WithSetUpAndTearDown ( ( longPath , _ ) =>
141
+ {
142
+ var buffer2 = new byte [ 10 ] ;
119
143
120
- Directory . CreateDirectory ( Path . GetDirectoryName ( longPath ) ! ) ;
144
+ Directory . CreateDirectory ( Path . GetDirectoryName ( longPath ) ! ) ;
121
145
122
- using ( var s3 = new FileStream ( longPath , FileMode . Create , FileAccess . Write , FileShare . None ) )
123
- {
124
- s3 . Write ( buffer1 , 0 , 10 ) ;
125
- }
146
+ using ( var s3 = new FileStream ( longPath , FileMode . Create , FileAccess . Write , FileShare . None ) )
147
+ {
148
+ s3 . Write ( buffer1 , 0 , 10 ) ;
149
+ }
126
150
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
- }
151
+ using ( var s4 = new FileStream ( longPath , FileMode . Open , FileAccess . Read , FileShare . None ) )
152
+ {
153
+ Assert . Equal ( 10 , s4 . Read ( buffer2 , 0 , 10 ) ) ;
154
+ Assert . True ( Enumerable . SequenceEqual ( buffer1 , buffer2 ) ) ;
155
+ }
156
+ } ) ;
132
157
}
133
158
134
159
[ Fact ]
135
160
public void FileDoesNotExist ( )
136
161
{
137
- // File does not exist
138
- Assert . Throws < System . IO . FileNotFoundException > ( ( ) =>
162
+ WithSetUpAndTearDown ( ( longPath , _ ) =>
139
163
{
140
- using ( new FileStream ( longPath , FileMode . Open , FileAccess . Read , FileShare . None ) )
164
+ // File does not exist
165
+ Assert . Throws < System . IO . FileNotFoundException > ( ( ) =>
141
166
{
142
- //
143
- }
167
+ using ( new FileStream ( longPath , FileMode . Open , FileAccess . Read , FileShare . None ) )
168
+ {
169
+ //
170
+ }
171
+ } ) ;
144
172
} ) ;
145
173
}
146
174
147
175
[ Fact ]
148
176
public void OverwriteFile ( )
149
177
{
150
- using ( var s1 = new FileStream ( longPath , FileMode . Create , FileAccess . Write , FileShare . None ) )
178
+ WithSetUpAndTearDown ( ( longPath , _ ) =>
151
179
{
152
- s1 . Write ( buffer1 , 0 , 10 ) ;
153
- }
180
+ using ( var s1 = new FileStream ( longPath , FileMode . Create , FileAccess . Write , FileShare . None ) )
181
+ {
182
+ s1 . Write ( buffer1 , 0 , 10 ) ;
183
+ }
154
184
155
- byte [ ] buffer2 = { 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 , 0 } ;
185
+ byte [ ] buffer2 = { 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 , 0 } ;
156
186
157
- using ( var s2 = new FileStream ( longPath , FileMode . Create , FileAccess . Write , FileShare . None ) )
158
- {
159
- s2 . Write ( buffer2 , 0 , 10 ) ;
160
- }
187
+ using ( var s2 = new FileStream ( longPath , FileMode . Create , FileAccess . Write , FileShare . None ) )
188
+ {
189
+ s2 . Write ( buffer2 , 0 , 10 ) ;
190
+ }
161
191
162
- byte [ ] buffer3 = new byte [ 10 ] ;
192
+ byte [ ] buffer3 = new byte [ 10 ] ;
163
193
164
- using ( var s3 = new FileStream ( longPath , FileMode . Open , FileAccess . Read , FileShare . None ) )
165
- {
166
- Assert . Equal ( 10 , s3 . Read ( buffer3 , 0 , 10 ) ) ;
167
- }
194
+ using ( var s3 = new FileStream ( longPath , FileMode . Open , FileAccess . Read , FileShare . None ) )
195
+ {
196
+ Assert . Equal ( 10 , s3 . Read ( buffer3 , 0 , 10 ) ) ;
197
+ }
168
198
169
- Assert . True ( Enumerable . SequenceEqual ( buffer2 , buffer3 ) ) ;
199
+ Assert . True ( Enumerable . SequenceEqual ( buffer2 , buffer3 ) ) ;
200
+ } ) ;
170
201
}
171
202
172
203
[ Fact ]
173
204
public void LongFileExists ( )
174
205
{
175
- Assert . False ( File . Exists ( "no such file" ) ) ;
176
- Assert . False ( File . Exists ( "\" :" ) ) ;
177
- Assert . False ( File . Exists ( @"C:\" ) ) ; // A directory
206
+ WithSetUpAndTearDown ( ( longPath , _ ) =>
207
+ {
208
+ Assert . False ( File . Exists ( "no such file" ) ) ;
209
+ Assert . False ( File . Exists ( "\" :" ) ) ;
210
+ Assert . False ( File . Exists ( @"C:\" ) ) ; // A directory
178
211
179
- Assert . False ( File . Exists ( longPath ) ) ;
180
- new FileStream ( longPath , FileMode . Create , FileAccess . Write , FileShare . None ) . Close ( ) ;
181
- Assert . True ( File . Exists ( longPath ) ) ;
212
+ Assert . False ( File . Exists ( longPath ) ) ;
213
+ new FileStream ( longPath , FileMode . Create , FileAccess . Write , FileShare . None ) . Close ( ) ;
214
+ Assert . True ( File . Exists ( longPath ) ) ;
215
+ } ) ;
182
216
}
183
217
}
184
218
}
0 commit comments