@@ -28,15 +28,32 @@ public override DirectoryInfoBase CreateDirectory(string path)
2828
2929 public override DirectoryInfoBase CreateDirectory ( string path , DirectorySecurity directorySecurity )
3030 {
31+ if ( path == null )
32+ {
33+ throw new ArgumentNullException ( "path" ) ;
34+ }
35+
36+ if ( path . Length == 0 )
37+ {
38+ throw new ArgumentException ( "Path cannot be the empty string or all whitespace." , "path" ) ;
39+ }
40+
41+ if ( mockFileDataAccessor . FileExists ( path ) )
42+ {
43+ var message = string . Format ( CultureInfo . InvariantCulture , @"Cannot create ""{0}"" because a file or directory with the same name already exists." , path ) ;
44+ var ex = new IOException ( message ) ;
45+ ex . Data . Add ( "Path" , path ) ;
46+ throw ex ;
47+ }
48+
3149 path = EnsurePathEndsWithDirectorySeparator ( mockFileDataAccessor . Path . GetFullPath ( path ) ) ;
50+
3251 if ( ! Exists ( path ) )
52+ {
3353 mockFileDataAccessor . AddDirectory ( path ) ;
34- var created = new MockDirectoryInfo ( mockFileDataAccessor , path ) ;
35-
36- var parent = GetParent ( path ) ;
37- if ( parent != null )
38- CreateDirectory ( GetParent ( path ) . FullName , directorySecurity ) ;
54+ }
3955
56+ var created = new MockDirectoryInfo ( mockFileDataAccessor , path ) ;
4057 return created ;
4158 }
4259
@@ -57,7 +74,7 @@ public override void Delete(string path, bool recursive)
5774 throw new DirectoryNotFoundException ( path + " does not exist or could not be found." ) ;
5875
5976 if ( ! recursive &&
60- affectedPaths . Count ( ) > 1 )
77+ affectedPaths . Count > 1 )
6178 throw new IOException ( "The directory specified by " + path + " is read-only, or recursive is false and " + path + " is not an empty directory." ) ;
6279
6380 foreach ( var affectedPath in affectedPaths )
@@ -218,11 +235,38 @@ public override string[] GetLogicalDrives()
218235
219236 public override DirectoryInfoBase GetParent ( string path )
220237 {
221- var parent = new DirectoryInfo ( path ) . Parent ;
222- if ( parent == null )
238+ if ( path == null )
239+ {
240+ throw new ArgumentNullException ( "path" ) ;
241+ }
242+
243+ if ( path . Length == 0 )
244+ {
245+ throw new ArgumentException ( "Path cannot be the empty string or all whitespace." , "path" ) ;
246+ }
247+
248+ if ( path . IndexOfAny ( mockFileDataAccessor . Path . GetInvalidPathChars ( ) ) > - 1 )
249+ {
250+ throw new ArgumentException ( "Path contains invalid path characters." , "path" ) ;
251+ }
252+
253+ var absolutePath = mockFileDataAccessor . Path . GetFullPath ( path ) ;
254+ var sepAsString = mockFileDataAccessor . Path . DirectorySeparatorChar . ToString ( CultureInfo . InvariantCulture ) ;
255+ var startIndex = absolutePath . EndsWith ( sepAsString , StringComparison . OrdinalIgnoreCase ) ? absolutePath . Length - 1 : absolutePath . Length ;
256+ var lastIndex = absolutePath . LastIndexOf ( mockFileDataAccessor . Path . DirectorySeparatorChar , startIndex - 1 ) ;
257+ if ( lastIndex < 0 )
258+ {
223259 return null ;
260+ }
261+
262+ var parentPath = absolutePath . Substring ( 0 , lastIndex ) ;
263+ if ( string . IsNullOrEmpty ( parentPath ) )
264+ {
265+ return null ;
266+ }
224267
225- return new MockDirectoryInfo ( mockFileDataAccessor , parent . FullName ) ;
268+ var parent = new MockDirectoryInfo ( mockFileDataAccessor , parentPath ) ;
269+ return parent ;
226270 }
227271
228272 public override void Move ( string sourceDirName , string destDirName ) {
0 commit comments