11using Xunit ;
22using System ;
3+ using System . Collections . Generic ;
34using System . IO ;
45using System . Linq ;
56using Semmle . Util ;
@@ -10,39 +11,51 @@ namespace SemmleTests.Semmle.Util
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 {
15- private static readonly string 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" ,
16+ private static readonly string tmpDir = Environment . GetEnvironmentVariable ( "TEST_TMPDIR" ) ?? Path . GetTempPath ( ) ;
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+ // 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 )
3842 {
43+ File . Delete ( path ) ;
3944 }
45+ }
46+
47+ private static void WithSetUpAndTearDown ( Action < string , string > test )
48+ {
49+ var longPath = MakeLongPath ( ) ;
50+ var shortPath = MakeShortPath ( ) ;
51+ Cleanup ( longPath , shortPath ) ;
4052 try
4153 {
42- File . Delete ( longPath ) ;
54+ test ( longPath , shortPath ) ;
4355 }
44- catch ( DirectoryNotFoundException )
56+ finally
4557 {
58+ Cleanup ( longPath , shortPath ) ;
4659 }
4760 }
4861
@@ -63,122 +76,143 @@ 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 ( ( 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 ( ( 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+ } ) ;
8098 }
8199
82100 [ Fact ]
83101 public void Replace ( )
84102 {
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+ } ) ;
94113 }
95114
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 } ;
97116
98117 [ Fact ]
99118 public void CreateShortStream ( )
100119 {
101- var buffer2 = new byte [ 10 ] ;
102-
103- using ( var s1 = new FileStream ( shortPath , FileMode . Create , FileAccess . Write , FileShare . None ) )
120+ WithSetUpAndTearDown ( ( _ , shortPath ) =>
104121 {
105- s1 . Write ( buffer1 , 0 , 10 ) ;
106- }
122+ var buffer2 = new byte [ 10 ] ;
107123
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+ } ) ;
113135 }
114136
115137 [ Fact ]
116138 public void CreateLongStream ( )
117139 {
118- var buffer2 = new byte [ 10 ] ;
140+ WithSetUpAndTearDown ( ( longPath , _ ) =>
141+ {
142+ var buffer2 = new byte [ 10 ] ;
119143
120- Directory . CreateDirectory ( Path . GetDirectoryName ( longPath ) ! ) ;
144+ Directory . CreateDirectory ( Path . GetDirectoryName ( longPath ) ! ) ;
121145
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+ }
126150
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+ } ) ;
132157 }
133158
134159 [ Fact ]
135160 public void FileDoesNotExist ( )
136161 {
137- // File does not exist
138- Assert . Throws < System . IO . FileNotFoundException > ( ( ) =>
162+ WithSetUpAndTearDown ( ( longPath , _ ) =>
139163 {
140- using ( new FileStream ( longPath , FileMode . Open , FileAccess . Read , FileShare . None ) )
164+ // File does not exist
165+ Assert . Throws < System . IO . FileNotFoundException > ( ( ) =>
141166 {
142- //
143- }
167+ using ( new FileStream ( longPath , FileMode . Open , FileAccess . Read , FileShare . None ) )
168+ {
169+ //
170+ }
171+ } ) ;
144172 } ) ;
145173 }
146174
147175 [ Fact ]
148176 public void OverwriteFile ( )
149177 {
150- using ( var s1 = new FileStream ( longPath , FileMode . Create , FileAccess . Write , FileShare . None ) )
178+ WithSetUpAndTearDown ( ( longPath , _ ) =>
151179 {
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+ }
154184
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 } ;
156186
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+ }
161191
162- byte [ ] buffer3 = new byte [ 10 ] ;
192+ byte [ ] buffer3 = new byte [ 10 ] ;
163193
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+ }
168198
169- Assert . True ( Enumerable . SequenceEqual ( buffer2 , buffer3 ) ) ;
199+ Assert . True ( Enumerable . SequenceEqual ( buffer2 , buffer3 ) ) ;
200+ } ) ;
170201 }
171202
172203 [ Fact ]
173204 public void LongFileExists ( )
174205 {
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
178211
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+ } ) ;
182216 }
183217 }
184218}
0 commit comments